Skip to content

Commit

Permalink
tokenBridge: fromIdentifier amount scaling fix
Browse files Browse the repository at this point in the history
We were not scaling the transfer amount on the VAA to the source chain
amount correctly.

Fixes #580
  • Loading branch information
kev1n-peters committed Sep 20, 2024
1 parent 5810ebb commit 983d728
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions connect/src/protocols/tokenBridge/tokenTransfer.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -167,9 +173,20 @@ export class TokenTransfer<N extends Network = Network>
let from = { chain: vaa.emitterChain, address: vaa.emitterAddress };
let { token, to } = vaa.payload;

let nativeAddress: NativeAddress<Chain>;
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;
Expand Down

0 comments on commit 983d728

Please sign in to comment.