|
1 | 1 | import dayjs from 'dayjs' |
2 | | -import { last } from 'lodash' |
| 2 | +import { findLast, last } from 'lodash' |
| 3 | +import { LessThan } from 'typeorm' |
3 | 4 | import { formatUnits } from 'viem' |
4 | 5 |
|
5 | 6 | import * as erc20Abi from '@abi/erc20' |
@@ -133,6 +134,16 @@ export const createOriginARMProcessors = ({ |
133 | 134 | })) |
134 | 135 | ) |
135 | 136 | } |
| 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 | + } |
136 | 147 | const getCurrentState = async (block: Block) => { |
137 | 148 | const stateId = getStateId(block) |
138 | 149 | if (states[states.length - 1]?.id === stateId) { |
@@ -227,8 +238,11 @@ export const createOriginARMProcessors = ({ |
227 | 238 | } |
228 | 239 | if (tracker(ctx, block)) { |
229 | 240 | // 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 | + ]) |
232 | 246 |
|
233 | 247 | // ArmDailyStat |
234 | 248 | const date = new Date(block.header.timestamp) |
@@ -263,8 +277,8 @@ export const createOriginARMProcessors = ({ |
263 | 277 | assetsPerShare: state.assetsPerShare, |
264 | 278 | apr: armDayApy.apr, |
265 | 279 | 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), |
268 | 282 | rateUSD: +formatUnits(rateUSD?.rate ?? 0n, 8), |
269 | 283 | }) |
270 | 284 | dailyStatsMap.set(currentDayId, armDailyStatEntity) |
|
0 commit comments