Skip to content

Commit

Permalink
chore: add network switch request to axelarApi validateNetwork
Browse files Browse the repository at this point in the history
  • Loading branch information
shane-moore committed Oct 24, 2023
1 parent 68358be commit dc23fec
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions packages/bridge-ts/src/axelar/AxelarApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -140,7 +141,14 @@ export class AxelarClient {
}
}

// eslint-disable-next-line class-methods-use-this
public async getEthereumAddress(): Promise<string> {
const { signer } = await this.getAxelarGateway()

await this.validateNetwork()

return await signer.getAddress()
}

private async validateNetwork() {
const provider = (window as any).ethereum

Expand All @@ -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<void>((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`),
)
}
}
}
}

0 comments on commit dc23fec

Please sign in to comment.