Skip to content

Commit

Permalink
fix: temporary poor man's stake-distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
slowbackspace committed Nov 26, 2021
1 parent 0d8c4d5 commit ff8caea
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 29 deletions.
Binary file not shown.
Binary file not shown.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
"@oclif/core": "1.0.4",
"@oclif/plugin-help": "^5",
"bech32": "^2.0.0",
"big.js": "^6.1.1",
"cli-ux": "^5.6.4",
"safe-stable-stringify": "^2.2.0",
"tslib": "^1"
},
"devDependencies": {
"@types/big.js": "^6.1.2",
"@types/jest": "^27.0.3",
"@types/node": "^14",
"@typescript-eslint/eslint-plugin": "^5.4.0",
Expand Down
104 changes: 75 additions & 29 deletions src/commands/query/stake-distribution.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { Responses } from '@blockfrost/blockfrost-js';
import Big from 'big.js';
import { cli } from 'cli-ux';
import { BaseCommand } from '../../helpers/BaseCommand';

Expand Down Expand Up @@ -40,72 +42,116 @@ import { BaseCommand } from '../../helpers/BaseCommand';
// "denominator": 20150911152895487
// },
// ...

type Data = {
pool_id: string;
numerator: BigInt;
denominator: BigInt;
};
export class StakeDistribution extends BaseCommand {
// TODO: toFile method
prettyPrint = (
res: {
pool_id: string;
live_stake: string;
}[],
) => {
prettyPrint = (res: Data[]) => {
const calculatedData = res.map(stake => ({
pool_id: stake.pool_id,
frac: Big(stake.numerator.toString()).div(Big(stake.denominator.toString())).toExponential(3),
}));
this.log();
cli.table(
res,
{
pool_id: {
header: 'PoolId',
minWidth: 30,
try {
cli.table(
calculatedData,
{
pool_id: {
header: 'PoolId',
minWidth: 30,
},
frac: {
header: 'Stake frac',
},
},
live_stake: {
header: 'Stake frac',
},
},
{ printLine: this.log },
);
{ printLine: console.log, 'no-truncate': true },
);
} catch (err) {
console.error(err);
}
this.log();
};

async toFile(data: Data[]): Promise<void> {
const stakePerPool: Record<
string,
{
numerator: BigInt;
denominator: BigInt;
}
> = {};
data.map(stake => {
stakePerPool[stake.pool_id] = {
numerator: stake.numerator,
denominator: stake.denominator,
};
});
super.toFile(stakePerPool);
}

doWork = async () => {
const client = await this.getClient();

// TODO: refactor once we have proper endpoint for stake distribution
const pools = await client.poolsAll();
const pools = (await client.poolsAll()).slice(0, 20);
const allStakes: {
pool_id: string;
live_stake: string;
live_stake: BigInt;
}[] = [];
const SMALL_BATCH = 10;
const LARGE_BATCH = 100;
let batch_size = LARGE_BATCH;
let stakesSum = BigInt(0);

const getPromiseBundle = (startIndex: number, batchSize: number) => {
const promises = [...Array(batchSize).keys()].map(i =>
client.poolsById(pools[startIndex + i]),
);
return promises;
const promises = [...Array(batchSize).keys()].map(i => {
const poolId = pools[startIndex + i];
return poolId ? client.poolsById(poolId) : undefined;
});
return promises.filter(p => !!p) as unknown as Responses['pool'][];
};

// cli.action.start('Fetching pools');
const progressBar = cli.progress({
format: '{value}/{total} pools {bar}',
barCompleteChar: '\u2588',
barIncompleteChar: '\u2591',
});
progressBar.start(pools.length, 0);
// Blockfrost API allows burst of 500 request, then we can only make 10 requests per second
for (let i = 0; i < pools.length; i += batch_size) {
this.log(`Fetching pools ${i + batch_size}/${pools.length}`);
// this.log(`Fetching pools ${i + batch_size}/${pools.length}`);
const promiseSlice = getPromiseBundle(i, batch_size);
if (batch_size === SMALL_BATCH) {
// wait 1 second before each small batch
await new Promise((resolve, reject) => setTimeout(() => resolve(true), 1000));
await new Promise(resolve => setTimeout(() => resolve(true), 1000));
}
const partialResults = await Promise.all(promiseSlice);
progressBar.update(i + batch_size);
partialResults.forEach(res => {
const liveStake = BigInt(res.live_stake);
allStakes.push({
pool_id: res.pool_id,
live_stake: res.live_stake,
live_stake: liveStake,
});
stakesSum += liveStake;
});
if (i >= 400) {
// after 400 request switch to small batch
batch_size = SMALL_BATCH;
}
}
// cli.action.stop();
progressBar.stop();

return allStakes;
const formattedStakes = allStakes.map(stake => ({
pool_id: stake.pool_id,
numerator: stake.live_stake,
denominator: stakesSum,
}));
return formattedStakes;
};
}
16 changes: 16 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1163,6 +1163,13 @@ __metadata:
languageName: node
linkType: hard

"@types/big.js@npm:^6.1.2":
version: 6.1.2
resolution: "@types/big.js@npm:6.1.2"
checksum: fb0daa4b23e1cb9d0d1c847722ae3764978d0290875d91687c6ecc7566860129ce86b190b732294484077d45f9b82ef3e97784ee32c83aef05e8c8433c4fde6a
languageName: node
linkType: hard

"@types/cacheable-request@npm:^6.0.1":
version: 6.0.2
resolution: "@types/cacheable-request@npm:6.0.2"
Expand Down Expand Up @@ -2054,6 +2061,13 @@ __metadata:
languageName: node
linkType: hard

"big.js@npm:^6.1.1":
version: 6.1.1
resolution: "big.js@npm:6.1.1"
checksum: ba5db278e7d6700d7f92b7421e851bb65b6336d90b882869696c50574623c6be31f81500c3195fb1b391045163ffe62745e1c2bc30b35cb15e1b3e3ed135266b
languageName: node
linkType: hard

"binaryextensions@npm:^2.1.2":
version: 2.3.0
resolution: "binaryextensions@npm:2.3.0"
Expand Down Expand Up @@ -2098,11 +2112,13 @@ __metadata:
"@blockfrost/blockfrost-js": 3.0.0-beta.10
"@oclif/core": 1.0.4
"@oclif/plugin-help": ^5
"@types/big.js": ^6.1.2
"@types/jest": ^27.0.3
"@types/node": ^14
"@typescript-eslint/eslint-plugin": ^5.4.0
"@typescript-eslint/parser": ^5.4.0
bech32: ^2.0.0
big.js: ^6.1.1
cli-ux: ^5.6.4
eslint: ^7.32.0
eslint-config-oclif: ^4.0.0
Expand Down

0 comments on commit ff8caea

Please sign in to comment.