-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(widget): connect wallet (#3262)
- Loading branch information
Showing
10 changed files
with
643 additions
and
53 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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
49 changes: 49 additions & 0 deletions
49
apps/widget-configurator/src/app/configurator/hooks/useProvider.ts
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,49 @@ | ||
import { useEffect, useState } from 'react' | ||
|
||
import { EthereumProvider } from '@cowprotocol/widget-lib' | ||
|
||
import { useAccount, useConfig } from 'wagmi' | ||
|
||
import EventEmitter from 'events' | ||
|
||
const onDisconnect = new EventEmitter() | ||
|
||
export function useProvider(): EthereumProvider | null { | ||
const config = useConfig() | ||
const { isDisconnected } = useAccount() | ||
const [provider, setProvider] = useState<EthereumProvider | null>(null) | ||
|
||
useEffect(() => { | ||
return config.subscribe(({ connector }) => { | ||
connector?.getProvider().then((provider) => { | ||
setProvider(getEthereumProvider(provider, onDisconnect)) | ||
}) | ||
}) | ||
}, [config]) | ||
|
||
useEffect(() => { | ||
if (!provider || !isDisconnected) return | ||
|
||
onDisconnect.emit('disconnect') | ||
}, [provider, isDisconnected]) | ||
|
||
return provider | ||
} | ||
|
||
function getEthereumProvider(provider: EthereumProvider, onDisconnect: EventEmitter): EthereumProvider { | ||
return { | ||
request(...args) { | ||
return provider.request(...args) | ||
}, | ||
enable(...args) { | ||
return provider.enable(...args) | ||
}, | ||
on(event: string, args: unknown) { | ||
if (event === 'disconnect' || event === 'close') { | ||
return onDisconnect.on('disconnect', args) | ||
} else { | ||
return provider.on(event, args) | ||
} | ||
}, | ||
} | ||
} |
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,20 @@ | ||
import { createWeb3Modal, defaultWagmiConfig } from '@web3modal/wagmi/react' | ||
import { gnosis, goerli, mainnet } from 'wagmi/chains' | ||
|
||
const WC_PROJECT_ID = process.env.REACT_APP_WC_PROJECT_ID | ||
const WC_DEFAULT_PROJECT_ID = 'a6cc11517a10f6f12953fd67b1eb67e7' | ||
|
||
const projectId = WC_PROJECT_ID || WC_DEFAULT_PROJECT_ID | ||
|
||
const metadata = { | ||
name: 'CoW Widget Configurator', | ||
description: 'Injectable UI of CoWSwap', | ||
url: 'https://swap.cow.fi', | ||
icons: ['https://swap.cow.fi/favicon.png'], | ||
} | ||
|
||
const chains = [mainnet, gnosis, goerli] | ||
|
||
export const wagmiConfig = defaultWagmiConfig({ chains, projectId, metadata }) | ||
|
||
createWeb3Modal({ wagmiConfig, projectId, chains }) |
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
Oops, something went wrong.