Skip to content

Commit

Permalink
more wc refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
brunobar79 committed Aug 29, 2024
1 parent 63aafb9 commit 443b545
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
2 changes: 1 addition & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ function App({ walletReady }: AppProps) {
}
identifyFlow();
eventSubscription.current = AppState.addEventListener('change', handleAppStateChange);
initWalletConnectListeners();

const p1 = analyticsV2.initializeRudderstack();
const p2 = setupDeeplinking();
const p3 = saveFCMToken();
Promise.all([p1, p2, p3]).then(() => {
initWalletConnectListeners();
PerformanceTracking.finishMeasuring(PerformanceMetrics.loadRootAppComponent);
analyticsV2.track(analyticsV2.event.applicationDidMount);
});
Expand Down
62 changes: 28 additions & 34 deletions src/walletConnect/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { formatJsonRpcResult, formatJsonRpcError } from '@json-rpc-tools/utils';
import { gretch } from 'gretchen';
import messaging from '@react-native-firebase/messaging';
import WalletConnectCore, { Core } from '@walletconnect/core';
import { Web3Wallet, Web3WalletTypes } from '@walletconnect/web3wallet';
import Client, { Web3Wallet, Web3WalletTypes } from '@walletconnect/web3wallet';
import { isHexString } from '@ethersproject/bytes';
import { toUtf8String } from '@ethersproject/strings';

Expand Down Expand Up @@ -94,54 +94,48 @@ export function maybeGoBackAndClearHasPendingRedirect({ delay = 0 }: { delay?: n
/**
* MAY BE UNDEFINED if WC v2 hasn't been instantiated yet
*/
let syncWeb3WalletClient: Awaited<ReturnType<(typeof Web3Wallet)['init']>> | undefined;

let lastConnector: string | undefined = undefined;

let walletConnectCore: WalletConnectCore | undefined;

let web3WalletClient: ReturnType<(typeof Web3Wallet)['init']> | undefined;

let initPromise: ReturnType<(typeof Web3Wallet)['init']> | null = null;
let initPromise: Promise<Client> | undefined = undefined;

let syncWeb3WalletClient: Client | undefined = undefined;

export const initializeWCv2 = async () => {
walletConnectCore = new Core({ projectId: WC_PROJECT_ID });

web3WalletClient = Web3Wallet.init({
core: walletConnectCore,
metadata: {
name: '🌈 Rainbow',
description: 'Rainbow makes exploring Ethereum fun and accessible 🌈',
url: 'https://rainbow.me',
icons: ['https://avatars2.githubusercontent.com/u/48327834?s=200&v=4'],
redirect: {
native: 'rainbow://wc',
universal: 'https://rnbwapp.com/wc',
if (!walletConnectCore) {
walletConnectCore = new Core({ projectId: WC_PROJECT_ID });
}

if (!web3WalletClient) {
// eslint-disable-next-line require-atomic-updates
web3WalletClient = Web3Wallet.init({
core: walletConnectCore,
metadata: {
name: '🌈 Rainbow',
description: 'Rainbow makes exploring Ethereum fun and accessible 🌈',
url: 'https://rainbow.me',
icons: ['https://avatars2.githubusercontent.com/u/48327834?s=200&v=4'],
redirect: {
native: 'rainbow://wc',
universal: 'https://rnbwapp.com/wc',
},
},
},
});
});
}

return web3WalletClient;
};

// this function ensures we only initialize the client once
export async function getWeb3WalletClient() {
if (!syncWeb3WalletClient) {
if (!initPromise) {
if (web3WalletClient) {
initPromise = web3WalletClient.then(client => {
syncWeb3WalletClient = client;
return client;
});
} else {
await initializeWCv2();
return getWeb3WalletClient();
}
}
// Wait for the initialization promise to resolve
return initPromise;
} else {
return syncWeb3WalletClient;
if (!initPromise) {
initPromise = initializeWCv2();
}

return initPromise;
}

/**
Expand Down

0 comments on commit 443b545

Please sign in to comment.