Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: adapt to Indexer's new block ID #749

Merged
merged 15 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
## Duplicate this file in the same directory and name it `.env`.
## Assign custom values for your project inside `.env`.

PORT=3000
URL=http://127.0.0.1:3000
## You need to export this variable manually to make it available for sequelize
## via `export DATABASE_URI=...` on the command line interface
# DATABASE_URI=<protocol://username:password@host:port/database>
DATABASE_URI=postgres://postgres:postgres@localhost:5432/postgres
# while using the "...kilt.io" endpoints, please add a trailing "/" so that polkadot.js displays the right colors.
BLOCKCHAIN_ENDPOINT=wss://kilt.ibp.network
Expand Down
2 changes: 1 addition & 1 deletion .nvmrc
Original file line number Diff line number Diff line change
@@ -1 +1 @@
lts/Iron
lts/Jod
2 changes: 2 additions & 0 deletions .sequelizerc
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require('dotenv').config();

module.exports = {
url: process.env.DATABASE_URI,
'migrations-path': 'src/migrations',
Expand Down
29 changes: 10 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,42 +46,33 @@ The database should start to populate from the configured indexer.
This project consciously did not define a `config/config.json` for sequelize.
Instead, it relies on the `url` property inside `.sequilizerc`.
In turn, the `url` takes `process.env.DATABASE_URI` as a value.
This means that `DATABASE_URI` needs to previously be set as an environment variable, sadly manually.

To be able to run any `sequelize-cli` commands, first you need to execute:

```
export DATABASE_URI=<protocol://username:password@host:port/database>
```

If you are using the default values, as created via `pnpm start-db`, it narrows it down to this:

```
export DATABASE_URI=postgres://postgres:postgres@localhost:5432/postgres
```

**Before introducing any changes to the database structure you new to recreate the past.**
**For this follow these steps:**

1. Start the database, if not running yet.
1. Make sure you have all dependencies installed, running:

```
pnpm install
```

2. Start the database, if not running yet.
Locally, you can run:
```
pnpm start-db
```
2. Run all past migrations, via:
3. Run all past migrations, via:

```
pnpm migrate
```

_Remember to manually set the `DATABASE_URI` first as described above._

3. Start the project to populate the database, by running:
4. Start the project to populate the database, by running:
```
pnpm dev
```
_After a while, you can stop the project, but the postgres container should continue running._
4. Add the seeds to the database:
5. Add the seeds to the database, by running:
```
pnpm seed
```
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"type": "module",
"packageManager": "[email protected]",
"engines": {
"node": ">=20"
"node": ">=22"
},
"scripts": {
"prettify": "prettier --write \"src/**/*.astro\" \"src/**/*.ts*\" \"src/**/*.css*\"",
Expand All @@ -27,6 +27,7 @@
"@polkadot/extension-dapp": "^0.46.5",
"@polkadot/util": "^12.6.2",
"astro": "^3.6.5",
"dotenv": "^16.4.7",
"got": "^12.6.1",
"http-status-codes": "^2.3.0",
"lodash-es": "^4.17.21",
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

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

2 changes: 1 addition & 1 deletion src/components/CTypeDetails/CTypeDetails.astro
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ linkToW3N.pathname = web3Name ?? creator;

const linkToBlock = new URL(`https://polkadot.js.org/apps/`);
linkToBlock.searchParams.set('rpc', blockchainEndpoint);
linkToBlock.hash = `/explorer/${block ? 'query/' + block : ''}`;
linkToBlock.hash = `/explorer/${block != null ? 'query/' + block : ''}`;
---

<section class={containerStyles.bigContainer}>
Expand Down
57 changes: 57 additions & 0 deletions src/migrations/20241206171516-integrateBlockNumbers.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
'use strict';

/** @type {import('sequelize-cli').Migration} */
module.exports = {
async up(queryInterface, Sequelize) {
// First, drop the 'search' column since it depends on 'block'
await queryInterface.removeColumn('CTypes', 'search');

// Transformation the type of 'block' from string to bigint
await queryInterface.changeColumn('CTypes', 'block', {
type: 'BIGINT using (block::bigint)',
allowNull: true,
});
// recreate the 'search' column
await queryInterface.addColumn(
'CTypes',
'search',
`tsvector generated always as (to_tsvector('english',
coalesce("id"::text, '') || ' ' ||
coalesce("schema"::text, '') || ' ' ||
coalesce("title"::text, '') || ' ' ||
coalesce("properties"::text, '') || ' ' ||
coalesce("type"::text, '') || ' ' ||
coalesce("creator"::text, '') || ' ' ||
coalesce("block"::text, '') || ' ' ||
coalesce("description"::text, ''))
) stored`,
);
},

async down(queryInterface, Sequelize) {
// First, drop the 'search' column since it depends on 'block'
await queryInterface.removeColumn('CTypes', 'search');

// Transformation the type of 'block' from bigint to string
await queryInterface.changeColumn('CTypes', 'block', {
type: Sequelize.STRING,
allowNull: true,
});

// recreate the 'search' column
await queryInterface.addColumn(
'CTypes',
'search',
`tsvector generated always as (to_tsvector('english',
coalesce("id"::text, '') || ' ' ||
coalesce("schema"::text, '') || ' ' ||
coalesce("title"::text, '') || ' ' ||
coalesce("properties"::text, '') || ' ' ||
coalesce("type"::text, '') || ' ' ||
coalesce("creator"::text, '') || ' ' ||
coalesce("block"::text, '') || ' ' ||
coalesce("description"::text, ''))
) stored`,
);
},
};
4 changes: 2 additions & 2 deletions src/models/ctype.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ interface CTypeDataInput extends Omit<ICType, '$id' | '$schema'> {
schema: ICType['$schema'];
creator: DidUri;
createdAt: Date;
block: string | null;
block: bigint | null;
description: string | null;
attestationsCreated?: number;
}
Expand Down Expand Up @@ -55,7 +55,7 @@ export const CTypeModelDefinition: ModelAttributes = {
allowNull: false,
},
block: {
type: DataTypes.STRING,
type: DataTypes.BIGINT,
},
description: {
type: DataTypes.STRING,
Expand Down
11 changes: 7 additions & 4 deletions src/utilities/indexer/queryCTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,14 @@ interface QueriedCType {
web3NameId: string;
};
registrationBlock: {
id: string; // Block Ordinal Number, without punctuation
/** Block Ordinal Number, without punctuation */
id: string;
hash: HexString;
timeStamp: string; // ISO8601 Date String, like 2022-02-09T13:09:18.217
/** ISO8601 Date String, like 2022-02-09T13:09:18.217 */
timeStamp: string;
};
definition: string; // stringified JSON of cType Schema
/** Stringified JSON of cType Schema */
definition: string;
}

export async function queryCTypes() {
Expand Down Expand Up @@ -83,7 +86,7 @@ export async function queryCTypes() {
schema: $schema,
createdAt: new Date(registrationBlock.timeStamp + 'Z'),
creator,
block: registrationBlock.id,
block: BigInt(registrationBlock.id),
...rest,
attestationsCreated,
});
Expand Down
10 changes: 5 additions & 5 deletions src/utilities/mockCTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export const mockCTypes: Record<string, CTypeData> = {
creator: 'did:kilt:4pehddkhEanexVTTzWAtrrfo2R7xPnePpuiJLC7shQU894aY',
createdAt: new Date('2023-05-01T12:00:00'),
description: 'This is some example cType data',
block: '123',
block: 123n,
attestationsCreated: 1,
tags: [
{
Expand All @@ -35,7 +35,7 @@ export const mockCTypes: Record<string, CTypeData> = {
creator: 'did:kilt:4pehddkhEanexVTTzWAtrrfo2R7xPnePpuiJLC7shQU894aY',
createdAt: new Date('2023-05-01T12:01:00'),
description: 'This is an example of a CType with a nested property',
block: '321',
block: 321n,
attestationsCreated: 22,
isHidden: false,
},
Expand All @@ -55,7 +55,7 @@ export const mockCTypes: Record<string, CTypeData> = {
creator: 'did:kilt:4pehddkhEanexVTTzWAtrrfo2R7xPnePpuiJLC7shQU894aY',
createdAt: new Date('2023-05-01T12:02:00'),
description: 'This is an example of a CType with a nested CType',
block: '456',
block: 456n,
attestationsCreated: 333,
isHidden: false,
},
Expand All @@ -68,7 +68,7 @@ export const mockCTypes: Record<string, CTypeData> = {
creator: 'did:kilt:4pehddkhEanexVTTzWAtrrfo2R7xPnePpuiJLC7shQU894aY',
createdAt: new Date('2023-05-01T12:00:00'),
description: 'This is some example cType data',
block: '123',
block: 123n,
attestationsCreated: 1,
tags: [
{
Expand Down Expand Up @@ -121,7 +121,7 @@ export const mockCTypes: Record<string, CTypeData> = {
creator: 'did:kilt:4rrkiRTZgsgxjJDFkLsivqqKTqdUTuxKk3FX3mKFAeMxsR5E',
attestationsCreated: 4444,
createdAt: new Date('2023-05-01T12:03:00'),
block: '456',
block: 456n,
isHidden: false,
},
};
Loading