Skip to content

Commit

Permalink
use stake
Browse files Browse the repository at this point in the history
  • Loading branch information
akorchyn committed Mar 18, 2024
1 parent a6794aa commit 8627593
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 8 deletions.
52 changes: 52 additions & 0 deletions snapshotter/prepareSnapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import pkg from 'pg';
const { Client } = pkg;
import fs from 'fs';
import { program } from 'commander';

program
.description('Load and process staking pools data from NEAR blockchain.')
.option('--block <type>', 'Block height of the snapshot', process.env.BLOCK)
.option('--dbname <type>', 'Database name', process.env.DB_NAME)
.option('--user <type>', 'Database user', process.env.DB_USER)
.option('--password <type>', 'Database password', process.env.DB_PASSWORD)
.option('--host <type>', 'Database host', process.env.DB_HOST)
.option('--table <type>', 'Target table name', process.env.TABLE_NAME)
.option('--json <type>', 'Path to the json with the stake data', process.env.JSON_PATH);

program.parse(process.argv);
const options = program.opts();

let blockId = options.block;
const dbParams = {
database: options.dbname,
user: options.user,
password: options.password,
host: options.host
};
const tableName = options.table;
let jsonPath = options.json;

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

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

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

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

const activityDataWithStake = activityData.map((activity) => {
const stake = stakeData[activity.account_id];
return {
...activity,
stake: stake ? stake : '0'
}
});

fs.writeFileSync(`snapshot-${blockId}.json`, JSON.stringify({ block_id: blockId, data: activityDataWithStake }));
19 changes: 11 additions & 8 deletions snapshotter/stake.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ program
program.parse(process.argv);
const options = program.opts();

let blockId = 108194270;
let blockId = options.block;
const dbParams = {
database: options.dbname,
user: options.user,
Expand Down Expand Up @@ -107,7 +107,7 @@ const processLockups = async (delegators) => {
}

lockupResults.forEach(result => {
delegators[result.account_id] = (delegators[result.account_id] ?? 0) + delegators[result.lockupAccount];
delegators[result.account_id] = (delegators[result.account_id] ?? new Big(0)).add(delegators[result.lockupAccount]);
delete delegators[result.lockupAccount];
});

Expand Down Expand Up @@ -179,10 +179,10 @@ async function loadDelegatorsFromValidators(validators) {
let results = {};
delegators.map(accounts => {
accounts.map(account => {
let stakedBalance = parseFloat(new Big(account.staked_balance).div(new Big(10).pow(24)).toFixed(2));
let stakedBalance = new Big(account.staked_balance);
if (stakedBalance > 0) {
let balance = results[account.account_id] ?? 0;
results[account.account_id] = balance + stakedBalance;
let balance = results[account.account_id] ?? new Big(0);
results[account.account_id] = balance.add(stakedBalance);
}
});
});
Expand Down Expand Up @@ -250,12 +250,12 @@ async function processGaps(accountsOrPools, client) {
for (let account_id of failedAccoutns) {
let stake = pools.find(pool => pool.account_id === account_id).stake;
console.log('Unsupported staking mechanism: Adding to the database:', account_id);
newDelegators[account_id] = (newDelegators[account_id] ?? 0) + stake;
newDelegators[account_id] = (newDelegators[account_id] ?? new Big(0)).add(stake);
added.push(account_id);
}
for (let account of users) {
console.log('Missed account: Adding to the database:', account.account_id);
newDelegators[account.account_id] = (newDelegators[account.account_id] ?? 0) + account.stake;
newDelegators[account.account_id] = (newDelegators[account.account_id] ?? new Big(0)).add(account.stake);
added.push(account.account_id);
}

Expand Down Expand Up @@ -290,7 +290,7 @@ async function checkAndFixGaps(delegators, client) {
result[account] = newDelegators[account];
}
for (let account of existedDelegators) {
result[account.account_id] = (result[account.account_id] ?? 0) + account.stake;
result[account.account_id] = (result[account.account_id] ?? new Big(0)).add(account.stake);
}
return result;
}
Expand All @@ -307,11 +307,14 @@ if (jsonPath === undefined) {
if (errors.length > 0) {
console.log("Errors", errors);
}
Object.keys(results).forEach(key => results[key] = results[key].toString());

fs.writeFileSync(`stakes_${blockId}.json`, JSON.stringify({ ...results }));
jsonPath = `stakes_${blockId}.json`;
}

const initialDelegators = JSON.parse(fs.readFileSync(jsonPath, 'utf-8'));
Object.keys(initialDelegators).forEach(key => initialDelegators[key] = new Big(initialDelegators[key]));


const client = new Client(dbParams);
Expand Down

0 comments on commit 8627593

Please sign in to comment.