Skip to content

Commit

Permalink
Fix wrong fallback logic on switch/add network
Browse files Browse the repository at this point in the history
  • Loading branch information
philogicae committed Sep 12, 2024
1 parent 3e86303 commit b39983a
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 8 deletions.
4 changes: 1 addition & 3 deletions packages/evm/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ export abstract class EVMAccount extends ECIESAccount {
public async changeNetwork(chainOrRpc: RpcType | RpcId = RpcId.ETH): Promise<void> {
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')
}
}
Expand Down
4 changes: 4 additions & 0 deletions packages/evm/src/provider/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const MetamaskErrorCodes: Record<string, number> = {
REJECTED: 4001,
UNRECOGNIZED: 4902,
}
22 changes: 18 additions & 4 deletions packages/evm/src/provider/rpc.ts
Original file line number Diff line number Diff line change
@@ -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`
Expand Down Expand Up @@ -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}`)
}
}
}

Expand All @@ -248,8 +256,14 @@ export class JsonRPCWallet extends BaseProviderWallet {
private async addNetwork(chainData: RpcType): Promise<void> {
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}`)
}
}
}
}
1 change: 0 additions & 1 deletion packages/superfluid/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b39983a

Please sign in to comment.