Skip to content

Commit

Permalink
Merge pull request #935 from autonomys/feat/convert-genesis-allocatio…
Browse files Browse the repository at this point in the history
…n-to-db-seeds

Add script to convert genesis allocation to SQL seeds
  • Loading branch information
marc-aurele-besner authored Nov 11, 2024
2 parents 8a535b9 + 4bdf44e commit c252b75
Show file tree
Hide file tree
Showing 9 changed files with 217,589 additions and 1 deletion.
3 changes: 2 additions & 1 deletion indexers/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"console": "hasura console",
"metadata": "hasura metadata apply --skip-update-check",
"migrate": "hasura migrate apply --database-name default",
"start": "yarn metadata && yarn migrate && yarn console"
"start": "yarn metadata && yarn migrate && yarn console",
"apply-seeds:mainnet": "hasura seeds apply --file genesis_allocation.sql --database-name default"
},
"devDependencies": {
"hasura-cli": "2.36.2"
Expand Down
6 changes: 6 additions & 0 deletions indexers/db/scripts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# DB scripts

## Genesis allocation to seeds

[genesis-allocation-to-seeds](./genesis-allocation-to-seeds/README.md)
This script will take the genesis allocation JSON and convert it to seeds. The seeds will be added to the database.
1 change: 1 addition & 0 deletions indexers/db/scripts/genisis-allocation-to-seeds/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
seeds/
11 changes: 11 additions & 0 deletions indexers/db/scripts/genisis-allocation-to-seeds/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Genesis allocation to seeds

## Description

This script will take the genesis allocation JSON and convert it to seeds. The seeds will be added to the database.

## Usage

```bash
deno run --allow-net --allow-write --allow-read --allow-env ./main.ts
```
9 changes: 9 additions & 0 deletions indexers/db/scripts/genisis-allocation-to-seeds/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"tasks": {
"dev": "deno run --watch main.ts"
},
"imports": {
"@autonomys/auto-utils": "npm:@autonomys/auto-utils@^1.0.4",
"@std/uuid": "jsr:@std/uuid@^1.0.4"
}
}
651 changes: 651 additions & 0 deletions indexers/db/scripts/genisis-allocation-to-seeds/deno.lock

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions indexers/db/scripts/genisis-allocation-to-seeds/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { NAMESPACE_DNS, v1, v5 } from "jsr:@std/uuid";
import { decode, Keyring } from "npm:@autonomys/auto-utils";

// Load the JSON data
const response = await fetch(
"https://raw.githubusercontent.com/autonomys/subspace/refs/heads/main/crates/subspace-node/src/genesis_allocations.json"
);
const data = await response.json();

// Ensure the seeds directory exists
await Deno.mkdir("seeds", { recursive: true });

// Open a file to write the SQL output
const accountsFile = await Deno.open("seeds/accounts.sql", {
write: true,
create: true,
truncate: true,
});
const accountHistoriesFile = await Deno.open("seeds/account_histories.sql", {
write: true,
create: true,
truncate: true,
});

// Iterate over each entry in the JSON data and generate the insert sql
for (const entry of data) {
const keyring = new Keyring({ type: "sr25519", ss58Format: 42 });
const accountId = keyring.encodeAddress(entry[0], 6094);
const freeBalance = (
BigInt(entry[1]) * BigInt(1000000000000000000)
).toString(); // Convert to 10^18 units
const reservedBalance = "0";
const totalBalance = freeBalance;
const createdAt = 0;
const updatedAt = 0;
const uniqueNamespace = await v1.generate();
const _id = await v5.generate(uniqueNamespace, accountId);
const _blockRange = "[0,)";

await accountsFile.write(
new TextEncoder().encode(
`INSERT INTO consensus.accounts (id, nonce, free, reserved, total, created_at, updated_at, _id, _block_range) VALUES ('${accountId}', 0, ${freeBalance}, ${reservedBalance}, ${totalBalance}, ${createdAt}, ${updatedAt}, '${_id}', '${_blockRange}');\n`
)
);
await accountHistoriesFile.write(
new TextEncoder().encode(
`INSERT INTO consensus.account_histories (id, nonce, free, reserved, total, created_at, updated_at, _id, _block_range) VALUES ('${accountId}', 0, ${freeBalance}, ${reservedBalance}, ${totalBalance}, ${createdAt}, ${updatedAt}, '${_id}', '${_blockRange}');\n`
)
);
}

// Close the file
accountsFile.close();
accountHistoriesFile.close();
216,854 changes: 216,854 additions & 0 deletions indexers/db/seeds/default/genesis_allocation.sql

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions indexers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"console:dev": "export $(grep -v '^#' ../.env | xargs) && export $(grep -v '^#' ../.env.dev | xargs) && lerna run console",
"metadata:dev": "export $(grep -v '^#' ../.env | xargs) && export $(grep -v '^#' ../.env.dev | xargs) && lerna run metadata",
"migrate:dev": "export $(grep -v '^#' ../.env | xargs) && export $(grep -v '^#' ../.env.dev | xargs) && lerna run migrate",
"apply-seeds:mainnet": "export $(grep -v '^#' ../.env | xargs) && export $(grep -v '^#' ../.env.dev | xargs) && lerna run apply-seeds:mainnet",
"prepack": "lerna run prepack",
"bootstrap": "lerna run bootstrap",
"graph": "nx graph"
Expand Down

0 comments on commit c252b75

Please sign in to comment.