-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
1,775 additions
and
1,090 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,50 @@ | ||
/* eslint-disable no-console */ | ||
import type { WidgetConfig } from '@lifi/widget'; | ||
import { LiFiWidget } from '@lifi/widget'; | ||
|
||
export const widgetConfig: Partial<WidgetConfig> = { | ||
containerStyle: { | ||
border: `1px solid rgb(234, 234, 234)`, | ||
borderRadius: '16px', | ||
}, | ||
}; | ||
import { Box } from '@mui/material'; | ||
import { useMemo } from 'react'; | ||
import { useConnect, useDisconnect } from 'wagmi'; | ||
import { WalletHeader } from './components/WalletHeader'; | ||
import { useEthersSigner, walletClientToSigner } from './hooks/useEthersSigner'; | ||
import { switchChain } from './utils/switchChain'; | ||
|
||
export function App() { | ||
return <LiFiWidget config={widgetConfig} integrator="vite-example" />; | ||
const { connectAsync, connectors } = useConnect(); | ||
const { disconnect } = useDisconnect(); | ||
const signer = useEthersSigner(); | ||
|
||
const widgetConfig: WidgetConfig = useMemo(() => { | ||
const config: WidgetConfig = { | ||
integrator: 'vite-example', | ||
containerStyle: { | ||
border: `1px solid rgb(234, 234, 234)`, | ||
borderRadius: '16px', | ||
}, | ||
walletManagement: { | ||
signer: signer, | ||
connect: async () => { | ||
const result = await connectAsync({ connector: connectors[0] }); | ||
const walletClient = await result.connector?.getWalletClient(); | ||
if (walletClient) { | ||
return walletClientToSigner(walletClient); | ||
} else { | ||
throw Error('WalletClient not found'); | ||
} | ||
}, | ||
disconnect: async () => { | ||
disconnect(); | ||
}, | ||
switchChain, | ||
}, | ||
}; | ||
|
||
return config; | ||
}, [signer, connectAsync, connectors, disconnect]); | ||
|
||
return ( | ||
<Box> | ||
<WalletHeader /> | ||
<LiFiWidget integrator="vite-example" config={widgetConfig} /> | ||
</Box> | ||
); | ||
} |
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,44 @@ | ||
/* eslint-disable no-console */ | ||
import { Box, Button, Typography } from '@mui/material'; | ||
import { useAccount, useConnect, useDisconnect } from 'wagmi'; | ||
|
||
export function WalletHeader() { | ||
const { address, isConnected } = useAccount(); | ||
const { connectAsync, connectors } = useConnect(); | ||
const { disconnect } = useDisconnect(); | ||
|
||
return ( | ||
<Box | ||
p={2} | ||
mb={2} | ||
display="flex" | ||
justifyContent="space-between" | ||
alignItems="center" | ||
borderBottom="1px solid #EEE" | ||
> | ||
<Typography px={2} fontWeight={600} fontSize={24}> | ||
Example | ||
</Typography> | ||
<Box display="flex" alignItems="center"> | ||
<Typography px={2}>{address}</Typography> | ||
{!isConnected ? ( | ||
<Button | ||
variant="contained" | ||
disableElevation | ||
onClick={() => connectAsync({ connector: connectors[0] })} | ||
> | ||
Connect | ||
</Button> | ||
) : ( | ||
<Button | ||
variant="contained" | ||
disableElevation | ||
onClick={() => disconnect()} | ||
> | ||
Disconnect | ||
</Button> | ||
)} | ||
</Box> | ||
</Box> | ||
); | ||
} |
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,16 @@ | ||
import { configureChains, createConfig } from 'wagmi'; | ||
import { mainnet } from 'wagmi/chains'; | ||
import { InjectedConnector } from 'wagmi/connectors/injected'; | ||
import { publicProvider } from 'wagmi/providers/public'; | ||
|
||
export const { chains, publicClient, webSocketPublicClient } = configureChains( | ||
[mainnet], | ||
[publicProvider()], | ||
); | ||
|
||
export const config = createConfig({ | ||
// autoConnect: true, | ||
connectors: [new InjectedConnector({ options: { shimDisconnect: true } })], | ||
publicClient, | ||
webSocketPublicClient, | ||
}); |
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,28 @@ | ||
import { Web3Provider } from '@ethersproject/providers'; | ||
import { getWalletClient, type WalletClient } from '@wagmi/core'; | ||
import { useMemo } from 'react'; | ||
import { useWalletClient } from 'wagmi'; | ||
|
||
export function walletClientToSigner(walletClient?: WalletClient | null) { | ||
if (walletClient) { | ||
const provider = new Web3Provider(walletClient.transport, 'any'); | ||
const signer = provider.getSigner(); | ||
return signer; | ||
} else { | ||
throw Error('WalletClient not found'); | ||
} | ||
} | ||
|
||
export async function walletClientToSignerAsync(chainId?: number) { | ||
const walletClient = await getWalletClient({ chainId }); | ||
return walletClientToSigner(walletClient); | ||
} | ||
|
||
export function useEthersSigner({ chainId }: { chainId?: number } = {}) { | ||
const { data: walletClient } = useWalletClient({ chainId }); | ||
|
||
return useMemo( | ||
() => (walletClient ? walletClientToSigner(walletClient) : undefined), | ||
[walletClient], | ||
); | ||
} |
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,9 +1,13 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom/client'; | ||
import { WagmiConfig } from 'wagmi'; | ||
import { App } from './App'; | ||
import { config } from './config'; | ||
|
||
ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render( | ||
<React.StrictMode> | ||
<App /> | ||
<WagmiConfig config={config}> | ||
<App /> | ||
</WagmiConfig> | ||
</React.StrictMode>, | ||
); |
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,17 @@ | ||
/* eslint-disable no-console */ | ||
import { getNetwork, switchNetwork } from '@wagmi/core'; | ||
import { walletClientToSignerAsync } from '../hooks/useEthersSigner'; | ||
|
||
export const switchChain = async (chainId: number) => { | ||
const network = getNetwork(); | ||
if (network.chain?.id !== chainId) { | ||
try { | ||
const chain = await switchNetwork({ chainId }); | ||
console.log(network.chain?.id, chain?.id); // logs the previous chainId and required chain id | ||
return await walletClientToSignerAsync(chain?.id); | ||
} catch { | ||
throw new Error("Couldn't switch chain."); | ||
} | ||
} | ||
return await walletClientToSignerAsync(network.chain?.id); | ||
}; |
Oops, something went wrong.