Skip to content

Commit 28edf52

Browse files
authored
Merge pull request #89 from euler-xyz/fix/contract-book
Patch contract book
2 parents 7f7bb78 + 6497932 commit 28edf52

1 file changed

Lines changed: 48 additions & 1 deletion

File tree

src/common/utils/contractBook.ts

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Address } from "viem"
1+
import { type Address, getAddress } from "viem"
22
import * as chains from "viem/chains"
33
import { logWarn } from "./logs"
44

@@ -62,6 +62,49 @@ const contractBook: any = {
6262
},
6363
}
6464

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+
65108
let refreshPromise: Promise<void> | null = null
66109

67110
async function queryDeployments(url: string): Promise<Deployment[]> {
@@ -99,6 +142,10 @@ export async function refreshContractBookAddresses() {
99142
peripheryAddrs.swapVerifier
100143
}
101144
}
145+
146+
// Re-assert env overrides last so a custom SwapVerifier is never reverted
147+
// to the canonical deployment address fetched above.
148+
applySwapVerifierEnvOverrides()
102149
})().finally(() => {
103150
refreshPromise = null
104151
})

0 commit comments

Comments
 (0)