rpc: implement getnep5balances RPC

This commit is contained in:
Evgenii Stratonikov 2020-03-05 14:50:06 +03:00
parent 6d270c4550
commit 2757882d26
7 changed files with 93 additions and 3 deletions

View file

@ -198,6 +198,10 @@ Methods:
getconnectioncountCalled.Inc()
results = s.coreServer.PeerCount()
case "getnep5balances":
getnep5balancesCalled.Inc()
results, resultsErr = s.getNEP5Balances(reqParams)
case "getversion":
getversionCalled.Inc()
results = result.Version{
@ -381,6 +385,31 @@ func (s *Server) getClaimable(ps request.Params) (interface{}, error) {
}, nil
}
func (s *Server) getNEP5Balances(ps request.Params) (interface{}, error) {
p, ok := ps.ValueWithType(0, request.StringT)
if !ok {
return nil, response.ErrInvalidParams
}
u, err := p.GetUint160FromHex()
if err != nil {
return nil, response.ErrInvalidParams
}
as := s.chain.GetAccountState(u)
bs := &result.NEP5Balances{Address: address.Uint160ToString(u)}
if as != nil {
for h, bal := range as.NEP5Balances {
amount := strconv.FormatInt(bal.Balance, 10)
bs.Balances = append(bs.Balances, result.NEP5Balance{
Asset: h,
Amount: amount,
LastUpdated: bal.LastUpdatedBlock,
})
}
}
return bs, nil
}
func (s *Server) getStorage(ps request.Params) (interface{}, error) {
param, ok := ps.Value(0)
if !ok {