From 4b8c46eff3cf67f54e8e94458752acd8fabe9c5d Mon Sep 17 00:00:00 2001 From: akorchyn Date: Mon, 18 Mar 2024 17:43:03 +0200 Subject: [PATCH] preparing snapshot script and fixing exponent notation --- snapshotter/prepareSnapshot.js | 35 +++++++++++++++++++++++++++++----- snapshotter/stake.js | 3 +++ 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/snapshotter/prepareSnapshot.js b/snapshotter/prepareSnapshot.js index 50d34e6..f232185 100644 --- a/snapshotter/prepareSnapshot.js +++ b/snapshotter/prepareSnapshot.js @@ -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.') @@ -16,6 +17,7 @@ program program.parse(process.argv); const options = program.opts(); + let blockId = options.block; const dbParams = { database: options.dbname, @@ -26,11 +28,15 @@ 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); @@ -38,15 +44,34 @@ const loadActivityData = async (client) => { } 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 })); diff --git a/snapshotter/stake.js b/snapshotter/stake.js index b288935..07caa46 100644 --- a/snapshotter/stake.js +++ b/snapshotter/stake.js @@ -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 ', 'Block ID to fetch data from', '108194270')