cli: use default adress in transfer

This commit is contained in:
Evgenii Stratonikov 2021-01-11 15:58:37 +03:00 committed by Evgeniy Stratonikov
parent ee10ea19e7
commit d4723d8e81
3 changed files with 48 additions and 4 deletions

View file

@ -111,10 +111,10 @@ func TestNEP17Transfer(t *testing.T) {
"neo-go", "wallet", "nep17", "transfer",
"--rpc-endpoint", "http://" + e.RPC.Addr,
"--wallet", validatorWallet,
"--from", validatorAddr,
"--to", w.Accounts[0].Address,
"--token", "NEO",
"--amount", "1",
"--from", validatorAddr,
}
t.Run("InvalidPassword", func(t *testing.T) {
@ -131,6 +131,33 @@ func TestNEP17Transfer(t *testing.T) {
require.NoError(t, err)
b, _ := e.Chain.GetGoverningTokenBalance(sh)
require.Equal(t, big.NewInt(1), b)
t.Run("default address", func(t *testing.T) {
const validatorDefault = "NbTiM6h8r99kpRtb428XcsUk1TzKed2gTc"
e.In.WriteString("one\r")
e.Run(t, "neo-go", "wallet", "nep17", "multitransfer",
"--rpc-endpoint", "http://"+e.RPC.Addr,
"--wallet", validatorWallet,
"--from", validatorAddr,
"NEO:"+validatorDefault+":42",
"GAS:"+validatorDefault+":7")
e.checkTxPersisted(t)
args := args[:len(args)-2] // cut '--from' argument
e.In.WriteString("one\r")
e.Run(t, args...)
e.checkTxPersisted(t)
sh, err := address.StringToUint160(w.Accounts[0].Address)
require.NoError(t, err)
b, _ := e.Chain.GetGoverningTokenBalance(sh)
require.Equal(t, big.NewInt(2), b)
sh, err = address.StringToUint160(validatorDefault)
require.NoError(t, err)
b, _ = e.Chain.GetGoverningTokenBalance(sh)
require.Equal(t, big.NewInt(41), b)
})
}
func TestNEP17MultiTransfer(t *testing.T) {

View file

@ -16,7 +16,7 @@
"deployed": false
},
"lock": false,
"isdefault": false
"isdefault": true
},
{
"address": "NUVPACMnKFhpuHjsRjhUvXz1XhqfGZYVtY",

View file

@ -359,7 +359,10 @@ func multiTransferNEP17(ctx *cli.Context) error {
defer wall.Close()
fromFlag := ctx.Generic("from").(*flags.Address)
from := fromFlag.Uint160()
from, err := getDefaultAddress(fromFlag, wall)
if err != nil {
return cli.NewExitError(err, 1)
}
acc, err := getDecryptedAccount(ctx, wall, from)
if err != nil {
return cli.NewExitError(err, 1)
@ -422,7 +425,10 @@ func transferNEP17(ctx *cli.Context) error {
defer wall.Close()
fromFlag := ctx.Generic("from").(*flags.Address)
from := fromFlag.Uint160()
from, err := getDefaultAddress(fromFlag, wall)
if err != nil {
return cli.NewExitError(err, 1)
}
acc, err := getDecryptedAccount(ctx, wall, from)
if err != nil {
return cli.NewExitError(err, 1)
@ -484,3 +490,14 @@ func signAndSendTransfer(ctx *cli.Context, c *client.Client, acc *wallet.Account
fmt.Fprintln(ctx.App.Writer, tx.Hash().StringLE())
return nil
}
func getDefaultAddress(fromFlag *flags.Address, w *wallet.Wallet) (util.Uint160, error) {
if fromFlag.IsSet {
return fromFlag.Uint160(), nil
}
addr := w.GetChangeAddress()
if addr.Equals(util.Uint160{}) {
return util.Uint160{}, errors.New("can't get default address")
}
return addr, nil
}