diff --git a/packages/safe-apps-web3modal/.gitignore b/packages/safe-apps-web3modal/.gitignore deleted file mode 100644 index 8a77bdf9..00000000 --- a/packages/safe-apps-web3modal/.gitignore +++ /dev/null @@ -1,7 +0,0 @@ -node_modules -dist -.DS_Store -.idea -.vscode -lerna-debug.log -yalc.lock \ No newline at end of file diff --git a/packages/safe-apps-web3modal/dist/index.d.ts b/packages/safe-apps-web3modal/dist/index.d.ts new file mode 100644 index 00000000..b1c788b9 --- /dev/null +++ b/packages/safe-apps-web3modal/dist/index.d.ts @@ -0,0 +1 @@ +export { SafeAppWeb3Modal } from './modal'; diff --git a/packages/safe-apps-web3modal/dist/index.js b/packages/safe-apps-web3modal/dist/index.js new file mode 100644 index 00000000..15a5ac5f --- /dev/null +++ b/packages/safe-apps-web3modal/dist/index.js @@ -0,0 +1,6 @@ +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.SafeAppWeb3Modal = void 0; +var modal_1 = require("./modal"); +Object.defineProperty(exports, "SafeAppWeb3Modal", { enumerable: true, get: function () { return modal_1.SafeAppWeb3Modal; } }); +//# sourceMappingURL=index.js.map \ No newline at end of file diff --git a/packages/safe-apps-web3modal/dist/index.js.map b/packages/safe-apps-web3modal/dist/index.js.map new file mode 100644 index 00000000..b7afb4fb --- /dev/null +++ b/packages/safe-apps-web3modal/dist/index.js.map @@ -0,0 +1 @@ +{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAAA,iCAA2C;AAAlC,yGAAA,gBAAgB,OAAA"} \ No newline at end of file diff --git a/packages/safe-apps-web3modal/dist/modal.d.ts b/packages/safe-apps-web3modal/dist/modal.d.ts new file mode 100644 index 00000000..13b6b713 --- /dev/null +++ b/packages/safe-apps-web3modal/dist/modal.d.ts @@ -0,0 +1,14 @@ +import SafeAppsSDK from '@gnosis.pm/safe-apps-sdk'; +import { SafeAppProvider } from '@gnosis.pm/safe-apps-provider'; +import Web3Modal, { ICoreOptions } from 'web3modal'; +export declare class SafeAppWeb3Modal extends Web3Modal { + private sdk; + private safe?; + private provider?; + private triedToConnect; + constructor(options?: Partial, sdk?: SafeAppsSDK); + private getConnectedSafe; + getProvider(): Promise; + requestProvider: () => Promise; + isSafeApp(): Promise; +} diff --git a/packages/safe-apps-web3modal/dist/modal.js b/packages/safe-apps-web3modal/dist/modal.js new file mode 100644 index 00000000..e546a370 --- /dev/null +++ b/packages/safe-apps-web3modal/dist/modal.js @@ -0,0 +1,48 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.SafeAppWeb3Modal = void 0; +const safe_apps_sdk_1 = __importDefault(require("@gnosis.pm/safe-apps-sdk")); +const safe_apps_provider_1 = require("@gnosis.pm/safe-apps-provider"); +const web3modal_1 = __importDefault(require("web3modal")); +class SafeAppWeb3Modal extends web3modal_1.default { + constructor(options, sdk) { + super(options); + this.triedToConnect = false; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + this.requestProvider = async () => { + if (await this.isSafeApp()) { + return this.getProvider(); + } + return this.connect(); + }; + this.sdk = sdk || new safe_apps_sdk_1.default(); + } + async getConnectedSafe() { + if (!this.safe && !this.triedToConnect) { + this.safe = await Promise.race([ + this.sdk.getSafeInfo(), + new Promise((resolve) => setTimeout(resolve, 200)), + ]); + this.triedToConnect = true; + } + return this.safe; + } + async getProvider() { + if (!this.provider) { + const safe = await this.getConnectedSafe(); + if (!safe) + throw Error('Could not load Safe information'); + this.provider = new safe_apps_provider_1.SafeAppProvider(safe, this.sdk); + } + return this.provider; + } + async isSafeApp() { + const safe = await this.getConnectedSafe(); + return !!safe; + } +} +exports.SafeAppWeb3Modal = SafeAppWeb3Modal; +//# sourceMappingURL=modal.js.map \ No newline at end of file diff --git a/packages/safe-apps-web3modal/dist/modal.js.map b/packages/safe-apps-web3modal/dist/modal.js.map new file mode 100644 index 00000000..f479c51a --- /dev/null +++ b/packages/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AAI,oCAAe,CAAC,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC;SACrD;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAUM,KAAK,CAAC,SAAS;QACpB,MAAM,IAAI,GAAG,MAAM,IAAI,CAAC,gBAAgB,EAAE,CAAC;QAE3C,OAAO,CAAC,CAAC,IAAI,CAAC;IAChB,CAAC;CACF;AA7CD,4CA6CC"} \ No newline at end of file diff --git a/packages/safe-apps-web3modal/src/modal.ts b/packages/safe-apps-web3modal/src/modal.ts index ab6f7099..79461425 100644 --- a/packages/safe-apps-web3modal/src/modal.ts +++ b/packages/safe-apps-web3modal/src/modal.ts @@ -4,31 +4,30 @@ import Web3Modal, { ICoreOptions } from 'web3modal'; export class SafeAppWeb3Modal extends Web3Modal { private sdk: SafeAppsSDK; - private cachedSafeInfo: SafeInfo | undefined; - private provider: SafeAppProvider | undefined; + private safe?: SafeInfo; + private provider?: SafeAppProvider; + private triedToConnect = false; constructor(options?: Partial, sdk?: SafeAppsSDK) { super(options); this.sdk = sdk || new SafeAppsSDK(); - this.safeInfo(); } - public async safeInfo(): Promise { - if (!this.cachedSafeInfo) - try { - this.cachedSafeInfo = await Promise.race([ - this.sdk.getSafeInfo(), - new Promise((_, reject) => setTimeout(() => reject(), 100)), - ]); - } catch (e) { - this.cachedSafeInfo = undefined; - } - return this.cachedSafeInfo; + private async getConnectedSafe(): Promise { + if (!this.safe && !this.triedToConnect) { + this.safe = await Promise.race([ + this.sdk.getSafeInfo(), + new Promise((resolve) => setTimeout(resolve, 200)), + ]); + this.triedToConnect = true; + } + + return this.safe; } - async getOrCreateProvider(): Promise { + async getProvider(): Promise { if (!this.provider) { - const safe = await this.safeInfo(); + const safe = await this.getConnectedSafe(); if (!safe) throw Error('Could not load Safe information'); this.provider = new SafeAppProvider(safe, this.sdk); } @@ -37,13 +36,15 @@ export class SafeAppWeb3Modal extends Web3Modal { // eslint-disable-next-line @typescript-eslint/no-explicit-any public requestProvider = async (): Promise => { - if (await this.safeInfo()) { - return this.getOrCreateProvider(); + if (await this.isSafeApp()) { + return this.getProvider(); } return this.connect(); }; - public async canAutoConnect(): Promise { - return (await this.safeInfo()) !== undefined || !!this.cachedProvider; + public async isSafeApp(): Promise { + const safe = await this.getConnectedSafe(); + + return !!safe; } }