From c4c2ce14655f7edc27929c7a1099704beffc8e3e Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Fri, 3 Jul 2020 18:10:07 +0300 Subject: [PATCH] rpc: support stringified address in getnep5balances RPC --- pkg/rpc/server/server.go | 2 +- pkg/rpc/server/server_test.go | 62 ++++++++++++++++++++--------------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/pkg/rpc/server/server.go b/pkg/rpc/server/server.go index 2b974f68c..9c66a05a0 100644 --- a/pkg/rpc/server/server.go +++ b/pkg/rpc/server/server.go @@ -496,7 +496,7 @@ func (s *Server) getApplicationLog(reqParams request.Params) (interface{}, *resp } func (s *Server) getNEP5Balances(ps request.Params) (interface{}, *response.Error) { - u, err := ps.ValueWithType(0, request.StringT).GetUint160FromHex() + u, err := ps.Value(0).GetUint160FromAddressOrHex() if err != nil { return nil, response.ErrInvalidParams } diff --git a/pkg/rpc/server/server_test.go b/pkg/rpc/server/server_test.go index ad409d066..a55e86358 100644 --- a/pkg/rpc/server/server_test.go +++ b/pkg/rpc/server/server_test.go @@ -129,33 +129,13 @@ var rpcTestCases = map[string][]rpcTestCase{ name: "positive", params: `["` + testchain.PrivateKeyByID(0).GetScriptHash().StringLE() + `"]`, result: func(e *executor) interface{} { return &result.NEP5Balances{} }, - check: func(t *testing.T, e *executor, acc interface{}) { - res, ok := acc.(*result.NEP5Balances) - require.True(t, ok) - rubles, err := util.Uint160DecodeStringLE(testContractHash) - require.NoError(t, err) - expected := result.NEP5Balances{ - Balances: []result.NEP5Balance{ - { - Asset: rubles, - Amount: "8.77", - LastUpdated: 6, - }, - { - Asset: e.chain.GoverningTokenHash(), - Amount: "99998000", - LastUpdated: 4, - }, - { - Asset: e.chain.UtilityTokenHash(), - Amount: "915.79002700", - LastUpdated: 6, - }}, - Address: testchain.PrivateKeyByID(0).GetScriptHash().StringLE(), - } - require.Equal(t, testchain.PrivateKeyByID(0).Address(), res.Address) - require.ElementsMatch(t, expected.Balances, res.Balances) - }, + check: checkNep5Balances, + }, + { + name: "positive_address", + params: `["` + address.Uint160ToString(testchain.PrivateKeyByID(0).GetScriptHash()) + `"]`, + result: func(e *executor) interface{} { return &result.NEP5Balances{} }, + check: checkNep5Balances, }, }, "getnep5transfers": { @@ -1110,3 +1090,31 @@ func doRPCCallOverHTTP(rpcCall string, url string, t *testing.T) []byte { assert.NoErrorf(t, err, "could not read response from the request: %s", rpcCall) return bytes.TrimSpace(body) } + +func checkNep5Balances(t *testing.T, e *executor, acc interface{}) { + res, ok := acc.(*result.NEP5Balances) + require.True(t, ok) + rubles, err := util.Uint160DecodeStringLE(testContractHash) + require.NoError(t, err) + expected := result.NEP5Balances{ + Balances: []result.NEP5Balance{ + { + Asset: rubles, + Amount: "8.77", + LastUpdated: 6, + }, + { + Asset: e.chain.GoverningTokenHash(), + Amount: "99998000", + LastUpdated: 4, + }, + { + Asset: e.chain.UtilityTokenHash(), + Amount: "915.79002700", + LastUpdated: 6, + }}, + Address: testchain.PrivateKeyByID(0).GetScriptHash().StringLE(), + } + require.Equal(t, testchain.PrivateKeyByID(0).Address(), res.Address) + require.ElementsMatch(t, expected.Balances, res.Balances) +}