-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5281d0a
commit 3becf08
Showing
10 changed files
with
756 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
packages/example-next/components/connectors/GnosisSafeCard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { useEffect } from 'react' | ||
import { gnosisSafe, hooks } from '../../connectors/gnosisSafe' | ||
import { Accounts } from '../Accounts' | ||
import { Card } from '../Card' | ||
import { Chain } from '../Chain' | ||
import { ConnectWithSelect } from '../ConnectWithSelect' | ||
import { Status } from '../Status' | ||
|
||
const { useChainId, useAccounts, useError, useIsActivating, useIsActive, useProvider, useENSNames } = hooks | ||
|
||
export default function GnosisSafeCard() { | ||
const chainId = useChainId() | ||
const accounts = useAccounts() | ||
const error = useError() | ||
const isActivating = useIsActivating() | ||
|
||
const isActive = useIsActive() | ||
|
||
const provider = useProvider() | ||
const ENSNames = useENSNames(provider) | ||
|
||
// attempt to connect eagerly on mount | ||
useEffect(() => { | ||
void gnosisSafe.connectEagerly() | ||
}, []) | ||
|
||
return ( | ||
<Card> | ||
<div> | ||
<b>Gnosis Safe</b> | ||
<Status isActivating={isActivating} error={error} isActive={isActive} /> | ||
<div style={{ marginBottom: '1rem' }} /> | ||
<Chain chainId={chainId} /> | ||
<Accounts accounts={accounts} provider={provider} ENSNames={ENSNames} /> | ||
</div> | ||
<div style={{ marginBottom: '1rem' }} /> | ||
<ConnectWithSelect | ||
connector={gnosisSafe} | ||
chainId={chainId} | ||
isActivating={isActivating} | ||
error={error} | ||
isActive={isActive} | ||
/> | ||
</Card> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { initializeConnector } from '@web3-react/core' | ||
import { GnosisSafe } from '@web3-react/gnosis-safe' | ||
|
||
export const [gnosisSafe, hooks] = initializeConnector<GnosisSafe>((actions) => new GnosisSafe(actions)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @web3-react/gnosis-safe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"name": "@web3-react/gnosis-safe", | ||
"keywords": [ | ||
"web3-react", | ||
"gnosis-safe" | ||
], | ||
"author": "Noah Zinsmeister <[email protected]>", | ||
"license": "GPL-3.0-or-later", | ||
"repository": "github:NoahZinsmeister/web3-react", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"version": "8.0.0-beta.0", | ||
"files": [ | ||
"dist/*" | ||
], | ||
"type": "commonjs", | ||
"types": "./dist/index.d.ts", | ||
"main": "./dist/index.js", | ||
"exports": "./dist/index.js", | ||
"scripts": { | ||
"prebuild": "rm -rf dist", | ||
"build": "tsc", | ||
"start": "tsc --watch" | ||
}, | ||
"dependencies": { | ||
"@gnosis.pm/safe-apps-provider": "^0.11.0", | ||
"@gnosis.pm/safe-apps-sdk": "^7.3.0", | ||
"@web3-react/types": "^8.0.13-beta.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
import type { SafeAppProvider } from '@gnosis.pm/safe-apps-provider' | ||
import type SafeAppsSDK from '@gnosis.pm/safe-apps-sdk' | ||
import type { Opts } from '@gnosis.pm/safe-apps-sdk' | ||
import type { Actions } from '@web3-react/types' | ||
import { Connector } from '@web3-react/types' | ||
|
||
export class NoSafeContext extends Error { | ||
public constructor() { | ||
super('The app is loaded outside safe context') | ||
this.name = NoSafeContext.name | ||
Object.setPrototypeOf(this, NoSafeContext.prototype) | ||
} | ||
} | ||
|
||
export class GnosisSafe extends Connector { | ||
/** {@inheritdoc Connector.provider} */ | ||
public provider: SafeAppProvider | undefined | ||
|
||
private readonly options?: Opts | ||
private eagerConnection?: Promise<void> | ||
|
||
/** | ||
* A `SafeAppsSDK` instance. | ||
*/ | ||
public sdk: SafeAppsSDK | undefined | ||
|
||
/** | ||
* @param connectEagerly - A flag indicating whether connection should be initiated when the class is constructed. | ||
*/ | ||
constructor(actions: Actions, connectEagerly = false, options?: Opts) { | ||
super(actions) | ||
|
||
if (connectEagerly && typeof window === 'undefined') { | ||
throw new Error('connectEagerly = true is invalid for SSR, instead use the connectEagerly method in a useEffect') | ||
} | ||
|
||
this.options = options | ||
|
||
if (connectEagerly) void this.connectEagerly() | ||
} | ||
|
||
// check if we're in an iframe | ||
private get inIframe() { | ||
if (typeof window === 'undefined') return false | ||
if (window !== window.parent) return true | ||
return false | ||
} | ||
|
||
private async isomorphicInitialize(): Promise<void> { | ||
if (this.eagerConnection) return this.eagerConnection | ||
|
||
// kick off import early to minimize waterfalls | ||
const SafeAppProviderPromise = import('@gnosis.pm/safe-apps-provider').then( | ||
({ SafeAppProvider }) => SafeAppProvider | ||
) | ||
|
||
await (this.eagerConnection = import('@gnosis.pm/safe-apps-sdk').then(async (m) => { | ||
this.sdk = new m.default(this.options) | ||
|
||
const safe = await Promise.race([ | ||
this.sdk.safe.getInfo(), | ||
new Promise<undefined>((resolve) => setTimeout(resolve, 500)), | ||
]) | ||
|
||
if (safe) { | ||
const SafeAppProvider = await SafeAppProviderPromise | ||
this.provider = new SafeAppProvider(safe, this.sdk) | ||
} | ||
})) | ||
} | ||
|
||
/** {@inheritdoc Connector.connectEagerly} */ | ||
public async connectEagerly(): Promise<void> { | ||
if (!this.inIframe) return | ||
|
||
const cancelActivation = this.actions.startActivation() | ||
|
||
await this.isomorphicInitialize() | ||
if (!this.provider) return cancelActivation() | ||
|
||
try { | ||
this.actions.update({ | ||
chainId: this.provider.chainId, | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
accounts: [await this.sdk!.safe.getInfo().then(({ safeAddress }) => safeAddress)], | ||
}) | ||
} catch (error) { | ||
console.debug('Could not connect eagerly', error) | ||
cancelActivation() | ||
} | ||
} | ||
|
||
public async activate(): Promise<void> { | ||
if (!this.inIframe) return this.actions.reportError(new NoSafeContext()) | ||
|
||
// only show activation if this is a first-time connection | ||
if (!this.sdk) this.actions.startActivation() | ||
|
||
await this.isomorphicInitialize() | ||
if (!this.provider) return this.actions.reportError(new NoSafeContext()) | ||
|
||
try { | ||
this.actions.update({ | ||
chainId: this.provider.chainId, | ||
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | ||
accounts: [await this.sdk!.safe.getInfo().then(({ safeAddress }) => safeAddress)], | ||
}) | ||
} catch (error) { | ||
this.actions.reportError(error as Error | undefined) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"extends": "../../tsconfig.json", | ||
"include": ["./src"], | ||
"compilerOptions": { | ||
"outDir": "./dist" | ||
} | ||
} |
Oops, something went wrong.