Skip to content

Commit

Permalink
Merge branch 'stage' into jon/fe-473-add-unit-tests-for-ibc-bridge-pr…
Browse files Browse the repository at this point in the history
…ovider-and-transfer-status
  • Loading branch information
jonator committed Jun 12, 2024
2 parents 5c2108c + 3c225f8 commit e7bc76a
Show file tree
Hide file tree
Showing 16 changed files with 476 additions and 604 deletions.
3 changes: 1 addition & 2 deletions packages/bridge/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@
"@osmosis-labs/tx": "^1.0.0",
"@osmosis-labs/utils": "^1.0.0",
"cachified": "^3.5.4",
"ethers": "^6.8.0",
"long": "^5.2.3",
"lru-cache": "^10.0.1",
"web3-utils": "^1.7.4",
"viem": "2.13.3",
"zod": "^3.22.4"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@ import { NativeEVMTokenConstantAddress } from "../../ethereum";
import { BridgeProviderContext } from "../../interface";
import { AxelarBridgeProvider } from "../index";

jest.mock("ethers", () => {
const originalModule = jest.requireActual("ethers");
jest.mock("viem", () => {
const originalModule = jest.requireActual("viem");
return {
...originalModule,
ethers: {
...originalModule.ethers,
JsonRpcProvider: jest.fn().mockImplementation(() => ({
estimateGas: jest.fn().mockResolvedValue("21000"),
_perform: jest.fn().mockResolvedValue("0x4a817c800"),
})),
},
createPublicClient: jest.fn().mockImplementation(() => ({
estimateGas: jest.fn().mockResolvedValue(BigInt("21000")),
getGasPrice: jest.fn().mockResolvedValue(BigInt("0x4a817c800")),
})),
http: jest.fn(),
};
});

Expand All @@ -42,6 +40,10 @@ beforeEach(() => {
);
});

afterEach(() => {
jest.clearAllMocks();
});

describe("AxelarBridgeProvider", () => {
let provider: AxelarBridgeProvider;
let ctx: BridgeProviderContext;
Expand Down Expand Up @@ -186,6 +188,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
52 changes: 28 additions & 24 deletions packages/bridge/src/axelar/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@ import { CoinPretty, Dec } from "@keplr-wallet/unit";
import type { IbcTransferMethod } from "@osmosis-labs/types";
import { getAssetFromAssetList, getKeyByValue } from "@osmosis-labs/utils";
import { cachified } from "cachified";
import { ethers } from "ethers";
import { hexToNumberString, toHex } from "web3-utils";
import {
Address,
createPublicClient,
encodeFunctionData,
erc20Abi,
http,
numberToHex,
} from "viem";

import { BridgeError, BridgeQuoteError } from "../errors";
import {
Erc20Abi,
EthereumChainInfo,
NativeEVMTokenConstantAddress,
} from "../ethereum";
import { EthereumChainInfo, NativeEVMTokenConstantAddress } from "../ethereum";
import {
BridgeAsset,
BridgeCoin,
Expand Down Expand Up @@ -260,26 +262,27 @@ export class AxelarBridgeProvider implements BridgeProvider {

if (transactionData.type === "evm") {
const evmChain = Object.values(EthereumChainInfo).find(
({ chainId }) => String(chainId) === String(params.fromChain.chainId)
({ id: chainId }) =>
String(chainId) === String(params.fromChain.chainId)
);

if (!evmChain) throw new Error("Could not find EVM chain");

const fromProvider = new ethers.JsonRpcProvider(evmChain.rpcUrls[0]);
const fromProvider = createPublicClient({
chain: evmChain,
transport: http(evmChain.rpcUrls.default.http[0]),
});

const gasAmountUsed = String(
await fromProvider.estimateGas({
from: params.fromAddress,
account: params.fromAddress as Address,
to: transactionData.to,
value: transactionData.value,
value: BigInt(transactionData.value ?? ""),
data: transactionData.data,
})
);
const gasPrice = hexToNumberString(
await fromProvider._perform({
method: "getGasPrice",
})
);

const gasPrice = (await fromProvider.getGasPrice()).toString();

const gasCost = new Dec(gasAmountUsed).mul(new Dec(gasPrice));
return {
Expand Down Expand Up @@ -356,17 +359,18 @@ export class AxelarBridgeProvider implements BridgeProvider {
if (isNativeToken) {
return {
type: "evm",
to: depositAddress,
value: toHex(fromAmount),
to: depositAddress as Address,
value: numberToHex(BigInt(fromAmount)),
};
} else {
return {
type: "evm",
to: fromAsset.address, // ERC20 token address
data: Erc20Abi.encodeFunctionData("transfer", [
depositAddress,
toHex(fromAmount),
]),
to: fromAsset.address as Address, // ERC20 token address
data: encodeFunctionData({
abi: erc20Abi,
functionName: "transfer",
args: [depositAddress as `0x${string}`, BigInt(fromAmount)],
}),
};
}
}
Expand Down Expand Up @@ -546,7 +550,7 @@ export class AxelarBridgeProvider implements BridgeProvider {
}

const ethereumChainName = Object.values(EthereumChainInfo).find(
({ chainId }) => String(chainId) === String(chain.chainId)
({ id: chainId }) => String(chainId) === String(chain.chainId)
)?.chainName;

if (!ethereumChainName) return undefined;
Expand Down
Loading

0 comments on commit e7bc76a

Please sign in to comment.