proposal-25-restore-rewards/utils/stakers.ts

48 lines
2.0 KiB
TypeScript

import Web3 from "web3";
import BigNumber from "bignumber.js"
import { AbiItem } from 'web3-utils';
import { EthAddress, IStaker } from "./@types/staker";
import * as dotenv from "dotenv";
import { governanceAddress, governanceRewardsProposalBlock, hackBlock, oldStakingAddress } from "./config";
import GovernanceAbi from "../abi/GovernanceAbi.json";
import StakingAbi from "../abi/StakingABI.json";
dotenv.config();
const web3 = new Web3(process.env.MAINNET_RPC_URL as string);
function getGovernanceFunctionSelector(functionName: string): string {
const lockFunctionAbi = GovernanceAbi.find(item => item.name == functionName);
if (!lockFunctionAbi) throw new Error(`Cannot find function ${functionName} in Governance contract ABI`);
const selector = web3.eth.abi.encodeFunctionSignature(lockFunctionAbi as AbiItem);
return selector;
}
async function getAddressesStakersWithProssibleRewards(): Promise<Array<EthAddress>> {
const governanceContract = new web3.eth.Contract(GovernanceAbi as AbiItem[], governanceAddress);
const rewardsUpdateEvents =
await governanceContract.getPastEvents("RewardUpdateSuccessful", { fromBlock: governanceRewardsProposalBlock });
const governanceStakers = rewardsUpdateEvents.map(event => event.returnValues.account as string);
return [...new Set(governanceStakers)];
}
export async function getStakersWithRewardsBeforeHack(): Promise<Array<IStaker>> {
const stakingContract = new web3.eth.Contract(StakingAbi as AbiItem[], oldStakingAddress);
const stakersAddresses = await getAddressesStakersWithProssibleRewards();
let stakersWithRewardsBeforeHack: Array<IStaker> = [];
for (const address of stakersAddresses) {
const rewardsBeforeHack = await stakingContract.methods.checkReward(address).call({}, hackBlock - 1);
if (BigNumber(rewardsBeforeHack).div(1e18).isGreaterThan(1))
stakersWithRewardsBeforeHack.push({ address, rewardBalance: BigNumber(rewardsBeforeHack) });
}
return stakersWithRewardsBeforeHack;
}