Skip to content

Commit d57e2a1

Browse files
fix: incorrect token information in response for wetheth (#1867)
1 parent 2f4ad4e commit d57e2a1

File tree

3 files changed

+64
-6
lines changed

3 files changed

+64
-6
lines changed

api/_utils.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,15 @@ export const getTokenByAddress = (
562562
}
563563
}
564564

565+
// For some chains, the same address is associated with both the ETH and WETH symbols in the constants file.
566+
// See: https://www.npmjs.com/package/@across-protocol/constants
567+
// This can cause issues when resolving the token.
568+
// To fix this, we will check if there is a WETH match and prioritize it over the ETH match.
569+
const wethMatch = matches.find(([symbol]) => symbol === "WETH");
570+
if (wethMatch) {
571+
return wethMatch[1];
572+
}
573+
565574
return matches[0][1];
566575
} catch (error) {
567576
return undefined;
@@ -2543,12 +2552,7 @@ export async function getTokenInfo({ chainId, address }: TokenOptions): Promise<
25432552
});
25442553
}
25452554

2546-
// Resolve token info statically
2547-
const token = Object.values(TOKEN_SYMBOLS_MAP).find((token) =>
2548-
Boolean(
2549-
token.addresses?.[chainId]?.toLowerCase() === address.toLowerCase()
2550-
)
2551-
);
2555+
const token = getTokenByAddress(address, chainId);
25522556

25532557
if (token) {
25542558
return {

e2e-api/swap/fetch-approval.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,24 @@ describe("GET /swap/approval", () => {
165165
);
166166
});
167167
});
168+
169+
test("should return WETH for Base and Linea", async () => {
170+
const params = {
171+
tradeType: "exactInput",
172+
amount: "10000000000000000",
173+
inputToken: "0x4200000000000000000000000000000000000006",
174+
outputToken: "0xe5D7C2a44FfDDf6b295A15c148167daaAf5Cf34f",
175+
originChainId: 8453,
176+
destinationChainId: 59144,
177+
depositor: "0xB8034521BB1a343D556e5005680B3F17FFc74BeD",
178+
recipient: "0xB8034521BB1a343D556e5005680B3F17FFc74BeD",
179+
includeSources: "uniswap_v3",
180+
};
181+
const response = await axios.get(SWAP_API_URL, {
182+
params,
183+
});
184+
expect(response.status).toBe(200);
185+
expect(response.data.inputToken.symbol).toBe("WETH");
186+
expect(response.data.outputToken.symbol).toBe("WETH");
187+
});
168188
});

test/api/_utils.test.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
validEvmAddress,
88
validSvmAddress,
99
validAddress,
10+
getTokenByAddress,
1011
} from "../../api/_utils";
1112
import { is } from "superstruct";
1213

@@ -109,6 +110,39 @@ describe("_utils", () => {
109110
});
110111
});
111112

113+
describe("#getTokenByAddress()", () => {
114+
// Iterate over all chain IDs to test the token resolution for each chain.
115+
for (const chainId of Object.values(CHAIN_IDs)) {
116+
if (typeof chainId !== "number") continue;
117+
118+
const weth = TOKEN_SYMBOLS_MAP.WETH.addresses[chainId];
119+
const eth = TOKEN_SYMBOLS_MAP.ETH.addresses[chainId];
120+
121+
// Test case where WETH and ETH have the same address.
122+
// In this case, we want to ensure that WETH is always returned to avoid ambiguity.
123+
if (weth && eth && weth.toLowerCase() === eth.toLowerCase()) {
124+
test(`should return WETH for chain ${chainId} when both ETH and WETH have the same address`, () => {
125+
const token = getTokenByAddress(weth, chainId);
126+
expect(token?.symbol).toBe("WETH");
127+
});
128+
} else {
129+
// Test case where WETH and ETH have different addresses.
130+
if (weth) {
131+
test(`should return WETH for chain ${chainId}`, () => {
132+
const token = getTokenByAddress(weth, chainId);
133+
expect(token?.symbol).toBe("WETH");
134+
});
135+
}
136+
if (eth) {
137+
test(`should return ETH for chain ${chainId}`, () => {
138+
const token = getTokenByAddress(eth, chainId);
139+
expect(token?.symbol).toBe("ETH");
140+
});
141+
}
142+
}
143+
}
144+
});
145+
112146
describe("#validateChainAndTokenParams()", () => {
113147
test("throw if 'destinationChainId' is not provided", () => {
114148
expect(() => validateChainAndTokenParams({})).toThrowError(

0 commit comments

Comments
 (0)