cli: add nep11 properties
command
This commit is contained in:
parent
ba7ebc2390
commit
f61ab6bd11
4 changed files with 87 additions and 1 deletions
|
@ -1,6 +1,7 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
|
@ -211,6 +212,22 @@ func TestNEP11_OwnerOf_BalanceOf_Transfer(t *testing.T) {
|
|||
e.Run(t, cmdTokensOf...)
|
||||
e.checkNextLine(t, string(tokenID))
|
||||
|
||||
// properties: no contract
|
||||
cmdProperties := []string{
|
||||
"neo-go", "wallet", "nep11", "properties",
|
||||
"--rpc-endpoint", "http://" + e.RPC.Addr,
|
||||
}
|
||||
e.RunWithError(t, cmdProperties...)
|
||||
cmdProperties = append(cmdProperties, "--token", h.StringLE())
|
||||
|
||||
// properties: no token ID
|
||||
e.RunWithError(t, cmdProperties...)
|
||||
cmdProperties = append(cmdProperties, "--id", string(tokenID))
|
||||
|
||||
// properties: ok
|
||||
e.Run(t, cmdProperties...)
|
||||
e.checkNextLine(t, fmt.Sprintf(`{"name":"HASHY %s"}`, string(tokenID)))
|
||||
|
||||
// tokensOf: good, several tokens
|
||||
tokenID1 := mint(t)
|
||||
e.Run(t, cmdTokensOf...)
|
||||
|
|
|
@ -14,6 +14,7 @@ import (
|
|||
"github.com/nspcc-dev/neo-go/pkg/rpc/client"
|
||||
"github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util"
|
||||
"github.com/nspcc-dev/neo-go/pkg/vm/stackitem"
|
||||
"github.com/nspcc-dev/neo-go/pkg/wallet"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
@ -91,6 +92,16 @@ func newNEP11Commands() []cli.Command {
|
|||
signer.
|
||||
`,
|
||||
},
|
||||
{
|
||||
Name: "properties",
|
||||
Usage: "print properties of NEP11 token",
|
||||
UsageText: "properties --rpc-endpoint <node> --timeout <time> --token <hash> --id <token-id>",
|
||||
Action: printNEP11Properties,
|
||||
Flags: append([]cli.Flag{
|
||||
tokenAddressFlag,
|
||||
tokenID,
|
||||
}, options.RPC...),
|
||||
},
|
||||
{
|
||||
Name: "ownerOf",
|
||||
Usage: "print owner of non-divisible NEP11 token with the specified ID",
|
||||
|
@ -344,3 +355,36 @@ func printNEP11Tokens(ctx *cli.Context) error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func printNEP11Properties(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)
|
||||
}
|
||||
|
||||
tokenID := ctx.String("id")
|
||||
if tokenID == "" {
|
||||
return cli.NewExitError(errors.New("token ID should be specified"), 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.NEP11Properties(tokenHash.Uint160(), tokenID)
|
||||
if err != nil {
|
||||
return cli.NewExitError(fmt.Sprintf("failed to call NEP11 `properties` method: %s", err.Error()), 1)
|
||||
}
|
||||
|
||||
bytes, err := stackitem.ToJSON(result)
|
||||
if err != nil {
|
||||
return cli.NewExitError(fmt.Sprintf("failed to convert result to JSON: %s", err), 1)
|
||||
}
|
||||
fmt.Fprintln(ctx.App.Writer, string(bytes))
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -284,3 +284,28 @@ func Update(nef, manifest []byte) {
|
|||
}
|
||||
management.Update(nef, manifest)
|
||||
}
|
||||
|
||||
// Properties returns properties of the given NFT.
|
||||
func Properties(id []byte) map[string]string {
|
||||
ctx := storage.GetReadOnlyContext()
|
||||
var tokens = []string{}
|
||||
key := mkTokensKey()
|
||||
val := storage.Get(ctx, key)
|
||||
if val != nil {
|
||||
tokens = std.Deserialize(val.([]byte)).([]string)
|
||||
}
|
||||
var exists bool
|
||||
for i := 0; i < len(tokens); i++ {
|
||||
if util.Equals(tokens[i], id) {
|
||||
exists = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !exists {
|
||||
panic("unknown token")
|
||||
}
|
||||
result := map[string]string{
|
||||
"name": "HASHY " + string(id),
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
name: "HASHY NFT"
|
||||
supportedstandards: ["NEP-11"]
|
||||
safemethods: ["balanceOf", "decimals", "symbol", "totalSupply", "tokensOf", "ownerOf", "tokens"]
|
||||
safemethods: ["balanceOf", "decimals", "symbol", "totalSupply", "tokensOf", "ownerOf", "tokens", "properties"]
|
||||
events:
|
||||
- name: Transfer
|
||||
parameters:
|
||||
|
|
Loading…
Reference in a new issue