rpc: provide timestamps in getnep5transfers
Set default value for the first timestamp to a week ago.
This commit is contained in:
parent
807338f97e
commit
412fc53cdd
2 changed files with 89 additions and 13 deletions
|
@ -530,12 +530,43 @@ func (s *Server) getNEP5Balances(ps request.Params) (interface{}, *response.Erro
|
||||||
return bs, nil
|
return bs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func getTimestamps(p1, p2 *request.Param) (uint64, uint64, error) {
|
||||||
|
var start, end uint64
|
||||||
|
if p1 != nil {
|
||||||
|
val, err := p1.GetInt()
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
start = uint64(val)
|
||||||
|
}
|
||||||
|
if p2 != nil {
|
||||||
|
val, err := p2.GetInt()
|
||||||
|
if err != nil {
|
||||||
|
return 0, 0, err
|
||||||
|
}
|
||||||
|
end = uint64(val)
|
||||||
|
}
|
||||||
|
return start, end, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Server) getNEP5Transfers(ps request.Params) (interface{}, *response.Error) {
|
func (s *Server) getNEP5Transfers(ps request.Params) (interface{}, *response.Error) {
|
||||||
u, err := ps.Value(0).GetUint160FromAddressOrHex()
|
u, err := ps.Value(0).GetUint160FromAddressOrHex()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, response.ErrInvalidParams
|
return nil, response.ErrInvalidParams
|
||||||
}
|
}
|
||||||
|
|
||||||
|
p1, p2 := ps.Value(1), ps.Value(2)
|
||||||
|
start, end, err := getTimestamps(p1, p2)
|
||||||
|
if err != nil {
|
||||||
|
return nil, response.NewInvalidParamsError(err.Error(), err)
|
||||||
|
}
|
||||||
|
if p2 == nil {
|
||||||
|
end = uint64(time.Now().Unix() * 1000)
|
||||||
|
if p1 == nil {
|
||||||
|
start = uint64(time.Now().Add(-time.Hour*24*7).Unix() * 1000)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
bs := &result.NEP5Transfers{
|
bs := &result.NEP5Transfers{
|
||||||
Address: address.Uint160ToString(u),
|
Address: address.Uint160ToString(u),
|
||||||
Received: []result.NEP5Transfer{},
|
Received: []result.NEP5Transfer{},
|
||||||
|
@ -543,6 +574,9 @@ func (s *Server) getNEP5Transfers(ps request.Params) (interface{}, *response.Err
|
||||||
}
|
}
|
||||||
cache := make(map[int32]decimals)
|
cache := make(map[int32]decimals)
|
||||||
err = s.chain.ForEachNEP5Transfer(u, func(tr *state.NEP5Transfer) error {
|
err = s.chain.ForEachNEP5Transfer(u, func(tr *state.NEP5Transfer) error {
|
||||||
|
if tr.Timestamp < start || tr.Timestamp > end {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
d, err := s.getDecimals(tr.Asset, cache)
|
d, err := s.getDecimals(tr.Asset, cache)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -10,6 +10,7 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
@ -149,15 +150,20 @@ var rpcTestCases = map[string][]rpcTestCase{
|
||||||
params: `["notahex"]`,
|
params: `["notahex"]`,
|
||||||
fail: true,
|
fail: true,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "invalid timestamp",
|
||||||
|
params: `["` + testchain.PrivateKeyByID(0).Address() + `", "notanumber"]`,
|
||||||
|
fail: true,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "positive",
|
name: "positive",
|
||||||
params: `["` + testchain.PrivateKeyByID(0).Address() + `"]`,
|
params: `["` + testchain.PrivateKeyByID(0).Address() + `", 0]`,
|
||||||
result: func(e *executor) interface{} { return &result.NEP5Transfers{} },
|
result: func(e *executor) interface{} { return &result.NEP5Transfers{} },
|
||||||
check: checkNep5Transfers,
|
check: checkNep5Transfers,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
name: "positive_hash",
|
name: "positive_hash",
|
||||||
params: `["` + testchain.PrivateKeyByID(0).GetScriptHash().StringLE() + `"]`,
|
params: `["` + testchain.PrivateKeyByID(0).GetScriptHash().StringLE() + `", 0]`,
|
||||||
result: func(e *executor) interface{} { return &result.NEP5Transfers{} },
|
result: func(e *executor) interface{} { return &result.NEP5Transfers{} },
|
||||||
check: checkNep5Transfers,
|
check: checkNep5Transfers,
|
||||||
},
|
},
|
||||||
|
@ -887,6 +893,24 @@ func testRPCProtocol(t *testing.T, doRPCCall func(string, string, *testing.T) []
|
||||||
|
|
||||||
assert.ElementsMatch(t, expected, actual)
|
assert.ElementsMatch(t, expected, actual)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
t.Run("getnep5transfers", func(t *testing.T) {
|
||||||
|
ps := []string{`"` + testchain.PrivateKeyByID(0).Address() + `"`}
|
||||||
|
h, err := e.chain.GetHeader(e.chain.GetHeaderHash(4))
|
||||||
|
require.NoError(t, err)
|
||||||
|
ps = append(ps, strconv.FormatUint(h.Timestamp, 10))
|
||||||
|
h, err = e.chain.GetHeader(e.chain.GetHeaderHash(5))
|
||||||
|
require.NoError(t, err)
|
||||||
|
ps = append(ps, strconv.FormatUint(h.Timestamp, 10))
|
||||||
|
|
||||||
|
p := strings.Join(ps, ", ")
|
||||||
|
rpc := fmt.Sprintf(`{"jsonrpc": "2.0", "id": 1, "method": "getnep5transfers", "params": [%s]}`, p)
|
||||||
|
body := doRPCCall(rpc, httpSrv.URL, t)
|
||||||
|
res := checkErrGetResult(t, body, false)
|
||||||
|
actual := new(result.NEP5Transfers)
|
||||||
|
require.NoError(t, json.Unmarshal(res, actual))
|
||||||
|
checkNep5TransfersAux(t, e, actual, 4, 5)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *executor) getHeader(s string) *block.Header {
|
func (e *executor) getHeader(s string) *block.Header {
|
||||||
|
@ -1011,6 +1035,10 @@ func checkNep5Balances(t *testing.T, e *executor, acc interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkNep5Transfers(t *testing.T, e *executor, acc interface{}) {
|
func checkNep5Transfers(t *testing.T, e *executor, acc interface{}) {
|
||||||
|
checkNep5TransfersAux(t, e, acc, 0, e.chain.HeaderHeight())
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkNep5TransfersAux(t *testing.T, e *executor, acc interface{}, start, end uint32) {
|
||||||
res, ok := acc.(*result.NEP5Transfers)
|
res, ok := acc.(*result.NEP5Transfers)
|
||||||
require.True(t, ok)
|
require.True(t, ok)
|
||||||
rublesHash, err := util.Uint160DecodeStringLE(testContractHash)
|
rublesHash, err := util.Uint160DecodeStringLE(testContractHash)
|
||||||
|
@ -1034,15 +1062,6 @@ func checkNep5Transfers(t *testing.T, e *executor, acc interface{}) {
|
||||||
txSendNEOHash := blockSendNEO.Transactions[0].Hash()
|
txSendNEOHash := blockSendNEO.Transactions[0].Hash()
|
||||||
expected := result.NEP5Transfers{
|
expected := result.NEP5Transfers{
|
||||||
Sent: []result.NEP5Transfer{
|
Sent: []result.NEP5Transfer{
|
||||||
{
|
|
||||||
Timestamp: blockSendRubles.Timestamp,
|
|
||||||
Asset: rublesHash,
|
|
||||||
Address: testchain.PrivateKeyByID(1).Address(),
|
|
||||||
Amount: "1.23",
|
|
||||||
Index: 6,
|
|
||||||
NotifyIndex: 0,
|
|
||||||
TxHash: txSendRublesHash,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
Timestamp: blockSendNEO.Timestamp,
|
Timestamp: blockSendNEO.Timestamp,
|
||||||
Asset: e.chain.GoverningTokenHash(),
|
Asset: e.chain.GoverningTokenHash(),
|
||||||
|
@ -1052,6 +1071,15 @@ func checkNep5Transfers(t *testing.T, e *executor, acc interface{}) {
|
||||||
NotifyIndex: 0,
|
NotifyIndex: 0,
|
||||||
TxHash: txSendNEOHash,
|
TxHash: txSendNEOHash,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Timestamp: blockSendRubles.Timestamp,
|
||||||
|
Asset: rublesHash,
|
||||||
|
Address: testchain.PrivateKeyByID(1).Address(),
|
||||||
|
Amount: "1.23",
|
||||||
|
Index: 6,
|
||||||
|
NotifyIndex: 0,
|
||||||
|
TxHash: txSendRublesHash,
|
||||||
|
},
|
||||||
},
|
},
|
||||||
Received: []result.NEP5Transfer{
|
Received: []result.NEP5Transfer{
|
||||||
{
|
{
|
||||||
|
@ -1127,6 +1155,20 @@ func checkNep5Transfers(t *testing.T, e *executor, acc interface{}) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
require.Equal(t, expected.Address, res.Address)
|
require.Equal(t, expected.Address, res.Address)
|
||||||
require.ElementsMatch(t, expected.Sent, res.Sent)
|
|
||||||
require.ElementsMatch(t, expected.Received, res.Received)
|
arr := make([]result.NEP5Transfer, 0, len(expected.Sent))
|
||||||
|
for i := range expected.Sent {
|
||||||
|
if expected.Sent[i].Index >= start && expected.Sent[i].Index <= end {
|
||||||
|
arr = append(arr, expected.Sent[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
require.ElementsMatch(t, arr, res.Sent)
|
||||||
|
|
||||||
|
arr = arr[:0]
|
||||||
|
for i := range expected.Received {
|
||||||
|
if expected.Received[i].Index >= start && expected.Received[i].Index <= end {
|
||||||
|
arr = append(arr, expected.Received[i])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
require.ElementsMatch(t, arr, res.Received)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue