-
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.
Init wagmi and connect wallet functionality (#17)
## Why Wordle3 didn't have the proper wallet connection logic to enable players to connect and interact with the game. ## How - By adding Wagmi and TanStack Query to enable wallet interaction wrapping App.jsx on the respective providers. - Also creates a custom ConnectWallet component that passes on the respective address for smart contract interaction. Style to be added later. Also adds new publicclients for future usage.
- Loading branch information
Showing
6 changed files
with
143 additions
and
46 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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { createPublicClient, http, publicActions, walletActions, getContract } from 'viem'; | ||
import { foundry } from 'viem/chains'; | ||
import WordleABI from '../abis/WordleABI.json'; | ||
import ERC20ABI from '../abis/ERC20ABI.json'; | ||
|
||
// Public client | ||
export const publicClient = createPublicClient({ | ||
chain: foundry, | ||
transport: http(), | ||
}); | ||
|
||
// Wordle3 Contract | ||
export const publicWordle = getContract({ | ||
address: '0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0', | ||
abi: WordleABI, | ||
client: publicClient, | ||
}); | ||
|
||
// Wordle Token contract | ||
export const publicWdt = getContract({ | ||
address: '0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512', | ||
abi: ERC20ABI, | ||
client: publicClient, | ||
}); |
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,50 @@ | ||
import React from 'react' | ||
import { useConnect } from 'wagmi' | ||
import { useAccount, useDisconnect, useEnsAvatar, useEnsName } from 'wagmi' | ||
|
||
|
||
/** | ||
* Account component that displays connected account and passes on | ||
* the address to the parent component for interaction. | ||
*/ | ||
function Account({onAddressChange}) { | ||
const { address } = useAccount() | ||
const { disconnect } = useDisconnect() | ||
const { data: ensName } = useEnsName({ address }) | ||
const { data: ensAvatar } = useEnsAvatar({ name: ensName! }) | ||
|
||
if (address) onAddressChange(address); | ||
|
||
return ( | ||
<div> | ||
{ensAvatar && <img alt="ENS Avatar" src={ensAvatar} />} | ||
{address && <div>{ensName ? `${ensName} (${address})` : address}</div>} | ||
<button onClick={() => disconnect()}>Disconnect</button> | ||
</div> | ||
) | ||
} | ||
|
||
/** | ||
* Account component that displays available wallet connectors | ||
* Connectors are also pulled from ../../wagmi.config.ts | ||
*/ | ||
function WalletOptions() { | ||
const { connectors, connect } = useConnect() | ||
|
||
return connectors.map((connector) => ( | ||
<button key={connector.uid} onClick={() => connect({ connector })}> | ||
{connector.name} | ||
</button> | ||
)) | ||
} | ||
|
||
/** | ||
* Wrapper for the login user flow. | ||
* Receives onAddressChange from parent component to lift the address | ||
* for smart contract operations | ||
*/ | ||
export function ConnectWallet({onAddressChange}) { | ||
const { isConnected } = useAccount() | ||
if (isConnected) return <Account onAddressChange={onAddressChange} /> | ||
return <WalletOptions /> | ||
} |
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,15 @@ | ||
import { http, createConfig } from 'wagmi' | ||
import { anvil } from 'wagmi/chains' | ||
import { injected, metaMask, safe } from 'wagmi/connectors' | ||
|
||
export const wagmiConfig = createConfig({ | ||
chains: [anvil], | ||
transports: { | ||
[anvil.id]: http(), | ||
}, | ||
connectors: [ | ||
injected(), | ||
metaMask(), | ||
safe(), | ||
] | ||
}) |