Skip to content

Commit

Permalink
πŸ§žβ€β™‚οΈ Fix disconnect wallet connect (#1018)
Browse files Browse the repository at this point in the history
  • Loading branch information
yivlad committed Nov 4, 2022
1 parent 94fe84f commit a8e4a94
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/tricky-cherries-study.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@usedapp/wallet-connect-connector': patch
'@usedapp/core': patch
---

Fix issue with not being able to open WalletConnect modal again after closing it
9 changes: 5 additions & 4 deletions packages/connectors/wallet-connect/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export class WalletConnectConnector implements Connector {
public readonly name = 'WalletConnect'

readonly update = new ConnectorEvent<ConnectorUpdateData>()
private walletConnectProvider: WalletConnectProvider | undefined

constructor(private opts: IWalletConnectProviderOptions) {}

private async init() {
if (this.provider) return
const walletConnectProvider = new WalletConnectProvider(this.opts)
this.provider = new providers.Web3Provider(walletConnectProvider)
await (this.provider?.provider as WalletConnectProvider).enable()
this.walletConnectProvider = new WalletConnectProvider(this.opts)
this.provider = new providers.Web3Provider(this.walletConnectProvider)
await this.walletConnectProvider.enable()
}

async connectEagerly(): Promise<void> {
Expand All @@ -44,6 +44,7 @@ export class WalletConnectConnector implements Connector {
}

async deactivate(): Promise<void> {
this.walletConnectProvider?.disconnect()
this.provider = undefined
}
}
Expand Down
22 changes: 18 additions & 4 deletions packages/core/src/providers/network/connectors/context.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ export interface ConnectorContextProviderProps {
children?: ReactNode
}

export interface ActivateOptions {
silently?: boolean
onSuccess?: () => void
}

export function ConnectorContextProvider({ children }: ConnectorContextProviderProps) {
const [controller, setController] = useState<ConnectorController>()
const [isLoading, setLoading] = useState(false)
Expand All @@ -46,7 +51,10 @@ export function ConnectorContextProvider({ children }: ConnectorContextProviderP
const [autoConnectTag, setAutoConnectTag] = useLocalStorage('usedapp:autoConnectTag')

const activate = useCallback(
async (providerOrConnector: JsonRpcProvider | ExternalProvider | Connector, silently = false) => {
async (
providerOrConnector: JsonRpcProvider | ExternalProvider | Connector,
{ silently, onSuccess }: ActivateOptions = { silently: false }
) => {
let controller: ConnectorController
if ('activate' in providerOrConnector) {
controller = new ConnectorController(providerOrConnector, config as any)
Expand All @@ -66,6 +74,7 @@ export function ConnectorContextProvider({ children }: ConnectorContextProviderP

setController(controller)
setLoading(false)
onSuccess?.()
} catch (error) {
controller.reportError(error as any)
} finally {
Expand All @@ -87,15 +96,20 @@ export function ConnectorContextProvider({ children }: ConnectorContextProviderP
if (!connectors[type]) {
throw new Error(`Connector ${type} is not configured`)
}
await activate(connectors[type])
setAutoConnectTag(type)
await activate(connectors[type], {
onSuccess: () => {
setAutoConnectTag(type)
},
})
},
[activate, setAutoConnectTag, connectors]
)

useEffect(() => {
if (autoConnect && autoConnectTag && connectors[autoConnectTag]) {
void activate(connectors[autoConnectTag], true)
void activate(connectors[autoConnectTag], {
silently: true,
})
}
}, [])

Expand Down

2 comments on commit a8e4a94

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸŽ‰ Published on https://example.usedapp.io as production
πŸš€ Deployed on https://63653550e654a900972a2a77--usedapp-example.netlify.app

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

πŸŽ‰ Published on https://usedapp.io as production
πŸš€ Deployed on https://6365355809a52c00602e386e--usedapp-website.netlify.app

Please sign in to comment.