Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into ID-1619
Browse files Browse the repository at this point in the history
  • Loading branch information
imx-mikhala committed May 8, 2024
2 parents db194f8 + 20c0e27 commit 0483640
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 153 deletions.
27 changes: 27 additions & 0 deletions packages/internal/bridge/sdk/src/lib/utils.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { ETH_MAINNET_TO_ZKEVM_MAINNET, ETH_SEPOLIA_TO_ZKEVM_TESTNET } from 'constants/bridges';
import { exportedForTesting } from './utils';

describe('utils', () => {
describe('getAddresses', () => {
it('should return mainnet address', () => {
const source = ETH_MAINNET_TO_ZKEVM_MAINNET.rootChainID;
const addresses = { mainnet: 'mainnet', testnet: 'testnet', devnet: 'devnet' };
const result = exportedForTesting.getAddresses(source, addresses);
expect(result).toEqual('mainnet');
});

it('should return testnet address', () => {
const source = ETH_SEPOLIA_TO_ZKEVM_TESTNET.rootChainID;
const addresses = { mainnet: 'mainnet', testnet: 'testnet', devnet: 'devnet' };
const result = exportedForTesting.getAddresses(source, addresses);
expect(result).toEqual('testnet');
});

it('should return devnet address in all other cases', () => {
const source = 'devnet';
const addresses = { mainnet: 'mainnet', testnet: 'testnet', devnet: 'devnet' };
const result = exportedForTesting.getAddresses(source, addresses);
expect(result).toEqual('devnet');
});
});
});
62 changes: 45 additions & 17 deletions packages/internal/bridge/sdk/src/lib/utils.ts
Original file line number Diff line number Diff line change
@@ -1,40 +1,68 @@
import {
ETH_MAINNET_TO_ZKEVM_MAINNET, childETHs, ETH_SEPOLIA_TO_ZKEVM_TESTNET, rootIMXs,
childAdaptors,
rootAdaptors,
childChains,
axelarGateways,
axelarAPIEndpoints,
tenderlyAPIEndpoints,
} from 'constants/bridges';
import { FungibleToken } from 'types';

export function getChildETH(source: string) {
let eth:string;
function getAddresses(source:string, addresses:Record<string, string>) {
let address:string;
if (source === ETH_MAINNET_TO_ZKEVM_MAINNET.rootChainID
|| source === ETH_MAINNET_TO_ZKEVM_MAINNET.childChainID) {
eth = childETHs.mainnet;
address = addresses.mainnet;
} else if (source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.rootChainID
|| source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.childChainID) {
eth = childETHs.testnet;
address = addresses.testnet;
} else {
eth = childETHs.devnet;
address = addresses.devnet;
}
return eth;
return address;
}

export function getChildETH(source: string) {
return getAddresses(source, childETHs);
}

export function isChildETH(token: FungibleToken, source: string) {
return token.toUpperCase() === getChildETH(source).toUpperCase();
}

export function getRootIMX(source: string) {
let rootIMX:string;
if (source === ETH_MAINNET_TO_ZKEVM_MAINNET.rootChainID
|| source === ETH_MAINNET_TO_ZKEVM_MAINNET.childChainID) {
rootIMX = rootIMXs.mainnet;
} else if (source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.rootChainID
|| source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.childChainID) {
rootIMX = rootIMXs.testnet;
} else {
rootIMX = rootIMXs.devnet;
}
return rootIMX;
return getAddresses(source, rootIMXs);
}

export function isRootIMX(token: FungibleToken, source: string) {
return token.toUpperCase() === getRootIMX(source).toUpperCase();
}

export function getChildAdaptor(source: string) {
return getAddresses(source, childAdaptors);
}

export function getRootAdaptor(source: string) {
return getAddresses(source, rootAdaptors);
}

export function getChildchain(source: string) {
return getAddresses(source, childChains);
}

export function getAxelarGateway(source: string) {
return getAddresses(source, axelarGateways);
}

export function getAxelarEndpoint(source:string) {
return getAddresses(source, axelarAPIEndpoints);
}

export function getTenderlyEndpoint(source:string) {
return getAddresses(source, tenderlyAPIEndpoints);
}

export const exportedForTesting = {
getAddresses,
};
146 changes: 10 additions & 136 deletions packages/internal/bridge/sdk/src/tokenBridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {
checkReceiver, validateBridgeReqArgs, validateChainConfiguration, validateChainIds,
validateGetFee,
} from 'lib/validation';
import { getRootIMX } from 'lib/utils';
import {
getAxelarEndpoint, getAxelarGateway, getChildAdaptor, getChildchain, getRootAdaptor, getRootIMX, getTenderlyEndpoint,
} from 'lib/utils';
import { TenderlySimulation } from 'types/tenderly';
import { calculateGasFee } from 'lib/gas';
import {
Expand All @@ -23,20 +25,12 @@ import {
ZKEVM_DEVNET_CHAIN_ID,
ZKEVM_MAINNET_CHAIN_ID,
ZKEVM_TESTNET_CHAIN_ID,
axelarAPIEndpoints,
tenderlyAPIEndpoints,
axelarChains,
bridgeMethods,
childWIMXs,
WITHDRAW_SIG,
childAdaptors,
rootAdaptors,
childChains,
SLOT_PREFIX_CONTRACT_CALL_APPROVED,
SLOT_POS_CONTRACT_CALL_APPROVED,
axelarGateways,
childETHs,
rootIMXs,
} from './constants/bridges';
import { ROOT_ERC20_BRIDGE_FLOW_RATE } from './contracts/ABIs/RootERC20BridgeFlowRate';
import { ERC20 } from './contracts/ABIs/ERC20';
Expand Down Expand Up @@ -841,9 +835,9 @@ export class TokenBridge {
const commandId = keccak256(
defaultAbiCoder.encode(['bytes', 'uint256'], [payload, new Date().getTime()]),
);
const sourceChain = this.getChildchain(destinationChainId);
const sourceAddress = ethers.utils.getAddress(this.getChildAdaptor(destinationChainId)).toString();
const destinationAddress = this.getRootAdaptor(destinationChainId);
const sourceChain = getChildchain(destinationChainId);
const sourceAddress = ethers.utils.getAddress(getChildAdaptor(destinationChainId)).toString();
const destinationAddress = getRootAdaptor(destinationChainId);
const payloadHash = keccak256(payload);
// Calculate slot key for given command ID.
const command = defaultAbiCoder.encode(
Expand All @@ -865,7 +859,7 @@ export class TokenBridge {
);

// Build simulation
const axelarGateway = this.getAxelarGateway(destinationChainId);
const axelarGateway = getAxelarGateway(destinationChainId);
const simulations: Array<TenderlySimulation> = [{
network_id: destinationChainId,
estimate_gas: true,
Expand Down Expand Up @@ -894,7 +888,7 @@ export class TokenBridge {
simulations: Array<TenderlySimulation>,
): Promise<Array<number>> {
let axiosResponse:AxiosResponse;
const tenderlyAPI = this.getTenderlyEndpoint(chainId);
const tenderlyAPI = getTenderlyEndpoint(chainId);
try {
axiosResponse = await axios.post(
tenderlyAPI,
Expand Down Expand Up @@ -998,126 +992,6 @@ export class TokenBridge {
return token.toUpperCase() === this.getWrappedIMX(source).toUpperCase();
}

private getRootIMX(source: string) {
let rootIMX:string;
if (source === ETH_MAINNET_TO_ZKEVM_MAINNET.rootChainID
|| source === ETH_MAINNET_TO_ZKEVM_MAINNET.childChainID) {
rootIMX = rootIMXs.mainnet;
} else if (source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.rootChainID
|| source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.childChainID) {
rootIMX = rootIMXs.testnet;
} else {
rootIMX = rootIMXs.devnet;
}
return rootIMX;
}

private isRootIMX(token: FungibleToken, source: string) {
return token.toUpperCase() === this.getRootIMX(source).toUpperCase();
}

private getChildETH(source: string) {
let eth:string;
if (source === ETH_MAINNET_TO_ZKEVM_MAINNET.rootChainID
|| source === ETH_MAINNET_TO_ZKEVM_MAINNET.childChainID) {
eth = childETHs.mainnet;
} else if (source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.rootChainID
|| source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.childChainID) {
eth = childETHs.testnet;
} else {
eth = childETHs.devnet;
}
return eth;
}

private isChildETH(token: FungibleToken, source: string) {
return token.toUpperCase() === this.getChildETH(source).toUpperCase();
}

private getChildAdaptor(source: string) {
let adaptor:string;
if (source === ETH_MAINNET_TO_ZKEVM_MAINNET.rootChainID
|| source === ETH_MAINNET_TO_ZKEVM_MAINNET.childChainID) {
adaptor = childAdaptors.mainnet;
} else if (source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.rootChainID
|| source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.childChainID) {
adaptor = childAdaptors.testnet;
} else {
adaptor = childAdaptors.devnet;
}
return adaptor;
}

private getRootAdaptor(source: string) {
let adaptor:string;
if (source === ETH_MAINNET_TO_ZKEVM_MAINNET.rootChainID
|| source === ETH_MAINNET_TO_ZKEVM_MAINNET.childChainID) {
adaptor = rootAdaptors.mainnet;
} else if (source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.rootChainID
|| source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.childChainID) {
adaptor = rootAdaptors.testnet;
} else {
adaptor = rootAdaptors.devnet;
}
return adaptor;
}

private getChildchain(source: string) {
let chain:string;
if (source === ETH_MAINNET_TO_ZKEVM_MAINNET.rootChainID
|| source === ETH_MAINNET_TO_ZKEVM_MAINNET.childChainID) {
chain = childChains.mainnet;
} else if (source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.rootChainID
|| source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.childChainID) {
chain = childChains.testnet;
} else {
chain = childChains.devnet;
}
return chain;
}

private getAxelarGateway(source: string) {
let gateway:string;
if (source === ETH_MAINNET_TO_ZKEVM_MAINNET.rootChainID
|| source === ETH_MAINNET_TO_ZKEVM_MAINNET.childChainID) {
gateway = axelarGateways.mainnet;
} else if (source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.rootChainID
|| source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.childChainID) {
gateway = axelarGateways.testnet;
} else {
gateway = axelarGateways.devnet;
}
return gateway;
}

private getAxelarEndpoint(source:string) {
let axelarAPIEndpoint:string;
if (source === ETH_MAINNET_TO_ZKEVM_MAINNET.rootChainID
|| source === ETH_MAINNET_TO_ZKEVM_MAINNET.childChainID) {
axelarAPIEndpoint = axelarAPIEndpoints.mainnet;
} else if (source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.rootChainID
|| source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.childChainID) {
axelarAPIEndpoint = axelarAPIEndpoints.testnet;
} else {
axelarAPIEndpoint = axelarAPIEndpoints.devnet;
}
return axelarAPIEndpoint;
}

private getTenderlyEndpoint(source:string) {
let tenderlyAPIEndpoint:string;
if (source === ETH_MAINNET_TO_ZKEVM_MAINNET.rootChainID
|| source === ETH_MAINNET_TO_ZKEVM_MAINNET.childChainID) {
tenderlyAPIEndpoint = tenderlyAPIEndpoints.mainnet;
} else if (source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.rootChainID
|| source === ETH_SEPOLIA_TO_ZKEVM_TESTNET.childChainID) {
tenderlyAPIEndpoint = tenderlyAPIEndpoints.testnet;
} else {
tenderlyAPIEndpoint = tenderlyAPIEndpoints.devnet;
}
return tenderlyAPIEndpoint;
}

/**
* Query the axelar fee for a transaction using axelarjs-sdk.
* @param {*} sourceChainId - The source chainId.
Expand Down Expand Up @@ -1149,7 +1023,7 @@ export class TokenBridge {
);
}

const axelarAPIEndpoint:string = this.getAxelarEndpoint(sourceChainId);
const axelarAPIEndpoint:string = getAxelarEndpoint(sourceChainId);

const estimateGasReq = {
method: 'estimateGasFee',
Expand Down Expand Up @@ -1280,7 +1154,7 @@ export class TokenBridge {
): Promise<Array<TxStatusResponseItem>> {
const txStatusItems:Array<TxStatusResponseItem> = [];
const statusPromises:Array<Promise<GMPStatusResponse>> = [];
const axelarAPIEndpoint:string = this.getAxelarEndpoint(sourceChainId);
const axelarAPIEndpoint:string = getAxelarEndpoint(sourceChainId);
const unpaidGasStatus = [GasPaidStatus.GAS_UNPAID, GasPaidStatus.GAS_PAID_NOT_ENOUGH_GAS];
const abiCoder = new ethers.utils.AbiCoder();

Expand Down

0 comments on commit 0483640

Please sign in to comment.