From dc23fec1f93ef6c0c433fcf7c09c75f24017e34d Mon Sep 17 00:00:00 2001 From: shane-moore Date: Tue, 24 Oct 2023 14:48:55 -0700 Subject: [PATCH] chore: add network switch request to axelarApi validateNetwork --- packages/bridge-ts/src/axelar/AxelarApi.ts | 38 +++++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/packages/bridge-ts/src/axelar/AxelarApi.ts b/packages/bridge-ts/src/axelar/AxelarApi.ts index 34e9077c3..2fe1ceb10 100644 --- a/packages/bridge-ts/src/axelar/AxelarApi.ts +++ b/packages/bridge-ts/src/axelar/AxelarApi.ts @@ -6,6 +6,7 @@ import { EvmChain, } from '@axelar-network/axelarjs-sdk' import { providers, Contract } from 'ethers' +import { GeneralException } from '@injectivelabs/exceptions' import { BigNumberInWei } from '@injectivelabs/utils' import { ErrorType, @@ -140,7 +141,14 @@ export class AxelarClient { } } - // eslint-disable-next-line class-methods-use-this + public async getEthereumAddress(): Promise { + const { signer } = await this.getAxelarGateway() + + await this.validateNetwork() + + return await signer.getAddress() + } + private async validateNetwork() { const provider = (window as any).ethereum @@ -159,13 +167,27 @@ export class AxelarClient { const network = await web3Provider.getNetwork() if (network.chainId !== MOONBEAM_MAINNET_CHAIN_ID) { - throw new MetamaskException( - new Error('Please switch to the Moonbeam network in Metamask'), - { - code: UnspecifiedErrorCode, - type: ErrorType.WalletError, - }, - ) + const chainIdToHex = MOONBEAM_MAINNET_CHAIN_ID.toString(16) + + try { + await Promise.race([ + web3Provider.provider.request!({ + method: 'wallet_switchEthereumChain', + params: [{ chainId: `0x${chainIdToHex}` }], + }), + new Promise((resolve) => + web3Provider.on('change', ({ chain }: any) => { + if (chain?.id === chainIdToHex) { + resolve() + } + }), + ), + ]) + } catch (e) { + throw new GeneralException( + new Error(`Please switch to the Moonbeam Network on Metamask`), + ) + } } } }