Skip to content

Commit

Permalink
update logic for getWarpRouteCollateralTokenSymbol
Browse files Browse the repository at this point in the history
  • Loading branch information
Mo-Hussain committed Dec 23, 2024
1 parent caa68db commit 9b84bd0
Showing 1 changed file with 34 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -323,15 +323,45 @@ async function getCoinGeckoApiKey(): Promise<string | undefined> {
}

function getWarpRouteCollateralTokenSymbol(warpCore: WarpCore): string {
const collateralToken = warpCore.tokens.find(
// We need to have a deterministic way to determine the symbol of the warp route
// as its used to identify the warp route in metrics. This method should support routes where:
// - All tokens have the same symbol, token standards can be all collateral, all synthetic or a mix)
// - All tokens have different symbol, but there is a collateral token to break the tie, where there are multiple collateral tokens, alphabetically first is chosen
// doesn't support routes where:
// - All tokens have different symbols and there is no collateral token to break the tie

// Get all unique symbols from the tokens array
const uniqueSymbols = new Set(warpCore.tokens.map((token) => token.symbol));

// If all tokens have the same symbol, return that symbol
if (uniqueSymbols.size === 1) {
return warpCore.tokens[0].symbol;
}

// Find all collateralized tokens
const collateralTokens = warpCore.tokens.filter(
(token) =>
token.isCollateralized() ||
token.standard === TokenStandard.EvmHypXERC20Lockbox,
);
if (!collateralToken) {
throw new Error('No collateral token found in warp route');

if (collateralTokens.length === 0) {
throw new Error(
`Unable to determine token symbol: tokens in route have different symbols
(${Array.from(uniqueSymbols).join(
', ',
)}) and no collateral token was found to break the tie`,
);
}

const collateralSymbols = collateralTokens.map((token) => token.symbol);
const uniqueCollateralSymbols = [...new Set(collateralSymbols)];

if (uniqueCollateralSymbols.length === 1) {
return uniqueCollateralSymbols[0];
} else {
return uniqueCollateralSymbols.sort()[0];
}
return collateralToken.symbol;
}

main().catch((err) => {
Expand Down

0 comments on commit 9b84bd0

Please sign in to comment.