Skip to content

Commit

Permalink
improvement: use numberToHex for better typesafety
Browse files Browse the repository at this point in the history
  • Loading branch information
JoseRFelix committed Jun 6, 2024
1 parent 9742f58 commit ea110ad
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,45 @@ describe("AxelarBridgeProvider", () => {
});
});

it("should create an EVM transaction with native token", async () => {
const mockDepositClient: Partial<AxelarAssetTransfer> = {
getDepositAddress: jest
.fn()
.mockResolvedValue("0x1234567890abcdef1234567890abcdef12345678"),
};

jest
.spyOn(provider, "getAssetTransferClient")
.mockResolvedValue(mockDepositClient as unknown as AxelarAssetTransfer);

const transaction = await provider.createEvmTransaction({
fromChain: { chainId: "1", chainName: "Ethereum", chainType: "evm" },
toChain: { chainId: "43114", chainName: "Avalanche", chainType: "evm" },
fromAsset: {
denom: "ETH",
address: NativeEVMTokenConstantAddress,
decimals: 18,
sourceDenom: "eth",
},
toAsset: {
denom: "AVAX",
address: "0x0000000000000000000000000000000000000000",
decimals: 18,
sourceDenom: "avax",
},
fromAmount: "1",
fromAddress: "0x1234567890abcdef1234567890abcdef12345678",
toAddress: "0xabcdefabcdefabcdefabcdefabcdefabcdefabcdef",
simulated: false,
});

expect(transaction).toEqual({
value: "0x1", // same as from amount
type: "evm",
to: "0x1234567890abcdef1234567890abcdef12345678",
});
});

it("should throw an error when creating an EVM transaction with a non-native token", async () => {
const mockDepositClient: Partial<AxelarAssetTransfer> = {
getDepositAddress: jest
Expand Down
4 changes: 2 additions & 2 deletions packages/bridge/src/axelar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
encodeFunctionData,
erc20Abi,
http,
toHex,
numberToHex,
} from "viem";

import { BridgeError, BridgeQuoteError } from "../errors";
Expand Down Expand Up @@ -360,7 +360,7 @@ export class AxelarBridgeProvider implements BridgeProvider {
return {
type: "evm",
to: depositAddress as Address,
value: toHex(fromAmount),
value: numberToHex(BigInt(fromAmount)),
};
} else {
return {
Expand Down
4 changes: 2 additions & 2 deletions packages/bridge/src/skip/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
http,
keccak256,
maxUint256,
toHex,
numberToHex,
} from "viem";

import { BridgeError, BridgeQuoteError } from "../errors";
Expand Down Expand Up @@ -286,7 +286,7 @@ export class SkipBridgeProvider implements BridgeProvider {
type: "evm",
to: message.to as Address,
data: `0x${message.data}`,
value: toHex(BigInt(message.value)),
value: numberToHex(BigInt(message.value)),
approvalTransactionRequest,
};
}
Expand Down
14 changes: 7 additions & 7 deletions packages/bridge/src/squid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
encodeFunctionData,
erc20Abi,
http,
toHex,
numberToHex,
} from "viem";

import { BridgeError, BridgeQuoteError } from "../errors";
Expand Down Expand Up @@ -311,19 +311,19 @@ export class SquidBridgeProvider implements BridgeProvider {
data: transactionRequest.data as Address,
value:
transactionRequest.routeType !== "SEND"
? toHex(BigInt(transactionRequest.value))
? numberToHex(BigInt(transactionRequest.value))
: undefined,
...(transactionRequest.maxPriorityFeePerGas
? {
gas: toHex(BigInt(transactionRequest.gasLimit)),
maxFeePerGas: toHex(BigInt(transactionRequest.maxFeePerGas)),
maxPriorityFeePerGas: toHex(
gas: numberToHex(BigInt(transactionRequest.gasLimit)),
maxFeePerGas: numberToHex(BigInt(transactionRequest.maxFeePerGas)),
maxPriorityFeePerGas: numberToHex(
BigInt(transactionRequest.maxPriorityFeePerGas)
),
}
: {
gas: toHex(BigInt(transactionRequest.gasLimit)),
gasPrice: toHex(BigInt(transactionRequest.gasPrice)),
gas: numberToHex(BigInt(transactionRequest.gasLimit)),
gasPrice: numberToHex(BigInt(transactionRequest.gasPrice)),
}),
approvalTransactionRequest: approvalTx,
};
Expand Down

0 comments on commit ea110ad

Please sign in to comment.