This repository has been archived by the owner on Dec 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcmd_transfer.go
97 lines (82 loc) · 2.45 KB
/
cmd_transfer.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
package main
import (
"context"
"encoding/hex"
"log"
"strconv"
"time"
"github.com/urfave/cli"
"github.com/the729/go-libra/client"
)
func cmdTransfer(ctx *cli.Context) error {
c, err := newClientFromWaypointOrFile(ServerAddr, TrustedWaypoint, KnownVersionFile)
if err != nil {
log.Fatal(err)
}
defer c.Close()
defer saveClientState(c, KnownVersionFile)
wallet, err := LoadAccounts(WalletFile)
if err != nil {
log.Fatal(err)
}
sender, err := wallet.GetAccount(ctx.Args().Get(0))
if err != nil {
return err
}
receiver, err := wallet.GetAccount(ctx.Args().Get(1))
if err != nil {
return err
}
amount, err := strconv.Atoi(ctx.Args().Get(2))
if err != nil {
return err
}
amountMicro := uint64(amount) * 1000000
log.Printf("Going to transfer %d microLibra from %s to %s", amountMicro, hex.EncodeToString(sender.Address[:]), hex.EncodeToString(receiver.Address[:]))
maxGasAmount := uint64(500000) // must > 260K for new payee account
gasUnitPrice := uint64(0)
expiration := time.Now().Add(1 * time.Minute)
if ctx.Args().Get(3) != "" {
if maxGasAmount, err = strconv.ParseUint(ctx.Args().Get(3), 10, 64); err != nil {
return err
}
}
if ctx.Args().Get(4) != "" {
if gasUnitPrice, err = strconv.ParseUint(ctx.Args().Get(4), 10, 64); err != nil {
return err
}
}
if ctx.Args().Get(5) != "" {
expSec, err := strconv.Atoi(ctx.Args().Get(5))
if err != nil {
return err
}
expiration = time.Now().Add(time.Duration(expSec) * time.Second)
}
log.Printf("Max gas: %d, Gas price: %d, Expiration: %v", maxGasAmount, gasUnitPrice, expiration)
log.Printf("Get current account sequence of sender...")
seq, err := c.QueryAccountSequenceNumber(context.Background(), sender.Address)
if err != nil {
log.Fatal(err)
}
log.Printf("... is %d", seq)
rawTxn, err := client.NewRawP2PTransaction(
sender.Address, receiver.Address, receiver.AuthKey[0:16],
seq, amountMicro, maxGasAmount, gasUnitPrice, expiration,
)
if err != nil {
log.Fatal(err)
}
log.Printf("Submit transaction...")
expectedSeq, err := c.SubmitRawTransaction(context.Background(), rawTxn, sender.PrivateKey)
if err != nil {
log.Fatal(err)
}
log.Printf("Waiting until transaction is included in ledger...")
err = c.PollSequenceUntil(context.Background(), sender.Address, expectedSeq, expiration)
if err != nil {
log.Fatal(err)
}
log.Printf("done.")
return nil
}