Skip to content

Commit

Permalink
feat!: support reinscription transfers (#348)
Browse files Browse the repository at this point in the history
* chore: progress

* feat: new apply

* fix: minted supply

* fix: track tx counts

* feat: rollbacks

* fix: transfer rollbacks

* fix: activity addresses

* fix: multiple transfer test

* fix: holders

* fix: operation indexes

* chore: draft

* fix: style

* chore: optimize migrations

* fix: initial inserts

* fix: get inscription endpoint

* fix: rollback

* fix: ordhook tests passing

* chore: logging

* fix: recursion refs

* fix: current location comparison

* fix: transfers table

* test: sat reinscription transfers

* fix: start applying counts

* fix: tests

* fix: all api tests passing

* test: transfers per block reinscription

* test: address transfer counts

* fix: brc20 tests

* chore: refactor block caches

* chore: clean unused exports
  • Loading branch information
rafaelcr committed Apr 26, 2024
1 parent 56a8851 commit 5422156
Show file tree
Hide file tree
Showing 48 changed files with 1,355 additions and 1,808 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ jobs:
- name: Lint Prettier
run: npm run lint:prettier

- name: Lint Unused Exports
run: npm run lint:unused-exports

test:
strategy:
fail-fast: false
Expand Down
22 changes: 22 additions & 0 deletions migrations/1676395230925_satoshis.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';

export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.createTable('satoshis', {
ordinal_number: {
type: 'numeric',
primaryKey: true,
},
rarity: {
type: 'text',
notNull: true,
},
coinbase_height: {
type: 'bigint',
notNull: true,
},
});
pgm.createIndex('satoshis', ['rarity']);
}
50 changes: 37 additions & 13 deletions migrations/1676395230930_inscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,33 @@ export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.createTable('inscriptions', {
id: {
type: 'bigserial',
primaryKey: true,
},
genesis_id: {
type: 'text',
primaryKey: true,
},
ordinal_number: {
type: 'numeric',
notNull: true,
},
number: {
type: 'bigint',
notNull: true,
},
sat_ordinal: {
type: 'numeric',
classic_number: {
type: 'bigint',
notNull: true,
},
sat_rarity: {
type: 'text',
block_height: {
type: 'bigint',
notNull: true,
},
sat_coinbase_height: {
tx_index: {
type: 'bigint',
notNull: true,
},
address: {
type: 'text',
},
mime_type: {
type: 'text',
notNull: true,
Expand All @@ -52,17 +55,38 @@ export function up(pgm: MigrationBuilder): void {
curse_type: {
type: 'text',
},
recursive: {
type: 'boolean',
default: false,
},
metadata: {
type: 'text',
},
parent: {
type: 'text',
},
timestamp: {
type: 'timestamptz',
notNull: true,
},
updated_at: {
type: 'timestamptz',
default: pgm.func('(NOW())'),
notNull: true,
},
});
pgm.createConstraint('inscriptions', 'inscriptions_number_unique', 'UNIQUE(number)');
pgm.createIndex('inscriptions', ['genesis_id']);
pgm.createConstraint(
'inscriptions',
'inscriptions_ordinal_number_fk',
'FOREIGN KEY(ordinal_number) REFERENCES satoshis(ordinal_number) ON DELETE CASCADE'
);
pgm.createIndex('inscriptions', ['mime_type']);
pgm.createIndex('inscriptions', ['sat_ordinal']);
pgm.createIndex('inscriptions', ['sat_rarity']);
pgm.createIndex('inscriptions', ['sat_coinbase_height']);
pgm.createIndex('inscriptions', ['recursive']);
pgm.createIndex('inscriptions', [
{ name: 'block_height', sort: 'DESC' },
{ name: 'tx_index', sort: 'DESC' },
]);
pgm.createIndex('inscriptions', ['address']);
pgm.createIndex('inscriptions', [{ name: 'updated_at', sort: 'DESC' }]);
}
41 changes: 18 additions & 23 deletions migrations/1677284495299_locations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,26 @@ import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.createType('transfer_type', ['transferred', 'spent_in_fees', 'burnt']);
pgm.createTable('locations', {
id: {
type: 'bigserial',
primaryKey: true,
},
inscription_id: {
type: 'bigint',
},
genesis_id: {
type: 'text',
ordinal_number: {
type: 'numeric',
notNull: true,
},
block_height: {
type: 'bigint',
notNull: true,
},
block_hash: {
type: 'text',
tx_index: {
type: 'bigint',
notNull: true,
},
tx_id: {
type: 'text',
notNull: true,
},
tx_index: {
type: 'bigint',
block_hash: {
type: 'text',
notNull: true,
},
address: {
Expand All @@ -51,26 +45,27 @@ export function up(pgm: MigrationBuilder): void {
value: {
type: 'numeric',
},
transfer_type: {
type: 'transfer_type',
notNull: true,
},
timestamp: {
type: 'timestamptz',
notNull: true,
},
});
pgm.createConstraint('locations', 'locations_pkey', {
primaryKey: ['ordinal_number', 'block_height', 'tx_index'],
});
pgm.createConstraint(
'locations',
'locations_inscription_id_fk',
'FOREIGN KEY(inscription_id) REFERENCES inscriptions(id) ON DELETE CASCADE'
'locations_ordinal_number_fk',
'FOREIGN KEY(ordinal_number) REFERENCES satoshis(ordinal_number) ON DELETE CASCADE'
);
pgm.createConstraint('locations', 'locations_output_offset_unique', 'UNIQUE(output, "offset")');
pgm.createIndex('locations', ['inscription_id']);
pgm.createIndex('locations', ['output', 'offset']);
pgm.createIndex('locations', ['timestamp']);
pgm.createIndex('locations', [
'genesis_id',
{ name: 'block_height', sort: 'DESC' },
{ name: 'tx_index', sort: 'DESC' },
]);
pgm.createIndex('locations', ['block_height']);
pgm.createIndex('locations', ['block_hash']);
pgm.createIndex('locations', ['address']);
pgm.createIndex('locations', ['timestamp']);
pgm.createIndex('locations', ['prev_output']);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.createTable('current_locations', {
inscription_id: {
type: 'bigint',
notNull: true,
},
location_id: {
type: 'bigint',
ordinal_number: {
type: 'numeric',
notNull: true,
},
block_height: {
Expand All @@ -27,10 +23,14 @@ export function up(pgm: MigrationBuilder): void {
});
pgm.createConstraint(
'current_locations',
'current_locations_inscription_id_unique',
'UNIQUE(inscription_id)'
'current_locations_locations_fk',
'FOREIGN KEY(ordinal_number, block_height, tx_index) REFERENCES locations(ordinal_number, block_height, tx_index) ON DELETE CASCADE'
);
pgm.createConstraint(
'locations',
'locations_satoshis_fk',
'FOREIGN KEY(ordinal_number) REFERENCES satoshis(ordinal_number) ON DELETE CASCADE'
);
pgm.createIndex('current_locations', ['location_id']);
pgm.createIndex('current_locations', ['block_height']);
pgm.createIndex('current_locations', ['ordinal_number'], { unique: true });
pgm.createIndex('current_locations', ['address']);
}
52 changes: 52 additions & 0 deletions migrations/1677284495501_inscription-transfers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';

export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.createTable('inscription_transfers', {
genesis_id: {
type: 'text',
notNull: true,
},
number: {
type: 'bigint',
notNull: true,
},
ordinal_number: {
type: 'numeric',
notNull: true,
},
block_height: {
type: 'bigint',
notNull: true,
},
tx_index: {
type: 'bigint',
notNull: true,
},
block_hash: {
type: 'text',
notNull: true,
},
block_transfer_index: {
type: 'int',
notNull: true,
},
});
pgm.createConstraint('inscription_transfers', 'inscription_transfers_pkey', {
primaryKey: ['block_height', 'block_transfer_index'],
});
pgm.createConstraint(
'inscription_transfers',
'inscription_transfers_locations_fk',
'FOREIGN KEY(ordinal_number, block_height, tx_index) REFERENCES locations(ordinal_number, block_height, tx_index) ON DELETE CASCADE'
);
pgm.createConstraint(
'inscription_transfers',
'inscription_transfers_satoshis_fk',
'FOREIGN KEY(ordinal_number) REFERENCES satoshis(ordinal_number) ON DELETE CASCADE'
);
pgm.createIndex('inscription_transfers', ['genesis_id']);
pgm.createIndex('inscription_transfers', ['number']);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.dropMaterializedView('chain_tip');
pgm.createTable('chain_tip', {
id: {
type: 'bool',
Expand All @@ -19,20 +18,9 @@ export function up(pgm: MigrationBuilder): void {
},
});
pgm.addConstraint('chain_tip', 'chain_tip_one_row', 'CHECK(id)');
pgm.sql(`
INSERT INTO chain_tip (block_height) (
SELECT GREATEST(MAX(block_height), 767430) AS block_height FROM locations
)
`);
pgm.sql(`INSERT INTO chain_tip DEFAULT VALUES`);
}

export function down(pgm: MigrationBuilder): void {
pgm.dropTable('chain_tip');
pgm.createMaterializedView(
'chain_tip',
{ data: true },
// Set block height 767430 (inscription #0 genesis) as default.
`SELECT GREATEST(MAX(block_height), 767430) AS block_height FROM locations`
);
pgm.createIndex('chain_tip', ['block_height'], { unique: true });
}
25 changes: 25 additions & 0 deletions migrations/1677284495992_inscription-recursions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';

export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.createTable('inscription_recursions', {
genesis_id: {
type: 'text',
notNull: true,
},
ref_genesis_id: {
type: 'text',
notNull: true,
},
});
pgm.createConstraint('inscription_recursions', 'inscription_recursions_pkey', {
primaryKey: ['genesis_id', 'ref_genesis_id'],
});
pgm.createConstraint(
'inscription_recursions',
'inscription_recursions_genesis_id_fk',
'FOREIGN KEY(genesis_id) REFERENCES inscriptions(genesis_id) ON DELETE CASCADE'
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.createTable('inscriptions_per_block', {
pgm.createTable('counts_by_block', {
block_height: {
type: 'bigint',
primaryKey: true,
Expand All @@ -26,4 +26,5 @@ export function up(pgm: MigrationBuilder): void {
notNull: true,
},
});
pgm.createIndex('counts_by_block', ['block_hash']);
}
14 changes: 0 additions & 14 deletions migrations/1677360299810_chain-tip.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { MigrationBuilder, ColumnDefinitions } from 'node-pg-migrate';
export const shorthands: ColumnDefinitions | undefined = undefined;

export function up(pgm: MigrationBuilder): void {
pgm.addColumn('inscriptions', {
classic_number: {
type: 'bigint',
pgm.createTable('counts_by_mime_type', {
mime_type: {
type: 'text',
primaryKey: true,
},
count: {
type: 'int',
notNull: true,
default: 0,
},
});
}

export function down(pgm: MigrationBuilder): void {
pgm.dropColumn('inscriptions', 'classic_number');
}
Loading

0 comments on commit 5422156

Please sign in to comment.