Skip to content

Commit

Permalink
refactor: enhance existedSDK check condition
Browse files Browse the repository at this point in the history
  • Loading branch information
q20274982 committed Oct 31, 2023
1 parent 484d367 commit e8155e5
Showing 1 changed file with 15 additions and 18 deletions.
33 changes: 15 additions & 18 deletions packages/blocto-sdk/src/providers/ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -538,22 +536,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)
);
}

Expand Down Expand Up @@ -877,9 +874,9 @@ export default class EthereumProvider
}

async handleDisconnect(): Promise<void> {
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);
Expand Down

0 comments on commit e8155e5

Please sign in to comment.