diff --git a/internal/storage/ledger/balances.go b/internal/storage/ledger/balances.go index ed3686542..b0aadc4a3 100644 --- a/internal/storage/ledger/balances.go +++ b/internal/storage/ledger/balances.go @@ -250,7 +250,6 @@ func (s *Store) GetBalances(ctx context.Context, query ledgercontroller.BalanceQ } // Fill empty balances with 0 value - // todo: still required as we insert balances earlier for account, assets := range query { if _, ok := ret[account]; !ok { ret[account] = map[string]*big.Int{} @@ -264,43 +263,4 @@ func (s *Store) GetBalances(ctx context.Context, query ledgercontroller.BalanceQ return ret, nil }) -} - -/** -SELECT "aggregated_volumes"."aggregated" -FROM - (SELECT aggregate_objects(json_build_object(asset, volumes)::JSONB) AS aggregated - FROM - (SELECT "asset", - json_build_object('input', sum((volumes->>'input')::numeric), 'output', sum((volumes->>'output')::numeric)) AS volumes - FROM - (SELECT * - FROM - (SELECT *, - coalesce(accounts_metadata.metadata, '{}'::JSONB) AS metadata - FROM - (SELECT "asset", - "accounts_address", - moves.post_commit_effective_volumes AS volumes - FROM - (SELECT DISTINCT ON (accounts_address, - asset) "accounts_address", - "asset", - first_value(post_commit_effective_volumes) OVER (PARTITION BY (accounts_address, asset) - ORDER BY effective_date DESC, seq DESC) AS post_commit_effective_volumes - FROM "3c29654b".moves - WHERE (ledger = '3c29654b') - AND (effective_date <= '2024-09-30T15:06:25.591246Z')) moves) accounts_volumes - LEFT JOIN - (SELECT DISTINCT ON (accounts_address) "accounts_address", - "metadata" - FROM "3c29654b".accounts_metadata - WHERE (ledger = '3c29654b') - AND (date <= '2024-09-30T15:06:25.591246Z') - ORDER BY "accounts_address", - "revision" DESC) accounts_metadata ON accounts_metadata.accounts_address = accounts_volumes.accounts_address) accounts - WHERE (metadata @> '{"category":"premium"}')) - VALUES - GROUP BY "asset") - VALUES) aggregated_volumes -*/ +} \ No newline at end of file diff --git a/internal/storage/ledger/balances_test.go b/internal/storage/ledger/balances_test.go index 899cbfd7c..d94164e1b 100644 --- a/internal/storage/ledger/balances_test.go +++ b/internal/storage/ledger/balances_test.go @@ -95,6 +95,20 @@ func TestBalancesGet(t *testing.T) { t.Run("balance query with empty balance", func(t *testing.T) { + tx, err := store.GetDB().BeginTx(ctx, &sql.TxOptions{}) + require.NoError(t, err) + t.Cleanup(func() { + require.NoError(t, tx.Rollback()) + }) + + store := store.WithDB(tx) + + count, err := store.GetDB().NewSelect(). + ModelTableExpr(store.GetPrefixedRelationName("accounts_volumes")). + Count(ctx) + require.NoError(t, err) + require.Equal(t, 1, count) + balances, err := store.GetBalances(ctx, ledgercontroller.BalanceQuery{ "world": {"USD"}, "not-existing": {"USD"}, @@ -106,6 +120,12 @@ func TestBalancesGet(t *testing.T) { require.Equal(t, big.NewInt(-100), balances["world"]["USD"]) require.Equal(t, big.NewInt(0), balances["not-existing"]["USD"]) + + count, err = store.GetDB().NewSelect(). + ModelTableExpr(store.GetPrefixedRelationName("accounts_volumes")). + Count(ctx) + require.NoError(t, err) + require.Equal(t, 2, count) }) }