rpc: support stringified address in getnep5balances RPC

This commit is contained in:
Evgenii Stratonikov 2020-07-03 18:10:07 +03:00 committed by Roman Khimov
parent 62bb130ccb
commit c4c2ce1465
2 changed files with 36 additions and 28 deletions

View file

@ -496,7 +496,7 @@ func (s *Server) getApplicationLog(reqParams request.Params) (interface{}, *resp
} }
func (s *Server) getNEP5Balances(ps request.Params) (interface{}, *response.Error) { 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 { if err != nil {
return nil, response.ErrInvalidParams return nil, response.ErrInvalidParams
} }

View file

@ -129,33 +129,13 @@ var rpcTestCases = map[string][]rpcTestCase{
name: "positive", name: "positive",
params: `["` + testchain.PrivateKeyByID(0).GetScriptHash().StringLE() + `"]`, params: `["` + testchain.PrivateKeyByID(0).GetScriptHash().StringLE() + `"]`,
result: func(e *executor) interface{} { return &result.NEP5Balances{} }, result: func(e *executor) interface{} { return &result.NEP5Balances{} },
check: func(t *testing.T, e *executor, acc interface{}) { check: checkNep5Balances,
res, ok := acc.(*result.NEP5Balances) },
require.True(t, ok) {
rubles, err := util.Uint160DecodeStringLE(testContractHash) name: "positive_address",
require.NoError(t, err) params: `["` + address.Uint160ToString(testchain.PrivateKeyByID(0).GetScriptHash()) + `"]`,
expected := result.NEP5Balances{ result: func(e *executor) interface{} { return &result.NEP5Balances{} },
Balances: []result.NEP5Balance{ check: checkNep5Balances,
{
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)
},
}, },
}, },
"getnep5transfers": { "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) assert.NoErrorf(t, err, "could not read response from the request: %s", rpcCall)
return bytes.TrimSpace(body) 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)
}