From b39983ab38e5cdf3930658e981ff3d1a18263f8d Mon Sep 17 00:00:00 2001 From: philogicae Date: Thu, 12 Sep 2024 20:03:34 +0300 Subject: [PATCH] Fix wrong fallback logic on switch/add network --- packages/evm/src/account.ts | 4 +--- packages/evm/src/provider/constants.ts | 4 ++++ packages/evm/src/provider/rpc.ts | 22 ++++++++++++++++++---- packages/superfluid/src/account.ts | 1 - 4 files changed, 23 insertions(+), 8 deletions(-) create mode 100644 packages/evm/src/provider/constants.ts diff --git a/packages/evm/src/account.ts b/packages/evm/src/account.ts index 19729003..09873c91 100644 --- a/packages/evm/src/account.ts +++ b/packages/evm/src/account.ts @@ -47,9 +47,7 @@ export abstract class EVMAccount extends ECIESAccount { public async changeNetwork(chainOrRpc: RpcType | RpcId = RpcId.ETH): Promise { if (this.wallet instanceof JsonRPCWallet) { await this.wallet.changeNetwork(chainOrRpc) - } - if (this.wallet instanceof ethers.Wallet) { - //await this.wallet.provider.send("wallet_switchEthereumChain", [{ chainId: chainId.toString(16) }]); + } else if (this.wallet instanceof ethers.Wallet) { throw new Error('Not implemented for ethers.Wallet') } } diff --git a/packages/evm/src/provider/constants.ts b/packages/evm/src/provider/constants.ts new file mode 100644 index 00000000..512a87ca --- /dev/null +++ b/packages/evm/src/provider/constants.ts @@ -0,0 +1,4 @@ +export const MetamaskErrorCodes: Record = { + REJECTED: 4001, + UNRECOGNIZED: 4902, +} diff --git a/packages/evm/src/provider/rpc.ts b/packages/evm/src/provider/rpc.ts index 047e9c99..c72f4095 100644 --- a/packages/evm/src/provider/rpc.ts +++ b/packages/evm/src/provider/rpc.ts @@ -1,5 +1,6 @@ import { ethers } from 'ethers' import { BaseProviderWallet } from '@aleph-sdk/account' +import { MetamaskErrorCodes } from './constants' const RPC_WARNING = `DEPRECATION WARNING: Encryption/Decryption features may become obsolete, for more information: https://github.com/aleph-im/aleph-sdk-ts/issues/37` @@ -226,8 +227,15 @@ export class JsonRPCWallet extends BaseProviderWallet { try { await this.provider.send('wallet_switchEthereumChain', [{ chainId: chainData.chainId }]) - } catch (error) { - await this.addNetwork(chainData) + } catch (e) { + const err = e as { code?: number; message?: string } + if (err?.code === MetamaskErrorCodes.UNRECOGNIZED) { + await this.addNetwork(chainData) + } else if (err?.code === MetamaskErrorCodes.REJECTED) { + console.warn(`[Switch Network]: ${err?.message}`) + } else { + throw new Error(`[Switch Network]: ${err?.message}`) + } } } @@ -248,8 +256,14 @@ export class JsonRPCWallet extends BaseProviderWallet { private async addNetwork(chainData: RpcType): Promise { try { await this.provider.send('wallet_addEthereumChain', [chainData]) - } catch (error) { - throw new Error(`Could not add network to provider: ${error}`) + await this.changeNetwork(chainData) + } catch (e) { + const err = e as { code?: number; message?: string } + if (err?.code === MetamaskErrorCodes.REJECTED) { + console.warn(`[Add Network]: ${err?.message}`) + } else { + throw new Error(`[Add Network]: ${err?.message}`) + } } } } diff --git a/packages/superfluid/src/account.ts b/packages/superfluid/src/account.ts index bce44906..a64f273d 100644 --- a/packages/superfluid/src/account.ts +++ b/packages/superfluid/src/account.ts @@ -21,7 +21,6 @@ import { Blockchain } from '@aleph-sdk/core' * It is used to represent a Superfluid account when publishing a message on the Aleph network. */ - export class SuperfluidAccount extends EVMAccount { public override wallet: JsonRPCWallet private account: EVMAccount