-
Notifications
You must be signed in to change notification settings - Fork 0
/
impawn.go
455 lines (395 loc) · 12.1 KB
/
impawn.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package main
import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"github.com/truechain/ups"
"github.com/truechain/ups/accounts/abi"
"github.com/truechain/ups/accounts/keystore"
"github.com/truechain/ups/cmd/utils"
"github.com/truechain/ups/common"
"github.com/truechain/ups/console"
"github.com/truechain/ups/core/types"
"github.com/truechain/ups/core/vm"
"github.com/truechain/ups/crypto"
"github.com/truechain/ups/upsclient"
"gopkg.in/urfave/cli.v1"
"io/ioutil"
"log"
"math"
"math/big"
"os"
"path/filepath"
"strings"
"time"
)
var (
key string
store string
ip string
port int
)
var (
abiStaking, _ = abi.JSON(strings.NewReader(vm.StakeABIJSON))
priKey *ecdsa.PrivateKey
from common.Address
trueValue uint64
fee uint64
holder common.Address
)
const (
datadirPrivateKey = "key"
datadirDefaultKeyStore = "keystore"
ImpawnAmount = 20000
)
func impawn(ctx *cli.Context) error {
loadPrivate(ctx)
conn, url := dialConn(ctx)
printBaseInfo(conn, url)
PrintBalance(conn, from)
value := trueToWei(ctx, false)
if trueValue < ImpawnAmount {
printError("Impawn value must bigger than ", ImpawnAmount)
}
fee = ctx.GlobalUint64(FeeFlag.Name)
checkFee(new(big.Int).SetUint64(fee))
pubkey, pk, _ := getPubKey(ctx, conn)
fmt.Println("Fee", fee, " Pubkey ", pubkey, " value ", value)
input := packInput("deposit", pk, new(big.Int).SetUint64(fee), value)
txHash := sendContractTransaction(conn, from, types.StakingAddress, nil, priKey, input)
getResult(conn, txHash, true, false)
return nil
}
func checkFee(fee *big.Int) {
if fee.Sign() < 0 || fee.Cmp(types.Base) > 0 {
printError("Please set correct fee value")
}
}
func getPubKey(ctx *cli.Context, conn *upsclient.Client) (string, []byte, error) {
var (
pubkey string
err error
)
if ctx.GlobalIsSet(PubKeyKeyFlag.Name) {
pubkey = ctx.GlobalString(PubKeyKeyFlag.Name)
} else if ctx.GlobalIsSet(BFTKeyKeyFlag.Name) {
bftKey, err := crypto.HexToECDSA(ctx.GlobalString(BFTKeyKeyFlag.Name))
if err != nil {
printError("bft key error", err)
}
pk := crypto.FromECDSAPub(&bftKey.PublicKey)
pubkey = common.Bytes2Hex(pk)
} else {
pubkey, err = conn.Pubkey(context.Background())
if err != nil {
printError("get pubkey error", err)
}
}
pk := common.Hex2Bytes(pubkey)
if err = types.ValidPk(pk); err != nil {
printError("ValidPk error", err)
}
return pubkey, pk, err
}
func sendContractTransaction(client *upsclient.Client, from, toAddress common.Address, value *big.Int, privateKey *ecdsa.PrivateKey, input []byte) common.Hash {
// Ensure a valid value field and resolve the account nonce
nonce, err := client.PendingNonceAt(context.Background(), from)
if err != nil {
log.Fatal(err)
}
gasPrice, err := client.SuggestGasPrice(context.Background())
if err != nil {
log.Fatal(err)
}
gasLimit := uint64(2100000) // in units
// If the contract surely has code (or code is not needed), estimate the transaction
msg := truechain.CallMsg{From: from, To: &toAddress, GasPrice: gasPrice, Value: value, Data: input}
gasLimit, err = client.EstimateGas(context.Background(), msg)
if err != nil {
fmt.Println("Contract exec failed", err)
}
if gasLimit < 1 {
gasLimit = 866328
}
// Create the transaction, sign it and schedule it for execution
tx := types.NewTransaction(nonce, toAddress, value, gasLimit, gasPrice, input)
chainID, err := client.ChainID(context.Background())
if err != nil {
log.Fatal(err)
}
fmt.Println("TX data nonce ", nonce, " transfer value ", value, " gasLimit ", gasLimit, " gasPrice ", gasPrice, " chainID ", chainID)
signedTx, err := types.SignTx(tx, types.NewTIP1Signer(chainID), privateKey)
if err != nil {
log.Fatal(err)
}
err = client.SendTransaction(context.Background(), signedTx)
if err != nil {
log.Fatal(err)
}
return signedTx.Hash()
}
func createKs() {
ks := keystore.NewKeyStore("./createKs", keystore.StandardScryptN, keystore.StandardScryptP)
password := "secret"
account, err := ks.NewAccount(password)
if err != nil {
log.Fatal(err)
}
fmt.Println(account.Address.Hex()) // 0x20F8D42FB0F667F2E53930fed426f225752453b3
}
func importKs(password string) common.Address {
file, err := getAllFile(datadirDefaultKeyStore)
if err != nil {
log.Fatal(err)
}
cks, _ := filepath.Abs(datadirDefaultKeyStore)
jsonBytes, err := ioutil.ReadFile(filepath.Join(cks, file))
if err != nil {
log.Fatal(err)
}
//password := "secret"
key, err := keystore.DecryptKey(jsonBytes, password)
if err != nil {
log.Fatal(err)
}
priKey = key.PrivateKey
from = crypto.PubkeyToAddress(priKey.PublicKey)
fmt.Println("address ", from.Hex())
return from
}
func loadPrivateKey(path string) common.Address {
var err error
if path == "" {
file, err := getAllFile(datadirPrivateKey)
if err != nil {
printError(" getAllFile file name error", err)
}
kab, _ := filepath.Abs(datadirPrivateKey)
path = filepath.Join(kab, file)
}
priKey, err = crypto.LoadECDSA(path)
if err != nil {
printError("LoadECDSA error", err)
}
from = crypto.PubkeyToAddress(priKey.PublicKey)
return from
}
func getAllFile(path string) (string, error) {
rd, err := ioutil.ReadDir(path)
if err != nil {
printError("path ", err)
}
for _, fi := range rd {
if fi.IsDir() {
fmt.Printf("[%s]\n", path+"\\"+fi.Name())
getAllFile(path + fi.Name() + "\\")
return "", errors.New("path error")
} else {
fmt.Println(path, "dir has ", fi.Name(), "file")
return fi.Name(), nil
}
}
return "", err
}
func printError(error ...interface{}) {
log.Fatal(error)
}
func trueToWei(ctx *cli.Context, zero bool) *big.Int {
trueValue = ctx.GlobalUint64(TrueValueFlag.Name)
if !zero && trueValue <= 0 {
printError("Value must bigger than 0")
}
baseUnit := new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
value := new(big.Int).Mul(big.NewInt(int64(trueValue)), baseUnit)
return value
}
func weiToTrue(value *big.Int) uint64 {
baseUnit := new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)
valueT := new(big.Int).Div(value, baseUnit).Uint64()
return valueT
}
func getResult(conn *upsclient.Client, txHash common.Hash, contract bool, delegate bool) {
fmt.Println("Please waiting ", " txHash ", txHash.String())
count := 0
for {
time.Sleep(time.Millisecond * 200)
_, isPending, err := conn.TransactionByHash(context.Background(), txHash)
if err != nil {
log.Fatal(err)
}
count++
if !isPending {
break
}
if count >= 40 {
fmt.Println("Please use querytx sub command query later.")
os.Exit(0)
}
}
queryTx(conn, txHash, contract, false, delegate)
}
func queryTx(conn *upsclient.Client, txHash common.Hash, contract bool, pending bool, delegate bool) {
if pending {
_, isPending, err := conn.TransactionByHash(context.Background(), txHash)
if err != nil {
log.Fatal(err)
}
if isPending {
println("In tx_pool no validator process this, please query later")
os.Exit(0)
}
}
receipt, err := conn.TransactionReceipt(context.Background(), txHash)
if err != nil {
log.Fatal(err)
}
if receipt.Status == types.ReceiptStatusSuccessful {
block, err := conn.BlockByHash(context.Background(), receipt.BlockHash)
if err != nil {
log.Fatal(err)
}
fmt.Println("Transaction Success", " block Number", receipt.BlockNumber.Uint64(), " block txs", len(block.Transactions()), "blockhash", block.Hash().Hex())
if contract && common.IsHexAddress(from.Hex()) {
queryStakingInfo(conn, false, delegate)
}
} else if receipt.Status == types.ReceiptStatusFailed {
fmt.Println("Transaction Failed ", " Block Number", receipt.BlockNumber.Uint64())
}
}
func packInput(abiMethod string, params ...interface{}) []byte {
input, err := abiStaking.Pack(abiMethod, params...)
if err != nil {
printError(abiMethod, " error ", err)
}
return input
}
func PrintBalance(conn *upsclient.Client, from common.Address) {
balance, err := conn.BalanceAt(context.Background(), from, nil)
if err != nil {
log.Fatal(err)
}
fbalance := new(big.Float)
fbalance.SetString(balance.String())
trueValue := new(big.Float).Quo(fbalance, big.NewFloat(math.Pow10(18)))
sbalance, err := conn.LockBalanceAt(context.Background(), from, nil)
fmt.Println("Your wallet valid balance is ", trueValue, "'true ", " lock balance is ", types.ToTrue(sbalance), "'true ")
}
func loadPrivate(ctx *cli.Context) {
key = ctx.GlobalString(KeyFlag.Name)
store = ctx.GlobalString(KeyStoreFlag.Name)
if key != "" {
loadPrivateKey(key)
} else if store != "" {
loadSigningKey(store)
} else {
printError("Must specify --key or --keystore")
}
if priKey == nil {
printError("load privateKey failed")
}
}
func dialConn(ctx *cli.Context) (*upsclient.Client, string) {
ip = ctx.GlobalString(utils.RPCListenAddrFlag.Name)
port = ctx.GlobalInt(utils.RPCPortFlag.Name)
url := fmt.Sprintf("http://%s", fmt.Sprintf("%s:%d", ip, port))
// Create an IPC based RPC connection to a remote node
// "http://39.100.97.129:8545"
conn, err := upsclient.Dial(url)
if err != nil {
log.Fatalf("Failed to connect to the Upschain client: %v", err)
}
return conn, url
}
func printBaseInfo(conn *upsclient.Client, url string) *types.Header {
header, err := conn.HeaderByNumber(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
if common.IsHexAddress(from.Hex()) {
fmt.Println("Connect url ", url, " current number ", header.Number.String(), " address ", from.Hex())
} else {
fmt.Println("Connect url ", url, " current number ", header.Number.String())
}
return header
}
// loadSigningKey loads a private key in Ethereum keystore format.
func loadSigningKey(keyfile string) common.Address {
keyjson, err := ioutil.ReadFile(keyfile)
if err != nil {
printError(fmt.Errorf("failed to read the keyfile at '%s': %v", keyfile, err))
}
password, _ := console.Stdin.PromptPassword("Please enter the password for '" + keyfile + "': ")
//password := "secret"
key, err := keystore.DecryptKey(keyjson, password)
if err != nil {
printError(fmt.Errorf("error decrypting key: %v", err))
}
priKey = key.PrivateKey
from = crypto.PubkeyToAddress(priKey.PublicKey)
//fmt.Println("address ", from.Hex(), "key", hex.EncodeToString(crypto.FromECDSA(priKey)))
return from
}
func queryRewardInfo(conn *upsclient.Client, number uint64, start bool) {
queryReward := number
var crc map[string]interface{}
crc, err = conn.GetChainRewardContent(context.Background(), from, new(big.Int).SetUint64(queryReward))
if err != nil {
printError("get chain reward content error", err)
}
if info, ok := crc["stakingReward"]; ok {
if info, ok := info.([]interface{}); ok {
fmt.Println("queryRewardInfo", info)
}
}
}
func queryStakingInfo(conn *upsclient.Client, query bool, delegate bool) {
header, err := conn.HeaderByNumber(context.Background(), nil)
if err != nil {
log.Fatal(err)
}
var input []byte
if delegate {
input = packInput("getDelegate", from, holder)
} else {
input = packInput("getDeposit", from)
}
msg := truechain.CallMsg{From: from, To: &types.StakingAddress, Data: input}
output, err := conn.CallContract(context.Background(), msg, header.Number)
if err != nil {
printError("method CallContract error", err)
}
if len(output) != 0 {
args := struct {
Staked *big.Int
Locked *big.Int
Unlocked *big.Int
}{}
err = abiStaking.Unpack(&args, "getDeposit", output)
if err != nil {
printError("abi error", err)
}
fmt.Println("Staked ", args.Staked.String(), "wei =", weiToTrue(args.Staked), "true Locked ",
args.Locked.String(), " wei =", weiToTrue(args.Locked), "true",
"Unlocked ", args.Unlocked.String(), " wei =", weiToTrue(args.Unlocked), "true")
if query && args.Locked.Sign() > 0 {
lockAssets, err := conn.GetLockedAsset(context.Background(), from, header.Number)
if err != nil {
printError("GetLockedAsset error", err)
}
for k, v := range lockAssets {
for m, n := range v.LockValue {
if !n.Locked {
fmt.Println("Your can instant withdraw", " count value ", n.Amount, " true")
} else {
if n.EpochID > 0 || n.Amount != "0" {
fmt.Println("Your can withdraw after height", n.Height.Uint64(), " count value ", n.Amount, " true index", k+m, " lock ", n.Locked)
}
}
}
}
}
} else {
fmt.Println("Contract query failed result len == 0")
}
}