-
Notifications
You must be signed in to change notification settings - Fork 0
/
1502.go
140 lines (125 loc) · 3.95 KB
/
1502.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package main
import (
"encoding/json"
"fmt"
"github.com/Zilliqa/gozilliqa-sdk/v3/account"
"github.com/Zilliqa/gozilliqa-sdk/v3/keytools"
"github.com/Zilliqa/gozilliqa-sdk/v3/provider"
"github.com/Zilliqa/gozilliqa-sdk/v3/transaction"
"github.com/Zilliqa/gozilliqa-sdk/v3/util"
"os"
"strconv"
)
func main() {
arg := os.Args[1]
chainId, err := strconv.Atoi(os.Args[2])
if err != nil {
fmt.Printf("Chain id is not an integer: %v\n", err)
return
}
// 0123456789012345678901234567890123456789012345678901234567890123
// "priv": "0F494B8312E8D257E51730C78F8FE3B47B6840C59AAAEC7C2EBE404A2DE8B25A",
privateKeyX := "0000000000000000000000000000000000000000000000000000000000000002"
privateKeyXHex := util.DecodeHex(privateKeyX)
fmt.Printf("X len = %d %s\n\n", len(privateKeyXHex), util.EncodeHex(privateKeyXHex))
privateKey := "e19d05c5452598e24caad4a0d85a49146f7be089515c905ae6a19e8a578a6930"
fmt.Printf("Private Key: %s\n\n", privateKey)
wallet := account.NewWallet()
wallet.AddByPrivateKey(privateKey)
fmt.Println("Wallet initialized; private key added.\n")
publicKeyBytes := keytools.GetPublicKeyFromPrivateKey(util.DecodeHex(privateKey), true)
publicKey := util.EncodeHex(publicKeyBytes)
fmt.Printf("pubkey: %s\n\n", publicKey)
address := keytools.GetAddressFromPublic(publicKeyBytes)
fmt.Printf("Address: %s\n\n", address)
// Init provider
// (host is for z2 network node 0)
host := arg
provider := provider.NewProvider(host)
fmt.Printf("Provider initialized with host: %s\n\n", host)
// Get balance and nonce
balAndNonce, err := provider.GetBalance(address)
if err != nil {
fmt.Printf("Error fetching balance and nonce: %v\n", err)
return
}
fmt.Printf("Balance and Nonce: \n")
prettyPrintJSON(balAndNonce)
fmt.Println()
// Get minimum gas price
gasPrice, err := provider.GetMinimumGasPrice()
if err != nil {
fmt.Printf("Error fetching minimum gas price: %v\n", err)
return
}
fmt.Printf("Minimum gas price: %s\n\n", gasPrice)
// Txn params
msgVersion := 1
version := util.Pack(chainId, msgVersion)
versionStr := strconv.FormatInt(int64(version), 10)
fmt.Printf("Transaction Version (packed): %d\n", version)
fmt.Printf("Transaction Version (string): %s\n\n", versionStr)
tx := &transaction.Transaction{
Version: versionStr,
Nonce: strconv.FormatInt(balAndNonce.Nonce+1, 10),
ToAddr: "0x4BAF5faDA8e5Db92C3d3242618c5B47133AE003C",
Amount: "10000000",
GasPrice: gasPrice,
GasLimit: "50000",
Code: "",
Data: "",
Priority: false,
SenderPubKey: publicKey,
}
fmt.Println("Transaction Object:")
prettyPrintJSON(tx)
fmt.Println()
// Sign the transaction
err = wallet.Sign(tx, *provider)
if err != nil {
fmt.Printf("Error signing transaction: %v\n", err)
return
}
fmt.Println("Transaction signed successfully.\n")
fmt.Println("Signed Transaction:")
prettyPrintJSON(tx)
fmt.Println()
// Send the transaction
rsp, err := provider.CreateTransaction(tx.ToTransactionPayload())
if err != nil {
fmt.Printf("Error creating transaction: %v\n", err)
return
}
// Check for errors in the response
if rsp.Error != nil {
fmt.Printf("Transaction error: %v\n", rsp.Error.Message)
return
}
fmt.Println("Transaction sent successfully")
fmt.Println("Transaction Response:")
prettyPrintJSON(rsp)
fmt.Println()
// Extract the transaction hash
if rsp.Result == nil {
fmt.Println("Error: transaction result is nil")
return
}
resMap := rsp.Result.(map[string]interface{})
hash, ok := resMap["TranID"].(string)
if !ok {
fmt.Println("Error: unable to retrieve txn hash")
return
}
fmt.Printf("Transaction Hash: %s\n\n", hash)
// Confirm the transaction
tx.Confirm(hash, 1000, 3, provider)
fmt.Printf("Transaction confirmation status: %d\n", tx.Status)
}
func prettyPrintJSON(v interface{}) {
jsonData, err := json.MarshalIndent(v, "", " ")
if err != nil {
fmt.Printf("Error marshaling JSON: %v\n", err)
return
}
fmt.Println(string(jsonData))
}