Skip to content

Commit

Permalink
fix: balance aggregated with address and metadata filters (#610)
Browse files Browse the repository at this point in the history
* fix: error handling at storage level

* fix: balance aggregation combining partial address filtering and metadata
  • Loading branch information
gfyrag authored Dec 9, 2024
1 parent ed8b08d commit c8a3f5b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
11 changes: 3 additions & 8 deletions internal/storage/ledger/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func (s *Store) selectAccountWithAssetAndVolumes(date *time.Time, useInsertionDa
ColumnExpr("*").
TableExpr("(?) accounts_volumes", selectAccountsWithVolumes)

needAccount := needAddressSegment
if needMetadata {
if s.ledger.HasFeature(features.FeatureAccountMetadataHistory, "SYNC") && date != nil && !date.IsZero() {
selectAccountsWithVolumes = selectAccountsWithVolumes.
Expand All @@ -103,17 +104,11 @@ func (s *Store) selectAccountWithAssetAndVolumes(date *time.Time, useInsertionDa
s.selectDistinctAccountMetadataHistories(date),
)
} else {
selectAccountsWithVolumes = selectAccountsWithVolumes.
Join(
`join (?) accounts on accounts.address = accounts_volumes.accounts_address`,
s.db.NewSelect().
ModelTableExpr(s.GetPrefixedRelationName("accounts")).
Where("ledger = ?", s.ledger.Name),
)
needAccount = true
}
}

if needAddressSegment {
if needAccount {
selectAccountsWithVolumes = s.db.NewSelect().
TableExpr(
"(?) accounts",
Expand Down
16 changes: 16 additions & 0 deletions internal/storage/ledger/balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,4 +325,20 @@ func TestBalancesAggregates(t *testing.T) {
),
}, ret)
})

t.Run("using a filter on metadata and on address", func(t *testing.T) {
t.Parallel()
ret, err := store.GetAggregatedBalances(ctx, ledgercontroller.NewGetAggregatedBalancesQuery(
ledgercontroller.PITFilter{},
query.And(
query.Match("address", "users:"),
query.Match("metadata[category]", "premium"),
),
false,
))
require.NoError(t, err)
require.Equal(t, ledger.BalancesByAssets{
"USD": big.NewInt(0).Mul(bigInt, big.NewInt(2)),
}, ret)
})
}

0 comments on commit c8a3f5b

Please sign in to comment.