Skip to content

Commit 03b8333

Browse files
committed
fix yield calculation issue caused by bad sort
1 parent 6d30803 commit 03b8333

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

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

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

@@ -154,12 +154,12 @@ export const createOriginARMProcessors = ({
154154
const redemptionMap = new Map<string, ArmWithdrawalRequest>()
155155
const swaps: ArmSwap[] = []
156156
const getStateId = (block: Block) => `${ctx.chain.id}:${block.header.height}:${armAddress}`
157-
const getPreviousState = async () => {
157+
const getPreviousState = async (block: Block) => {
158158
return (
159-
last(states) ??
159+
findLast(states, (state) => state.blockNumber < block.header.height) ??
160160
(await ctx.store.findOne(ArmState, {
161161
order: { timestamp: 'DESC' },
162-
where: { chainId: ctx.chain.id, address: armAddress },
162+
where: { chainId: ctx.chain.id, address: armAddress, blockNumber: LessThan(block.header.height) },
163163
}))
164164
)
165165
}
@@ -179,7 +179,7 @@ export const createOriginARMProcessors = ({
179179
if (armStateEntity) {
180180
return armStateEntity
181181
}
182-
const previousState = await getPreviousState()
182+
const previousState = await getPreviousState(block)
183183
const lidoArmContract = new originLidoArmAbi.Contract(ctx, block.header, armAddress)
184184
const armContract = new originArmAbi.Contract(ctx, block.header, armAddress)
185185
const controllerContract = new originLidoArmCapManagerAbi.Contract(ctx, block.header, capManagerAddress)
@@ -235,6 +235,7 @@ export const createOriginARMProcessors = ({
235235
})
236236
armStateEntity.totalYield = calculateTotalYield(armStateEntity)
237237
states.push(armStateEntity)
238+
states.sort((a, b) => a.blockNumber - b.blockNumber) // sort ascending
238239
return armStateEntity
239240
}
240241
const calculateTotalYield = (state: ArmState) =>

0 commit comments

Comments
 (0)