From 44c70c71a73833ca36881ec84981b38deca663a8 Mon Sep 17 00:00:00 2001 From: Calvin Chang Date: Tue, 31 Oct 2023 12:59:41 +0800 Subject: [PATCH] refactor: enhance existedSDK check condition --- packages/blocto-sdk/src/providers/ethereum.ts | 33 +++++++++---------- 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/packages/blocto-sdk/src/providers/ethereum.ts b/packages/blocto-sdk/src/providers/ethereum.ts index e275d8c0..b8737bdf 100644 --- a/packages/blocto-sdk/src/providers/ethereum.ts +++ b/packages/blocto-sdk/src/providers/ethereum.ts @@ -174,11 +174,9 @@ export default class EthereumProvider } #checkNetworkMatched(): void { - const existedSDK = (window as any).ethereum; if ( - existedSDK && - existedSDK.isBlocto && - parseChainId(existedSDK.chainId) !== parseChainId(this.chainId) + this.existedSDK?.isBlocto && + parseChainId(this.existedSDK.chainId) !== parseChainId(this.chainId) ) { throw ethErrors.provider.chainDisconnected(); } @@ -314,13 +312,13 @@ export default class EthereumProvider const { blockchainName, switchableNetwork, sessionKey } = await this.#getBloctoProperties(); - const existedSDK = (window as any).ethereum; - if (existedSDK && existedSDK.isBlocto) { + + if (this.existedSDK?.isBlocto) { if (payload.method === 'wallet_switchEthereumChain') { if (!payload?.params?.[0]?.chainId) { throw ethErrors.rpc.invalidParams(); } - return existedSDK.request(payload).then(() => { + return this.existedSDK.request(payload).then(() => { this.networkVersion = `${parseChainId(payload?.params?.[0].chainId)}`; this.chainId = `0x${parseChainId( payload?.params?.[0].chainId @@ -329,7 +327,7 @@ export default class EthereumProvider return null; }); } - return existedSDK.request(payload); + return this.existedSDK.request(payload); } // method that doesn't require user to be connected @@ -541,22 +539,21 @@ export default class EthereumProvider const { walletServer, blockchainName, sessionKey } = await this.#getBloctoProperties(); - const existedSDK = (window as any).ethereum; - if (existedSDK && existedSDK.isBlocto) { - if (existedSDK.chainId !== this.chainId) { - await existedSDK.request({ + if (this.existedSDK?.isBlocto) { + if (this.existedSDK.chainId !== this.chainId) { + await this.existedSDK.request({ method: 'wallet_addEthereumChain', params: [{ chainId: this.chainId }], }); - await existedSDK.request({ + await this.existedSDK.request({ method: 'wallet_switchEthereumChain', params: [{ chainId: this.chainId }], }); - setEvmAddress(sessionKey, blockchainName, [existedSDK.address]); + setEvmAddress(sessionKey, blockchainName, [this.existedSDK.address]); } return new Promise((resolve, reject) => // add a small delay to make sure the network has been switched - setTimeout(() => existedSDK.enable().then(resolve).catch(reject), 10) + setTimeout(() => this.existedSDK.enable().then(resolve).catch(reject), 10) ); } @@ -876,9 +873,9 @@ export default class EthereumProvider } async handleDisconnect(): Promise { - const existedSDK = (window as any).ethereum; - if (existedSDK && existedSDK.isBlocto) { - return existedSDK.request({ method: 'wallet_disconnect' }); + + if (this.existedSDK?.isBlocto) { + return this.existedSDK.request({ method: 'wallet_disconnect' }); } const { sessionKey } = await this.#getBloctoProperties(); removeAllEvmAddress(sessionKey);