Skip to content

Commit

Permalink
refactor: use blocto supportChainList and move into try catch block
Browse files Browse the repository at this point in the history
  • Loading branch information
akira02 committed Dec 22, 2023
1 parent ed0b99b commit 093617f
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions adapters/wagmi-connector/src/connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,23 +133,23 @@ class BloctoConnector extends Connector<BloctoProvider, BloctoOptions> {
}

async switchChain(chainId: number): Promise<Chain> {
const provider = await this.getProvider();
const id = numberToHex(chainId);
const chain = this.chains.find((x) => x.id === chainId);
const { networks } = await fetch(
'https://api.blocto.app/networks/evm'
).then((response) => response.json());
const evmSupportMap = networks.reduce(
(a: any, v: any) => ({ ...a, [v.chain_id]: v }),
{}
);
const isBloctoSupportChain = evmSupportMap[`${chainId}`];

if (!chain || !isBloctoSupportChain) {
throw new SwitchChainError(new Error(`Blocto unsupported chain: ${id}`));
}

try {
const provider = await this.getProvider();
const id = numberToHex(chainId);
const chain = this.chains.find((x) => x.id === chainId);
const networks = await provider.supportChainList();
const evmSupportMap = networks.reduce(
(a: any, v: any) => ({ ...a, [v.chain_id]: v }),
{}
);
const isBloctoSupportChain = evmSupportMap[`${chainId}`];

if (!chain || !isBloctoSupportChain) {
throw new SwitchChainError(
new Error(`Blocto unsupported chain: ${id}`)
);
}

await provider.request({
method: 'wallet_addEthereumChain',
params: [{ chainId: id, rpcUrls: chain?.rpcUrls.default.http }],
Expand Down

0 comments on commit 093617f

Please sign in to comment.