|
1 | | -import type { Address } from "viem" |
| 1 | +import { type Address, getAddress } from "viem" |
2 | 2 | import * as chains from "viem/chains" |
3 | 3 | import { logWarn } from "./logs" |
4 | 4 |
|
@@ -62,6 +62,49 @@ const contractBook: any = { |
62 | 62 | }, |
63 | 63 | } |
64 | 64 |
|
| 65 | +const SWAP_VERIFIER_ENV_PREFIX = "EULER_SWAP_VERIFIER_ADDRESS_" |
| 66 | + |
| 67 | +// Per-chain SwapVerifier overrides from env, e.g. |
| 68 | +// EULER_SWAP_VERIFIER_ADDRESS_8453=0x... . Applied AFTER |
| 69 | +// refreshContractBookAddresses so a custom SwapVerifier deployment is never |
| 70 | +// reverted to the canonical EulerChains.json address. This mirrors the override |
| 71 | +// the Lite app applies to its /api/euler-chains payload, keeping the quote's |
| 72 | +// verify.verifierAddress in sync with the verifier the Lite SDK validates |
| 73 | +// against (a mismatch makes the SDK reject every quote). |
| 74 | +function readSwapVerifierEnvOverrides( |
| 75 | + env: NodeJS.ProcessEnv = process.env, |
| 76 | +): Record<number, Address> { |
| 77 | + const overrides: Record<number, Address> = {} |
| 78 | + for (const [key, rawValue] of Object.entries(env)) { |
| 79 | + if (!key.startsWith(SWAP_VERIFIER_ENV_PREFIX)) continue |
| 80 | + const value = rawValue?.trim() |
| 81 | + if (!value) continue |
| 82 | + const chainId = Number(key.slice(SWAP_VERIFIER_ENV_PREFIX.length)) |
| 83 | + if (!Number.isSafeInteger(chainId) || chainId <= 0) { |
| 84 | + logWarn({ name: "Invalid SwapVerifier override env key", key }) |
| 85 | + continue |
| 86 | + } |
| 87 | + try { |
| 88 | + overrides[chainId] = getAddress(value) |
| 89 | + } catch { |
| 90 | + logWarn({ name: "Invalid SwapVerifier override address", key, value }) |
| 91 | + } |
| 92 | + } |
| 93 | + return overrides |
| 94 | +} |
| 95 | + |
| 96 | +function applySwapVerifierEnvOverrides(env: NodeJS.ProcessEnv = process.env) { |
| 97 | + for (const [chainId, address] of Object.entries( |
| 98 | + readSwapVerifierEnvOverrides(env), |
| 99 | + )) { |
| 100 | + contractBook.swapVerifier.address[Number(chainId)] = address |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +// Apply once at module load so the override holds even before the first |
| 105 | +// deployment refresh resolves. |
| 106 | +applySwapVerifierEnvOverrides() |
| 107 | + |
65 | 108 | let refreshPromise: Promise<void> | null = null |
66 | 109 |
|
67 | 110 | async function queryDeployments(url: string): Promise<Deployment[]> { |
@@ -99,6 +142,10 @@ export async function refreshContractBookAddresses() { |
99 | 142 | peripheryAddrs.swapVerifier |
100 | 143 | } |
101 | 144 | } |
| 145 | + |
| 146 | + // Re-assert env overrides last so a custom SwapVerifier is never reverted |
| 147 | + // to the canonical deployment address fetched above. |
| 148 | + applySwapVerifierEnvOverrides() |
102 | 149 | })().finally(() => { |
103 | 150 | refreshPromise = null |
104 | 151 | }) |
|
0 commit comments