Add transaction fetch to mock api

This commit is contained in:
lash 2024-11-03 15:13:59 +00:00
parent a4443cbe4c
commit da46b6ca1a
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
2 changed files with 55 additions and 3 deletions

View File

@ -1,6 +1,7 @@
package nats
import (
"bytes"
"context"
"testing"
"time"
@ -104,7 +105,19 @@ func(m mockApi) FetchVouchers(ctx context.Context, publicKey string) ([]dataserv
}
func(m mockApi) FetchTransactions(ctx context.Context, publicKey string) ([]dataserviceapi.Last10TxResponse, error) {
return nil, nil
logg.DebugCtxf(ctx, "mockapi fetchtransactions", "key", publicKey)
return []dataserviceapi.Last10TxResponse{
dataserviceapi.Last10TxResponse{
Sender: "0xeae046BF396e91f5A8D74f863dC57c107c8a4a70",
Recipient: "B3117202371853e24B725d4169D87616A7dDb127",
TransferValue: "1337",
ContractAddress: "0x765DE816845861e75A25fCA122bb6898B8B1282a",
TxHash: "0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
DateBlock: time.Unix(1730592500, 0),
TokenSymbol: "FOO",
TokenDecimals: "6",
},
}, nil
}
func(m mockApi) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) {
@ -163,4 +176,32 @@ func TestHandleMsg(t *testing.T) {
data: []byte(data),
}
sub.handleEvent(msg)
store := common.UserDataStore{
Db: userDb,
}
v, err := store.ReadEntry(ctx, aliceSession, common.DATA_ACTIVE_SYM)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(v, []byte("FOO")) {
t.Fatalf("expected 'FOO', got %s", v)
}
v, err = store.ReadEntry(ctx, aliceSession, common.DATA_ACTIVE_BAL)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(v, []byte("362436")) {
t.Fatalf("expected '362436', got %s", v)
}
v, err = store.ReadEntry(ctx, aliceSession, common.DATA_TRANSACTIONS)
if err != nil {
t.Fatal(err)
}
if len(v) == 0 {
t.Fatal("no transaction data")
}
t.Logf("tx %s", v)
}

View File

@ -7,6 +7,7 @@ import (
"strconv"
geEvent "github.com/grassrootseconomics/eth-tracker/pkg/event"
dataserviceapi "github.com/grassrootseconomics/ussd-data-service/pkg/api"
"git.defalsify.org/vise.git/db"
"git.grassecon.net/urdt/ussd/common"
@ -31,7 +32,11 @@ type eventTokenTransfer struct {
VoucherAddress string
}
func updateTokenTransferList(ctx context.Context, store common.UserDataStore, identity lookup.Identity) error {
func formatTransaction(idx int, tx dataserviceapi.Last10TxResponse) string {
return fmt.Sprintf("%d %s %s", idx, tx.DateBlock, tx.TxHash[:10])
}
func updateTokenTransferList(ctx context.Context, store *common.UserDataStore, identity lookup.Identity) error {
var r []string
txs, err := lookup.Api.FetchTransactions(ctx, identity.ChecksumAddress)
@ -40,10 +45,11 @@ func updateTokenTransferList(ctx context.Context, store common.UserDataStore, id
}
for i, tx := range(txs) {
r = append(r, fmt.Sprintf("%d %s %s", i, tx.DateBlock, tx.TxHash[:10]))
r = append(r, formatTransaction(i, tx))
}
s := strings.Join(r, "\n")
return store.WriteEntry(ctx, identity.SessionId, common.DATA_TRANSACTIONS, []byte(s))
}
@ -138,6 +144,11 @@ func updateToken(ctx context.Context, store *common.UserDataStore, identity look
return err
}
err = updateTokenTransferList(ctx, store, identity)
if err != nil {
return err
}
return nil
}