Skip to content

Commit

Permalink
fix: Caches and remove poolinfo from masterchef cache
Browse files Browse the repository at this point in the history
  • Loading branch information
memoyil committed Oct 3, 2024
1 parent 9adfdaf commit d126d94
Showing 1 changed file with 19 additions and 15 deletions.
34 changes: 19 additions & 15 deletions apps/web/src/state/farmsV4/state/poolApr/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ const masterChefV3CacheMap = new Map<
{
totalAllocPoint: bigint
latestPeriodCakePerSecond: bigint
poolInfo: readonly [bigint, `0x${string}`, `0x${string}`, `0x${string}`, number, bigint, bigint]
}
>()

Expand All @@ -82,17 +81,17 @@ export const getV3PoolCakeApr = async (pool: V3PoolInfo, cakePrice: BigNumber):
}
}

const hasCache = masterChefV3CacheMap.has(pool.chainId)

const [totalAllocPoint, latestPeriodCakePerSecond, poolInfo] = await Promise.all([
hasCache ? masterChefV3CacheMap.get(pool.chainId)!.totalAllocPoint : masterChefV3.read.totalAllocPoint(),
hasCache
? masterChefV3CacheMap.get(pool.chainId)!.latestPeriodCakePerSecond
: masterChefV3.read.latestPeriodCakePerSecond(),
hasCache ? masterChefV3CacheMap.get(pool.chainId)!.poolInfo : masterChefV3.read.poolInfo([BigInt(pool.pid)]),
masterChefV3CacheMap.get(pool.chainId)?.totalAllocPoint ?? masterChefV3.read.totalAllocPoint(),
masterChefV3CacheMap.get(pool.chainId)?.latestPeriodCakePerSecond ?? masterChefV3.read.latestPeriodCakePerSecond(),
masterChefV3.read.poolInfo([BigInt(pool.pid)]),
])

if (!hasCache) masterChefV3CacheMap.set(pool.chainId, { totalAllocPoint, latestPeriodCakePerSecond, poolInfo })
masterChefV3CacheMap.set(pool.chainId, {
...(masterChefV3CacheMap.get(pool.chainId) ?? {}),
totalAllocPoint,
latestPeriodCakePerSecond,
})

const cakePerYear = new BigNumber(SECONDS_PER_YEAR)
.times(latestPeriodCakePerSecond.toString())
Expand Down Expand Up @@ -233,16 +232,21 @@ const getV3PoolsCakeAprByChainId = async (pools: V3PoolInfo[], chainId: number,

if (!masterChefV3 || !client) return {}

const [totalAllocPoint, latestPeriodCakePerSecond] = await Promise.all([
masterChefV3CacheMap.get(chainId)?.totalAllocPoint ?? masterChefV3.read.totalAllocPoint(),
masterChefV3CacheMap.get(chainId)?.latestPeriodCakePerSecond ?? masterChefV3.read.latestPeriodCakePerSecond(),
])

masterChefV3CacheMap.set(chainId, {
...(masterChefV3CacheMap.get(chainId) ?? {}),
totalAllocPoint,
latestPeriodCakePerSecond,
})

const validPools = pools.filter((pool) => {
return pool.pid && pool.chainId === chainId
})

const masterChefV3Cache = masterChefV3CacheMap.get(chainId)
const [totalAllocPoint, latestPeriodCakePerSecond] = await Promise.all([
masterChefV3Cache ? masterChefV3Cache.totalAllocPoint : masterChefV3.read.totalAllocPoint(),
masterChefV3Cache ? masterChefV3Cache.latestPeriodCakePerSecond : masterChefV3.read.latestPeriodCakePerSecond(),
])

const poolInfoCalls = validPools.map(
(pool) =>
({
Expand Down

0 comments on commit d126d94

Please sign in to comment.