Skip to content

Commit

Permalink
Merge pull request #62 from oasysgames/feat/add_staking_reward
Browse files Browse the repository at this point in the history
fix to export reward difference between epochs
  • Loading branch information
girafferz authored Dec 4, 2023
2 parents d0c8e95 + daa75f4 commit 6a8a0b9
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
28 changes: 22 additions & 6 deletions src/execute/executeStakerReward.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import moment = require('moment-timezone');
import { BigNumber } from 'ethers';
import { GoogleSpreadsheet } from 'google-spreadsheet';
import {
getAdditionalDataForStakerReward,
Expand Down Expand Up @@ -45,12 +46,9 @@ export const main = async (argv: stakerRewardArgs) => {
loopAsync.map(async (i: number) => {
console.log('RUNNING EPOCH ', i);
const epochData = await subgraph.getEpoch(i);
const nextEpochData = await subgraph.getEpoch(i + 1);

//validate epoches
if (epochData.epoches.length == 0) {
console.log(`epoch: can not nex to epoch ${epochData}`);
return;
}
const epoch =
typeof epochData.epoches[0].epoch === 'string'
? epochData.epoches[0].epoch
Expand All @@ -59,8 +57,13 @@ export const main = async (argv: stakerRewardArgs) => {
typeof epochData.epoches[0].block === 'string'
? epochData.epoches[0].block
: '';
const nextBlockByEpoch =
typeof nextEpochData.epoches[0].block === 'string'
? nextEpochData.epoches[0].block
: '';
if (!epoch) throw new Error('Can not get epoch data');
if (!block) throw new Error('Can not get block data');
if (!nextBlockByEpoch) throw new Error('Can not get block data');

//get oas price per epoch
//get price by time UTC
Expand All @@ -82,13 +85,26 @@ export const main = async (argv: stakerRewardArgs) => {
block,
timestamp,
};
//get stakerReward of staker by validator
const stakerReward = await subgraph.getStakerReward(

//Because use promise All, must get the staking reward of both epochs to calculate it
// reward of this epoch
const reward = await subgraph.getStakerReward(
parseInt(block, 10),
validator_address,
staker_address,
);

const nextReward = await subgraph.getStakerReward(
parseInt(nextBlockByEpoch, 10),
validator_address,
staker_address,
);

//get staker reward gap between epochs
const stakerReward = BigNumber.from(nextReward).sub(
BigNumber.from(reward),
);

//format data
const { rowData } = getAdditionalDataForStakerReward(
oasPrices,
Expand Down
17 changes: 7 additions & 10 deletions src/module/ValidatorStake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ export const getEpoches = async (
argv: validatorRewardArgs,
subgraph: Subgraph,
) => {
const latestEpoch = await subgraph.getLatestEpoch();
const latestEpochResult = await subgraph.getLatestEpoch();
const latestEpoch = latestEpochResult.epoches[0]?.epoch;
let from = argv.from_epoch;
let to = argv.to_epoch;

if (!argv.to_epoch) {
to = latestEpoch.epoches[0]?.epoch - 1;
to = latestEpoch - 1;
}

if (!argv.from_epoch) {
Expand All @@ -44,9 +45,7 @@ export const getEpoches = async (
from_time.utc().unix(),
);
from =
epochData.epoches.length > 0
? epochData.epoches[0].epoch
: latestEpoch.epoches[0].epoch;
epochData.epoches.length > 0 ? epochData.epoches[0].epoch : latestEpoch;
}

if (argv.to_date) {
Expand All @@ -58,17 +57,15 @@ export const getEpoches = async (
to_time.utc().unix(),
);
to =
epochData.epoches.length > 0
? epochData.epoches[0].epoch
: latestEpoch.epoches[0].epoch;
epochData.epoches.length > 0 ? epochData.epoches[0].epoch : latestEpoch;
}

if (from > to) {
to = from;
}

if (to > latestEpoch.epoches[0].epoch) {
to = latestEpoch.epoches[0].epoch;
if (to >= latestEpoch) {
to = latestEpoch - 1;
}
console.log('FROM EPOCH: ', from);
console.log('TO EPOCH: ', to);
Expand Down

0 comments on commit 6a8a0b9

Please sign in to comment.