diff --git a/src/pg/brc20/brc20-block-cache.ts b/src/pg/brc20/brc20-block-cache.ts index 3bd35659..5f60d1a6 100644 --- a/src/pg/brc20/brc20-block-cache.ts +++ b/src/pg/brc20/brc20-block-cache.ts @@ -33,14 +33,15 @@ export class Brc20BlockCache { } deploy(operation: BitcoinBrc20DeployOperation, tx_id: string, tx_index: number) { + const zero = BigNumber('0'); this.tokens.push({ block_height: this.blockHeight, genesis_id: operation.deploy.inscription_id, tx_id, address: operation.deploy.address, ticker: operation.deploy.tick, - max: operation.deploy.max, - limit: operation.deploy.lim, + max: BigNumber(operation.deploy.max).toString(), + limit: BigNumber(operation.deploy.lim).toString(), decimals: operation.deploy.dec, self_mint: operation.deploy.self_mint, }); @@ -50,8 +51,8 @@ export class Brc20BlockCache { genesis_id: operation.deploy.inscription_id, ticker: operation.deploy.tick, address: operation.deploy.address, - avail_balance: '0', - trans_balance: '0', + avail_balance: zero.toString(), + trans_balance: zero.toString(), operation: DbBrc20Operation.deploy, }); this.increaseOperationCount(DbBrc20Operation.deploy); @@ -60,36 +61,38 @@ export class Brc20BlockCache { } mint(operation: BitcoinBrc20MintOperation, tx_index: number) { + const zero = BigNumber('0'); + const amt = BigNumber(operation.mint.amt).abs(); this.operations.push({ block_height: this.blockHeight, tx_index, genesis_id: operation.mint.inscription_id, ticker: operation.mint.tick, address: operation.mint.address, - avail_balance: operation.mint.amt, - trans_balance: '0', + avail_balance: amt.toString(), + trans_balance: zero.toString(), operation: DbBrc20Operation.mint, }); - const amt = BigNumber(operation.mint.amt); this.increaseTokenMintedSupply(operation.mint.tick, amt); this.increaseTokenTxCount(operation.mint.tick); this.increaseOperationCount(DbBrc20Operation.mint); this.increaseAddressOperationCount(operation.mint.address, DbBrc20Operation.mint); - this.updateAddressBalance(operation.mint.tick, operation.mint.address, amt, BigNumber(0), amt); + this.updateAddressBalance(operation.mint.tick, operation.mint.address, amt, zero, amt); } transfer(operation: BitcoinBrc20TransferOperation, tx_index: number) { + const zero = BigNumber('0'); + const amt = BigNumber(operation.transfer.amt).abs(); this.operations.push({ block_height: this.blockHeight, tx_index, genesis_id: operation.transfer.inscription_id, ticker: operation.transfer.tick, address: operation.transfer.address, - avail_balance: BigNumber(operation.transfer.amt).negated().toString(), - trans_balance: operation.transfer.amt, + avail_balance: amt.negated().toString(), + trans_balance: amt.toString(), operation: DbBrc20Operation.transfer, }); - const amt = BigNumber(operation.transfer.amt); this.increaseOperationCount(DbBrc20Operation.transfer); this.increaseTokenTxCount(operation.transfer.tick); this.increaseAddressOperationCount(operation.transfer.address, DbBrc20Operation.transfer); @@ -98,19 +101,21 @@ export class Brc20BlockCache { operation.transfer.address, amt.negated(), amt, - BigNumber(0) + zero ); } transferSend(operation: BitcoinBrc20TransferSendOperation, tx_index: number) { + const amt = BigNumber(operation.transfer_send.amt).abs(); + const zero = BigNumber('0'); this.operations.push({ block_height: this.blockHeight, tx_index, genesis_id: operation.transfer_send.inscription_id, ticker: operation.transfer_send.tick, address: operation.transfer_send.sender_address, - avail_balance: '0', - trans_balance: BigNumber(operation.transfer_send.amt).negated().toString(), + avail_balance: zero.toString(), + trans_balance: amt.negated().toString(), operation: DbBrc20Operation.transferSend, }); this.transferReceivers.set( @@ -123,11 +128,10 @@ export class Brc20BlockCache { genesis_id: operation.transfer_send.inscription_id, ticker: operation.transfer_send.tick, address: operation.transfer_send.receiver_address, - avail_balance: operation.transfer_send.amt, + avail_balance: amt.toString(), trans_balance: '0', operation: DbBrc20Operation.transferReceive, }); - const amt = BigNumber(operation.transfer_send.amt); this.increaseOperationCount(DbBrc20Operation.transferSend); this.increaseTokenTxCount(operation.transfer_send.tick); this.increaseAddressOperationCount( @@ -143,7 +147,7 @@ export class Brc20BlockCache { this.updateAddressBalance( operation.transfer_send.tick, operation.transfer_send.sender_address, - BigNumber('0'), + zero, amt.negated(), amt.negated() ); @@ -151,7 +155,7 @@ export class Brc20BlockCache { operation.transfer_send.tick, operation.transfer_send.receiver_address, amt, - BigNumber(0), + zero, amt ); } diff --git a/src/pg/brc20/brc20-pg-store.ts b/src/pg/brc20/brc20-pg-store.ts index 5f727507..137f808a 100644 --- a/src/pg/brc20/brc20-pg-store.ts +++ b/src/pg/brc20/brc20-pg-store.ts @@ -373,49 +373,51 @@ export class Brc20PgStore extends BasePgStoreModule { WHERE ticker IN ${sql(filters.ticker)} ` : sql`SELECT NULL AS count` - }) - SELECT - e.operation, - e.avail_balance, - e.trans_balance, - e.address, - e.to_address, - d.ticker, - e.genesis_id AS inscription_id, - i.block_height, - l.block_hash, - l.tx_id, - l.timestamp, - l.output, - l.offset, - d.max AS deploy_max, - d.limit AS deploy_limit, - d.decimals AS deploy_decimals, - ${ - needsGlobalEventCount || needsAddressEventCount || needsTickerCount - ? sql`(SELECT count FROM event_count)` - : sql`COUNT(*) OVER()` - } AS total - FROM brc20_operations AS e - INNER JOIN brc20_tokens AS d ON d.ticker = e.ticker - INNER JOIN inscriptions AS i ON i.genesis_id = e.genesis_id - INNER JOIN locations AS l ON i.ordinal_number = l.ordinal_number AND e.block_height = l.block_height AND e.tx_index = l.tx_index - WHERE TRUE - ${ - operationsFilter - ? sql`AND e.operation IN ${sql(operationsFilter)}` - : sql`AND e.operation <> 'transfer_receive'` - } - ${filters.ticker ? sql`AND e.ticker IN ${sql(filters.ticker)}` : sql``} - ${filters.block_height ? sql`AND e.block_height = ${filters.block_height}` : sql``} - ${ - filters.address - ? sql`AND (e.address = ${filters.address} OR e.to_address = ${filters.address})` - : sql`` - } - ORDER BY e.block_height DESC, e.tx_index DESC - LIMIT ${page.limit} - OFFSET ${page.offset} + }), + operations AS ( + SELECT + e.operation, + e.avail_balance, + e.trans_balance, + e.address, + e.to_address, + e.ticker, + e.genesis_id AS inscription_id, + d.max AS deploy_max, + d.limit AS deploy_limit, + d.decimals AS deploy_decimals, + i.ordinal_number, + e.block_height, + e.tx_index, + ${ + needsGlobalEventCount || needsAddressEventCount || needsTickerCount + ? sql`(SELECT count FROM event_count)` + : sql`COUNT(*) OVER()` + } AS total + FROM brc20_operations AS e + INNER JOIN brc20_tokens AS d ON d.ticker = e.ticker + INNER JOIN inscriptions AS i ON i.genesis_id = e.genesis_id + WHERE TRUE + ${ + operationsFilter + ? sql`AND e.operation IN ${sql(operationsFilter)}` + : sql`AND e.operation <> 'transfer_receive'` + } + ${filters.ticker ? sql`AND e.ticker IN ${sql(filters.ticker)}` : sql``} + ${filters.block_height ? sql`AND e.block_height = ${filters.block_height}` : sql``} + ${ + filters.address + ? sql`AND (e.address = ${filters.address} OR e.to_address = ${filters.address})` + : sql`` + } + ORDER BY e.block_height DESC, e.tx_index DESC + LIMIT ${page.limit} + OFFSET ${page.offset} + ) + SELECT o.*, l.block_hash, l.tx_id, l.timestamp, l.output, l.offset + FROM operations AS o + INNER JOIN locations AS l USING (ordinal_number, block_height, tx_index) + ORDER BY o.block_height DESC, o.tx_index DESC `; return { total: results[0]?.total ?? 0,