From b2268aaf8cf4e0df8f340995194528565c73fb02 Mon Sep 17 00:00:00 2001 From: kev1n-peters <96065607+kev1n-peters@users.noreply.github.com> Date: Mon, 23 Sep 2024 11:32:47 -0500 Subject: [PATCH] tokenBridge: fromIdentifier amount scaling fix (#700) * tokenBridge: fromIdentifier amount scaling fix We were not scaling the transfer amount on the VAA to the source chain amount correctly. Fixes #580 * fix toNative chain --- .../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..03eb569b1 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(from.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;