Skip to content

Commit 1a5c4d8

Browse files
committed
improve(TokenUtils): Replace getL1TokenInfo
This function is kind of dangerous in that it intuitively returns the `TokenInfo` for the L1 token matching the passed in l2 token/chain but it actually returns the L2 token decimals. I could see this function being misused in the future. This PR will force users to use `getL1TokenAddress` to get the L1 token address for an l2 token and use `getTokenInfo` to get the decimals/symbol for the same l2 token.
1 parent ecbb02c commit 1a5c4d8

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "@across-protocol/sdk",
33
"author": "UMA Team",
4-
"version": "4.1.61",
4+
"version": "4.1.62",
55
"license": "AGPL-3.0",
66
"homepage": "https://docs.across.to/reference/sdk",
77
"files": [

src/utils/TokenUtils.ts

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,15 @@ export function isStablecoin(tokenSymbol: string): boolean {
102102
);
103103
}
104104

105+
/**
106+
* @notice Returns the Token info for the token mapping in TOKEN_SYMBOLS_MAP matching the given l2TokenAddress
107+
* and chainId. If the chain is the hub chain, then will remap the L1 token to its equivalent L1 token symbol for example
108+
* it will always return a token info with symbol USDC and never USDC.e if chainId = mainnet.
109+
* @param l2TokenAddress
110+
* @param chainId
111+
* @param tokenMapping
112+
* @returns
113+
*/
105114
export function getTokenInfo(l2TokenAddress: string, chainId: number, tokenMapping = TOKEN_SYMBOLS_MAP): L1Token {
106115
// @dev This might give false positives if tokens on different networks have the same address. I'm not sure how
107116
// to get around this...
@@ -137,19 +146,18 @@ export function getUsdcSymbol(l2Token: string, chainId: number): string | undefi
137146
);
138147
}
139148

140-
export function getL1TokenInfo(l2TokenAddress: string, chainId: number): L1Token {
149+
/**
150+
* @notice Returns the l1 token address matching the given l2TokenAddress and chainId.
151+
*/
152+
export function getL1TokenAddress(l2TokenAddress: string, chainId: number): string {
141153
if (chainIsL1(chainId))
142-
throw new Error("chainId should be an L2 chainId because many mappings re-use the same L1 token address");
154+
return l2TokenAddress;
143155
const tokenObject = Object.values(TOKEN_SYMBOLS_MAP).find(({ addresses }) => addresses[chainId] === l2TokenAddress);
144156
const l1TokenAddress = tokenObject?.addresses[chainIsProd(chainId) ? CHAIN_IDs.MAINNET : CHAIN_IDs.SEPOLIA];
145157
if (!l1TokenAddress) {
146158
throw new Error(
147159
`TokenUtils#getL1TokenInfo: Unable to resolve l1 token address in TOKEN_SYMBOLS_MAP for L2 token ${l2TokenAddress} on chain ${chainId}`
148160
);
149161
}
150-
return {
151-
address: l1TokenAddress,
152-
symbol: tokenObject.symbol,
153-
decimals: tokenObject.decimals,
154-
};
162+
return l1TokenAddress;
155163
}

0 commit comments

Comments
 (0)