From 8ccfcf67bf410269f740d25020fb68cccb51f4e7 Mon Sep 17 00:00:00 2001 From: woodenfurniture <125113430+woodenfurniture@users.noreply.github.com> Date: Thu, 6 Jun 2024 09:36:33 +1000 Subject: [PATCH] fix: fetch rune address to avoid keccak hashed rune address issue from logs --- .../calculateRewards/calculateRewards.ts | 8 +--- .../getLatestRuneAddressByAccount.ts | 24 ----------- .../getStakingInfoByAccount.ts | 41 +++++++++++++++++++ scripts/rewards-distribution/index.ts | 14 ++++--- scripts/rewards-distribution/types.ts | 7 ++++ 5 files changed, 58 insertions(+), 36 deletions(-) delete mode 100644 scripts/rewards-distribution/getLatestRuneAddressByAccount.ts create mode 100644 scripts/rewards-distribution/getStakingInfoByAccount.ts create mode 100644 scripts/rewards-distribution/types.ts diff --git a/scripts/rewards-distribution/calculateRewards/calculateRewards.ts b/scripts/rewards-distribution/calculateRewards/calculateRewards.ts index 27d17b7..fb8efda 100644 --- a/scripts/rewards-distribution/calculateRewards/calculateRewards.ts +++ b/scripts/rewards-distribution/calculateRewards/calculateRewards.ts @@ -3,13 +3,7 @@ import { RFoxLog, StakeLog, UnstakeLog } from "../events"; import { isLogType } from "../helpers"; import { REWARD_RATE, WAD } from "../constants"; import assert from "assert"; - -export type StakingInfo = { - stakingBalance: bigint; - earnedRewards: bigint; - rewardPerTokenStored: bigint; - runeAddress: string; -}; +import { StakingInfo } from "../types"; const getEmptyStakingInfo = () => { return { diff --git a/scripts/rewards-distribution/getLatestRuneAddressByAccount.ts b/scripts/rewards-distribution/getLatestRuneAddressByAccount.ts deleted file mode 100644 index 94296e8..0000000 --- a/scripts/rewards-distribution/getLatestRuneAddressByAccount.ts +++ /dev/null @@ -1,24 +0,0 @@ -import { Address } from "viem"; -import { RFoxLog, StakeLog, SetRuneAddressLog } from "./events"; -import { 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] = stakeLog.args.runeAddress; - } - - if (isLogType("SetRuneAddress", log)) { - const setRuneAddressLog = log as SetRuneAddressLog; - runeAddressByAccount[setRuneAddressLog.args.account] = - setRuneAddressLog.args.newRuneAddress; - } - } - - return runeAddressByAccount; -}; diff --git a/scripts/rewards-distribution/getStakingInfoByAccount.ts b/scripts/rewards-distribution/getStakingInfoByAccount.ts new file mode 100644 index 0000000..9c5c467 --- /dev/null +++ b/scripts/rewards-distribution/getStakingInfoByAccount.ts @@ -0,0 +1,41 @@ +import { Address, PublicClient } from "viem"; +import { stakingV1Abi } from "./generated/abi-types"; +import { ARBITRUM_RFOX_PROXY_CONTRACT_ADDRESS } from "./constants"; +import { StakingInfo } from "./types"; + +export const getStakingInfoByAccount = async ( + publicClient: PublicClient, + accounts: Address[], + blockNumber: bigint, +) => { + const runeAddressByAccount: Record = {}; + + // Note we need to query these sequentially until we have higher rate limits + // TODO: Promise.all when we have higher rate limits + for (const account of accounts) { + const [ + stakingBalance, + unstakingBalance, + earnedRewards, + rewardPerTokenStored, + runeAddress, + ] = await publicClient.readContract({ + // TODO: dotenv or similar for contract addresses + address: ARBITRUM_RFOX_PROXY_CONTRACT_ADDRESS, + abi: stakingV1Abi, + functionName: "stakingInfo", + args: [account], + blockNumber, + }); + + runeAddressByAccount[account] = { + stakingBalance, + unstakingBalance, + earnedRewards, + rewardPerTokenStored, + runeAddress, + }; + } + + return runeAddressByAccount; +}; diff --git a/scripts/rewards-distribution/index.ts b/scripts/rewards-distribution/index.ts index 2e9f020..4b7ddb1 100644 --- a/scripts/rewards-distribution/index.ts +++ b/scripts/rewards-distribution/index.ts @@ -15,7 +15,7 @@ import { import { distributeAmount } from "./distributeAmount/distributeAmount"; import { Address } from "viem"; import { orderBy } from "lodash"; -import { getLatestRuneAddressByAccount } from "./getLatestRuneAddressByAccount"; +import { getStakingInfoByAccount } from "./getStakingInfoByAccount"; const main = async () => { const [currentBlock, [initLog]] = await Promise.all([ @@ -82,8 +82,12 @@ const main = async () => { orderedLogs, ); - // Get the latest rune address for each account - const runeAddressByAccount = getLatestRuneAddressByAccount(orderedLogs); + const accounts = Object.keys(earnedRewardsByAccount) as Address[]; + const epochEndStakingInfoByAccount = await getStakingInfoByAccount( + publicClient, + accounts, + toBlock, + ); await validateRewardsDistribution( publicClient, @@ -102,8 +106,8 @@ const main = async () => { console.log("Rewards distribution calculated successfully!"); - const tableRows = Object.entries(runeAddressByAccount).map( - ([account, runeAddress]) => { + const tableRows = Object.entries(epochEndStakingInfoByAccount).map( + ([account, { runeAddress }]) => { return { account, runeAddress, diff --git a/scripts/rewards-distribution/types.ts b/scripts/rewards-distribution/types.ts new file mode 100644 index 0000000..85998c5 --- /dev/null +++ b/scripts/rewards-distribution/types.ts @@ -0,0 +1,7 @@ +export type StakingInfo = { + stakingBalance: bigint; + unstakingBalance: bigint; + earnedRewards: bigint; + rewardPerTokenStored: bigint; + runeAddress: string; +};