Skip to content

Commit

Permalink
fix: optimize brc-20 activity query (#362)
Browse files Browse the repository at this point in the history
* fix: optimize brc-20 activity query

* fix: bignumber processing

* fix: use bignumber on max and lim
  • Loading branch information
rafaelcr committed Jun 16, 2024
1 parent 0dee9c5 commit e8a4fea
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 61 deletions.
40 changes: 22 additions & 18 deletions src/pg/brc20/brc20-block-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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(
Expand All @@ -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(
Expand All @@ -143,15 +147,15 @@ export class Brc20BlockCache {
this.updateAddressBalance(
operation.transfer_send.tick,
operation.transfer_send.sender_address,
BigNumber('0'),
zero,
amt.negated(),
amt.negated()
);
this.updateAddressBalance(
operation.transfer_send.tick,
operation.transfer_send.receiver_address,
amt,
BigNumber(0),
zero,
amt
);
}
Expand Down
88 changes: 45 additions & 43 deletions src/pg/brc20/brc20-pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e8a4fea

Please sign in to comment.