Skip to content

Commit b9275ec

Browse files
committed
fix yield and fees calculation
1 parent 993cc26 commit b9275ec

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

src/templates/origin-arm/origin-arm.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import dayjs from 'dayjs'
2-
import { last } from 'lodash'
2+
import { findLast, last } from 'lodash'
3+
import { LessThan } from 'typeorm'
34
import { formatUnits } from 'viem'
45

56
import * as erc20Abi from '@abi/erc20'
@@ -133,6 +134,16 @@ export const createOriginARMProcessors = ({
133134
}))
134135
)
135136
}
137+
const getYesterdayState = async (block: Block) => {
138+
const startOfToday = dayjs(block.header.timestamp).startOf('day').toDate()
139+
return (
140+
findLast(states, (state) => state.timestamp < startOfToday) ??
141+
(await ctx.store.findOne(ArmState, {
142+
order: { timestamp: 'DESC' },
143+
where: { chainId: ctx.chain.id, address: armAddress, timestamp: LessThan(startOfToday) },
144+
}))
145+
)
146+
}
136147
const getCurrentState = async (block: Block) => {
137148
const stateId = getStateId(block)
138149
if (states[states.length - 1]?.id === stateId) {
@@ -227,8 +238,11 @@ export const createOriginARMProcessors = ({
227238
}
228239
if (tracker(ctx, block)) {
229240
// ArmState
230-
const state = await getCurrentState(block)
231-
const rateUSD = await ensureExchangeRate(ctx, block, 'ETH', 'USD')
241+
const [state, yesterdayState, rateUSD] = await Promise.all([
242+
getCurrentState(block),
243+
getYesterdayState(block),
244+
ensureExchangeRate(ctx, block, 'ETH', 'USD'),
245+
])
232246

233247
// ArmDailyStat
234248
const date = new Date(block.header.timestamp)
@@ -263,8 +277,8 @@ export const createOriginARMProcessors = ({
263277
assetsPerShare: state.assetsPerShare,
264278
apr: armDayApy.apr,
265279
apy: armDayApy.apy,
266-
fees: state.totalFees - (previousDailyStat?.fees ?? 0n),
267-
yield: state.totalYield - (previousDailyStat?.yield ?? 0n),
280+
fees: state.totalFees - (yesterdayState?.totalFees ?? 0n),
281+
yield: state.totalYield - (yesterdayState?.totalYield ?? 0n),
268282
rateUSD: +formatUnits(rateUSD?.rate ?? 0n, 8),
269283
})
270284
dailyStatsMap.set(currentDayId, armDailyStatEntity)

0 commit comments

Comments
 (0)