From df7ae17198c04634a3f416e15836983c1f45294d Mon Sep 17 00:00:00 2001 From: woodenfurniture <125113430+woodenfurniture@users.noreply.github.com> Date: Wed, 29 May 2024 17:42:26 +1000 Subject: [PATCH] feat: add progress bar to rewards validation --- scripts/rewards-distribution/index.ts | 40 ++----------- scripts/rewards-distribution/validation.ts | 66 ++++++++++++++++++++++ 2 files changed, 72 insertions(+), 34 deletions(-) create mode 100644 scripts/rewards-distribution/validation.ts diff --git a/scripts/rewards-distribution/index.ts b/scripts/rewards-distribution/index.ts index 6f04b52..9939810 100644 --- a/scripts/rewards-distribution/index.ts +++ b/scripts/rewards-distribution/index.ts @@ -8,7 +8,7 @@ import { publicClient } from "./client"; import { calculateRewards } from "./calculateRewards/calculateRewards"; import { stakingV1Abi } from "./generated/abi-types"; import assert from "assert"; -import { Address } from "viem"; +import { validateRewardsDistribution } from "./validation"; const inquireBlockRange = async (): Promise<{ fromBlock: bigint; @@ -130,40 +130,12 @@ const main = async () => { logs, ); - // validate rewards per account against the contract - for (const [account, calculatedReward] of Object.entries( + await validateRewardsDistribution( + publicClient, earnedRewardsByAccount, - )) { - const [previousTotalEarnedForAccount, currentTotalEarnedForAccount] = - await Promise.all([ - publicClient.readContract({ - // TODO: dotenv or similar for contract addresses - address: ARBITRUM_RFOX_PROXY_CONTRACT_ADDRESS, - abi: stakingV1Abi, - functionName: "earned", - args: [account as Address], - blockNumber: fromBlock - 1n, // The end of the previous epoch - }), - publicClient.readContract({ - // TODO: dotenv or similar for contract addresses - address: ARBITRUM_RFOX_PROXY_CONTRACT_ADDRESS, - abi: stakingV1Abi, - functionName: "earned", - args: [account as Address], - blockNumber: toBlock, - }), - ]); - - const onChainReward = - currentTotalEarnedForAccount - previousTotalEarnedForAccount; - - assert( - calculatedReward === onChainReward, - `Expected reward for ${account} to be ${onChainReward}, got ${calculatedReward}`, - ); - } - - console.log("Validation passed."); + fromBlock, + toBlock, + ); // TODO: Confirm details again before proceeding }; diff --git a/scripts/rewards-distribution/validation.ts b/scripts/rewards-distribution/validation.ts new file mode 100644 index 0000000..4cd84be --- /dev/null +++ b/scripts/rewards-distribution/validation.ts @@ -0,0 +1,66 @@ +import cliProgress from "cli-progress"; +import colors from "ansi-colors"; +import { Address, PublicClient } from "viem"; +import { ARBITRUM_RFOX_PROXY_CONTRACT_ADDRESS } from "./constants"; +import { stakingV1Abi } from "./generated/abi-types"; +import assert from "assert"; + +export const validateRewardsDistribution = async ( + publicClient: PublicClient, + earnedRewardsByAccount: Record, + fromBlock: bigint, + toBlock: bigint, +) => { + const progressBar = new cliProgress.SingleBar({ + format: + "Validation Progress |" + + colors.cyan("{bar}") + + "| {percentage}% || {value}/{total} Accounts", + barCompleteChar: "\u2588", + barIncompleteChar: "\u2591", + hideCursor: true, + }); + + progressBar.start(Object.keys(earnedRewardsByAccount).length, 0, { + speed: "N/A", + }); + + // validate rewards per account against the contract + for (const [account, calculatedReward] of Object.entries( + earnedRewardsByAccount, + )) { + const [previousTotalEarnedForAccount, currentTotalEarnedForAccount] = + await Promise.all([ + publicClient.readContract({ + // TODO: dotenv or similar for contract addresses + address: ARBITRUM_RFOX_PROXY_CONTRACT_ADDRESS, + abi: stakingV1Abi, + functionName: "earned", + args: [account as Address], + blockNumber: fromBlock - 1n, // The end of the previous epoch + }), + publicClient.readContract({ + // TODO: dotenv or similar for contract addresses + address: ARBITRUM_RFOX_PROXY_CONTRACT_ADDRESS, + abi: stakingV1Abi, + functionName: "earned", + args: [account as Address], + blockNumber: toBlock, + }), + ]); + + const onChainReward = + currentTotalEarnedForAccount - previousTotalEarnedForAccount; + + assert( + calculatedReward === onChainReward, + `Expected reward for ${account} to be ${onChainReward}, got ${calculatedReward}`, + ); + + progressBar.increment(); + } + + progressBar.stop(); + + console.log("Validation passed."); +};