2020-08-29 11:41:33 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-01 12:40:32 +00:00
|
|
|
"io"
|
2020-08-31 09:42:42 +00:00
|
|
|
"math/big"
|
2020-09-01 12:40:32 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
2020-08-29 11:41:33 +00:00
|
|
|
"strconv"
|
2020-09-28 14:24:21 +00:00
|
|
|
"strings"
|
2020-08-29 11:41:33 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-12-13 18:25:04 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/core/native/nativenames"
|
2020-08-31 09:42:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/address"
|
2020-12-01 08:40:58 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/encoding/fixedn"
|
2020-08-31 09:42:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
|
|
|
"github.com/stretchr/testify/require"
|
2020-08-29 11:41:33 +00:00
|
|
|
)
|
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func TestNEP17Balance(t *testing.T) {
|
2020-08-29 11:41:33 +00:00
|
|
|
e := newExecutor(t, true)
|
2020-11-24 08:14:25 +00:00
|
|
|
cmdbalance := []string{"neo-go", "wallet", "nep17", "balance"}
|
2020-09-28 14:24:21 +00:00
|
|
|
cmdbase := append(cmdbalance,
|
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
2020-08-29 11:41:33 +00:00
|
|
|
"--wallet", validatorWallet,
|
2020-09-28 14:24:21 +00:00
|
|
|
)
|
2020-09-28 15:43:53 +00:00
|
|
|
cmd := append(cmdbase, "--address", validatorAddr)
|
2020-08-29 11:41:33 +00:00
|
|
|
t.Run("NEO", func(t *testing.T) {
|
|
|
|
b, index := e.Chain.GetGoverningTokenBalance(validatorHash)
|
|
|
|
checkResult := func(t *testing.T) {
|
2020-09-28 14:24:21 +00:00
|
|
|
e.checkNextLine(t, "^\\s*Account\\s+"+validatorAddr)
|
2020-12-13 18:25:04 +00:00
|
|
|
e.checkNextLine(t, "^\\s*NEO:\\s+NeoToken \\("+e.Chain.GoverningTokenHash().StringLE()+"\\)")
|
2021-01-11 12:02:17 +00:00
|
|
|
e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+b.String()+"$")
|
2020-08-29 11:41:33 +00:00
|
|
|
e.checkNextLine(t, "^\\s*Updated\\s*:\\s*"+strconv.FormatUint(uint64(index), 10))
|
|
|
|
e.checkEOF(t)
|
|
|
|
}
|
|
|
|
t.Run("Alias", func(t *testing.T) {
|
2020-12-13 18:05:49 +00:00
|
|
|
e.Run(t, append(cmd, "--token", "NEO")...)
|
2020-08-29 11:41:33 +00:00
|
|
|
checkResult(t)
|
|
|
|
})
|
|
|
|
t.Run("Hash", func(t *testing.T) {
|
|
|
|
e.Run(t, append(cmd, "--token", e.Chain.GoverningTokenHash().StringLE())...)
|
|
|
|
checkResult(t)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
t.Run("GAS", func(t *testing.T) {
|
2020-12-13 18:05:49 +00:00
|
|
|
e.Run(t, append(cmd, "--token", "GAS")...)
|
2020-09-28 14:24:21 +00:00
|
|
|
e.checkNextLine(t, "^\\s*Account\\s+"+validatorAddr)
|
2020-12-13 18:25:04 +00:00
|
|
|
e.checkNextLine(t, "^\\s*GAS:\\s+GasToken \\("+e.Chain.UtilityTokenHash().StringLE()+"\\)")
|
2020-08-29 11:41:33 +00:00
|
|
|
b := e.Chain.GetUtilityTokenBalance(validatorHash)
|
2021-01-11 12:02:17 +00:00
|
|
|
e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+fixedn.Fixed8(b.Int64()).String()+"$")
|
2020-08-29 11:41:33 +00:00
|
|
|
})
|
2020-09-28 14:24:21 +00:00
|
|
|
t.Run("all accounts", func(t *testing.T) {
|
|
|
|
e.Run(t, cmdbase...)
|
2021-03-05 07:18:03 +00:00
|
|
|
addr1, err := address.StringToUint160("NTh9TnZTstvAePEYWDGLLxidBikJE24uTo")
|
2020-09-28 14:24:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
e.checkNextLine(t, "^Account "+address.Uint160ToString(addr1))
|
2020-12-13 18:25:04 +00:00
|
|
|
e.checkNextLine(t, "^\\s*GAS:\\s+GasToken \\("+e.Chain.UtilityTokenHash().StringLE()+"\\)")
|
2020-09-28 14:24:21 +00:00
|
|
|
balance := e.Chain.GetUtilityTokenBalance(addr1)
|
2021-01-11 12:02:17 +00:00
|
|
|
e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+fixedn.Fixed8(balance.Int64()).String()+"$")
|
2020-09-28 14:24:21 +00:00
|
|
|
e.checkNextLine(t, "^\\s*Updated:")
|
|
|
|
e.checkNextLine(t, "^\\s*$")
|
|
|
|
|
2021-03-09 15:11:21 +00:00
|
|
|
addr2, err := address.StringToUint160("NgEisvCqr2h8wpRxQb7bVPWUZdbVCY8Uo6")
|
2020-09-28 14:24:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
e.checkNextLine(t, "^Account "+address.Uint160ToString(addr2))
|
|
|
|
e.checkNextLine(t, "^\\s*$")
|
|
|
|
|
2021-03-09 15:11:21 +00:00
|
|
|
addr3, err := address.StringToUint160("NNudMSGzEoktFzdYGYoNb3bzHzbmM1genF")
|
2020-09-28 14:24:21 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
e.checkNextLine(t, "^Account "+address.Uint160ToString(addr3))
|
|
|
|
// The order of assets is undefined.
|
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
line := e.getNextLine(t)
|
|
|
|
if strings.Contains(line, "GAS") {
|
2020-12-13 18:25:04 +00:00
|
|
|
e.checkLine(t, line, "^\\s*GAS:\\s+GasToken \\("+e.Chain.UtilityTokenHash().StringLE()+"\\)")
|
2020-09-28 14:24:21 +00:00
|
|
|
balance = e.Chain.GetUtilityTokenBalance(addr3)
|
2021-01-11 12:02:17 +00:00
|
|
|
e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+fixedn.Fixed8(balance.Int64()).String()+"$")
|
2020-09-28 14:24:21 +00:00
|
|
|
e.checkNextLine(t, "^\\s*Updated:")
|
|
|
|
} else {
|
|
|
|
balance, index := e.Chain.GetGoverningTokenBalance(validatorHash)
|
2020-12-13 18:25:04 +00:00
|
|
|
e.checkLine(t, line, "^\\s*NEO:\\s+NeoToken \\("+e.Chain.GoverningTokenHash().StringLE()+"\\)")
|
2021-01-11 12:02:17 +00:00
|
|
|
e.checkNextLine(t, "^\\s*Amount\\s*:\\s*"+balance.String()+"$")
|
2020-09-28 14:24:21 +00:00
|
|
|
e.checkNextLine(t, "^\\s*Updated\\s*:\\s*"+strconv.FormatUint(uint64(index), 10))
|
|
|
|
}
|
|
|
|
}
|
2021-03-02 12:43:09 +00:00
|
|
|
|
|
|
|
e.checkNextLine(t, "^\\s*$")
|
2021-03-09 15:11:21 +00:00
|
|
|
addr4, err := address.StringToUint160("NTe3yHH5zsaEGvEHTsFRpCjTef6Aod4yb6") // deployed verify.go contract
|
2021-03-02 12:43:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
e.checkNextLine(t, "^Account "+address.Uint160ToString(addr4))
|
2020-09-28 14:24:21 +00:00
|
|
|
e.checkEOF(t)
|
|
|
|
})
|
|
|
|
t.Run("Bad token", func(t *testing.T) {
|
|
|
|
e.Run(t, append(cmd, "--token", "kek")...)
|
|
|
|
e.checkNextLine(t, "^\\s*Account\\s+"+validatorAddr)
|
|
|
|
e.checkEOF(t)
|
|
|
|
})
|
|
|
|
t.Run("Bad wallet", func(t *testing.T) {
|
|
|
|
e.RunWithError(t, append(cmdbalance, "--wallet", "/dev/null")...)
|
|
|
|
})
|
|
|
|
t.Run("Bad address", func(t *testing.T) {
|
2020-09-28 15:43:53 +00:00
|
|
|
e.RunWithError(t, append(cmdbalance, "--rpc-endpoint", "http://"+e.RPC.Addr, "--wallet", validatorWallet, "--address", "xxx")...)
|
2020-08-29 11:41:33 +00:00
|
|
|
})
|
|
|
|
return
|
|
|
|
}
|
2020-08-31 09:42:42 +00:00
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func TestNEP17Transfer(t *testing.T) {
|
2020-08-31 09:42:42 +00:00
|
|
|
w, err := wallet.NewWalletFromFile("testdata/testwallet.json")
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
e := newExecutor(t, true)
|
|
|
|
args := []string{
|
2020-11-24 08:14:25 +00:00
|
|
|
"neo-go", "wallet", "nep17", "transfer",
|
2020-08-31 09:42:42 +00:00
|
|
|
"--rpc-endpoint", "http://" + e.RPC.Addr,
|
|
|
|
"--wallet", validatorWallet,
|
|
|
|
"--to", w.Accounts[0].Address,
|
2020-12-13 18:05:49 +00:00
|
|
|
"--token", "NEO",
|
2020-08-31 09:42:42 +00:00
|
|
|
"--amount", "1",
|
2021-01-11 12:58:37 +00:00
|
|
|
"--from", validatorAddr,
|
2020-08-31 09:42:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
t.Run("InvalidPassword", func(t *testing.T) {
|
|
|
|
e.In.WriteString("onetwothree\r")
|
|
|
|
e.RunWithError(t, args...)
|
|
|
|
e.In.Reset()
|
|
|
|
})
|
|
|
|
|
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.Run(t, args...)
|
2020-09-01 13:24:01 +00:00
|
|
|
e.checkTxPersisted(t)
|
2020-08-31 09:42:42 +00:00
|
|
|
|
|
|
|
sh, err := address.StringToUint160(w.Accounts[0].Address)
|
|
|
|
require.NoError(t, err)
|
|
|
|
b, _ := e.Chain.GetGoverningTokenBalance(sh)
|
|
|
|
require.Equal(t, big.NewInt(1), b)
|
2021-01-11 12:58:37 +00:00
|
|
|
|
|
|
|
t.Run("default address", func(t *testing.T) {
|
2021-03-05 07:18:03 +00:00
|
|
|
const validatorDefault = "NTh9TnZTstvAePEYWDGLLxidBikJE24uTo"
|
2021-01-11 12:58:37 +00:00
|
|
|
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)
|
|
|
|
})
|
2020-08-31 09:42:42 +00:00
|
|
|
}
|
2020-09-01 12:20:38 +00:00
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func TestNEP17MultiTransfer(t *testing.T) {
|
2020-09-01 13:24:01 +00:00
|
|
|
privs, _ := generateKeys(t, 3)
|
2020-09-01 12:20:38 +00:00
|
|
|
|
|
|
|
e := newExecutor(t, true)
|
2020-12-13 18:25:04 +00:00
|
|
|
neoContractHash, err := e.Chain.GetNativeContractScriptHash(nativenames.Neo)
|
2020-09-25 16:32:53 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-01 12:20:38 +00:00
|
|
|
args := []string{
|
2020-11-24 08:14:25 +00:00
|
|
|
"neo-go", "wallet", "nep17", "multitransfer",
|
2020-10-14 15:13:20 +00:00
|
|
|
"--rpc-endpoint", "http://" + e.RPC.Addr,
|
2020-09-01 12:20:38 +00:00
|
|
|
"--wallet", validatorWallet,
|
|
|
|
"--from", validatorAddr,
|
2020-12-13 18:05:49 +00:00
|
|
|
"NEO:" + privs[0].Address() + ":42",
|
2020-09-01 12:20:38 +00:00
|
|
|
"GAS:" + privs[1].Address() + ":7",
|
2020-09-25 16:32:53 +00:00
|
|
|
neoContractHash.StringLE() + ":" + privs[2].Address() + ":13",
|
2020-09-01 12:20:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
e.In.WriteString("one\r")
|
|
|
|
e.Run(t, args...)
|
2020-09-01 13:24:01 +00:00
|
|
|
e.checkTxPersisted(t)
|
2020-09-01 12:20:38 +00:00
|
|
|
|
|
|
|
b, _ := e.Chain.GetGoverningTokenBalance(privs[0].GetScriptHash())
|
|
|
|
require.Equal(t, big.NewInt(42), b)
|
|
|
|
b = e.Chain.GetUtilityTokenBalance(privs[1].GetScriptHash())
|
2020-12-01 08:40:58 +00:00
|
|
|
require.Equal(t, big.NewInt(int64(fixedn.Fixed8FromInt64(7))), b)
|
2020-09-01 12:20:38 +00:00
|
|
|
b, _ = e.Chain.GetGoverningTokenBalance(privs[2].GetScriptHash())
|
|
|
|
require.Equal(t, big.NewInt(13), b)
|
|
|
|
}
|
2020-09-01 12:40:32 +00:00
|
|
|
|
2020-11-24 08:14:25 +00:00
|
|
|
func TestNEP17ImportToken(t *testing.T) {
|
2020-09-01 12:40:32 +00:00
|
|
|
e := newExecutor(t, true)
|
|
|
|
|
|
|
|
tmpDir := os.TempDir()
|
|
|
|
walletPath := path.Join(tmpDir, "walletForImport.json")
|
|
|
|
defer os.Remove(walletPath)
|
|
|
|
|
2020-12-13 18:25:04 +00:00
|
|
|
neoContractHash, err := e.Chain.GetNativeContractScriptHash(nativenames.Neo)
|
2020-09-25 16:32:53 +00:00
|
|
|
require.NoError(t, err)
|
2020-12-13 18:25:04 +00:00
|
|
|
gasContractHash, err := e.Chain.GetNativeContractScriptHash(nativenames.Gas)
|
2020-09-25 16:32:53 +00:00
|
|
|
require.NoError(t, err)
|
2020-09-01 12:40:32 +00:00
|
|
|
e.Run(t, "neo-go", "wallet", "init", "--wallet", walletPath)
|
2020-11-24 08:14:25 +00:00
|
|
|
e.Run(t, "neo-go", "wallet", "nep17", "import",
|
2020-09-01 12:40:32 +00:00
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--wallet", walletPath,
|
2020-09-25 16:32:53 +00:00
|
|
|
"--token", gasContractHash.StringLE())
|
2020-11-24 08:14:25 +00:00
|
|
|
e.Run(t, "neo-go", "wallet", "nep17", "import",
|
2020-09-01 12:40:32 +00:00
|
|
|
"--rpc-endpoint", "http://"+e.RPC.Addr,
|
|
|
|
"--wallet", walletPath,
|
2020-09-25 16:32:53 +00:00
|
|
|
"--token", neoContractHash.StringLE())
|
2020-09-01 12:40:32 +00:00
|
|
|
|
|
|
|
t.Run("Info", func(t *testing.T) {
|
|
|
|
checkGASInfo := func(t *testing.T) {
|
2020-12-13 18:25:04 +00:00
|
|
|
e.checkNextLine(t, "^Name:\\s*GasToken")
|
2020-12-13 18:05:49 +00:00
|
|
|
e.checkNextLine(t, "^Symbol:\\s*GAS")
|
2020-09-25 16:32:53 +00:00
|
|
|
e.checkNextLine(t, "^Hash:\\s*"+gasContractHash.StringLE())
|
2020-09-01 12:40:32 +00:00
|
|
|
e.checkNextLine(t, "^Decimals:\\s*8")
|
2020-09-25 16:32:53 +00:00
|
|
|
e.checkNextLine(t, "^Address:\\s*"+address.Uint160ToString(gasContractHash))
|
2020-09-01 12:40:32 +00:00
|
|
|
}
|
|
|
|
t.Run("WithToken", func(t *testing.T) {
|
2020-11-24 08:14:25 +00:00
|
|
|
e.Run(t, "neo-go", "wallet", "nep17", "info",
|
2020-09-25 16:32:53 +00:00
|
|
|
"--wallet", walletPath, "--token", gasContractHash.StringLE())
|
2020-09-01 12:40:32 +00:00
|
|
|
checkGASInfo(t)
|
|
|
|
})
|
|
|
|
t.Run("NoToken", func(t *testing.T) {
|
2020-11-24 08:14:25 +00:00
|
|
|
e.Run(t, "neo-go", "wallet", "nep17", "info",
|
2020-09-01 12:40:32 +00:00
|
|
|
"--wallet", walletPath)
|
|
|
|
checkGASInfo(t)
|
|
|
|
_, err := e.Out.ReadString('\n')
|
|
|
|
require.NoError(t, err)
|
2020-12-13 18:25:04 +00:00
|
|
|
e.checkNextLine(t, "^Name:\\s*NeoToken")
|
2020-12-13 18:05:49 +00:00
|
|
|
e.checkNextLine(t, "^Symbol:\\s*NEO")
|
2020-09-25 16:32:53 +00:00
|
|
|
e.checkNextLine(t, "^Hash:\\s*"+neoContractHash.StringLE())
|
2020-09-01 12:40:32 +00:00
|
|
|
e.checkNextLine(t, "^Decimals:\\s*0")
|
2020-09-25 16:32:53 +00:00
|
|
|
e.checkNextLine(t, "^Address:\\s*"+address.Uint160ToString(neoContractHash))
|
2020-09-01 12:40:32 +00:00
|
|
|
})
|
|
|
|
t.Run("Remove", func(t *testing.T) {
|
|
|
|
e.In.WriteString("y\r")
|
2020-11-24 08:14:25 +00:00
|
|
|
e.Run(t, "neo-go", "wallet", "nep17", "remove",
|
2020-09-25 16:32:53 +00:00
|
|
|
"--wallet", walletPath, "--token", neoContractHash.StringLE())
|
2020-11-24 08:14:25 +00:00
|
|
|
e.Run(t, "neo-go", "wallet", "nep17", "info",
|
2020-09-01 12:40:32 +00:00
|
|
|
"--wallet", walletPath)
|
|
|
|
checkGASInfo(t)
|
|
|
|
_, err := e.Out.ReadString('\n')
|
|
|
|
require.Equal(t, err, io.EOF)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|