Skip to content

Commit

Permalink
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 056b159 commit f36a6b1
Show file tree
Hide file tree
Showing 9 changed files with 1,130 additions and 1,208 deletions.
2 changes: 1 addition & 1 deletion frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@metamask/onboarding": "^1.0.1",
"@vuelidate/core": "^2.0.0-alpha.43",
"@vuelidate/validators": "^2.0.0-alpha.31",
"@walletconnect/web3-provider": "^1.7.8",
"@walletconnect/ethereum-provider": "^2.3.2",
"autoprefixer": "^10",
"ethers": "^5.5.1",
"lodash.debounce": "^4.0.8",
Expand Down
1 change: 1 addition & 0 deletions frontend/src/components/WalletMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ const walletOptions = ref([
{
name: 'WalletConnect',
icon: new URL('../assets/images/walletconnect.svg', import.meta.url).href,
classes: 'w-16 h-16',
description: 'Connect using mobile wallet',
connect: connectWalletConnect,
connecting: connectingWalletConnect,
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/services/web3-provider/util-export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import { CoinbaseWalletSDK } from '@coinbase/wallet-sdk';
import detectEthereumProvider from '@metamask/detect-provider';
import MetaMaskOnboarding from '@metamask/onboarding';
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore TODO: Install & use type declarations once we migrate to WalletConnect v2
import WalletConnect from '@walletconnect/web3-provider/dist/umd/index.min.js';
import WalletConnect from '@walletconnect/ethereum-provider';

export { CoinbaseWalletSDK, detectEthereumProvider, MetaMaskOnboarding, WalletConnect };
31 changes: 22 additions & 9 deletions frontend/src/services/web3-provider/walletconnect-provider.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
import { hexValue } from 'ethers/lib/utils';

import { EthereumProvider } from '@/services/web3-provider/ethereum-provider';
import type { Eip1193Provider } from '@/services/web3-provider/types';
import type { Eip1193Provider } from '@/services/web3-provider';
import { EthereumProvider } from '@/services/web3-provider';
import { WalletConnect } from '@/services/web3-provider/util-export';

const BEAMER_PROJECT_ID = 'b0909ba73ce9e30c4decb50a963c9b2a';

export async function createWalletConnectProvider(rpcList: {
[chainId: string]: string;
}): Promise<WalletConnectProvider | undefined> {
const provider = new WalletConnect({
rpc: rpcList,
}) as typeof WalletConnect;
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
const modalClosed = new Promise((resolve) => {
provider.modal?.subscribeModal(async (newState) => {
if (!newState.open) {
resolve(undefined);
}
});
});

const enabled = provider.enable();

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

if (provider.connected) {
const walletConnectProvider = new WalletConnectProvider(
provider as unknown as Eip1193Provider,
);
const walletConnectProvider = new WalletConnectProvider(provider);
await walletConnectProvider.init();
return walletConnectProvider;
}
Expand Down
18 changes: 18 additions & 0 deletions frontend/tests/setup/window.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
WalletConnect's modal library uses the window object for detecting mobile devices
therefore a window mock is required globally
*/

Object.defineProperty(window, 'matchMedia', {
writable: true,
value: vi.fn().mockImplementation((query) => ({
matches: false,
media: query,
onchange: null,
addListener: vi.fn(), // deprecated
removeListener: vi.fn(), // deprecated
addEventListener: vi.fn(),
removeEventListener: vi.fn(),
dispatchEvent: vi.fn(),
})),
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import * as utils from '@/services/web3-provider/util-export';
import { generateChain, getRandomEthereumAddress } from '~/utils/data_generators';
import { MockedWalletConnectConnector, MockedWeb3Provider } from '~/utils/mocks/ethereum-provider';

vi.mock('@ethersproject/providers');
vi.mock('ethers/lib/utils');
vi.mock('@ethersproject/providers');
vi.mock('@/services/web3-provider/util-export');

function mockWeb3Provider(): MockedWeb3Provider {
Expand All @@ -21,9 +21,10 @@ function mockWeb3Provider(): MockedWeb3Provider {

function mockWalletConnectConnector(): MockedWalletConnectConnector {
const walletConnect = new MockedWalletConnectConnector();
walletConnect.init = vi.fn().mockImplementation(() => walletConnect);

Object.defineProperty(utils, 'WalletConnect', {
value: vi.fn().mockImplementation(() => walletConnect),
value: walletConnect,
});

return walletConnect;
Expand Down
2 changes: 1 addition & 1 deletion frontend/tests/utils/mocks/ethereum-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ export class MockedWalletConnectConnector extends MockedEip1193Provider {
}

enable = vi.fn();
init = vi.fn().mockImplementation(() => new MockedWalletConnectConnector());
}

export class MockedCoinbaseConnector extends MockedEip1193Provider {
constructor(public connected: boolean = false) {
super({ isCoinbase: true });
Expand Down
6 changes: 6 additions & 0 deletions frontend/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,18 @@ export default defineConfig({
'@': source_directory,
'~': test_directory,
config: config_directory,
/*
Fixes the misconfigured resolution (import resolves to a .cjs file) inside walletconnect's package.json
TODO: remove when they address this issue: https://github.com/WalletConnect/walletconnect-monorepo/issues/1943
*/
'@walletconnect/ethereum-provider': '@walletconnect/ethereum-provider/dist/index.umd.js',
},
},
test: {
globals: true,
reporters: ['default', 'junit'],
outputFile: path.resolve(test_output_directory, 'junit.xml'),
setupFiles: path.resolve(test_directory, 'setup', 'window.ts'),
mockReset: true,
environment: 'jsdom',
coverage: {
Expand Down
Loading

0 comments on commit f36a6b1

Please sign in to comment.