/
transactioncmd.go
224 lines (193 loc) · 5.86 KB
/
transactioncmd.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2021 The Alaya Network Authors
// This file is part of Alaya-Go.
//
// Alaya-Go is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Alaya-Go is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Alaya-Go. If not, see <http://www.gnu.org/licenses/>.
package core
import (
"crypto/ecdsa"
"encoding/json"
"fmt"
"math/big"
"strconv"
"strings"
"gopkg.in/urfave/cli.v1"
"github.com/AlayaNetwork/Alaya-Go/common"
"github.com/AlayaNetwork/Alaya-Go/common/hexutil"
"github.com/AlayaNetwork/Alaya-Go/core/types"
"github.com/AlayaNetwork/Alaya-Go/rlp"
)
var (
SendTransactionCmd = cli.Command{
Name: "sendTransaction",
Usage: "send a transaction",
Action: sendTransactionCmd,
Flags: sendTransactionCmdFlags,
}
SendRawTransactionCmd = cli.Command{
Name: "sendRawTransaction",
Usage: "send a raw transaction",
Action: sendRawTransactionCmd,
Flags: sendRawTransactionCmdFlags,
}
GetTxReceiptCmd = cli.Command{
Name: "getTxReceipt",
Usage: "get transaction receipt by hash",
Action: getTxReceiptCmd,
Flags: getTxReceiptCmdFlags,
}
)
func getTxReceiptCmd(c *cli.Context) {
hash := c.String(TransactionHashFlag.Name)
parseConfigJson(c.String(ConfigPathFlag.Name))
GetTxReceipt(hash)
}
func GetTxReceipt(txHash string) (Receipt, error) {
var receipt = Receipt{}
res, _ := Send([]string{txHash}, "platon_getTransactionReceipt")
e := json.Unmarshal([]byte(res), &receipt)
if e != nil {
panic(fmt.Sprintf("parse get receipt result error ! \n %s", e.Error()))
}
if receipt.Result.BlockHash == "" {
panic("no receipt found")
}
out, _ := json.MarshalIndent(receipt, "", " ")
fmt.Println(string(out))
return receipt, nil
}
func sendTransactionCmd(c *cli.Context) error {
from := c.String(TxFromFlag.Name)
to := c.String(TxToFlag.Name)
value := c.String(TransferValueFlag.Name)
parseConfigJson(c.String(ConfigPathFlag.Name))
hash, err := SendTransaction(from, to, value)
if err != nil {
return fmt.Errorf("Send transaction error: %v", err)
}
fmt.Printf("tx hash: %s", hash)
return nil
}
func sendRawTransactionCmd(c *cli.Context) error {
from := c.String(TxFromFlag.Name)
to := c.String(TxToFlag.Name)
value := c.String(TransferValueFlag.Name)
pkFile := c.String(PKFilePathFlag.Name)
parseConfigJson(c.String(ConfigPathFlag.Name))
hash, err := SendRawTransaction(from, to, value, pkFile)
if err != nil {
return fmt.Errorf("Send transaction error: %v", err)
}
fmt.Printf("tx hash: %s", hash)
return nil
}
func SendTransaction(from, to, value string) (string, error) {
var tx TxParams
if from == "" {
from = config.From
}
tx.From = from
tx.To = to
tx.Gas = config.Gas
tx.GasPrice = config.GasPrice
//todo
if !strings.HasPrefix(value, "0x") {
intValue, err := strconv.ParseInt(value, 10, 64)
if err != nil {
panic(fmt.Sprintf("transfer value to int error.%s", err))
}
value = hexutil.EncodeBig(big.NewInt(intValue))
}
tx.Value = value
params := make([]TxParams, 1)
params[0] = tx
res, _ := Send(params, "platon_sendTransaction")
response := parseResponse(res)
return response.Result, nil
}
func SendRawTransaction(from, to, value string, pkFilePath string) (string, error) {
if len(accountPool) == 0 {
parsePkFile(pkFilePath)
}
var v int64
var err error
if strings.HasPrefix(value, "0x") {
bigValue, _ := hexutil.DecodeBig(value)
v = bigValue.Int64()
} else {
v, err = strconv.ParseInt(value, 10, 64)
if err != nil {
panic(fmt.Sprintf("transfer value to int error.%s", err))
}
}
////
//
//for k, v := range accountPool {
// fmt.Println("acc", k.Hex())
// fmt.Println("value", fmt.Sprintf("%+v", v))
//}
acc, ok := accountPool[common.MustBech32ToAddress(from)]
if !ok {
return "", fmt.Errorf("private key not found in private key file,addr:%s", from)
}
nonce := getNonce(from)
nonce++
//// getBalance
//
//unlock := JsonParam{
// Jsonrpc: "2.0",
// Method: "personal_unlockAccount",
// // {"method": "platon_getBalance", "params": [account, pwd, expire]}
// // {"jsonrpc":"2.0", "method":"eth_getBalance","params":["0xde1e758511a7c67e7db93d1c23c1060a21db4615","latest"],"id":67}
// Params: []interface{}{from, "latest"},
// Id: 1,
//}
//
//// unlock
//s, e := HttpPost(unlock)
//if nil != e {
// fmt.Println("the gat balance err:", e)
//}
//fmt.Println("the balance:", s)
newTx := getSignedTransaction(from, to, v, acc.Priv, nonce)
hash, err := sendRawTransaction(newTx)
if err != nil {
panic(err)
}
return hash, nil
}
func sendRawTransaction(transaction *types.Transaction) (string, error) {
bytes, _ := rlp.EncodeToBytes(transaction)
res, err := Send([]string{hexutil.Encode(bytes)}, "platon_sendRawTransaction")
if err != nil {
panic(err)
}
response := parseResponse(res)
return response.Result, nil
}
func getSignedTransaction(from, to string, value int64, priv *ecdsa.PrivateKey, nonce uint64) *types.Transaction {
gas, _ := strconv.Atoi(config.Gas)
gasPrice, _ := new(big.Int).SetString(config.GasPrice, 10)
newTx, err := types.SignTx(types.NewTransaction(nonce, common.MustBech32ToAddress(to), big.NewInt(value), uint64(gas), gasPrice, []byte{}), types.NewEIP155Signer(new(big.Int).SetInt64(100)), priv)
if err != nil {
panic(fmt.Errorf("sign error,%s", err.Error()))
}
return newTx
}
func getNonce(addr string) uint64 {
res, _ := Send([]string{addr, "latest"}, "platon_getTransactionCount")
response := parseResponse(res)
nonce, _ := hexutil.DecodeBig(response.Result)
fmt.Println(addr, nonce)
return nonce.Uint64()
}