Skip to content

Commit

Permalink
👛 Add wallet-connect example (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaretskyi authored Mar 10, 2022
1 parent ad021b0 commit e9907a5
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/shiny-planes-sin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@usedapp/core': minor
'@usedapp/example': patch
---

Add support for wallet-connect provider
9 changes: 5 additions & 4 deletions packages/core/src/hooks/useEthers.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import { useCallback } from 'react'
import { JsonRpcProvider } from '@ethersproject/providers'
import { ExternalProvider, JsonRpcProvider } from '@ethersproject/providers'
import { useInjectedNetwork, useNetwork } from '../providers'
import { EventEmitter } from 'events'
import { useLocalStorage } from './useLocalStorage'

type MaybePromise<T> = Promise<T> | any

type SupportedProviders =
| JsonRpcProvider
| EventEmitter
| { getProvider: () => JsonRpcProvider | EventEmitter; activate: () => Promise<void> }
| ExternalProvider
| { getProvider: () => MaybePromise<JsonRpcProvider | ExternalProvider>; activate: () => Promise<any> }

export type Web3Ethers = {
activate: (provider: SupportedProviders) => Promise<void>
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/providers/network/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { EventEmitter } from 'events'
export const NetworkContext = createContext<{
update: (network: Partial<Network>) => void
reportError: (error: Error) => void
activate: (provider: JsonRpcProvider | (EventEmitter & ExternalProvider)) => Promise<void>
activate: (provider: JsonRpcProvider | ExternalProvider) => Promise<void>
deactivate: () => void
network: Network
}>({
Expand Down
3 changes: 1 addition & 2 deletions packages/core/src/providers/network/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { NetworkContext } from './context'
import { defaultNetworkState, networksReducer } from './reducer'
import { Network } from './model'
import { JsonRpcProvider, Web3Provider, ExternalProvider, Provider } from '@ethersproject/providers'
import { EventEmitter } from 'events'
import { subscribeToProviderEvents } from '../../helpers/eip1193'

interface NetworkProviderProps {
Expand Down Expand Up @@ -50,7 +49,7 @@ export function NetworkProvider({ children }: NetworkProviderProps) {
}, [])

const activate = useCallback(
async (provider: JsonRpcProvider | (EventEmitter & ExternalProvider)) => {
async (provider: JsonRpcProvider | ExternalProvider) => {
const wrappedProvider = Provider.isProvider(provider) ? provider : new Web3Provider(provider)
try {
const account = await tryToGetAccount(wrappedProvider)
Expand Down
2 changes: 2 additions & 0 deletions packages/example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { SendEtherPage } from './pages/SendEtherPage'
import { NotificationsList } from './components/Transactions/History'
import { Web3Modal } from './pages/Web3Modal'
import { Web3ReactConnector } from './pages/Web3ReactConnector'
import { WalletConnect } from './pages/WalletConnect'

export function App() {
return (
Expand All @@ -29,6 +30,7 @@ export function App() {
<Route exact path="/transactions" component={Transactions} />
<Route exact path="/web3modal" component={Web3Modal} />
<Route exact path="/web3react" component={Web3ReactConnector} />
<Route exact path="/wallet-connect" component={WalletConnect} />
<Redirect exact from="/" to="/balance" />
</Switch>
</BrowserRouter>
Expand Down
4 changes: 4 additions & 0 deletions packages/example/src/components/TopBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ export function TopBar() {
{' '}
Web3React Connector example{' '}
</HeaderLink>
<HeaderLink activeClassName="active-page" to="/wallet-connect">
{' '}
WalletConnect example{' '}
</HeaderLink>
</HeaderNavLinks>
</HeaderNav>
</HeaderContainer>
Expand Down
57 changes: 57 additions & 0 deletions packages/example/src/pages/WalletConnect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from 'react'
import { formatEther } from '@ethersproject/units'
import { useEtherBalance, useEthers } from '@usedapp/core'
import { Container, ContentBlock, ContentRow, MainContent, Section, SectionRow } from '../components/base/base'
import { Label } from '../typography/Label'
import { TextInline } from '../typography/Text'
import { Title } from '../typography/Title'
import { Button } from '../components/base/Button'
import WalletConnectProvider from '@walletconnect/web3-provider'

const STAKING_CONTRACT = '0x00000000219ab540356cBB839Cbe05303d7705Fa'

export function WalletConnect() {
const { account, activate } = useEthers()

async function onConnect() {
const provider = new WalletConnectProvider({
infuraId: '57fc2c19095745e59ab96a4aa87dada8',
})
await provider.enable()
activate(provider)
}

const userBalance = useEtherBalance(account)
const stakingBalance = useEtherBalance(STAKING_CONTRACT)

return (
<MainContent>
<Container>
<Section>
<SectionRow>
<Title>WalletConnect Usage Example</Title>
<Button onClick={onConnect}>Connect</Button>
</SectionRow>
<ContentBlock>
{stakingBalance && (
<ContentRow>
<Label>ETH2 staking contract holds:</Label> <TextInline>{formatEther(stakingBalance)}</TextInline>{' '}
<Label>ETH</Label>
</ContentRow>
)}
{account && (
<ContentRow>
<Label>Account:</Label> <TextInline>{account}</TextInline>
</ContentRow>
)}
{userBalance && (
<ContentRow>
<Label>Ether balance:</Label> <TextInline>{formatEther(userBalance)}</TextInline> <Label>ETH</Label>
</ContentRow>
)}
</ContentBlock>
</Section>
</Container>
</MainContent>
)
}

1 comment on commit e9907a5

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Please sign in to comment.