Skip to content

Commit 637c7c1

Browse files
committed
account for multichain
1 parent e25f7de commit 637c7c1

3 files changed

Lines changed: 29 additions & 10 deletions

File tree

src/shared/post-processors/exchange-rates/mainnetCurrencies.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
import { invertMap } from '@originprotocol/squid-utils'
2+
import { baseAddresses } from '@utils/addresses-base'
3+
import { plumeAddresses } from '@utils/addresses-plume'
4+
import { sonicAddresses } from '@utils/addresses-sonic'
5+
26
import {
37
BaseCurrencyAddress,
48
BaseCurrencySymbol,
@@ -51,3 +55,22 @@ export type CurrencySymbol = MainnetCurrencySymbol | BaseCurrencySymbol | SonicC
5155
export type CurrencyAddress = MainnetCurrencyAddress | BaseCurrencyAddress | SonicCurrencyAddress | PlumeCurrencyAddress
5256

5357
export type Currency = CurrencySymbol | CurrencyAddress
58+
59+
const tokensByChain: Record<number, Record<string, string>> = {
60+
1: mainnetCurrencies,
61+
8453: baseAddresses.tokens,
62+
146: sonicAddresses.tokens,
63+
98865: plumeAddresses.tokens,
64+
}
65+
66+
/**
67+
* Convert a Currency (symbol or address) to its address.
68+
*/
69+
export const currencyToAddress = (chainId: number, currency: Currency): string => {
70+
if (currency.startsWith('0x')) return currency.toLowerCase()
71+
const tokens = tokensByChain[chainId]
72+
if (!tokens) throw new Error(`Unknown chainId: ${chainId}`)
73+
const address = tokens[currency]
74+
if (!address) throw new Error(`Unknown currency symbol: ${currency} for chainId: ${chainId}`)
75+
return address.toLowerCase()
76+
}

src/templates/erc20/erc20-entry.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import * as abi from '@abi/erc20'
22
import { ERC20 } from '@model'
33
import { Context, Processor } from '@originprotocol/squid-utils'
4-
import { TokenAddress } from '@utils/symbols'
4+
import { Currency, currencyToAddress } from '@shared/post-processors/exchange-rates/mainnetCurrencies'
55

66
/**
77
* Create ERC20 entry only (no event tracking).
88
*/
9-
export const createERC20Entry = ({ from, address }: { from: number; address: TokenAddress | string }): Processor => {
9+
export const createERC20Entry = ({ from, addressOrSymbol }: { from: number; addressOrSymbol: Currency }): Processor => {
1010
let erc20: ERC20 | undefined
1111
const initialize = async (ctx: Context) => {
1212
if (erc20) return
1313
const block = ctx.blocks.find((b) => b.header.height >= from)
1414
if (!block) return
15+
const address = currencyToAddress(ctx.chain.id, addressOrSymbol)
1516
erc20 = await ctx.store.findOne(ERC20, { where: { chainId: ctx.chain.id, address } })
1617
try {
1718
if (!erc20) {

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

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@ import * as originLidoArmCapManagerAbi from '@abi/origin-lido-arm-cap-manager';
1414
import { Arm, ArmDailyStat, ArmState, ArmSwap, ArmWithdrawalRequest, TraderateChanged } from '@model';
1515
import { Block, Context, EvmBatchProcessor, Processor, blockFrequencyTracker, calculateAPY, logFilter } from '@originprotocol/squid-utils';
1616
import { ensureExchangeRate } from '@shared/post-processors/exchange-rates';
17-
import { Currency, mainnetCurrencies, MainnetCurrencySymbol } from '@shared/post-processors/exchange-rates/mainnetCurrencies';
17+
import { Currency } from '@shared/post-processors/exchange-rates/mainnetCurrencies';
1818
import { createERC20Entry } from '@templates/erc20/erc20-entry';
1919
import { createERC20EventTracker } from '@templates/erc20/erc20-event';
2020
import { createEventProcessor } from '@templates/events/createEventProcessor'
2121
import { traceFilter } from '@utils/traceFilter'
2222

23-
const currencyToAddress = (currency: Currency): string => {
24-
if (currency.startsWith('0x')) return currency.toLowerCase()
25-
return mainnetCurrencies[currency as MainnetCurrencySymbol].toLowerCase()
26-
}
27-
2823
export const createOriginARMProcessors = ({
2924
name,
3025
from,
@@ -473,11 +468,11 @@ export const createOriginARMProcessors = ({
473468
}),
474469
createERC20Entry({
475470
from,
476-
address: currencyToAddress(token0),
471+
addressOrSymbol: token0,
477472
}),
478473
createERC20Entry({
479474
from,
480-
address: currencyToAddress(token1),
475+
addressOrSymbol: token1,
481476
}),
482477
]
483478
}

0 commit comments

Comments
 (0)