diff --git a/packages/bridge/src/nitro/index.ts b/packages/bridge/src/nitro/index.ts index 7ee424997b..02c334ed64 100644 --- a/packages/bridge/src/nitro/index.ts +++ b/packages/bridge/src/nitro/index.ts @@ -32,56 +32,68 @@ export class NitroBridgeProvider implements BridgeProvider { async getSupportedAssets({ asset, }: GetBridgeSupportedAssetsParams): Promise<(BridgeChain & BridgeAsset)[]> { - // just supports TRX, TRX.rt from Tron - - const assets = this.ctx.assetLists.flatMap(({ assets }) => assets); - const assetListAsset = assets.find( - (a) => a.coinMinimalDenom.toLowerCase() === asset.address.toLowerCase() - ); - - if (assetListAsset) { - const variants = assets - .filter((a) => a?.variantGroupKey === assetListAsset.variantGroupKey) - .sort((a) => - a.coinMinimalDenom === assetListAsset.variantGroupKey ? -1 : 1 - ); - - const tronAsset = variants.length > 1 ? variants[1] : variants[0]; + try { + // just supports TRX, TRX.rt from Tron - const tronCounterparty = tronAsset.counterparty.find( - (c) => c.chainName === "tron" + const assets = this.ctx.assetLists.flatMap(({ assets }) => assets); + const assetListAsset = assets.find( + (a) => a.coinMinimalDenom.toLowerCase() === asset.address.toLowerCase() ); - if (tronCounterparty) { - const isTRC20Token = tronCounterparty.sourceDenom.startsWith("T"); - const address = isTRC20Token - ? Buffer.from(bs58.decode(tronCounterparty.sourceDenom)) - .toString("hex") - // Tron addresses in hexadecimal format always start with '41'. - // This prefix should be replaced with '0x' to convert it to a valid address. - .replace("41", "0x") - .substring(0, 42) // Tron addresses are 21 bytes long, so we need to truncate the last byte. - : undefined; - - const resolvedAddress = - tronCounterparty.sourceDenom === "sun" - ? NativeEVMTokenConstantAddress // Nitro uses the constant address to reference the native token - : address ?? tronCounterparty.sourceDenom; - - return [ - { - chainType: "tron", - chainId: TronChainInfo.chainId, - chainName: TronChainInfo.chainName, - denom: tronCounterparty.symbol, - address: resolvedAddress, - decimals: tronCounterparty.decimals, - }, - ]; + if (assetListAsset) { + const variants = assets + .filter((a) => a?.variantGroupKey === assetListAsset.variantGroupKey) + .sort((a) => + a.coinMinimalDenom === assetListAsset.variantGroupKey ? -1 : 1 + ); + + const tronAsset = variants.length > 1 ? variants[1] : variants[0]; + + const tronCounterparty = tronAsset.counterparty.find( + (c) => c.chainName === "tron" + ); + + if (tronCounterparty) { + const isTRC20Token = tronCounterparty.sourceDenom.startsWith("T"); + const address = isTRC20Token + ? Buffer.from(bs58.decode(tronCounterparty.sourceDenom)) + .toString("hex") + // Tron addresses in hexadecimal format always start with '41'. + // This prefix should be replaced with '0x' to convert it to a valid address. + .replace("41", "0x") + .substring(0, 42) // Tron addresses are 21 bytes long, so we need to truncate the last byte. + : undefined; + + const resolvedAddress = + tronCounterparty.sourceDenom === "sun" + ? NativeEVMTokenConstantAddress // Nitro uses the constant address to reference the native token + : address ?? tronCounterparty.sourceDenom; + + return [ + { + chainType: "tron", + chainId: TronChainInfo.chainId, + chainName: TronChainInfo.chainName, + denom: tronCounterparty.symbol, + address: resolvedAddress, + decimals: tronCounterparty.decimals, + }, + ]; + } } - } - return []; + return []; + } catch (e) { + // Avoid returning options if there's an unexpected error, such as the provider being down + if (process.env.NODE_ENV !== "production") { + console.error( + NitroBridgeProvider.ID, + "failed to get supported assets:", + e + ); + } + return []; + } } async getTransactionData(): Promise { @@ -104,13 +116,7 @@ export class NitroBridgeProvider implements BridgeProvider { url.searchParams.set("toChain", String(toChain.chainId)); } - const setTokenParam = ( - asset: NonNullable< - | GetBridgeExternalUrlParams["fromAsset"] - | GetBridgeExternalUrlParams["toAsset"] - >, - param: string - ) => { + const setTokenParam = (asset: BridgeAsset, param: string) => { if (asset.address.includes("alloyed")) { const assets = this.ctx.assetLists.flatMap(({ assets }) => assets); const alloy = assets.find((a) => a.coinMinimalDenom === asset.address);