Skip to content

Commit

Permalink
fix: use brc20_total_balances table when inserting new transfer (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaelcr committed Mar 7, 2024
1 parent 2877071 commit 925fb0e
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions src/pg/brc20/brc20-pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -340,20 +340,13 @@ export class Brc20PgStore extends BasePgStoreModule {
pointer: DbLocationPointerInsert;
}): Promise<void> {
if (transfer.reveal.location.transfer_type != DbLocationTransferType.transferred) return;
// Check the following conditions:
// * Do we have enough available balance to do this transfer?
const transferRes = await this.sql`
WITH balance_data AS (
SELECT b.brc20_deploy_id, COALESCE(SUM(b.avail_balance), 0) AS avail_balance
FROM brc20_balances AS b
INNER JOIN brc20_deploys AS d ON b.brc20_deploy_id = d.id
WHERE d.ticker_lower = LOWER(${transfer.brc20.tick})
AND b.address = ${transfer.pointer.address}
GROUP BY b.brc20_deploy_id
),
validated_transfer AS (
SELECT * FROM balance_data
WHERE avail_balance >= ${transfer.brc20.amt}::numeric
WITH validated_transfer AS (
SELECT brc20_deploy_id, avail_balance
FROM brc20_total_balances
WHERE brc20_deploy_id = (SELECT id FROM brc20_deploys WHERE ticker_lower = LOWER(${transfer.brc20.tick}))
AND address = ${transfer.pointer.address}
AND avail_balance >= ${transfer.brc20.amt}::numeric
),
transfer_insert AS (
INSERT INTO brc20_transfers (inscription_id, brc20_deploy_id, block_height, tx_id, from_address, to_address, amount) (
Expand Down

0 comments on commit 925fb0e

Please sign in to comment.