Skip to content

Commit

Permalink
fix(storage): fix parsing balances to big int (#455)
Browse files Browse the repository at this point in the history
* fix(storage): fix parsing balances to big int

* fix(balances): add tests with big ints
  • Loading branch information
paul-nicolas committed Aug 30, 2023
1 parent aa3a91f commit 78a58db
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 4 deletions.
5 changes: 2 additions & 3 deletions pkg/storage/sqlstorage/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"encoding/base64"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/formancehq/stack/libs/go-libs/api"
Expand Down Expand Up @@ -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)
Expand Down
78 changes: 77 additions & 1 deletion pkg/storage/sqlstorage/balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sqlstorage_test
import (
"context"
"testing"
"time"

"github.com/numary/ledger/pkg/core"
"github.com/numary/ledger/pkg/ledger"
Expand All @@ -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)

Expand Down Expand Up @@ -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)
})
}
1 change: 1 addition & 0 deletions pkg/storage/sqlstorage/store_ledger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
} {
Expand Down

0 comments on commit 78a58db

Please sign in to comment.