Skip to content

Commit

Permalink
feat: support transfers by ordinal number instead of inscription id (…
Browse files Browse the repository at this point in the history
…#296)

* feat: transfers by ordinal number

* fix: upgrade client
  • Loading branch information
g0drlc committed Feb 2, 2024
1 parent 6a93991 commit 2137497
Show file tree
Hide file tree
Showing 13 changed files with 323 additions and 166 deletions.
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"@fastify/swagger": "^8.3.1",
"@fastify/type-provider-typebox": "^3.2.0",
"@hirosystems/api-toolkit": "^1.3.1",
"@hirosystems/chainhook-client": "^1.5.0",
"@hirosystems/chainhook-client": "^1.6.0",
"@semantic-release/changelog": "^6.0.3",
"@semantic-release/commit-analyzer": "^10.0.4",
"@semantic-release/git": "^10.0.1",
Expand Down
23 changes: 12 additions & 11 deletions src/pg/brc20/brc20-pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import * as postgres from 'postgres';
import { hexToBuffer } from '../../api/util/helpers';
import {
DbInscriptionIndexPaging,
DbInscriptionInsert,
DbLocationInsert,
InscriptionData,
DbLocationPointerInsert,
DbLocationTransferType,
DbPaginatedResult,
DbRevealInsert,
InscriptionEventData,
LocationData,
InscriptionRevealData,
} from '../types';
import {
BRC20_DEPLOYS_COLUMNS,
Expand Down Expand Up @@ -39,13 +40,13 @@ export class Brc20PgStore extends BasePgStoreModule {
}

async insertOperations(args: {
reveals: DbRevealInsert[];
reveals: InscriptionEventData[];
pointers: DbLocationPointerInsert[];
}): Promise<void> {
for (const [i, reveal] of args.reveals.entries()) {
const pointer = args.pointers[i];
if (parseInt(pointer.block_height) < BRC20_GENESIS_BLOCK) continue;
if (reveal.inscription) {
if ('inscription' in reveal) {
if (
reveal.inscription.classic_number < 0 ||
reveal.inscription.number < 0 ||
Expand Down Expand Up @@ -75,7 +76,7 @@ export class Brc20PgStore extends BasePgStoreModule {
}

async applyTransfer(args: {
reveal: DbRevealInsert;
reveal: InscriptionEventData;
pointer: DbLocationPointerInsert;
}): Promise<void> {
await this.sqlWriteTransaction(async sql => {
Expand Down Expand Up @@ -207,7 +208,7 @@ export class Brc20PgStore extends BasePgStoreModule {

private async insertDeploy(deploy: {
brc20: Brc20Deploy;
reveal: DbRevealInsert;
reveal: InscriptionEventData;
pointer: DbLocationPointerInsert;
}): Promise<void> {
if (deploy.reveal.location.transfer_type != DbLocationTransferType.transferred) return;
Expand Down Expand Up @@ -257,7 +258,7 @@ export class Brc20PgStore extends BasePgStoreModule {

private async insertMint(mint: {
brc20: Brc20Mint;
reveal: DbRevealInsert;
reveal: InscriptionEventData;
pointer: DbLocationPointerInsert;
}): Promise<void> {
if (mint.reveal.location.transfer_type != DbLocationTransferType.transferred) return;
Expand Down Expand Up @@ -335,7 +336,7 @@ export class Brc20PgStore extends BasePgStoreModule {

private async insertTransfer(transfer: {
brc20: Brc20Transfer;
reveal: DbRevealInsert;
reveal: InscriptionEventData;
pointer: DbLocationPointerInsert;
}): Promise<void> {
if (transfer.reveal.location.transfer_type != DbLocationTransferType.transferred) return;
Expand Down Expand Up @@ -406,7 +407,7 @@ export class Brc20PgStore extends BasePgStoreModule {
);
}

async rollBackInscription(args: { inscription: DbInscriptionInsert }): Promise<void> {
async rollBackInscription(args: { inscription: InscriptionData }): Promise<void> {
const events = await this.sql<DbBrc20Event[]>`
SELECT e.* FROM brc20_events AS e
INNER JOIN inscriptions AS i ON i.id = e.inscription_id
Expand All @@ -430,7 +431,7 @@ export class Brc20PgStore extends BasePgStoreModule {
}
}

async rollBackLocation(args: { location: DbLocationInsert }): Promise<void> {
async rollBackLocation(args: { location: LocationData }): Promise<void> {
const events = await this.sql<DbBrc20Event[]>`
SELECT e.* FROM brc20_events AS e
INNER JOIN locations AS l ON l.id = e.genesis_location_id
Expand Down
4 changes: 2 additions & 2 deletions src/pg/brc20/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Static, Type } from '@fastify/type-provider-typebox';
import { TypeCompiler } from '@sinclair/typebox/compiler';
import BigNumber from 'bignumber.js';
import { hexToBuffer } from '../../api/util/helpers';
import { DbInscriptionInsert } from '../types';
import { InscriptionData } from '../types';

const Brc20TickerSchema = Type.String({ minLength: 1 });
const Brc20NumberSchema = Type.RegEx(/^((\d+)|(\d*\.?\d+))$/);
Expand Down Expand Up @@ -51,7 +51,7 @@ const UINT64_MAX = BigNumber('18446744073709551615'); // 20 digits
const numExceedsMax = (num: string) => num.length >= 20 && UINT64_MAX.isLessThan(num);

// For testing only
export function brc20FromInscription(inscription: DbInscriptionInsert): Brc20 | undefined {
export function brc20FromInscription(inscription: InscriptionData): Brc20 | undefined {
if (inscription.number < 0) return;
if (inscription.mime_type !== 'text/plain' && inscription.mime_type !== 'application/json')
return;
Expand Down
10 changes: 5 additions & 5 deletions src/pg/counts/counts-pg-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { SatoshiRarity } from '../../api/util/ordinal-satoshi';
import {
DbInscription,
DbInscriptionIndexFilters,
DbInscriptionInsert,
InscriptionData,
DbInscriptionType,
DbLocationInsert,
RevealLocationData,
DbLocationPointer,
} from '../types';
import { DbInscriptionIndexResultCountType } from './types';
Expand Down Expand Up @@ -55,7 +55,7 @@ export class CountsPgStore extends BasePgStoreModule {
}
}

async applyInscriptions(writes: DbInscriptionInsert[]): Promise<void> {
async applyInscriptions(writes: InscriptionData[]): Promise<void> {
if (writes.length === 0) return;
const mimeType = new Map<string, number>();
const rarity = new Map<string, number>();
Expand Down Expand Up @@ -105,8 +105,8 @@ export class CountsPgStore extends BasePgStoreModule {
}

async rollBackInscription(args: {
inscription: DbInscriptionInsert;
location: DbLocationInsert;
inscription: InscriptionData;
location: RevealLocationData;
}): Promise<void> {
await this.sql`
WITH decrease_mime_type AS (
Expand Down
25 changes: 15 additions & 10 deletions src/pg/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,12 @@ import {
BitcoinInscriptionTransferred,
} from '@hirosystems/chainhook-client';
import { ENV } from '../env';
import { DbLocationTransferType, DbRevealInsert } from './types';
import {
DbLocationTransferType,
InscriptionEventData,
InscriptionTransferData,
InscriptionRevealData,
} from './types';
import { OrdinalSatoshi } from '../api/util/ordinal-satoshi';

/**
Expand Down Expand Up @@ -83,13 +88,13 @@ export function removeNullBytes(input: string): string {
return input.replace(/\x00/g, '');
}

function revealInsertFromOrdhookInscriptionRevealed(args: {
function updateFromOrdhookInscriptionRevealed(args: {
block_height: number;
block_hash: string;
tx_id: string;
timestamp: number;
reveal: BitcoinInscriptionRevealed;
}): DbRevealInsert {
}): InscriptionRevealData {
const satoshi = new OrdinalSatoshi(args.reveal.ordinal_number);
const satpoint = parseSatPoint(args.reveal.satpoint_post_inscription);
const recursive_refs = getInscriptionRecursion(args.reveal.content_bytes);
Expand Down Expand Up @@ -130,14 +135,14 @@ function revealInsertFromOrdhookInscriptionRevealed(args: {
};
}

function revealInsertFromOrdhookInscriptionTransferred(args: {
function updateFromOrdhookInscriptionTransferred(args: {
block_height: number;
block_hash: string;
tx_id: string;
timestamp: number;
blockTransferIndex: number;
transfer: BitcoinInscriptionTransferred;
}): DbRevealInsert {
}): InscriptionTransferData {
const satpoint = parseSatPoint(args.transfer.satpoint_post_transfer);
const prevSatpoint = parseSatPoint(args.transfer.satpoint_pre_transfer);
return {
Expand All @@ -147,7 +152,7 @@ function revealInsertFromOrdhookInscriptionTransferred(args: {
tx_id: args.tx_id,
tx_index: args.transfer.tx_index,
block_transfer_index: args.blockTransferIndex,
genesis_id: args.transfer.inscription_id,
ordinal_number: args.transfer.ordinal_number.toString(),
address: args.transfer.destination.value ?? null,
output: `${satpoint.tx_id}:${satpoint.vout}`,
offset: satpoint.offset ?? null,
Expand All @@ -164,18 +169,18 @@ function revealInsertFromOrdhookInscriptionTransferred(args: {
};
}

export function revealInsertsFromOrdhookEvent(event: BitcoinEvent): DbRevealInsert[] {
export function revealInsertsFromOrdhookEvent(event: BitcoinEvent): InscriptionEventData[] {
// Keep the relative ordering of a transfer within a block for faster future reads.
let blockTransferIndex = 0;
const block_height = event.block_identifier.index;
const block_hash = normalizedHexString(event.block_identifier.hash);
const writes: DbRevealInsert[] = [];
const writes: InscriptionEventData[] = [];
for (const tx of event.transactions) {
const tx_id = normalizedHexString(tx.transaction_identifier.hash);
for (const operation of tx.metadata.ordinal_operations) {
if (operation.inscription_revealed)
writes.push(
revealInsertFromOrdhookInscriptionRevealed({
updateFromOrdhookInscriptionRevealed({
block_hash,
block_height,
tx_id,
Expand All @@ -185,7 +190,7 @@ export function revealInsertsFromOrdhookEvent(event: BitcoinEvent): DbRevealInse
);
if (operation.inscription_transferred)
writes.push(
revealInsertFromOrdhookInscriptionTransferred({
updateFromOrdhookInscriptionTransferred({
block_hash,
block_height,
tx_id,
Expand Down
Loading

0 comments on commit 2137497

Please sign in to comment.