From be74cc6b557b37d8255bf9d68be156753cf2f7b0 Mon Sep 17 00:00:00 2001 From: Roman Khimov Date: Fri, 12 Aug 2022 15:30:55 +0300 Subject: [PATCH] cli: use nep17 wrapper to implement commands --- cli/query/query.go | 26 ++++++++++---------------- cli/wallet/nep17.go | 5 ++++- cli/wallet/wallet.go | 10 +++++++++- pkg/rpcclient/unwrap/unwrap.go | 18 ++++++++++-------- 4 files changed, 33 insertions(+), 26 deletions(-) diff --git a/cli/query/query.go b/cli/query/query.go index 226585d8f..2d390e42f 100644 --- a/cli/query/query.go +++ b/cli/query/query.go @@ -19,7 +19,9 @@ import ( "github.com/nspcc-dev/neo-go/pkg/encoding/address" "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/neorpc/result" - "github.com/nspcc-dev/neo-go/pkg/smartcontract" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/unwrap" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/vm" "github.com/nspcc-dev/neo-go/pkg/vm/stackitem" @@ -289,29 +291,21 @@ func queryVoter(ctx *cli.Context) error { if err != nil { return cli.NewExitError(fmt.Errorf("failed to get NEO contract hash: %w", err), 1) } - res, err := c.InvokeFunction(neoHash, "getAccountState", []smartcontract.Parameter{ - { - Type: smartcontract.Hash160Type, - Value: addr, - }, - }, nil) + inv := invoker.New(c, nil) + neoToken := nep17.NewReader(inv, neoHash) + + itm, err := unwrap.Item(inv.Call(neoHash, "getAccountState", addr)) if err != nil { return cli.NewExitError(err, 1) } - if res.State != "HALT" { - return cli.NewExitError(fmt.Errorf("invocation failed: %s", res.FaultException), 1) - } - if len(res.Stack) == 0 { - return cli.NewExitError("result stack is empty", 1) - } st := new(state.NEOBalance) - if _, ok := res.Stack[0].(stackitem.Null); !ok { - err = st.FromStackItem(res.Stack[0]) + if _, ok := itm.(stackitem.Null); !ok { + err = st.FromStackItem(itm) if err != nil { return cli.NewExitError(fmt.Errorf("failed to convert account state from stackitem: %w", err), 1) } } - dec, err := c.NEP17Decimals(neoHash) + dec, err := neoToken.Decimals() if err != nil { return cli.NewExitError(fmt.Errorf("failed to get decimals: %w", err), 1) } diff --git a/cli/wallet/nep17.go b/cli/wallet/nep17.go index 43ff8d418..f7596945b 100644 --- a/cli/wallet/nep17.go +++ b/cli/wallet/nep17.go @@ -17,6 +17,8 @@ import ( "github.com/nspcc-dev/neo-go/pkg/encoding/fixedn" "github.com/nspcc-dev/neo-go/pkg/neorpc/result" "github.com/nspcc-dev/neo-go/pkg/rpcclient" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/invoker" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/util" "github.com/nspcc-dev/neo-go/pkg/wallet" @@ -286,7 +288,8 @@ func getNativeNEP17Symbol(c *rpcclient.Client, name string) (string, util.Uint16 if err != nil { return "", util.Uint160{}, fmt.Errorf("failed to get native %s hash: %w", name, err) } - symbol, err := c.NEP17Symbol(h) + nepTok := nep17.NewReader(invoker.New(c, nil), h) + symbol, err := nepTok.Symbol() if err != nil { return "", util.Uint160{}, fmt.Errorf("failed to get native %s symbol: %w", name, err) } diff --git a/cli/wallet/wallet.go b/cli/wallet/wallet.go index e7ea77d51..21a8d514b 100644 --- a/cli/wallet/wallet.go +++ b/cli/wallet/wallet.go @@ -6,6 +6,7 @@ import ( "errors" "fmt" "io" + "math/big" "os" "strings" @@ -17,6 +18,8 @@ import ( "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/crypto/keys" "github.com/nspcc-dev/neo-go/pkg/encoding/address" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/actor" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17" "github.com/nspcc-dev/neo-go/pkg/smartcontract" "github.com/nspcc-dev/neo-go/pkg/smartcontract/manifest" "github.com/nspcc-dev/neo-go/pkg/vm" @@ -329,11 +332,16 @@ func claimGas(ctx *cli.Context) error { return cli.NewExitError(err, 1) } + act, err := actor.NewSimple(c, acc) + if err != nil { + return cli.NewExitError(err, 1) + } neoContractHash, err := c.GetNativeContractHash(nativenames.Neo) if err != nil { return cli.NewExitError(err, 1) } - hash, err := c.TransferNEP17(acc, scriptHash, neoContractHash, 0, 0, nil, nil) + neoToken := nep17.New(act, neoContractHash) + hash, _, err := neoToken.Transfer(scriptHash, scriptHash, big.NewInt(0), nil) if err != nil { return cli.NewExitError(err, 1) } diff --git a/pkg/rpcclient/unwrap/unwrap.go b/pkg/rpcclient/unwrap/unwrap.go index 25d7961d5..208b041c1 100644 --- a/pkg/rpcclient/unwrap/unwrap.go +++ b/pkg/rpcclient/unwrap/unwrap.go @@ -26,7 +26,7 @@ import ( // BigInt expects correct execution (HALT state) with a single stack item // returned. A big.Int is extracted from this item and returned. func BigInt(r *result.Invoke, err error) (*big.Int, error) { - itm, err := getSingleItem(r, err) + itm, err := Item(r, err) if err != nil { return nil, err } @@ -36,7 +36,7 @@ func BigInt(r *result.Invoke, err error) (*big.Int, error) { // Bool expects correct execution (HALT state) with a single stack item // returned. A bool is extracted from this item and returned. func Bool(r *result.Invoke, err error) (bool, error) { - itm, err := getSingleItem(r, err) + itm, err := Item(r, err) if err != nil { return false, err } @@ -46,7 +46,7 @@ func Bool(r *result.Invoke, err error) (bool, error) { // Int64 expects correct execution (HALT state) with a single stack item // returned. An int64 is extracted from this item and returned. func Int64(r *result.Invoke, err error) (int64, error) { - itm, err := getSingleItem(r, err) + itm, err := Item(r, err) if err != nil { return 0, err } @@ -80,7 +80,7 @@ func LimitedInt64(r *result.Invoke, err error, min int64, max int64) (int64, err // Bytes expects correct execution (HALT state) with a single stack item // returned. A slice of bytes is extracted from this item and returned. func Bytes(r *result.Invoke, err error) ([]byte, error) { - itm, err := getSingleItem(r, err) + itm, err := Item(r, err) if err != nil { return nil, err } @@ -142,7 +142,7 @@ func Uint256(r *result.Invoke, err error) (util.Uint256, error) { // item returned. If this item is an iterator it's returned to the caller along // with the session ID. func SessionIterator(r *result.Invoke, err error) (uuid.UUID, result.Iterator, error) { - itm, err := getSingleItem(r, err) + itm, err := Item(r, err) if err != nil { return uuid.UUID{}, result.Iterator{}, err } @@ -161,7 +161,7 @@ func SessionIterator(r *result.Invoke, err error) (uuid.UUID, result.Iterator, e // be used for structures as well since they're also represented as slices of // stack items (the number of them and their types are structure-specific). func Array(r *result.Invoke, err error) ([]stackitem.Item, error) { - itm, err := getSingleItem(r, err) + itm, err := Item(r, err) if err != nil { return nil, err } @@ -193,7 +193,7 @@ func ArrayOfBytes(r *result.Invoke, err error) ([][]byte, error) { // Map expects correct execution (HALT state) with a single stack item // returned. A stackitem.Map is extracted from this item and returned. func Map(r *result.Invoke, err error) (*stackitem.Map, error) { - itm, err := getSingleItem(r, err) + itm, err := Item(r, err) if err != nil { return nil, err } @@ -213,7 +213,9 @@ func checkResOK(r *result.Invoke, err error) error { return nil } -func getSingleItem(r *result.Invoke, err error) (stackitem.Item, error) { +// Item returns a stack item from the result if execution was successful (HALT +// state) and if it's the only element on the result stack. +func Item(r *result.Invoke, err error) (stackitem.Item, error) { err = checkResOK(r, err) if err != nil { return nil, err