-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: bring back walletconnect v1 (#773)
* feat: bring back wc1 * chore: set version to latest npm, we will bump on next release * feat: add wc side by side example * chore: add wc1 type * fix * fix * Update example/components/connectorCards/WalletConnectV2Card.tsx Co-authored-by: Zach Pomerantz <[email protected]> * fix: wc * fix types --------- Co-authored-by: Zach Pomerantz <[email protected]>
- Loading branch information
Showing
20 changed files
with
982 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { URI_AVAILABLE } from '@web3-react/walletconnect-v2' | ||
import { useEffect, useState } from 'react' | ||
|
||
import { MAINNET_CHAINS } from '../../chains' | ||
import { hooks, walletConnectV2 } from '../../connectors/walletConnectV2' | ||
import { Card } from '../Card' | ||
|
||
const CHAIN_IDS = Object.keys(MAINNET_CHAINS).map(Number) | ||
|
||
const { useChainId, useAccounts, useIsActivating, useIsActive, useProvider, useENSNames } = hooks | ||
|
||
export default function WalletConnectV2Card() { | ||
const chainId = useChainId() | ||
const accounts = useAccounts() | ||
const isActivating = useIsActivating() | ||
|
||
const isActive = useIsActive() | ||
|
||
const provider = useProvider() | ||
const ENSNames = useENSNames(provider) | ||
|
||
const [error, setError] = useState(undefined) | ||
|
||
// log URI when available | ||
useEffect(() => { | ||
walletConnectV2.events.on(URI_AVAILABLE, (uri: string) => { | ||
console.log(`uri: ${uri}`) | ||
}) | ||
}, []) | ||
|
||
// attempt to connect eagerly on mount | ||
useEffect(() => { | ||
walletConnectV2.connectEagerly().catch((error) => { | ||
console.debug('Failed to connect eagerly to walletconnect', error) | ||
}) | ||
}, []) | ||
|
||
return ( | ||
<Card | ||
connector={walletConnectV2} | ||
activeChainId={chainId} | ||
chainIds={CHAIN_IDS} | ||
isActivating={isActivating} | ||
isActive={isActive} | ||
error={error} | ||
setError={setError} | ||
accounts={accounts} | ||
provider={provider} | ||
ENSNames={ENSNames} | ||
/> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,14 @@ | ||
import { initializeConnector } from '@web3-react/core' | ||
import { WalletConnect as WalletConnectV2 } from '@web3-react/walletconnect-v2' | ||
import { WalletConnect } from '@web3-react/walletconnect' | ||
|
||
import { MAINNET_CHAINS } from '../chains' | ||
import { URLS } from '../chains' | ||
|
||
export const [walletConnectV2, hooks] = initializeConnector<WalletConnectV2>( | ||
export const [walletConnect, hooks] = initializeConnector<WalletConnect>( | ||
(actions) => | ||
new WalletConnectV2({ | ||
new WalletConnect({ | ||
actions, | ||
options: { | ||
projectId: process.env.walletConnectProjectId, | ||
chains: Object.keys(MAINNET_CHAINS).map(Number), | ||
rpc: URLS, | ||
}, | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { initializeConnector } from '@web3-react/core' | ||
import { WalletConnect as WalletConnectV2 } from '@web3-react/walletconnect-v2' | ||
|
||
import { MAINNET_CHAINS } from '../chains' | ||
|
||
export const [walletConnectV2, hooks] = initializeConnector<WalletConnectV2>( | ||
(actions) => | ||
new WalletConnectV2({ | ||
actions, | ||
options: { | ||
projectId: process.env.walletConnectProjectId, | ||
chains: Object.keys(MAINNET_CHAINS).map(Number), | ||
}, | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# @web3-react/walletconnect |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
{ | ||
"name": "@web3-react/walletconnect", | ||
"keywords": [ | ||
"web3-react", | ||
"walletconnect" | ||
], | ||
"author": "Noah Zinsmeister <[email protected]>", | ||
"license": "GPL-3.0-or-later", | ||
"repository": "github:Uniswap/web3-react", | ||
"publishConfig": { | ||
"access": "public" | ||
}, | ||
"version": "8.1.0-beta.0", | ||
"files": [ | ||
"dist/*" | ||
], | ||
"type": "commonjs", | ||
"types": "./dist/index.d.ts", | ||
"main": "./dist/index.js", | ||
"exports": "./dist/index.js", | ||
"scripts": { | ||
"prebuild": "rm -rf dist", | ||
"build": "tsc", | ||
"start": "tsc --watch" | ||
}, | ||
"dependencies": { | ||
"@web3-react/types": "^8.1.0-beta.0", | ||
"eventemitter3": "^4.0.7", | ||
"@walletconnect/ethereum-provider": "^1.7.8" | ||
}, | ||
"devDependencies": { | ||
"@web3-react/store": "^8.1.0-beta.0", | ||
"@walletconnect/types": "^1.7.8" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
import { createWeb3ReactStoreAndActions } from '@web3-react/store' | ||
import type { Actions, RequestArguments, Web3ReactStore } from '@web3-react/types' | ||
import EventEmitter from 'node:events' | ||
import { WalletConnect } from '.' | ||
import { MockEIP1193Provider } from '../../eip1193/src/mock' | ||
|
||
// necessary because walletconnect returns chainId as a number | ||
class MockMockWalletConnectProvider extends MockEIP1193Provider { | ||
public connector = new EventEmitter() | ||
|
||
public eth_chainId_number = jest.fn((chainId?: string) => | ||
chainId === undefined ? chainId : Number.parseInt(chainId, 16) | ||
) | ||
|
||
public request(x: RequestArguments): Promise<unknown> { | ||
if (x.method === 'eth_chainId') { | ||
return Promise.resolve(this.eth_chainId_number(this.chainId)) | ||
} else { | ||
return super.request(x) | ||
} | ||
} | ||
} | ||
|
||
jest.mock('@walletconnect/ethereum-provider', () => MockMockWalletConnectProvider) | ||
|
||
const chainId = '0x1' | ||
const accounts: string[] = [] | ||
|
||
describe('WalletConnect', () => { | ||
let store: Web3ReactStore | ||
let connector: WalletConnect | ||
let mockProvider: MockMockWalletConnectProvider | ||
|
||
describe('works', () => { | ||
beforeEach(async () => { | ||
let actions: Actions | ||
;[store, actions] = createWeb3ReactStoreAndActions() | ||
connector = new WalletConnect({ actions, options: { rpc: {} } }) | ||
}) | ||
|
||
test('#activate', async () => { | ||
await connector.connectEagerly().catch(() => {}) | ||
|
||
mockProvider = connector.provider as unknown as MockMockWalletConnectProvider | ||
mockProvider.chainId = chainId | ||
mockProvider.accounts = accounts | ||
|
||
await connector.activate() | ||
|
||
expect(mockProvider.eth_requestAccounts).toHaveBeenCalled() | ||
expect(mockProvider.eth_accounts).not.toHaveBeenCalled() | ||
expect(mockProvider.eth_chainId_number).toHaveBeenCalled() | ||
expect(mockProvider.eth_chainId_number.mock.invocationCallOrder[0]) | ||
.toBeGreaterThan(mockProvider.eth_requestAccounts.mock.invocationCallOrder[0]) | ||
|
||
expect(store.getState()).toEqual({ | ||
chainId: Number.parseInt(chainId, 16), | ||
accounts, | ||
activating: false, | ||
error: undefined, | ||
}) | ||
}) | ||
}) | ||
}) |
Oops, something went wrong.