Skip to content

Commit

Permalink
improvement: @jonator feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseRFelix committed Aug 8, 2024
1 parent fc3f11a commit edd70b1
Showing 1 changed file with 58 additions and 52 deletions.
110 changes: 58 additions & 52 deletions packages/bridge/src/nitro/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<BridgeTransactionRequest> {
Expand All @@ -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);
Expand Down

0 comments on commit edd70b1

Please sign in to comment.