github.com/aergoio/aergo@v1.3.1/cmd/brick/exec/injectAccount.go (about) 1 package exec 2 3 import ( 4 "fmt" 5 "math/big" 6 7 "github.com/aergoio/aergo/cmd/brick/context" 8 "github.com/aergoio/aergo/contract" 9 ) 10 11 func init() { 12 registerExec(&injectAccount{}) 13 } 14 15 type injectAccount struct{} 16 17 func (c *injectAccount) Command() string { 18 return "inject" 19 } 20 21 func (c *injectAccount) Syntax() string { 22 return fmt.Sprintf("%s %s", context.AccountSymbol, context.AmountSymbol) 23 } 24 25 func (c *injectAccount) Usage() string { 26 return fmt.Sprintf("inject <account_name> <amount>") 27 } 28 29 func (c *injectAccount) Describe() string { 30 return "create an account with a given amount of balance" 31 } 32 33 func (c *injectAccount) Validate(args string) error { 34 35 // is chain is loaded? 36 if context.Get() == nil { 37 return fmt.Errorf("load chain first") 38 } 39 40 _, _, err := c.parse(args) 41 42 return err 43 } 44 45 func (c *injectAccount) parse(args string) (string, *big.Int, error) { 46 splitArgs := context.SplitSpaceAndAccent(args, false) 47 if len(splitArgs) < 2 { 48 return "", nil, fmt.Errorf("need 2 arguments. usage: %s", c.Usage()) 49 } 50 51 amount, success := new(big.Int).SetString(splitArgs[1].Text, 10) 52 if success == false { 53 return "", nil, fmt.Errorf("fail to parse number %s", splitArgs[1].Text) 54 } 55 56 return splitArgs[0].Text, amount, nil 57 } 58 59 func (c *injectAccount) Run(args string) (string, error) { 60 accountName, amount, _ := c.parse(args) 61 62 err := context.Get().ConnectBlock( 63 contract.NewLuaTxAccountBig(accountName, amount), 64 ) 65 66 if err != nil { 67 return "", err 68 } 69 70 Index(context.AccountSymbol, accountName) 71 72 return "inject an account successfully", nil 73 }