Skip to content

Commit f5460b1

Browse files
committed
use external tokenlist api
1 parent fb63a7e commit f5460b1

37 files changed

+813
-813
lines changed

.env.template

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ CORS_ORIGIN="*" # Allowed CORS origin, adjust as necessary
99
COMMON_RATE_LIMIT_WINDOW_MS="1000" # Window size for rate limiting (ms)
1010
COMMON_RATE_LIMIT_MAX_REQUESTS="20" # Max number of requests per window per IP
1111

12+
# Optional endpoint to fetch the tokenlists expects `?chainId` query param
13+
TOKENLIST_URL=""
14+
1215
# RPCs
1316
# mainnet
1417
RPC_URL_1=""

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,4 +155,7 @@ const pipeline = [
155155
]
156156
```
157157

158-
Additionally there is a script to generate auxiliary config files, executed by `pnpm run generate-config`. Currently it fetches the list of supported [Pendle aggregators](./src/swapService/strategies/balmySDK/sources/pendle/pendleAggregators.json) for all chains
158+
Additionally there is a script to generate auxiliary config files, executed by `pnpm run generate-config`. Currently it fetches the list of supported [Pendle aggregators](./src/swapService/strategies/balmySDK/sources/pendle/pendleAggregators.json) for all chains
159+
160+
## Tokenlists
161+
The router relies on tokenlists for all swapped tokens. They can be fetched from `TOKENLIST_URL` or read from JSON files in `tokenLists` folder if the configuration is missing. Note the files are not updated currently. Please reach out on [Euler discord](https://discord.com/invite/pTTnr7b4mT) if you need access to the tokenlist endpoint.

src/common/utils/tokenList.ts

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import fs from "node:fs"
2-
import { type Address, isAddressEqual } from "viem"
2+
import type { Address } from "viem"
3+
import { RPC_URLS } from "./viemClients"
34

45
export type TokenListItem = {
5-
addressInfo: Address
6+
address: Address
67
chainId: number
78
decimals: number
89
logoURI: string
@@ -22,7 +23,8 @@ export type TokenListItem = {
2223
}
2324

2425
const cache: Record<number, TokenListItem[]> = {}
25-
;(function buildCache() {
26+
27+
const loadTokenlistsFromFiles = () => {
2628
let dir = `${__dirname}/../tokenLists`
2729
let files
2830
try {
@@ -39,6 +41,26 @@ const cache: Record<number, TokenListItem[]> = {}
3941
fs.readFileSync(`${dir}/${file}`).toString(),
4042
) as TokenListItem[]
4143
}
44+
}
45+
;(function buildCache() {
46+
const tokenlistURL = process.env.TOKENLIST_URL
47+
if (!tokenlistURL) {
48+
console.warn(
49+
"Missing TOKENLIST_URL configuration. Falling back to static files",
50+
)
51+
loadTokenlistsFromFiles()
52+
return
53+
}
54+
55+
Promise.all(
56+
Object.keys(RPC_URLS).map(async (chainId) => {
57+
const response = await fetch(`${tokenlistURL}?chainId=${chainId}`)
58+
if (!response.ok) {
59+
throw new Error(`Failed fetching tokenlist for chain ${chainId}`)
60+
}
61+
cache[Number(chainId)] = (await response.json()) as TokenListItem[]
62+
}),
63+
)
4264
})()
4365

4466
export default function getTokenList(chainId: number): TokenListItem[] {

src/common/utils/viemClients.ts

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,18 @@ export const plasma = defineChain({
152152

153153
export const RPC_URLS: Record<number, string> = {
154154
[chains.mainnet.id]: process.env.RPC_URL_1 || "",
155-
[chains.sepolia.id]: process.env.RPC_URL_11155111 || "",
156155
[chains.arbitrum.id]: process.env.RPC_URL_42161 || "",
157156
[chains.base.id]: process.env.RPC_URL_8453 || "",
158-
[bartio.id]: process.env.RPC_URL_80084 || "",
159157
[berachain.id]: process.env.RPC_URL_80094 || "",
160158
[chains.avalanche.id]: process.env.RPC_URL_43114 || "",
161-
[146]: process.env.RPC_URL_146 || "",
162-
[130]: process.env.RPC_URL_130 || "",
159+
[chains.bsc.id]: process.env.RPC_URL_56 || "",
160+
[chains.linea.id]: process.env.RPC_URL_56 || "",
161+
[sonicnetwork.id]: process.env.RPC_URL_146 || "",
162+
[unichain.id]: process.env.RPC_URL_130 || "",
163+
[60808]: process.env.RPC_URL_130 || "", //bob
164+
[1923]: process.env.RPC_URL_130 || "", //swell
163165
[tac.id]: process.env.RPC_URL_239 || "",
164-
[chains.foundry.id]: process.env.RPC_URL_31337 || "http://localhost:8545",
166+
[plasma.id]: process.env.RPC_URL_9745 || "",
165167
} as const
166168

167169
export const createHttp = (chainId: number) =>
@@ -178,16 +180,7 @@ export function createChainConfig(chain: Chain) {
178180
}
179181

180182
export const createClients = (): Record<number, Client<Transport, Chain>> => ({
181-
[bartio.id]: createChainConfig(bartio),
182183
[chains.mainnet.id]: createChainConfig(chains.mainnet),
183-
[chains.sepolia.id]: createClient({
184-
chain: chains.sepolia,
185-
transport: http(RPC_URLS[chains.sepolia.id]),
186-
}),
187-
[chains.foundry.id]: createClient({
188-
chain: chains.foundry,
189-
transport: http(RPC_URLS[chains.foundry.id]),
190-
}),
191184
[chains.arbitrum.id]: createChainConfig(chains.arbitrum),
192185
[chains.base.id]: createChainConfig(chains.base),
193186
[sonicnetwork.id]: createClient({
@@ -208,6 +201,10 @@ export const createClients = (): Record<number, Client<Transport, Chain>> => ({
208201
chain: tac,
209202
transport: http(RPC_URLS[tac.id]),
210203
}),
204+
[plasma.id]: createClient({
205+
chain: plasma,
206+
transport: http(RPC_URLS[plasma.id]),
207+
}),
211208
[chains.linea.id]: createChainConfig(chains.linea),
212209
})
213210

src/swapService/quoters/quoter1Inch.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ export async function fetch1InchQuote(
66
swapParams: SwapParams,
77
): Promise<SwapQuote> {
88
const params = new URLSearchParams({
9-
src: swapParams.tokenIn.addressInfo,
10-
dst: swapParams.tokenOut.addressInfo,
9+
src: swapParams.tokenIn.address,
10+
dst: swapParams.tokenOut.address,
1111
amount: String(swapParams.amount),
1212
from: swapParams.from || getSwapper(swapParams.chainId),
1313
origin: swapParams.origin,

src/swapService/quoters/quoterKyberswap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ export async function fetchKyberswapQuote(
3737
skipBuild?: boolean,
3838
): Promise<SwapQuote> {
3939
const params = new URLSearchParams({
40-
tokenIn: swapParams.tokenIn.addressInfo,
41-
tokenOut: swapParams.tokenOut.addressInfo,
40+
tokenIn: swapParams.tokenIn.address,
41+
tokenOut: swapParams.tokenOut.address,
4242
amountIn: swapParams.amount.toString(),
4343
gasInclude: "true",
4444
})

src/swapService/quoters/quoterLifi.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,8 @@ export async function fetchLiFiExactInQuote(
4444
const params = new URLSearchParams({
4545
fromChain: chainKey,
4646
toChain: chainKey,
47-
fromToken: swapParams.tokenIn.addressInfo,
48-
toToken: swapParams.tokenOut.addressInfo,
47+
fromToken: swapParams.tokenIn.address,
48+
toToken: swapParams.tokenOut.address,
4949
fromAddress: swapParams.from || getSwapper(swapParams.chainId),
5050
toAddress: swapParams.receiver,
5151
fromAmount: String(swapParams.amount),
@@ -93,8 +93,8 @@ export async function fetchLiFiExactOutQuote(
9393
const params = new URLSearchParams({
9494
fromChain: chainKey,
9595
toChain: chainKey,
96-
fromToken: swapParams.tokenIn.addressInfo,
97-
toToken: swapParams.tokenOut.addressInfo,
96+
fromToken: swapParams.tokenIn.address,
97+
toToken: swapParams.tokenOut.address,
9898
fromAddress: swapParams.from,
9999
toAddress: swapParams.receiver,
100100
toAmount: String(swapParams.amount),

src/swapService/quoters/quoterPendle.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ export async function fetchPendleQuote(swapParams: SwapParams) {
1616
receiver: swapParams.receiver,
1717
slippage: String(swapParams.slippage / 100), // 1 = 100%
1818
enableAggregator: "true",
19-
tokenIn: swapParams.tokenIn.addressInfo,
20-
tokenOut: swapParams.tokenOut.addressInfo,
19+
tokenIn: swapParams.tokenIn.address,
20+
tokenOut: swapParams.tokenOut.address,
2121
amountIn: String(swapParams.amount),
2222
})
2323

src/swapService/quoters/quoterUniswap.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,15 @@ export async function fetchUniswapQuote(
3636

3737
const baseCurrency = new Token(
3838
swapParams.chainId,
39-
swapParams.tokenOut.addressInfo,
39+
swapParams.tokenOut.address,
4040
swapParams.tokenOut.decimals,
4141
swapParams.tokenOut.symbol,
4242
swapParams.tokenOut.name,
4343
)
4444

4545
const quoteCurrency = new Token(
4646
swapParams.chainId,
47-
swapParams.tokenIn.addressInfo,
47+
swapParams.tokenIn.address,
4848
swapParams.tokenIn.decimals,
4949
swapParams.tokenIn.symbol,
5050
swapParams.tokenIn.name,

src/swapService/strategies/balmySDK/tokenlistMetadataSource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export class TokenlistMetadataSource
2828
const allTokens = getAllTokenLists()
2929
for (const { chainId, token } of params.tokens) {
3030
const tokenListItem = allTokens[chainId]?.find((t) =>
31-
isAddressEqual(t.addressInfo, token as Address),
31+
isAddressEqual(t.address, token as Address),
3232
)
3333
if (tokenListItem) {
3434
if (!result[chainId]) result[chainId] = {}

0 commit comments

Comments
 (0)