Skip to content

Commit

Permalink
fixup! frontend: integrate wallet connect v2
Browse files Browse the repository at this point in the history
  • Loading branch information
GabrielBuragev committed Feb 20, 2023
1 parent 31a040b commit 9a77c7c
Showing 1 changed file with 24 additions and 30 deletions.
54 changes: 24 additions & 30 deletions frontend/src/services/web3-provider/walletconnect-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,38 +9,32 @@ const BEAMER_PROJECT_ID = 'b0909ba73ce9e30c4decb50a963c9b2a';
export async function createWalletConnectProvider(rpcList: {
[chainId: string]: string;
}): Promise<WalletConnectProvider | undefined> {
/*
Due to a bug in @walletconnect/ethereum-provider (https://github.com/WalletConnect/walletconnect-monorepo/issues/1957)
we are wrapping the code in a promise with a self-invokable async function
so we can shortcut the lack of resolution in the `provider.enable()` promise
*/

return new Promise((resolve) => {
(async () => {
const provider = await WalletConnect.init({
chains: Object.keys(rpcList).map((chainId) => parseInt(chainId)),
projectId: BEAMER_PROJECT_ID,
rpcMap: rpcList,
});

// Fixes the lack of promise resolution in `provider.enable()` when modal is closed
provider.modal?.subscribeModal(async (newState) => {
if (!newState.open && !provider.session) {
resolve(undefined);
}
});

await provider.enable();

if (provider.connected) {
const walletConnectProvider = new WalletConnectProvider(provider);
await walletConnectProvider.init();
return resolve(walletConnectProvider);
}
const provider = await WalletConnect.init({
chains: Object.keys(rpcList).map((chainId) => parseInt(chainId)),
projectId: BEAMER_PROJECT_ID,
rpcMap: rpcList,
});

resolve(undefined);
})();
// Fixes the lack of promise resolution in `provider.enable()` when modal is closed
const modalClosed = new Promise((resolve) => {
provider.modal?.subscribeModal(async (newState) => {
if (!newState.open) {
resolve(undefined);
}
});
});

const enabled = provider.enable();

await Promise.race([modalClosed, enabled]);

if (provider.connected) {
const walletConnectProvider = new WalletConnectProvider(provider);
await walletConnectProvider.init();
return walletConnectProvider;
}

return undefined;
}

export class WalletConnectProvider extends EthereumProvider {
Expand Down

0 comments on commit 9a77c7c

Please sign in to comment.