Skip to content

Commit

Permalink
sum rewards script
Browse files Browse the repository at this point in the history
  • Loading branch information
bostjan-kovac committed Oct 26, 2023
1 parent cafb388 commit 0cdff50
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

"scripts": {
"process-staking-rewards": "yarn ts-node src/processStakingRewards.ts",
"sum-staking-rewards": "yarn ts-node src/sumStakingRewards.ts",
"build": "npm-run-all yarn tsc",
"--- development scripts ---": "",
"sync": "ts-node ./src/scripts/sync-artifacts.ts",
Expand Down
37 changes: 37 additions & 0 deletions src/services/CalculatingRewardsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -624,6 +624,43 @@ export class CalculatingRewardsService {
return rewardsData;
}

public async sumRewards(firstRewardEpoch: number, numberOfEpochs: number) {
let rewardsData: RewardsData[] = [];
for (let epoch = firstRewardEpoch; epoch < firstRewardEpoch + numberOfEpochs; epoch++ ) {
const filesPath = `generated-files/reward-epochs-${epoch}-${epoch}`;
const json = JSON.parse(fs.readFileSync(`${filesPath}/data-reward-manager.json`, 'utf8'));
const addresses: string[] = json.addresses;
const rewardAmounts: string[] = json.rewardAmounts;

for(let i = 0; i < addresses.length; i++) {
const address = addresses[i];
const index = rewardsData.findIndex(rewardedData => rewardedData.address == address);
if (index > -1) {
rewardsData[index].amount += BigInt(rewardAmounts[i]);
}
else {
rewardsData.push({
address: address,
amount: BigInt(rewardAmounts[i])
});
}
}
}
let dataRewardManager = {} as DataValidatorRewardManager;
let arrayAddresses = rewardsData.map(recipient => {
return recipient.address;
});
let arrayAmounts = rewardsData.map(recipient => {
return recipient.amount.toString();
});
dataRewardManager.addresses = arrayAddresses;
dataRewardManager.rewardAmounts = arrayAmounts;
let epochRewardsJSON = JSON.stringify(dataRewardManager, (_, v) => typeof v === 'bigint' ? v.toString() : v, 2);
const generatedFilesPath = "generated-files/validator-rewards"
fs.mkdirSync(generatedFilesPath, { recursive: true });
fs.writeFileSync(`${generatedFilesPath}/epochs-${firstRewardEpoch}-${firstRewardEpoch + numberOfEpochs - 1}.json`, epochRewardsJSON, "utf8");
}

}


53 changes: 53 additions & 0 deletions src/sumStakingRewards.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env node

import dotenv from 'dotenv';
import { iocContainer } from './ioc';
import { ConfigurationService } from './services/ConfigurationService';
import { CalculatingRewardsService } from './services/CalculatingRewardsService';

// initialize configuration
dotenv.config();

let yargs = require('yargs');

let args = yargs
.option('config', {
alias: 'c',
type: 'string',
description: 'The path to json config file with network information',
default: 'configs/networks/flare.json',
})
.option('firstEpoch', { alias: 'f', type: 'number', description: 'First reward epoch to calculate rewards for' })
.option('ftsoPerformanceWei', { alias: 'p', type: 'string', description: 'Required FTSO performance (received FTSO rewards in wei) for the node to be eligible for staking rewards. Performance should be strictly greater than ftsoPerformance' })
.option('boostingFactor', { alias: 'b', type: 'number', description: 'Boosting factor (for calculating boost amount)' })
.option('votePowerCapBIPS', { alias: 'v', type: 'number', description: 'Cap vote power to x% of total stake amount' })
.option('numUnrewardedEpochs', { alias: 'u', type: 'number', description: 'Number of reward epochs for which validators and delegators were not yet rewarded' })
.option('uptimeVotigPeriodLength', { alias: 'l', type: 'number', description: 'Length of voting period (which starts at the end of reward epoch) for reward epoch uptime' })
.option('batchSize', { alias: 'b', type: 'number', description: 'Batch size for blocks to process events' })
.option('rps', { alias: 'r', type: 'number', description: 'Request per second' })
.option('uptimeVotingThreshold', { alias: 't', type: 'number', description: 'Required number of votes for uptime to be considered high enough' })
.option('minForBEBGwei', { alias: 'm', type: 'string', description: 'Minimal amount (in gwei) of total self-bond to be eligible for boosting' })
.option('rewardAmountEpochWei', { alias: 'a', type: 'string', description: 'Reward amount (in wei) to be distributed per reward epoch' })
.option('apiPath', { alias: 'y', type: 'string', description: 'Api for validators and delegators' })
.argv;


process.env.CONFIG_FILE = args['config'];

const calculatingRewardsService = iocContainer(null).get(CalculatingRewardsService);
const configurationService = iocContainer(null).get(ConfigurationService);


async function runProcessSumRewards() {
let firstRewardEpoch = args['firstEpoch'] ? args['firstEpoch'] : configurationService.firstRewardEpoch;
let numUnrewardedEpochs = args['numUnrewardedEpochs'] ? args['numUnrewardedEpochs'] : configurationService.numUnrewardedEpochs;

await calculatingRewardsService.sumRewards(firstRewardEpoch, numUnrewardedEpochs);
}

runProcessSumRewards()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});

0 comments on commit 0cdff50

Please sign in to comment.