From 78a58dbcae0abc98df426d2503fe819b7962e9f2 Mon Sep 17 00:00:00 2001 From: Paul Nicolas Date: Wed, 30 Aug 2023 16:11:12 +0200 Subject: [PATCH] fix(storage): fix parsing balances to big int (#455) * fix(storage): fix parsing balances to big int * fix(balances): add tests with big ints --- pkg/storage/sqlstorage/balances.go | 5 +- pkg/storage/sqlstorage/balances_test.go | 78 ++++++++++++++++++++- pkg/storage/sqlstorage/store_ledger_test.go | 1 + 3 files changed, 80 insertions(+), 4 deletions(-) diff --git a/pkg/storage/sqlstorage/balances.go b/pkg/storage/sqlstorage/balances.go index 29a154b84..6756ac5bd 100644 --- a/pkg/storage/sqlstorage/balances.go +++ b/pkg/storage/sqlstorage/balances.go @@ -5,7 +5,6 @@ import ( "encoding/base64" "encoding/json" "fmt" - "strconv" "strings" "github.com/formancehq/stack/libs/go-libs/api" @@ -189,11 +188,11 @@ func (s *Store) GetBalances(ctx context.Context, q ledger.BalancesQuery) (api.Cu split := strings.Split(agg, ",") asset := split[0] balancesString := split[1] - balances, err := strconv.ParseInt(balancesString, 10, 64) + balances, err := core.ParseMonetaryInt(balancesString) if err != nil { return api.Cursor[core.AccountsBalances]{}, s.error(err) } - accountsBalances[currentAccount][asset] = core.NewMonetaryInt(balances) + accountsBalances[currentAccount][asset] = balances } accounts = append(accounts, accountsBalances) diff --git a/pkg/storage/sqlstorage/balances_test.go b/pkg/storage/sqlstorage/balances_test.go index f70cae53d..4966aa17f 100644 --- a/pkg/storage/sqlstorage/balances_test.go +++ b/pkg/storage/sqlstorage/balances_test.go @@ -3,6 +3,7 @@ package sqlstorage_test import ( "context" "testing" + "time" "github.com/numary/ledger/pkg/core" "github.com/numary/ledger/pkg/ledger" @@ -12,7 +13,6 @@ import ( ) func testGetBalances(t *testing.T, store *sqlstorage.Store) { - err := store.Commit(context.Background(), tx1, tx2, tx3) require.NoError(t, err) @@ -171,3 +171,79 @@ func testGetBalancesAggregated(t *testing.T, store *sqlstorage.Store) { "USD": core.NewMonetaryInt(0), }, cursor) } + +func testGetBalancesBigInts(t *testing.T, store *sqlstorage.Store) { + amount, _ := core.ParseMonetaryInt("5522360000000000000000") + var txBigInts = core.ExpandedTransaction{ + Transaction: core.Transaction{ + TransactionData: core.TransactionData{ + Postings: []core.Posting{ + { + Source: "world", + Destination: "central_bank", + Amount: amount, + Asset: "USD", + }, + }, + Reference: "tx1BigInts", + Timestamp: now.Add(-3 * time.Hour), + }, + }, + PostCommitVolumes: core.AccountsAssetsVolumes{ + "world": { + "USD": { + Input: core.NewMonetaryInt(0), + Output: amount, + }, + }, + "central_bank": { + "USD": { + Input: amount, + Output: core.NewMonetaryInt(0), + }, + }, + }, + PreCommitVolumes: core.AccountsAssetsVolumes{ + "world": { + "USD": { + Input: core.NewMonetaryInt(0), + Output: core.NewMonetaryInt(0), + }, + }, + "central_bank": { + "USD": { + Input: core.NewMonetaryInt(0), + Output: core.NewMonetaryInt(0), + }, + }, + }, + } + + err := store.Commit(context.Background(), txBigInts) + require.NoError(t, err) + + negativeAmount, _ := core.ParseMonetaryInt("-5522360000000000000000") + t.Run("all accounts", func(t *testing.T) { + cursor, err := store.GetBalances(context.Background(), + ledger.BalancesQuery{ + PageSize: 10, + }) + assert.NoError(t, err) + assert.Equal(t, 10, cursor.PageSize) + assert.Equal(t, false, cursor.HasMore) + assert.Equal(t, "", cursor.Previous) + assert.Equal(t, "", cursor.Next) + assert.Equal(t, []core.AccountsBalances{ + { + "world": core.AssetsBalances{ + "USD": negativeAmount, + }, + }, + { + "central_bank": core.AssetsBalances{ + "USD": amount, + }, + }, + }, cursor.Data) + }) +} diff --git a/pkg/storage/sqlstorage/store_ledger_test.go b/pkg/storage/sqlstorage/store_ledger_test.go index a464216f0..269d832e4 100644 --- a/pkg/storage/sqlstorage/store_ledger_test.go +++ b/pkg/storage/sqlstorage/store_ledger_test.go @@ -43,6 +43,7 @@ func TestStore(t *testing.T) { {name: "Mapping", fn: testMapping}, {name: "TooManyClient", fn: testTooManyClient}, {name: "GetBalances", fn: testGetBalances}, + {name: "GetBalancesBigInts", fn: testGetBalancesBigInts}, {name: "GetBalancesAggregated", fn: testGetBalancesAggregated}, {name: "CreateIK", fn: testIKS}, } {