diff --git a/scripts/rewards-distribution/calculateRewards/calculateRewards.ts b/scripts/rewards-distribution/calculateRewards/calculateRewards.ts index a5a595f..991a26a 100644 --- a/scripts/rewards-distribution/calculateRewards/calculateRewards.ts +++ b/scripts/rewards-distribution/calculateRewards/calculateRewards.ts @@ -1,6 +1,6 @@ import { Address, Block } from "viem"; import { RFoxLog, StakeLog, UnstakeLog } from "../events"; -import { getStakingAmount, isLogType } from "../helpers"; +import { isLogType } from "../helpers"; import { REWARD_RATE, WAD } from "../constants"; import assert from "assert"; @@ -143,14 +143,14 @@ export const calculateRewards = ( // the previous epoch. This prevents us missing rewards for the first block in the epoch. previousEpochEndBlock: Block, epochEndBlock: Block, - logs: { log: RFoxLog; timestamp: bigint }[], + orderedLogs: { log: RFoxLog; timestamp: bigint }[], ) => { let totalStaked = 0n; let rewardPerTokenStored = 0n; let lastUpdateTimestamp = contractCreationBlock.timestamp; const stakingInfoByAccount: Record
= {}; - const stakingLogs = logs.filter( + const stakingLogs = orderedLogs.filter( ( logWithTimestamp, ): logWithTimestamp is { log: StakeLog | UnstakeLog; timestamp: bigint } => @@ -250,19 +250,13 @@ export const calculateRewards = ( ); } - const epochMetadataByAccount: Record< - Address, - { runeAddress: string; earnedRewards: bigint } - > = {}; + const epochMetadataByAccount: Record = {}; for (const [account, epochEndReward] of Object.entries( epochEndRewardsByAccount, )) { - epochMetadataByAccount[account as Address] = { - runeAddress: stakingInfoByAccount[account as Address].runeAddress, - earnedRewards: - epochEndReward - (epochStartRewardsByAccount[account as Address] ?? 0n), - }; + epochMetadataByAccount[account as Address] = + epochEndReward - (epochStartRewardsByAccount[account as Address] ?? 0n); } return epochMetadataByAccount; diff --git a/scripts/rewards-distribution/distributeAmount/distributeAmount.ts b/scripts/rewards-distribution/distributeAmount/distributeAmount.ts index 025d9ff..29ec168 100644 --- a/scripts/rewards-distribution/distributeAmount/distributeAmount.ts +++ b/scripts/rewards-distribution/distributeAmount/distributeAmount.ts @@ -8,8 +8,8 @@ export const distributeAmount = ( totalRuneAmountToDistroBaseUnit: bigint, earnedRewardsByAccount: Record, ) => { - // Set the precision to the maximum possible value to avoid rounding errors - BigNumber.config({ DECIMAL_PLACES: 1e9 }); + // Set the precision to a high value to avoid rounding errors + BigNumber.config({ DECIMAL_PLACES: 100 }); const totalEarnedRewards = Object.values(earnedRewardsByAccount).reduce( (sum, earnedRewards) => sum + earnedRewards, diff --git a/scripts/rewards-distribution/getLatestRuneAddressByAccount.ts b/scripts/rewards-distribution/getLatestRuneAddressByAccount.ts new file mode 100644 index 0000000..3ff5286 --- /dev/null +++ b/scripts/rewards-distribution/getLatestRuneAddressByAccount.ts @@ -0,0 +1,27 @@ +import { Address } from "viem"; +import { RFoxLog, StakeLog, SetRuneAddressLog } from "./events"; +import { hexToUtf8, isLogType } from "./helpers"; + +export const getLatestRuneAddressByAccount = ( + orderedLogs: { log: RFoxLog; timestamp: bigint }[], +) => { + const runeAddressByAccount: Record = {}; + + for (const { log } of orderedLogs) { + if (isLogType("Stake", log)) { + const stakeLog = log as StakeLog; + runeAddressByAccount[stakeLog.args.account] = hexToUtf8( + stakeLog.args.runeAddress, + ); + } + + if (isLogType("SetRuneAddress", log)) { + const setRuneAddressLog = log as SetRuneAddressLog; + runeAddressByAccount[setRuneAddressLog.args.account] = hexToUtf8( + setRuneAddressLog.args.newRuneAddress, + ); + } + } + + return runeAddressByAccount; +}; diff --git a/scripts/rewards-distribution/helpers.ts b/scripts/rewards-distribution/helpers.ts index a022725..98ee42d 100644 --- a/scripts/rewards-distribution/helpers.ts +++ b/scripts/rewards-distribution/helpers.ts @@ -3,11 +3,12 @@ import { ARBITRUM_RFOX_PROXY_CONTRACT_ADDRESS, GET_LOGS_BLOCK_STEP_SIZE, } from "./constants"; -import { AbiEvent, Block, Log, PublicClient } from "viem"; +import { AbiEvent, Log, PublicClient } from "viem"; import cliProgress from "cli-progress"; import colors from "ansi-colors"; -import { RFoxEvent, RFoxLog, StakeLog, UnstakeLog } from "./events"; +import { RFoxLog, StakeLog, UnstakeLog } from "./events"; import { stakingV1Abi } from "./generated/abi-types"; +import assert from "assert"; // we cache promises to prevent async race conditions hydrating the cache const blockNumberToTimestampCache: Record