cli: add nep11 [tokens | tokensOf | ownerOf] commands

This commit is contained in:
Anna Shaleva 2021-04-28 17:50:12 +03:00
parent e27c894338
commit ba7ebc2390
2 changed files with 143 additions and 21 deletions

View file

@ -115,6 +115,7 @@ func TestNEP11_OwnerOf_BalanceOf_Transfer(t *testing.T) {
// deploy NFT HASHY contract // deploy NFT HASHY contract
h := deployNFTContract(t, e) h := deployNFTContract(t, e)
mint := func(t *testing.T) []byte {
// mint 1 HASHY token by transferring 10 GAS to HASHY contract // mint 1 HASHY token by transferring 10 GAS to HASHY contract
e.In.WriteString(nftOwnerPass + "\r") e.In.WriteString(nftOwnerPass + "\r")
e.Run(t, "neo-go", "wallet", "nep17", "transfer", e.Run(t, "neo-go", "wallet", "nep17", "transfer",
@ -136,6 +137,10 @@ func TestNEP11_OwnerOf_BalanceOf_Transfer(t *testing.T) {
tokenID, err := hashyMintEvent.Item.Value().([]stackitem.Item)[3].TryBytes() tokenID, err := hashyMintEvent.Item.Value().([]stackitem.Item)[3].TryBytes()
require.NoError(t, err) require.NoError(t, err)
require.NotNil(t, tokenID) require.NotNil(t, tokenID)
return tokenID
}
tokenID := mint(t)
// check the balance // check the balance
cmdCheckBalance := []string{"neo-go", "wallet", "nep11", "balance", cmdCheckBalance := []string{"neo-go", "wallet", "nep11", "balance",
@ -191,6 +196,43 @@ func TestNEP11_OwnerOf_BalanceOf_Transfer(t *testing.T) {
e.Run(t, cmdOwnerOf...) e.Run(t, cmdOwnerOf...)
e.checkNextLine(t, nftOwnerAddr) e.checkNextLine(t, nftOwnerAddr)
// tokensOf: missing contract hash
cmdTokensOf := []string{"neo-go", "wallet", "nep11", "tokensOf",
"--rpc-endpoint", "http://" + e.RPC.Addr,
}
e.RunWithError(t, cmdTokensOf...)
cmdTokensOf = append(cmdTokensOf, "--token", h.StringLE())
// tokensOf: missing owner address
e.RunWithError(t, cmdTokensOf...)
cmdTokensOf = append(cmdTokensOf, "--address", nftOwnerAddr)
// tokensOf: good
e.Run(t, cmdTokensOf...)
e.checkNextLine(t, string(tokenID))
// tokensOf: good, several tokens
tokenID1 := mint(t)
e.Run(t, cmdTokensOf...)
e.checkNextLine(t, string(tokenID))
e.checkNextLine(t, string(tokenID1))
// tokens: missing contract hash
cmdTokens := []string{"neo-go", "wallet", "nep11", "tokens",
"--rpc-endpoint", "http://" + e.RPC.Addr,
}
e.RunWithError(t, cmdTokens...)
cmdTokens = append(cmdTokens, "--token", h.StringLE())
// tokens: good, several tokens
e.Run(t, cmdTokens...)
e.checkNextLine(t, string(tokenID))
e.checkNextLine(t, string(tokenID1))
// balance check: several tokens, ok
e.Run(t, append(cmdCheckBalance, "--token", h.StringLE())...)
checkBalanceResult(t, nftOwnerAddr, "2")
cmdTransfer := []string{ cmdTransfer := []string{
"neo-go", "wallet", "nep11", "transfer", "neo-go", "wallet", "nep11", "transfer",
"--rpc-endpoint", "http://" + e.RPC.Addr, "--rpc-endpoint", "http://" + e.RPC.Addr,
@ -217,7 +259,7 @@ func TestNEP11_OwnerOf_BalanceOf_Transfer(t *testing.T) {
// check balance after transfer // check balance after transfer
e.Run(t, append(cmdCheckBalance, "--token", h.StringLE())...) e.Run(t, append(cmdCheckBalance, "--token", h.StringLE())...)
checkBalanceResult(t, nftOwnerAddr, "0") checkBalanceResult(t, nftOwnerAddr, "1") // tokenID1
} }
func deployNFTContract(t *testing.T, e *executor) util.Uint160 { func deployNFTContract(t *testing.T, e *executor) util.Uint160 {

View file

@ -23,6 +23,10 @@ func newNEP11Commands() []cli.Command {
Name: "token", Name: "token",
Usage: "Token contract address or hash in LE", Usage: "Token contract address or hash in LE",
} }
ownerAddressFlag := flags.AddressFlag{
Name: "address",
Usage: "NFT owner address or hash in LE",
}
tokenID := cli.StringFlag{ tokenID := cli.StringFlag{
Name: "id", Name: "id",
Usage: "Token ID", Usage: "Token ID",
@ -97,6 +101,25 @@ func newNEP11Commands() []cli.Command {
tokenID, tokenID,
}, options.RPC...), }, options.RPC...),
}, },
{
Name: "tokensOf",
Usage: "print list of tokens IDs for the specified divisible NFT owner",
UsageText: "tokensOf --rpc-endpoint <node> --timeout <time> --token <hash> --address <addr>",
Action: printNEP11TokensOf,
Flags: append([]cli.Flag{
tokenAddressFlag,
ownerAddressFlag,
}, options.RPC...),
},
{
Name: "tokens",
Usage: "print list of tokens IDs minted by the specified NFT (optional method)",
UsageText: "tokens --rpc-endpoint <node> --timeout <time> --token <hash>",
Action: printNEP11Tokens,
Flags: append([]cli.Flag{
tokenAddressFlag,
}, options.RPC...),
},
} }
} }
@ -264,3 +287,60 @@ func printNEP11Owner(ctx *cli.Context) error {
fmt.Fprintln(ctx.App.Writer, address.Uint160ToString(result)) fmt.Fprintln(ctx.App.Writer, address.Uint160ToString(result))
return nil return nil
} }
func printNEP11TokensOf(ctx *cli.Context) error {
var err error
tokenHash := ctx.Generic("token").(*flags.Address)
if !tokenHash.IsSet {
return cli.NewExitError("token contract hash was not set", 1)
}
acc := ctx.Generic("address").(*flags.Address)
if !acc.IsSet {
return cli.NewExitError("owner address flag was not set", 1)
}
gctx, cancel := options.GetTimeoutContext(ctx)
defer cancel()
c, err := options.GetRPCClient(gctx, ctx)
if err != nil {
return cli.NewExitError(err, 1)
}
result, err := c.NEP11TokensOf(tokenHash.Uint160(), acc.Uint160())
if err != nil {
return cli.NewExitError(fmt.Sprintf("failed to call NEP11 `tokensOf` method: %s", err.Error()), 1)
}
for i := range result {
fmt.Fprintln(ctx.App.Writer, result[i])
}
return nil
}
func printNEP11Tokens(ctx *cli.Context) error {
var err error
tokenHash := ctx.Generic("token").(*flags.Address)
if !tokenHash.IsSet {
return cli.NewExitError("token contract hash was not set", 1)
}
gctx, cancel := options.GetTimeoutContext(ctx)
defer cancel()
c, err := options.GetRPCClient(gctx, ctx)
if err != nil {
return cli.NewExitError(err, 1)
}
result, err := c.NEP11Tokens(tokenHash.Uint160())
if err != nil {
return cli.NewExitError(fmt.Sprintf("failed to call optional NEP11 `tokens` method: %s", err.Error()), 1)
}
for i := range result {
fmt.Fprintln(ctx.App.Writer, result[i])
}
return nil
}