Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add walletconnect support #20

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"react-useportal": "^1.0.14",
"styled-components": "^5.3.1",
"use-nft": "^0.10.1",
"use-wallet": "^0.12.3"
"use-wallet": "^0.13.1"
},
"devDependencies": {
"@types/react": "^17.0.0",
Expand Down
Binary file added src/assets/images/walletconnect.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/constants/networks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const SUPPORTED_NETWORKS = {
mainnet: 1,
matic: 137,
fantom: 250,
arbitrum: 42161,
ropsten: 3,
rinkeby: 4,
goerli: 5,
mumbai: 80001,
};
34 changes: 25 additions & 9 deletions src/constants/rpc.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
if (!import.meta.env.REACT_APP_ETHEREUM_RPC_URL) {
throw new Error('must set REACT_APP_ETHEREUM_RPC_URL environment variable');
}

if (!import.meta.env.REACT_APP_RINKEBY_RPC_URL) {
throw new Error('must set REACT_APP_RINKEBY_RPC_URL environment variable');
}
import { SUPPORTED_NETWORKS } from './networks';

export const RPC_URLS: Record<string, string> = {
1: String(import.meta.env.REACT_APP_ETHEREUM_RPC_URL ?? ''),
4: String(import.meta.env.REACT_APP_RINKEBY_RPC_URL ?? ''),
[SUPPORTED_NETWORKS.mainnet]: String(
import.meta.env.REACT_APP_ETHEREUM_RPC_URL ?? ''
),
[SUPPORTED_NETWORKS.matic]: String(
import.meta.env.REACT_APP_POLYGON_RPC_URL ?? ''
),
[SUPPORTED_NETWORKS.fantom]: String(
import.meta.env.REACT_APP_FANTOM_RPC_URL ?? ''
),
[SUPPORTED_NETWORKS.arbitrum]: String(
import.meta.env.REACT_APP_ARBITRUM_RPC_URL ?? ''
),
[SUPPORTED_NETWORKS.rinkeby]: String(
import.meta.env.REACT_APP_RINKEBY_RPC_URL ?? ''
),
[SUPPORTED_NETWORKS.ropsten]: String(
import.meta.env.REACT_APP_ROPSTEN_RPC_URL ?? ''
),
[SUPPORTED_NETWORKS.goerli]: String(
import.meta.env.REACT_APP_GOERLI_RPC_URL ?? ''
),
[SUPPORTED_NETWORKS.mumbai]: String(
import.meta.env.REACT_APP_MUMBAI_RPC_URL ?? ''
),
};
8 changes: 8 additions & 0 deletions src/constants/wallets.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import metaMaskIconUrl from '../assets/images/metamask.png';
import walletConnectIconUrl from '../assets/images/walletconnect.png';

interface WalletInfo {
name: string;
Expand All @@ -14,4 +15,11 @@ export const SUPPORTED_WALLETS: Record<string, WalletInfo> = {
iconURL: metaMaskIconUrl,
description: 'Easy-to-use browser extension.',
},
walletconnect: {
name: 'WalletConnect',
connector: 'walletconnect',
iconURL: walletConnectIconUrl,
description:
'Connect to mobile wallets with QR code scanning or deep linking.',
},
};
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { StrictMode } from 'react';
import { render } from 'react-dom';
import App from './App';
import './polyfills';

render(
<StrictMode>
Expand Down
9 changes: 9 additions & 0 deletions src/polyfills.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { Buffer } from 'buffer';

if (window.global === undefined) {
window.global = window;
}

if (window.Buffer === undefined) {
window.Buffer = Buffer;
}
11 changes: 10 additions & 1 deletion src/providers/WalletProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { UseWalletProvider } from 'use-wallet';
import { RPC_URLS } from '../constants/rpc';

interface Props {
children: React.ReactNode;
}

export default ({ children }: Props) => (
<UseWalletProvider>{children}</UseWalletProvider>
<UseWalletProvider
connectors={{
walletconnect: {
rpc: RPC_URLS,
Copy link
Member Author

Choose a reason for hiding this comment

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

This is the correct configuration, but differs from what's defined in the use-wallet documentation -- will open a PR against the repo soon to resolve that.

},
}}
>
{children}
</UseWalletProvider>
);
3 changes: 3 additions & 0 deletions vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ export default defineConfig({
sourcemap: true,
manifest: true,
},
define: {
'process.env': process.env,
Copy link
Member Author

Choose a reason for hiding this comment

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

@therealparmesh Lemme know if there's a more preferable way to do this in vite-land.

Copy link
Contributor

@therealparmesh therealparmesh Dec 17, 2021

Choose a reason for hiding this comment

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

@zakangelle

I believe Vite already provides these variables within import.meta: https://vitejs.dev/guide/env-and-mode.html#env-variables

FWIW, this seems to be closer to browser standards than process.env: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import.meta

Also, here are some usages of import.meta in this repository.

Copy link
Member Author

Choose a reason for hiding this comment

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

The tricky part is we're using a library (as a transitive dependency) which references process.env assuming its variables are string replaced at build-time (which isn't the case w/ Vite), so we need to inject an empty process.env object to unbreak a runtime error thrown in that library. Not sure if there's a better way to do that though.

This section of the library README alludes to a similar issue which happens with Angular 11 builds.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah I see what you mean! Then yeah, this is the best way to go.

Copy link
Contributor

Choose a reason for hiding this comment

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

Doh! I should've read the PR description first.

As a side note: Those last 2 points are definitely worth fixing in that library.

},
});
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -9796,10 +9796,10 @@ use-ssr@^1.0.22:
resolved "https://registry.yarnpkg.com/use-ssr/-/use-ssr-1.0.24.tgz#213a3df58f5ab9268e6fe1a57ad0a9de91e514d1"
integrity sha512-0MFps7ezL57/3o0yl4CvrHLlp9z20n1rQZV/lSRz7if+TUoM6POU1XdOvEjIgjgKeIhTEye1U0khrIYWCTWw4g==

use-wallet@^0.12.3:
version "0.12.3"
resolved "https://registry.yarnpkg.com/use-wallet/-/use-wallet-0.12.3.tgz#7f5fda322414be30283255207ae4a75f7b4509f0"
integrity sha512-36Z4gXv8SmiJNEmkAJmSWi0HneYYBQP39RQDgQUxvm9OJmq+KPait+8NiJgLCkkxScrdDme8vCZ3OKyynVDZOA==
use-wallet@^0.13.1:
version "0.13.1"
resolved "https://registry.yarnpkg.com/use-wallet/-/use-wallet-0.13.1.tgz#5239d5163c235681c0440557ceb238290bc2e757"
integrity sha512-BMjKTOSIdhH/ouPTwNoVIWMIdLHK8rPdn8ERcqpV6ZcUvXni8eC2pvD2Ntk3ym6WPEmCI/xrv9hX849ak+aHug==
dependencies:
"@aragon/provided-connector" "^6.0.8"
"@typescript-eslint/parser" "^4.1.0"
Expand Down