rpc: return raw values in getnep5*

C# nodes format result without using decimals.
This commit is contained in:
Evgenii Stratonikov 2020-08-03 10:48:12 +03:00
parent c6d33c5841
commit c4c9f9225c
2 changed files with 8 additions and 78 deletions

View file

@ -5,7 +5,6 @@ import (
"encoding/hex"
"encoding/json"
"fmt"
"math"
"net"
"net/http"
"strconv"
@ -27,9 +26,7 @@ import (
"github.com/nspcc-dev/neo-go/pkg/rpc/request"
"github.com/nspcc-dev/neo-go/pkg/rpc/response"
"github.com/nspcc-dev/neo-go/pkg/rpc/response/result"
"github.com/nspcc-dev/neo-go/pkg/smartcontract"
"github.com/nspcc-dev/neo-go/pkg/util"
"github.com/nspcc-dev/neo-go/pkg/vm/emit"
"github.com/pkg/errors"
"go.uber.org/zap"
)
@ -577,13 +574,8 @@ func (s *Server) getNEP5Balances(ps request.Params) (interface{}, *response.Erro
Balances: []result.NEP5Balance{},
}
if as != nil {
cache := make(map[util.Uint160]int64)
for h, bal := range as.Trackers {
dec, err := s.getDecimals(h, cache)
if err != nil {
continue
}
amount := amountToString(bal.Balance, dec)
amount := strconv.FormatInt(bal.Balance, 10)
bs.Balances = append(bs.Balances, result.NEP5Balance{
Asset: h,
Amount: amount,
@ -606,7 +598,6 @@ func (s *Server) getNEP5Transfers(ps request.Params) (interface{}, *response.Err
Sent: []result.NEP5Transfer{},
}
lg := s.chain.GetNEP5TransferLog(u)
cache := make(map[util.Uint160]int64)
err = lg.ForEach(func(tr *state.NEP5Transfer) error {
transfer := result.NEP5Transfer{
Timestamp: tr.Timestamp,
@ -616,12 +607,8 @@ func (s *Server) getNEP5Transfers(ps request.Params) (interface{}, *response.Err
NotifyIndex: tr.Index,
}
d, err := s.getDecimals(tr.Asset, cache)
if err != nil {
return nil
}
if tr.Amount > 0 { // token was received
transfer.Amount = amountToString(tr.Amount, d)
transfer.Amount = strconv.FormatInt(tr.Amount, 10)
if !tr.From.Equals(util.Uint160{}) {
transfer.Address = address.Uint160ToString(tr.From)
}
@ -629,7 +616,7 @@ func (s *Server) getNEP5Transfers(ps request.Params) (interface{}, *response.Err
return nil
}
transfer.Amount = amountToString(-tr.Amount, d)
transfer.Amount = strconv.FormatInt(-tr.Amount, 10)
if !tr.To.Equals(util.Uint160{}) {
transfer.Address = address.Uint160ToString(tr.To)
}
@ -642,63 +629,6 @@ func (s *Server) getNEP5Transfers(ps request.Params) (interface{}, *response.Err
return bs, nil
}
func amountToString(amount int64, decimals int64) string {
if decimals == 0 {
return strconv.FormatInt(amount, 10)
}
pow := int64(math.Pow10(int(decimals)))
q := amount / pow
r := amount % pow
if r == 0 {
return strconv.FormatInt(q, 10)
}
fs := fmt.Sprintf("%%d.%%0%dd", decimals)
return fmt.Sprintf(fs, q, r)
}
func (s *Server) getDecimals(h util.Uint160, cache map[util.Uint160]int64) (int64, *response.Error) {
if d, ok := cache[h]; ok {
return d, nil
}
m, err := s.chain.GetNEP5Metadata(h)
if err == nil {
cache[h] = m.Decimals
return m.Decimals, nil
}
script, err := request.CreateFunctionInvocationScript(h, request.Params{
{
Type: request.StringT,
Value: "decimals",
},
{
Type: request.ArrayT,
Value: []request.Param{},
},
})
if err != nil {
return 0, response.NewInternalServerError("Can't create script", err)
}
res := s.runScriptInVM(script)
if res == nil || res.State != "HALT" || len(res.Stack) == 0 {
return 0, response.NewInternalServerError("execution error", errors.New("no result"))
}
var d int64
switch item := res.Stack[len(res.Stack)-1]; item.Type {
case smartcontract.IntegerType:
d = item.Value.(int64)
case smartcontract.ByteArrayType:
d = emit.BytesToInt(item.Value.([]byte)).Int64()
default:
return 0, response.NewInternalServerError("invalid result", errors.New("not an integer"))
}
if d < 0 {
return 0, response.NewInternalServerError("incorrect result", errors.New("negative result"))
}
cache[h] = d
return d, nil
}
func (s *Server) getProof(ps request.Params) (interface{}, *response.Error) {
root, err := ps.Value(0).GetUint256()
if err != nil {

View file

@ -1153,7 +1153,7 @@ func checkNep5Balances(t *testing.T, e *executor, acc interface{}) {
require.True(t, ok)
require.Equal(t, "AKkkumHbBipZ46UMZJoFynJMXzSRnBvKcs", res.Address)
require.Equal(t, 1, len(res.Balances))
require.Equal(t, "8.80", res.Balances[0].Amount)
require.Equal(t, "880", res.Balances[0].Amount)
require.Equal(t, testContractHash, res.Balances[0].Asset.StringLE())
require.Equal(t, uint32(210), res.Balances[0].LastUpdated)
}
@ -1170,22 +1170,22 @@ func checkNep5Transfers(t *testing.T, e *executor, acc interface{}) {
require.NoError(t, err)
require.Equal(t, 3, len(res.Received))
require.Equal(t, "10", res.Received[0].Amount)
require.Equal(t, "1000", res.Received[0].Amount)
require.Equal(t, assetHashOld, res.Received[0].Asset)
require.Equal(t, address.Uint160ToString(assetHashOld), res.Received[0].Address)
require.Equal(t, "0.02", res.Received[1].Amount)
require.Equal(t, "2", res.Received[1].Amount)
require.Equal(t, assetHash, res.Received[1].Asset)
require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Received[1].Address)
require.Equal(t, uint32(0), res.Received[1].NotifyIndex)
require.Equal(t, "0.01", res.Received[2].Amount)
require.Equal(t, "1", res.Received[2].Amount)
require.Equal(t, assetHash, res.Received[2].Asset)
require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Received[2].Address)
require.Equal(t, uint32(1), res.Received[2].NotifyIndex)
require.Equal(t, 1, len(res.Sent))
require.Equal(t, "1.23", res.Sent[0].Amount)
require.Equal(t, "123", res.Sent[0].Amount)
require.Equal(t, assetHashOld, res.Sent[0].Asset)
require.Equal(t, "AWLYWXB8C9Lt1nHdDZJnC5cpYJjgRDLk17", res.Sent[0].Address)
require.Equal(t, uint32(0), res.Sent[0].NotifyIndex)