Skip to content

Commit

Permalink
preparing snapshot script and fixing exponent notation
Browse files Browse the repository at this point in the history
  • Loading branch information
akorchyn committed Mar 18, 2024
1 parent 7c01267 commit 4b8c46e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 5 deletions.
35 changes: 30 additions & 5 deletions snapshotter/prepareSnapshot.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import pkg from 'pg';
const { Client } = pkg;
import fs from 'fs';
import { program } from 'commander';
import assert from 'assert';

program
.description('Load and process staking pools data from NEAR blockchain.')
Expand All @@ -16,6 +17,7 @@ program
program.parse(process.argv);
const options = program.opts();


let blockId = options.block;
const dbParams = {
database: options.dbname,
Expand All @@ -26,27 +28,50 @@ const dbParams = {
const tableName = options.table;
let jsonPath = options.json;

console.log("Creating snapshot for block", blockId);

const stakeData = JSON.parse(fs.readFileSync(jsonPath, 'utf-8'));

console.log("Loaded stake data for", Object.keys(stakeData).length, "accounts");

const loadActivityData = async (client) => {
const query = `
SELECT * from ${tableName})
SELECT * from ${tableName}
`;

const res = await client.query(query);
return res.rows;
}

const client = new Client(dbParams);
await client.connect();
const activityData = await loadActivityData(client);
client.end();
await client.end();

console.log("Loaded activity data for", activityData.length, "accounts");

const activityDataWithStake = activityData.map((activity) => {
const stake = stakeData[activity.account_id];
const stake = stakeData[activity.signer_account_id] ?? '0';
if (stake > 0) {
console.log(`Account ${activity.account_id} has stake ${stake}`);
}
const example_months = activity.example_months.split(',');
const example_transaction_hashes = activity.example_transaction_hashes.split(',');
const active_months = parseInt(activity.active_months);
assert(example_months.length === example_transaction_hashes.length, 'Length of example_months and example_transaction_hashes should be the same');
assert(example_months.length === active_months, 'Length of example_months should be the same as active_months');

return {
...activity,
stake: stake ? stake : '0'
account_id: activity.signer_account_id,
example_months,
example_transaction_hashes,
active_months,
transactions: activity.transactions,
stake
}
});

console.log(`Writing snapshot to snapshot-${blockId}.json`);
console.log(activityDataWithStake);

fs.writeFileSync(`snapshot-${blockId}.json`, JSON.stringify({ block_id: blockId, data: activityDataWithStake }));
3 changes: 3 additions & 0 deletions snapshotter/stake.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import { program } from 'commander';

const EMPTY_HASH = '11111111111111111111111111111111'

// We don't want to have exponential notation in toString
Big.PE = 1000;

program
.description('Load and process staking pools data from NEAR blockchain.')
.option('--block <type>', 'Block ID to fetch data from', '108194270')
Expand Down

0 comments on commit 4b8c46e

Please sign in to comment.