Skip to content

Commit

Permalink
chore: clean todo
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent 04cbd94 commit 101d554
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 41 deletions.
42 changes: 1 addition & 41 deletions internal/storage/ledger/balances.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -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
*/
}
20 changes: 20 additions & 0 deletions internal/storage/ledger/balances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"},
Expand All @@ -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)
})
}

Expand Down

0 comments on commit 101d554

Please sign in to comment.