cli: use nep17 wrapper to implement commands

This commit is contained in:
Roman Khimov 2022-08-12 15:30:55 +03:00
parent 309358c85b
commit be74cc6b55
4 changed files with 33 additions and 26 deletions

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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)
}

View file

@ -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