Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions api/_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,15 @@ export const getTokenByAddress = (
}
}

// For some chains, the same address is associated with both the ETH and WETH symbols in the constants file.
// See: https://www.npmjs.com/package/@across-protocol/constants
// This can cause issues when resolving the token.
// To fix this, we will check if there is a WETH match and prioritize it over the ETH match.
const wethMatch = matches.find(([symbol]) => symbol === "WETH");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: We didn't specify it in the ticket but now that I review this, this bug happens for all native tokens, e.g. XPL, GHO. So this fix will not work for them 🤔 We can tackle this is in a separte PR/issue though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (wethMatch) {
return wethMatch[1];
}

return matches[0][1];
} catch (error) {
return undefined;
Expand Down Expand Up @@ -2543,12 +2552,7 @@ export async function getTokenInfo({ chainId, address }: TokenOptions): Promise<
});
}

// Resolve token info statically
const token = Object.values(TOKEN_SYMBOLS_MAP).find((token) =>
Boolean(
token.addresses?.[chainId]?.toLowerCase() === address.toLowerCase()
)
);
const token = getTokenByAddress(address, chainId);

if (token) {
return {
Expand Down
20 changes: 20 additions & 0 deletions e2e-api/swap/fetch-approval.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,4 +165,24 @@ describe("GET /swap/approval", () => {
);
});
});

test("should return WETH for Base and Linea", async () => {
const params = {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: I also didn't specify it in the ticket but what happens if you set the zero address as the inputToken? It seems that this also resolves to the wrong token information. But as above we can tackle this in a separate PR/issue

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. If the inputToken has a zero address, then neither ETH nor WETH will be found as a token, as neither of them specifies the zero address anywhere. I think that the native token aspect is handled here and it get's resolved as WETH in the case of native ETH.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tradeType: "exactInput",
amount: "10000000000000000",
inputToken: "0x4200000000000000000000000000000000000006",
outputToken: "0xe5D7C2a44FfDDf6b295A15c148167daaAf5Cf34f",
originChainId: 8453,
destinationChainId: 59144,
depositor: "0xB8034521BB1a343D556e5005680B3F17FFc74BeD",
recipient: "0xB8034521BB1a343D556e5005680B3F17FFc74BeD",
includeSources: "uniswap_v3",
};
const response = await axios.get(SWAP_API_URL, {
params,
});
expect(response.status).toBe(200);
expect(response.data.inputToken.symbol).toBe("WETH");
expect(response.data.outputToken.symbol).toBe("WETH");
});
});
34 changes: 34 additions & 0 deletions test/api/_utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
validEvmAddress,
validSvmAddress,
validAddress,
getTokenByAddress,
} from "../../api/_utils";
import { is } from "superstruct";

Expand Down Expand Up @@ -109,6 +110,39 @@ describe("_utils", () => {
});
});

describe("#getTokenByAddress()", () => {
// Iterate over all chain IDs to test the token resolution for each chain.
for (const chainId of Object.values(CHAIN_IDs)) {
if (typeof chainId !== "number") continue;

const weth = TOKEN_SYMBOLS_MAP.WETH.addresses[chainId];
const eth = TOKEN_SYMBOLS_MAP.ETH.addresses[chainId];

// Test case where WETH and ETH have the same address.
// In this case, we want to ensure that WETH is always returned to avoid ambiguity.
if (weth && eth && weth.toLowerCase() === eth.toLowerCase()) {
test(`should return WETH for chain ${chainId} when both ETH and WETH have the same address`, () => {
const token = getTokenByAddress(weth, chainId);
expect(token?.symbol).toBe("WETH");
});
} else {
// Test case where WETH and ETH have different addresses.
if (weth) {
test(`should return WETH for chain ${chainId}`, () => {
const token = getTokenByAddress(weth, chainId);
expect(token?.symbol).toBe("WETH");
});
}
if (eth) {
test(`should return ETH for chain ${chainId}`, () => {
const token = getTokenByAddress(eth, chainId);
expect(token?.symbol).toBe("ETH");
});
}
}
}
});

describe("#validateChainAndTokenParams()", () => {
test("throw if 'destinationChainId' is not provided", () => {
expect(() => validateChainAndTokenParams({})).toThrowError(
Expand Down