diff --git a/cli/wallet/nep17.go b/cli/wallet/nep17.go index f7596945b..0ef3463dc 100644 --- a/cli/wallet/nep17.go +++ b/cli/wallet/nep17.go @@ -17,6 +17,7 @@ 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/gas" "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" @@ -241,7 +242,8 @@ func getNEP17Balance(ctx *cli.Context) error { if err != nil { // Try to get native NEP17 with matching symbol. var gasSymbol, neoSymbol string - gasSymbol, h, err = getNativeNEP17Symbol(c, nativenames.Gas) + g := gas.NewReader(invoker.New(c, nil)) + gasSymbol, err = g.Symbol() if err != nil { continue } @@ -253,6 +255,8 @@ func getNEP17Balance(ctx *cli.Context) error { if neoSymbol != name { continue } + } else { + h = gas.Hash } } } diff --git a/pkg/rpcclient/gas/gas.go b/pkg/rpcclient/gas/gas.go new file mode 100644 index 000000000..a19dde6bc --- /dev/null +++ b/pkg/rpcclient/gas/gas.go @@ -0,0 +1,26 @@ +/* +Package gas provides a convenience wrapper for GAS contract to use it via RPC. + +GAS itself only has standard NEP-17 methods, so this package only contains its +hash and allows to create NEP-17 structures in an easier way. +*/ +package gas + +import ( + "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" + "github.com/nspcc-dev/neo-go/pkg/core/state" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/nep17" +) + +// Hash stores the hash of the native GAS contract. +var Hash = state.CreateNativeContractHash(nativenames.Gas) + +// NewReader creates a NEP-17 reader for the GAS contract. +func NewReader(invoker nep17.Invoker) *nep17.TokenReader { + return nep17.NewReader(invoker, Hash) +} + +// New creates a NEP-17 contract instance for the native GAS contract. +func New(actor nep17.Actor) *nep17.Token { + return nep17.New(actor, Hash) +} diff --git a/pkg/rpcclient/gas/gas_test.go b/pkg/rpcclient/gas/gas_test.go new file mode 100644 index 000000000..eb49affda --- /dev/null +++ b/pkg/rpcclient/gas/gas_test.go @@ -0,0 +1,40 @@ +package gas + +import ( + "testing" + + "github.com/nspcc-dev/neo-go/pkg/core/transaction" + "github.com/nspcc-dev/neo-go/pkg/neorpc/result" + "github.com/nspcc-dev/neo-go/pkg/util" + "github.com/stretchr/testify/require" +) + +type testAct struct { + err error + res *result.Invoke + tx *transaction.Transaction + txh util.Uint256 + vub uint32 +} + +func (t *testAct) Call(contract util.Uint160, operation string, params ...interface{}) (*result.Invoke, error) { + return t.res, t.err +} +func (t *testAct) MakeRun(script []byte) (*transaction.Transaction, error) { + return t.tx, t.err +} +func (t *testAct) MakeUnsignedRun(script []byte, attrs []transaction.Attribute) (*transaction.Transaction, error) { + return t.tx, t.err +} +func (t *testAct) SendRun(script []byte) (util.Uint256, uint32, error) { + return t.txh, t.vub, t.err +} + +func TestNew(t *testing.T) { + ta := &testAct{} + gr := NewReader(ta) + require.NotNil(t, gr) + + g := New(ta) + require.NotNil(t, g) +} diff --git a/pkg/services/rpcsrv/client_test.go b/pkg/services/rpcsrv/client_test.go index e6d54d6a2..116f02ab3 100644 --- a/pkg/services/rpcsrv/client_test.go +++ b/pkg/services/rpcsrv/client_test.go @@ -20,7 +20,6 @@ import ( "github.com/nspcc-dev/neo-go/pkg/config" "github.com/nspcc-dev/neo-go/pkg/core" "github.com/nspcc-dev/neo-go/pkg/core/fee" - "github.com/nspcc-dev/neo-go/pkg/core/native/nativenames" "github.com/nspcc-dev/neo-go/pkg/core/native/noderoles" "github.com/nspcc-dev/neo-go/pkg/core/transaction" "github.com/nspcc-dev/neo-go/pkg/crypto/hash" @@ -31,6 +30,7 @@ import ( "github.com/nspcc-dev/neo-go/pkg/network" "github.com/nspcc-dev/neo-go/pkg/rpcclient" "github.com/nspcc-dev/neo-go/pkg/rpcclient/actor" + "github.com/nspcc-dev/neo-go/pkg/rpcclient/gas" "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/nns" @@ -781,14 +781,11 @@ func TestCreateNEP17TransferTx(t *testing.T) { acc := wallet.NewAccountFromPrivateKey(priv) addr := priv.PublicKey().GetScriptHash() - gasContractHash, err := c.GetNativeContractHash(nativenames.Gas) - require.NoError(t, err) - t.Run("default scope", func(t *testing.T) { act, err := actor.NewSimple(c, acc) require.NoError(t, err) - gas := nep17.New(act, gasContractHash) - tx, err := gas.TransferUnsigned(addr, util.Uint160{}, big.NewInt(1000), nil) + gasprom := gas.New(act) + tx, err := gasprom.TransferUnsigned(addr, util.Uint160{}, big.NewInt(1000), nil) require.NoError(t, err) require.NoError(t, acc.SignTx(testchain.Network(), tx)) require.NoError(t, chain.VerifyTx(tx)) @@ -805,8 +802,8 @@ func TestCreateNEP17TransferTx(t *testing.T) { Account: acc, }}) require.NoError(t, err) - gas := nep17.New(act, gasContractHash) - _, err = gas.TransferUnsigned(addr, util.Uint160{}, big.NewInt(1000), nil) + gasprom := gas.New(act) + _, err = gasprom.TransferUnsigned(addr, util.Uint160{}, big.NewInt(1000), nil) require.Error(t, err) }) t.Run("customcontracts scope", func(t *testing.T) { @@ -814,13 +811,13 @@ func TestCreateNEP17TransferTx(t *testing.T) { Signer: transaction.Signer{ Account: priv.PublicKey().GetScriptHash(), Scopes: transaction.CustomContracts, - AllowedContracts: []util.Uint160{gasContractHash}, + AllowedContracts: []util.Uint160{gas.Hash}, }, Account: acc, }}) require.NoError(t, err) - gas := nep17.New(act, gasContractHash) - tx, err := gas.TransferUnsigned(addr, util.Uint160{}, big.NewInt(1000), nil) + gasprom := gas.New(act) + tx, err := gasprom.TransferUnsigned(addr, util.Uint160{}, big.NewInt(1000), nil) require.NoError(t, err) require.NoError(t, acc.SignTx(testchain.Network(), tx)) require.NoError(t, chain.VerifyTx(tx))