forked from aergoio/aergo
/
injectAccount.go
73 lines (53 loc) · 1.51 KB
/
injectAccount.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
package exec
import (
"fmt"
"math/big"
"github.com/aergoio/aergo/cmd/brick/context"
"github.com/aergoio/aergo/contract"
)
func init() {
registerExec(&injectAccount{})
}
type injectAccount struct{}
func (c *injectAccount) Command() string {
return "inject"
}
func (c *injectAccount) Syntax() string {
return fmt.Sprintf("%s %s", context.AccountSymbol, context.AmountSymbol)
}
func (c *injectAccount) Usage() string {
return fmt.Sprintf("inject <account_name> <amount>")
}
func (c *injectAccount) Describe() string {
return "create an account with a given amount of balance"
}
func (c *injectAccount) 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 *injectAccount) parse(args string) (string, *big.Int, error) {
splitArgs := context.SplitSpaceAndAccent(args, false)
if len(splitArgs) < 2 {
return "", nil, fmt.Errorf("need 2 arguments. usage: %s", c.Usage())
}
amount, success := new(big.Int).SetString(splitArgs[1].Text, 10)
if success == false {
return "", nil, fmt.Errorf("fail to parse number %s", splitArgs[1].Text)
}
return splitArgs[0].Text, amount, nil
}
func (c *injectAccount) Run(args string) (string, error) {
accountName, amount, _ := c.parse(args)
err := context.Get().ConnectBlock(
contract.NewLuaTxAccountBig(accountName, amount),
)
if err != nil {
return "", err
}
Index(context.AccountSymbol, accountName)
return "inject an account successfully", nil
}