Skip to content

Commit

Permalink
Throws error if users closes QR Modal without connecting.
Browse files Browse the repository at this point in the history
  • Loading branch information
ggaabe committed Apr 30, 2023
1 parent d2bdbb8 commit f5e0abc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 1 deletion.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ init(
The connect function can be used to connect a wallet to a dApp. The wallet
chosen needs to be configured in the `init` function above.

With the WalletConnect connector, if the user closes the QR modal without
scanning the QR code, the `connect` function will throw an error. It is important
to catch this error and handle it appropriately to ensure your application does not hang.

```ts
import { connect, getActiveConnector } from 'adalib'

Expand Down Expand Up @@ -111,7 +115,9 @@ switchConnector(FlintConnector.connectorName)
const flintWalletAPI = await connect()
```


Note: Sometimes the connection will die and you will need to reconnect. You will
need to manually call `disconnect` on the connector before calling `connect` again.
This helps ensure that a fresh connection state is created.

<!-- # Folders
-->
Expand Down
3 changes: 3 additions & 0 deletions adalib-example/src/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ function Home() {
console.log('CIP-30 API Created', { api });

setEnabledAPI(api);
})
.catch(e => {
console.log('Error enabling connector', e);
});
}, [setEnabledAPI]);

Expand Down
8 changes: 8 additions & 0 deletions src/connectors/walletconnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,13 +181,19 @@ export class WalletConnectConnector implements Connector {
// WC concept
const provider = await UniversalProviderFactory.getProvider();
this.provider = provider;
// let isConnected = false;

return new Promise<string>((resolve, reject) => {
provider.on('display_uri', (uri: string) => {
if (this.qrcode) {
const ModalCtrl = createW3mModalCtrl([chainID]);

ModalCtrl.openModal({ uri, standaloneChains: [chainID] });
ModalCtrl.subscribeModal(newState => {
if (!this.enabled && !newState.open) {
reject(new Error('User closed modal before connecting'));
}
});
} else resolve(uri);
});

Expand All @@ -206,6 +212,8 @@ export class WalletConnectConnector implements Connector {
setAddress(address);

const ModalCtrl = createW3mModalCtrl();

this.enabled = true;
ModalCtrl.closeModal();

resolve(address);
Expand Down

0 comments on commit f5e0abc

Please sign in to comment.