diff --git a/packages/internal/bridge/sdk/src/lib/utils.test.ts b/packages/internal/bridge/sdk/src/lib/utils.test.ts new file mode 100644 index 0000000000..50d690b058 --- /dev/null +++ b/packages/internal/bridge/sdk/src/lib/utils.test.ts @@ -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'); + }); + }); +}); diff --git a/packages/internal/bridge/sdk/src/lib/utils.ts b/packages/internal/bridge/sdk/src/lib/utils.ts index 5a460edb89..f8a9b4c27e 100644 --- a/packages/internal/bridge/sdk/src/lib/utils.ts +++ b/packages/internal/bridge/sdk/src/lib/utils.ts @@ -1,20 +1,30 @@ 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) { + 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) { @@ -22,19 +32,37 @@ export function isChildETH(token: FungibleToken, source: string) { } 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, +}; diff --git a/packages/internal/bridge/sdk/src/tokenBridge.ts b/packages/internal/bridge/sdk/src/tokenBridge.ts index 89ade8c0cc..a49b8413a5 100644 --- a/packages/internal/bridge/sdk/src/tokenBridge.ts +++ b/packages/internal/bridge/sdk/src/tokenBridge.ts @@ -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 { @@ -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'; @@ -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( @@ -865,7 +859,7 @@ export class TokenBridge { ); // Build simulation - const axelarGateway = this.getAxelarGateway(destinationChainId); + const axelarGateway = getAxelarGateway(destinationChainId); const simulations: Array = [{ network_id: destinationChainId, estimate_gas: true, @@ -894,7 +888,7 @@ export class TokenBridge { simulations: Array, ): Promise> { let axiosResponse:AxiosResponse; - const tenderlyAPI = this.getTenderlyEndpoint(chainId); + const tenderlyAPI = getTenderlyEndpoint(chainId); try { axiosResponse = await axios.post( tenderlyAPI, @@ -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. @@ -1149,7 +1023,7 @@ export class TokenBridge { ); } - const axelarAPIEndpoint:string = this.getAxelarEndpoint(sourceChainId); + const axelarAPIEndpoint:string = getAxelarEndpoint(sourceChainId); const estimateGasReq = { method: 'estimateGasFee', @@ -1280,7 +1154,7 @@ export class TokenBridge { ): Promise> { const txStatusItems:Array = []; const statusPromises:Array> = []; - 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();