rpc: implement getnep5transfers RPC

This commit is contained in:
Evgenii Stratonikov 2020-03-05 15:16:03 +03:00
parent 2757882d26
commit 95a8fa234f
13 changed files with 199 additions and 3 deletions

View file

@ -202,6 +202,10 @@ Methods:
getnep5balancesCalled.Inc()
results, resultsErr = s.getNEP5Balances(reqParams)
case "getnep5transfers":
getnep5transfersCalled.Inc()
results, resultsErr = s.getNEP5Transfers(reqParams)
case "getversion":
getversionCalled.Inc()
results = result.Version{
@ -410,6 +414,47 @@ func (s *Server) getNEP5Balances(ps request.Params) (interface{}, error) {
return bs, nil
}
func (s *Server) getNEP5Transfers(ps request.Params) (interface{}, error) {
p, ok := ps.ValueWithType(0, request.StringT)
if !ok {
return nil, response.ErrInvalidParams
}
u, err := p.GetUint160FromAddress()
if err != nil {
return nil, response.ErrInvalidParams
}
bs := &result.NEP5Transfers{Address: address.Uint160ToString(u)}
lg := s.chain.GetNEP5TransferLog(u)
err = lg.ForEach(func(tr *state.NEP5Transfer) error {
transfer := result.NEP5Transfer{
Timestamp: tr.Timestamp,
Asset: tr.Asset,
Index: tr.Block,
TxHash: tr.Tx,
}
if tr.Amount > 0 { // token was received
transfer.Amount = strconv.FormatInt(tr.Amount, 10)
if !tr.From.Equals(util.Uint160{}) {
transfer.Address = address.Uint160ToString(tr.From)
}
bs.Received = append(bs.Received, transfer)
return nil
}
transfer.Amount = strconv.FormatInt(-tr.Amount, 10)
if !tr.From.Equals(util.Uint160{}) {
transfer.Address = address.Uint160ToString(tr.To)
}
bs.Sent = append(bs.Sent, transfer)
return nil
})
if err != nil {
return nil, response.NewInternalServerError("invalid NEP5 transfer log", err)
}
return bs, nil
}
func (s *Server) getStorage(ps request.Params) (interface{}, error) {
param, ok := ps.Value(0)
if !ok {