Compare commits

..

No commits in common. "a3e229304746ac0c8e837d016cca96704ef91c96" and "da46b6ca1a42fd4a922e955a7f53e29be6aabb98" have entirely different histories.

View File

@ -3,8 +3,6 @@ package nats
import ( import (
"bytes" "bytes"
"context" "context"
"fmt"
"strconv"
"testing" "testing"
"time" "time"
@ -17,23 +15,13 @@ import (
"git.grassecon.net/urdt/ussd/config" "git.grassecon.net/urdt/ussd/config"
"git.grassecon.net/urdt/ussd/models" "git.grassecon.net/urdt/ussd/models"
"git.grassecon.net/term/lookup" "git.grassecon.net/term/lookup"
"git.grassecon.net/term/event"
) )
func init() {
}
const ( const (
txBlock = 42
tokenAddress = "0x765DE816845861e75A25fCA122bb6898B8B1282a"
tokenSymbol = "FOO"
tokenName = "Foo Token"
tokenDecimals = 6
txValue = 1337
tokenBalance = 362436
txTimestamp = 1730592500
txHash = "0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789"
sinkAddress = "0xb42C5920014eE152F2225285219407938469BBfA"
aliceChecksum = "0xeae046BF396e91f5A8D74f863dC57c107c8a4a70" aliceChecksum = "0xeae046BF396e91f5A8D74f863dC57c107c8a4a70"
bobChecksum = "0xB3117202371853e24B725d4169D87616A7dDb127"
aliceSession = "5553425"
) )
// TODO: jetstream, would have been nice of you to provide an easier way to make a mock msg // TODO: jetstream, would have been nice of you to provide an easier way to make a mock msg
@ -108,10 +96,10 @@ func(m mockApi) FetchVouchers(ctx context.Context, publicKey string) ([]dataserv
logg.DebugCtxf(ctx, "mockapi fetchvouchers", "key", publicKey) logg.DebugCtxf(ctx, "mockapi fetchvouchers", "key", publicKey)
return []dataserviceapi.TokenHoldings{ return []dataserviceapi.TokenHoldings{
dataserviceapi.TokenHoldings{ dataserviceapi.TokenHoldings{
ContractAddress: tokenAddress, ContractAddress: "0xeE0A29AE1BB7a033c8277C04780c4aBcf4388E93",
TokenSymbol: tokenSymbol, TokenSymbol: "FOO",
TokenDecimals: strconv.Itoa(tokenDecimals), TokenDecimals: "6",
Balance: strconv.Itoa(tokenBalance), Balance: "362436",
}, },
}, nil }, nil
} }
@ -120,24 +108,24 @@ func(m mockApi) FetchTransactions(ctx context.Context, publicKey string) ([]data
logg.DebugCtxf(ctx, "mockapi fetchtransactions", "key", publicKey) logg.DebugCtxf(ctx, "mockapi fetchtransactions", "key", publicKey)
return []dataserviceapi.Last10TxResponse{ return []dataserviceapi.Last10TxResponse{
dataserviceapi.Last10TxResponse{ dataserviceapi.Last10TxResponse{
Sender: aliceChecksum, Sender: "0xeae046BF396e91f5A8D74f863dC57c107c8a4a70",
Recipient: bobChecksum, Recipient: "B3117202371853e24B725d4169D87616A7dDb127",
TransferValue: strconv.Itoa(txValue), TransferValue: "1337",
ContractAddress: tokenAddress, ContractAddress: "0x765DE816845861e75A25fCA122bb6898B8B1282a",
TxHash: txHash, TxHash: "0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
DateBlock: time.Unix(txTimestamp, 0), DateBlock: time.Unix(1730592500, 0),
TokenSymbol: tokenSymbol, TokenSymbol: "FOO",
TokenDecimals: strconv.Itoa(tokenDecimals), TokenDecimals: "6",
}, },
}, nil }, nil
} }
func(m mockApi) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) { func(m mockApi) VoucherData(ctx context.Context, address string) (*models.VoucherDataResult, error) {
return &models.VoucherDataResult{ return &models.VoucherDataResult{
TokenSymbol: tokenSymbol, TokenSymbol: "FOO",
TokenName: tokenName, TokenName: "Foo Token",
TokenDecimals: strconv.Itoa(tokenDecimals), TokenDecimals: "6",
SinkAddress: sinkAddress, SinkAddress: "0xb42C5920014eE152F2225285219407938469BBfA",
}, nil }, nil
} }
@ -161,6 +149,7 @@ func TestHandleMsg(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
aliceSession := "5553425"
userDb.SetSession(alice) userDb.SetSession(alice)
userDb.SetPrefix(db.DATATYPE_USERDATA) userDb.SetPrefix(db.DATATYPE_USERDATA)
err = userDb.Put(ctx, common.PackKey(common.DATA_PUBLIC_KEY_REVERSE, []byte{}), []byte(aliceSession)) err = userDb.Put(ctx, common.PackKey(common.DATA_PUBLIC_KEY_REVERSE, []byte{}), []byte(aliceSession))
@ -170,19 +159,19 @@ func TestHandleMsg(t *testing.T) {
sub := NewNatsSubscription(userDb) sub := NewNatsSubscription(userDb)
data := fmt.Sprintf(`{ data := `{
"block": %d, "block": 42,
"contractAddress": "%s", "contractAddress": "0x765DE816845861e75A25fCA122bb6898B8B1282a",
"success": true, "success": true,
"timestamp": %d, "timestamp": 1730592500,
"transactionHash": "%s", "transactionHash": "0xabcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789",
"transactionType": "TOKEN_TRANSFER", "transactionType": "TOKEN_TRANSFER",
"payload": { "payload": {
"from": "%s", "from": "0xeae046BF396e91f5A8D74f863dC57c107c8a4a70",
"to": "%s", "to": "B3117202371853e24B725d4169D87616A7dDb127",
"value": "%d" "value": "1337"
} }
}`, txBlock, tokenAddress, txTimestamp, txHash, aliceChecksum, bobChecksum, txValue) }`
msg := &testMsg{ msg := &testMsg{
data: []byte(data), data: []byte(data),
} }
@ -195,34 +184,24 @@ func TestHandleMsg(t *testing.T) {
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !bytes.Equal(v, []byte(tokenSymbol)) { if !bytes.Equal(v, []byte("FOO")) {
t.Fatalf("expected '%s', got %s", tokenSymbol, v) t.Fatalf("expected 'FOO', got %s", v)
} }
v, err = store.ReadEntry(ctx, aliceSession, common.DATA_ACTIVE_BAL) v, err = store.ReadEntry(ctx, aliceSession, common.DATA_ACTIVE_BAL)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !bytes.Equal(v, []byte(strconv.Itoa(tokenBalance))) { if !bytes.Equal(v, []byte("362436")) {
t.Fatalf("expected '%d', got %s", tokenBalance, v) t.Fatalf("expected '362436', got %s", v)
} }
v, err = store.ReadEntry(ctx, aliceSession, common.DATA_TRANSACTIONS) v, err = store.ReadEntry(ctx, aliceSession, common.DATA_TRANSACTIONS)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
if !bytes.Contains(v, []byte("abcdef")) { if len(v) == 0 {
t.Fatal("no transaction data") t.Fatal("no transaction data")
} }
t.Logf("tx %s", v)
userDb.SetPrefix(event.DATATYPE_USERSUB)
userDb.SetSession(aliceSession)
k := append([]byte("vouchers"), []byte("sym")...)
v, err = userDb.Get(ctx, k)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(v, []byte(fmt.Sprintf("1:%s", tokenSymbol))) {
t.Fatalf("expected '1:%s', got %s", tokenSymbol, v)
}
} }