forked from aergoio/aergo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sendCoin.go
88 lines (67 loc) · 1.83 KB
/
sendCoin.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
package exec
import (
"fmt"
"math/big"
"strings"
"github.com/aergoio/aergo/cmd/brick/context"
"github.com/aergoio/aergo/contract"
)
func init() {
registerExec(&sendCoin{})
}
type sendCoin struct{}
func (c *sendCoin) Command() string {
return "send"
}
func (c *sendCoin) Syntax() string {
return fmt.Sprintf("%s %s %s", context.AccountSymbol,
context.AccountSymbol, context.AmountSymbol)
}
func (c *sendCoin) Usage() string {
return "send <sender_name> <receiver_name> <amount>"
}
func (c *sendCoin) Describe() string {
return "send aergo from sender to receiver"
}
func (c *sendCoin) Validate(args string) error {
// is chain is loaded?
if context.Get() == nil {
return fmt.Errorf("load chain first")
}
_, _, _, err := c.parse(args)
return err
}
func (c *sendCoin) parse(args string) (string, string, *big.Int, error) {
splitArgs := context.SplitSpaceAndAccent(args, false)
if len(splitArgs) < 3 {
return "", "", nil, fmt.Errorf("need 3 arguments. usage: %s", c.Usage())
}
amount, success := new(big.Int).SetString(splitArgs[2].Text, 10)
if success == false {
return "", "", nil, fmt.Errorf("fail to parse number %s", splitArgs[1].Text)
}
return splitArgs[0].Text,
splitArgs[1].Text,
amount,
nil
}
func (c *sendCoin) Run(args string) (string, error) {
senderName, receiverName, amount, _ := c.parse(args)
// assuming target is contract
err := context.Get().ConnectBlock(
contract.NewLuaTxCallBig(senderName, receiverName, amount, ""),
)
if err != nil && strings.HasPrefix(err.Error(), "cannot find contract") {
// retry to normal address
err := context.Get().ConnectBlock(
contract.NewLuaTxSendBig(senderName, receiverName, amount),
)
if err != nil {
return "", err
}
} else if err != nil {
return "", err
}
Index(context.AccountSymbol, receiverName)
return "send aergo successfully", nil
}