From ea110ad76172921f98c11cfc19b28a7db556de6e Mon Sep 17 00:00:00 2001 From: Jose Felix Date: Thu, 6 Jun 2024 15:19:00 -0500 Subject: [PATCH] improvement: use numberToHex for better typesafety --- .../__tests__/axelar-bridge-provider.spec.ts | 39 +++++++++++++++++++ packages/bridge/src/axelar/index.ts | 4 +- packages/bridge/src/skip/index.ts | 4 +- packages/bridge/src/squid/index.ts | 14 +++---- 4 files changed, 50 insertions(+), 11 deletions(-) diff --git a/packages/bridge/src/axelar/__tests__/axelar-bridge-provider.spec.ts b/packages/bridge/src/axelar/__tests__/axelar-bridge-provider.spec.ts index 9760336f42..2c8b5e876f 100644 --- a/packages/bridge/src/axelar/__tests__/axelar-bridge-provider.spec.ts +++ b/packages/bridge/src/axelar/__tests__/axelar-bridge-provider.spec.ts @@ -184,6 +184,45 @@ describe("AxelarBridgeProvider", () => { }); }); + it("should create an EVM transaction with native token", async () => { + const mockDepositClient: Partial = { + 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 = { getDepositAddress: jest diff --git a/packages/bridge/src/axelar/index.ts b/packages/bridge/src/axelar/index.ts index 03f841b89a..8ea7e35371 100644 --- a/packages/bridge/src/axelar/index.ts +++ b/packages/bridge/src/axelar/index.ts @@ -12,7 +12,7 @@ import { encodeFunctionData, erc20Abi, http, - toHex, + numberToHex, } from "viem"; import { BridgeError, BridgeQuoteError } from "../errors"; @@ -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 { diff --git a/packages/bridge/src/skip/index.ts b/packages/bridge/src/skip/index.ts index d6673b236c..c0a5d5e20c 100644 --- a/packages/bridge/src/skip/index.ts +++ b/packages/bridge/src/skip/index.ts @@ -11,7 +11,7 @@ import { http, keccak256, maxUint256, - toHex, + numberToHex, } from "viem"; import { BridgeError, BridgeQuoteError } from "../errors"; @@ -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, }; } diff --git a/packages/bridge/src/squid/index.ts b/packages/bridge/src/squid/index.ts index 03eb9ebfe6..183c01b79e 100644 --- a/packages/bridge/src/squid/index.ts +++ b/packages/bridge/src/squid/index.ts @@ -14,7 +14,7 @@ import { encodeFunctionData, erc20Abi, http, - toHex, + numberToHex, } from "viem"; import { BridgeError, BridgeQuoteError } from "../errors"; @@ -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, };