From 983d72851ee0519aeb7da868711b18bdec4ce1d7 Mon Sep 17 00:00:00 2001 From: Kevin Peters Date: Fri, 20 Sep 2024 15:16:27 -0500 Subject: [PATCH] tokenBridge: fromIdentifier amount scaling fix We were not scaling the transfer amount on the VAA to the source chain amount correctly. Fixes #580 --- .../protocols/tokenBridge/tokenTransfer.ts | 23 ++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/connect/src/protocols/tokenBridge/tokenTransfer.ts b/connect/src/protocols/tokenBridge/tokenTransfer.ts index 0677dd513..b36435682 100644 --- a/connect/src/protocols/tokenBridge/tokenTransfer.ts +++ b/connect/src/protocols/tokenBridge/tokenTransfer.ts @@ -1,5 +1,11 @@ import type { Chain, Network } from "@wormhole-foundation/sdk-base"; -import { amount, encoding, finality, guardians, toChain as toChainName } from "@wormhole-foundation/sdk-base"; +import { + amount, + encoding, + finality, + guardians, + toChain as toChainName, +} from "@wormhole-foundation/sdk-base"; import type { AttestationId, AutomaticTokenBridge, @@ -167,9 +173,20 @@ export class TokenTransfer let from = { chain: vaa.emitterChain, address: vaa.emitterAddress }; let { token, to } = vaa.payload; + let nativeAddress: NativeAddress; + if (token.chain === from.chain) { + nativeAddress = await wh.getTokenNativeAddress(from.chain, token.chain, token.address); + } else { + const fromChain = await wh.getChain(from.chain); + const tb = await fromChain.getTokenBridge(); + const wrapped = await tb.getWrappedAsset(token); + nativeAddress = toNative(token.chain, wrapped.toString()); + } + + const decimals = await wh.getDecimals(from.chain, nativeAddress); const scaledAmount = amount.scale( - amount.fromBaseUnits(token.amount, TokenTransfer.MAX_DECIMALS), - await wh.getDecimals(token.chain, token.address), + amount.fromBaseUnits(token.amount, Math.min(decimals, TokenTransfer.MAX_DECIMALS)), + decimals, ); let nativeGasAmount: bigint = 0n;