Skip to content

Commit

Permalink
Add TraderateChanged for ARM
Browse files Browse the repository at this point in the history
  • Loading branch information
apexearth committed Oct 28, 2024
1 parent dbe7144 commit fe39206
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 32 deletions.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,17 @@ type ArmWithdrawalRequest @entity {
queued: BigInt!
claimed: Boolean!
}

type TraderateChanged @entity {
id: ID!
chainId: Int! @index
txHash: String! @index
timestamp: DateTime! @index
blockNumber: Int! @index
address: String! @index
traderate0: BigInt!
traderate1: BigInt!
}
type CoinGeckoCoinData @entity {
id: ID!
product: String! @index
Expand Down
11 changes: 11 additions & 0 deletions schema/arm.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,14 @@ type ArmWithdrawalRequest @entity {
queued: BigInt!
claimed: Boolean!
}

type TraderateChanged @entity {
id: ID!
chainId: Int! @index
txHash: String! @index
timestamp: DateTime! @index
blockNumber: Int! @index
address: String! @index
traderate0: BigInt!
traderate1: BigInt!
}
1 change: 1 addition & 0 deletions src/model/generated/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ export * from "./arm.model"
export * from "./armState.model"
export * from "./armDailyStat.model"
export * from "./armWithdrawalRequest.model"
export * from "./traderateChanged.model"
export * from "./coinGeckoCoinData.model"
export * from "./esToken.model"
export * from "./esAccount.model"
Expand Down
37 changes: 37 additions & 0 deletions src/model/generated/traderateChanged.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import {Entity as Entity_, Column as Column_, PrimaryColumn as PrimaryColumn_, IntColumn as IntColumn_, Index as Index_, StringColumn as StringColumn_, DateTimeColumn as DateTimeColumn_, BigIntColumn as BigIntColumn_} from "@subsquid/typeorm-store"

@Entity_()
export class TraderateChanged {
constructor(props?: Partial<TraderateChanged>) {
Object.assign(this, props)
}

@PrimaryColumn_()
id!: string

@Index_()
@IntColumn_({nullable: false})
chainId!: number

@Index_()
@StringColumn_({nullable: false})
txHash!: string

@Index_()
@DateTimeColumn_({nullable: false})
timestamp!: Date

@Index_()
@IntColumn_({nullable: false})
blockNumber!: number

@Index_()
@StringColumn_({nullable: false})
address!: string

@BigIntColumn_({nullable: false})
traderate0!: bigint

@BigIntColumn_({nullable: false})
traderate1!: bigint
}
4 changes: 0 additions & 4 deletions src/oeth/processors/strategies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,8 @@ const eventProcessors = [
...OETH_NATIVE_STRATEGY_ADDRESSES.map((address) =>
createEventProcessor({
address,
eventName: 'AccountingConsensusRewards',
event: nativeStakingAbi.events.AccountingConsensusRewards,
from: 20046251,
Entity: AccountingConsensusRewards,
mapEntity: (ctx, block, log, decoded) =>
new AccountingConsensusRewards({
id: `${ctx.chain.id}:${log.id}`,
Expand All @@ -188,10 +186,8 @@ const eventProcessors = [
),
createEventProcessor({
address: addresses.oeth.nativeStakingFeeAccumulator,
eventName: 'ExecutionRewardsCollected',
event: feeAccumulatorAbi.events.ExecutionRewardsCollected,
from: 20046238,
Entity: ExecutionRewardsCollected,
mapEntity: (ctx, block, log, decoded) =>
new ExecutionRewardsCollected({
id: `${ctx.chain.id}:${log.id}`,
Expand Down
5 changes: 1 addition & 4 deletions src/templates/events/createEventProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,11 @@ import { DecodedStruct, Struct } from '@subsquid/evm-codec'
import { EvmBatchProcessor } from '@subsquid/evm-processor'
import { Entity } from '@subsquid/typeorm-store/lib/store'
import { logFilter } from '@utils/logFilter'
import { EntityClassT } from '@utils/type'

export const createEventProcessor = <T extends Struct, EventEntity extends Entity>(params: {
eventName: string
event: ReturnType<typeof event<T>>
address: string
from: number
Entity: EntityClassT<EventEntity>
mapEntity: (ctx: Context, block: Block, log: Log, decoded: DecodedStruct<IndexedCodecs<T>>) => EventEntity
extraFilterArgs?: {
topic1?: string[]
Expand All @@ -35,7 +32,7 @@ export const createEventProcessor = <T extends Struct, EventEntity extends Entit
for (const log of block.logs) {
if (filter.matches(log)) {
const decoded = params.event.decode(log)
const entity = new params.Entity(params.mapEntity(ctx, block, log, decoded))
const entity = params.mapEntity(ctx, block, log, decoded)
entities.push(entity)
}
}
Expand Down
55 changes: 33 additions & 22 deletions src/templates/origin-arm/origin-arm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { last } from 'lodash'
import * as erc20Abi from '@abi/erc20'
import * as originLidoArmAbi from '@abi/origin-lido-arm'
import * as originLidoArmCapManagerAbi from '@abi/origin-lido-arm-cap-manager'
import { Arm, ArmDailyStat, ArmState, ArmWithdrawalRequest } from '@model'
import { Arm, ArmDailyStat, ArmState, ArmWithdrawalRequest, TraderateChanged } from '@model'
import { Block, Context, Processor } from '@processor'
import { EvmBatchProcessor } from '@subsquid/evm-processor'
import { createERC20SimpleTracker } from '@templates/erc20-simple'
import { createEventProcessor } from '@templates/events/createEventProcessor'
import { blockFrequencyTracker } from '@utils/blockFrequencyUpdater'
import { calculateAPY } from '@utils/calculateAPY'
import { logFilter } from '@utils/logFilter'
Expand Down Expand Up @@ -48,6 +49,23 @@ export const createOriginARMProcessors = ({
topic0: [originLidoArmAbi.events.FeeCollected.topic],
range: { from },
})
const tradeRateProcessor = createEventProcessor({
event: originLidoArmAbi.events.TraderateChanged,
address: armAddress,
from,
mapEntity: (ctx, block, log, decoded) => {
return new TraderateChanged({
id: `${ctx.chain.id}:${log.id}`,
chainId: ctx.chain.id,
txHash: log.transactionHash,
timestamp: new Date(block.header.timestamp),
blockNumber: block.header.height,
address: armAddress,
traderate0: decoded.traderate0,
traderate1: decoded.traderate1,
})
},
})
const tracker = blockFrequencyTracker({ from })
let armEntity: Arm
let initialized = false
Expand Down Expand Up @@ -92,6 +110,7 @@ export const createOriginARMProcessors = ({
p.addLog(depositFilter.value)
p.addLog(withdrawalFilter.value)
p.addLog(feeCollectedFilter.value)
tradeRateProcessor.setup(p)
},
initialize,
process: async (ctx: Context) => {
Expand All @@ -103,7 +122,7 @@ export const createOriginARMProcessors = ({
const dailyStatsMap = new Map<string, ArmDailyStat>()
const redemptionMap = new Map<string, ArmWithdrawalRequest>()
const getStateId = (block: Block) => `${ctx.chain.id}:${block.header.height}:${armAddress}`
const getPreviousState = async (block: Block) => {
const getPreviousState = async () => {
return (
last(states) ??
(await ctx.store.findOne(ArmState, {
Expand All @@ -117,28 +136,19 @@ export const createOriginARMProcessors = ({
if (states[states.length - 1]?.id === stateId) {
return states[states.length - 1]
}
const previousState = await getPreviousState(block)
const previousState = await getPreviousState()
const armContract = new originLidoArmAbi.Contract(ctx, block.header, armAddress)
const controllerContract = new originLidoArmCapManagerAbi.Contract(ctx, block.header, capManagerAddress)
const [
assets0,
assets1,
outstandingAssets1,
totalAssets,
totalAssetsCap,
totalSupply,
assetsPerShare,
feesAccrued,
] = await Promise.all([
new erc20Abi.Contract(ctx, block.header, armEntity.token0).balanceOf(armAddress),
new erc20Abi.Contract(ctx, block.header, armEntity.token1).balanceOf(armAddress),
armContract.lidoWithdrawalQueueAmount(),
armContract.totalAssets(),
controllerContract.totalAssetsCap(),
armContract.totalSupply(),
armContract.previewRedeem(10n ** 18n),
armContract.feesAccrued(),
])
const [assets0, assets1, outstandingAssets1, totalAssets, totalAssetsCap, totalSupply, assetsPerShare] =
await Promise.all([
new erc20Abi.Contract(ctx, block.header, armEntity.token0).balanceOf(armAddress),
new erc20Abi.Contract(ctx, block.header, armEntity.token1).balanceOf(armAddress),
armContract.lidoWithdrawalQueueAmount(),
armContract.totalAssets(),
controllerContract.totalAssetsCap(),
armContract.totalSupply(),
armContract.previewRedeem(10n ** 18n),
])
const date = new Date(block.header.timestamp)
const armStateEntity = new ArmState({
id: stateId,
Expand Down Expand Up @@ -259,6 +269,7 @@ export const createOriginARMProcessors = ({
await ctx.store.insert(states)
await ctx.store.upsert([...dailyStatsMap.values()])
await ctx.store.upsert([...redemptionMap.values()])
await tradeRateProcessor.process(ctx)
},
},
// The ARM is an ERC20, so we can use the ERC20SimpleTracker to track holder balances
Expand Down

0 comments on commit fe39206

Please sign in to comment.