Skip to content

Commit

Permalink
feat: btc indexer poc, update types and tables (#7)
Browse files Browse the repository at this point in the history
* chore: indexer draft

* chore: ideas

* fix: draft of new scan

* refactor: some

* feat: successful import

* feat: track chain tip

* feat: run modeS

* fix: bigint

* fix: filters and ordering

* docs: types
  • Loading branch information
rafaelcr committed Feb 27, 2023
1 parent bb70987 commit 6ff2bd9
Show file tree
Hide file tree
Showing 21 changed files with 1,516 additions and 1,271 deletions.
40 changes: 40 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,46 @@
},
"killBehavior": "polite",
},
{
"type": "node",
"request": "launch",
"name": "Run: readonly",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/src/index.ts"
],
"outputCapture": "std",
"internalConsoleOptions": "openOnSessionStart",
"env": {
"NODE_ENV": "development",
"TS_NODE_SKIP_IGNORE": "true",
"RUN_MODE": "readonly"
},
"killBehavior": "polite",
},
{
"type": "node",
"request": "launch",
"name": "Run: writeonly",
"runtimeArgs": [
"-r",
"ts-node/register"
],
"args": [
"${workspaceFolder}/src/index.ts"
],
"outputCapture": "std",
"internalConsoleOptions": "openOnSessionStart",
"env": {
"NODE_ENV": "development",
"TS_NODE_SKIP_IGNORE": "true",
"RUN_MODE": "writeonly"
},
"killBehavior": "polite",
},
{
"type": "node",
"request": "launch",
Expand Down
53 changes: 7 additions & 46 deletions migrations/1676395230930_inscriptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,43 +9,11 @@ export function up(pgm: MigrationBuilder): void {
type: 'serial',
primaryKey: true,
},
inscription_id: {
genesis_id: {
type: 'text',
notNull: true,
},
offset: {
type: 'int',
notNull: true,
},
block_height: {
type: 'int',
notNull: true,
},
block_hash: {
type: 'bytea',
notNull: true,
},
tx_id: {
type: 'bytea',
notNull: true,
},
address: {
type: 'text',
notNull: true,
},
sat_ordinal: {
type: 'numeric',
notNull: true,
},
sat_point: {
type: 'text',
notNull: true,
},
sat_rarity: {
type: 'text',
notNull: true,
},
fee: {
number: {
type: 'int',
notNull: true,
},
Expand All @@ -65,20 +33,13 @@ export function up(pgm: MigrationBuilder): void {
type: 'bytea',
notNull: true,
},
timestamp: {
type: 'timestamptz',
fee: {
type: 'bigint',
notNull: true,
},
});
pgm.createConstraint(
'inscriptions',
'inscriptions_inscription_id_unique',
'UNIQUE(inscription_id)'
);
pgm.createIndex('inscriptions', ['sat_ordinal']);
pgm.createIndex('inscriptions', ['sat_rarity']);
pgm.createIndex('inscriptions', ['block_height']);
pgm.createIndex('inscriptions', ['block_hash']);
pgm.createIndex('inscriptions', ['address']);
pgm.createConstraint('inscriptions', 'inscriptions_genesis_id_unique', 'UNIQUE(genesis_id)');
pgm.createIndex('inscriptions', ['genesis_id']);
pgm.createIndex('inscriptions', ['number']);
pgm.createIndex('inscriptions', ['mime_type']);
}
81 changes: 81 additions & 0 deletions migrations/1677284495299_locations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/* 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('locations', {
id: {
type: 'serial',
primaryKey: true,
},
inscription_id: {
type: 'int',
notNull: true,
},
block_height: {
type: 'int',
notNull: true,
},
block_hash: {
type: 'text',
notNull: true,
},
tx_id: {
type: 'text',
notNull: true,
},
address: {
type: 'text',
notNull: true,
},
output: {
type: 'text',
notNull: true,
},
offset: {
type: 'bigint',
notNull: true,
},
value: {
type: 'bigint',
notNull: true,
},
sat_ordinal: {
type: 'bigint',
notNull: true,
},
sat_rarity: {
type: 'text',
notNull: true,
},
timestamp: {
type: 'timestamptz',
notNull: true,
},
genesis: {
type: 'boolean',
default: true,
notNull: true,
},
current: {
type: 'boolean',
default: true,
notNull: true,
},
});
pgm.createConstraint(
'locations',
'locations_inscription_id_fk',
'FOREIGN KEY(inscription_id) REFERENCES inscriptions(id) ON DELETE CASCADE'
);
pgm.createConstraint(
'locations',
'locations_inscription_id_block_hash_unique',
'UNIQUE(inscription_id, block_hash)'
);
pgm.createIndex('locations', ['block_height']);
pgm.createIndex('locations', ['block_hash']);
pgm.createIndex('locations', ['address']);
pgm.createIndex('locations', ['output']);
}
27 changes: 27 additions & 0 deletions migrations/1677360299810_chain-tip.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* 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('chain_tip', {
id: {
type: 'bool',
primaryKey: true,
default: true,
},
block_height: {
type: 'int',
notNull: true,
default: 1,
},
});
// Ensure only a single row can exist
pgm.addConstraint('chain_tip', 'chain_tip_one_row', 'CHECK(id)');
// Create the single row
pgm.sql('INSERT INTO chain_tip VALUES(DEFAULT)');
}

export function down(pgm: MigrationBuilder): void {
pgm.dropTable('chain_tip');
}
Loading

0 comments on commit 6ff2bd9

Please sign in to comment.