-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add sign msg, send tx and get balance
- Loading branch information
Showing
7 changed files
with
2,160 additions
and
6,322 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
.vscode |
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
Large diffs are not rendered by default.
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
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,74 @@ | ||
import { useDisconnect, useAppKit, useAppKitNetwork } from '@reown/appkit/react' | ||
import { useEffect } from 'react'; | ||
import { useDisconnect, useAppKit, useAppKitNetwork, useAppKitAccount } from '@reown/appkit/react' | ||
import { parseGwei, type Address } from 'viem' | ||
import { useEstimateGas, useSendTransaction, useSignMessage, useBalance } from 'wagmi' | ||
import { networks } from '../config' | ||
|
||
export const ActionButtonList = () => { | ||
const { disconnect } = useDisconnect(); | ||
const { open } = useAppKit(); | ||
const { switchNetwork } = useAppKitNetwork(); | ||
// test transaction | ||
const TEST_TX = { | ||
to: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045" as Address, // vitalik address | ||
value: parseGwei('0.0001') | ||
} | ||
|
||
interface ActionButtonListProps { | ||
sendHash: (hash: `0x${string}` ) => void; | ||
sendSignMsg: (hash: string) => void; | ||
sendBalance: (balance: string) => void; | ||
} | ||
|
||
export const ActionButtonList = ({ sendHash, sendSignMsg, sendBalance }: ActionButtonListProps) => { | ||
const { disconnect } = useDisconnect(); // AppKit hook to disconnect | ||
const { open } = useAppKit(); // AppKit hook to open the modal | ||
const { switchNetwork } = useAppKitNetwork(); // AppKithook to switch network | ||
const { address } = useAppKitAccount() // AppKit hook to get the address | ||
|
||
const { data: gas } = useEstimateGas({...TEST_TX}); // Wagmi hook to estimate gas | ||
const { data: hash, sendTransaction, } = useSendTransaction(); // Wagmi hook to send a transaction | ||
const { signMessageAsync } = useSignMessage() // Wagmi hook to sign a message | ||
const { refetch } = useBalance({ | ||
address: address as Address | ||
}); // Wagmi hook to get the balance | ||
|
||
useEffect(() => { | ||
if (hash) { | ||
sendHash(hash); | ||
} | ||
}, [hash]); | ||
|
||
// function to send a tx | ||
const sendTx = () => { | ||
try { | ||
sendTransaction({ | ||
...TEST_TX, | ||
gas // Add the gas to the transaction | ||
}); | ||
} catch (err) { | ||
console.log('Error sending transaction:', err); | ||
} | ||
} | ||
|
||
// function to sing a msg | ||
const signMsg = async () => { | ||
const msg = "Hello Reown AppKit!" // message to sign | ||
const sig = await signMessageAsync({ message: msg, account: address as Address }); | ||
sendSignMsg(sig); | ||
} | ||
|
||
// function to get the balance | ||
const getBalance = async () => { | ||
const balance = await refetch() | ||
sendBalance(balance?.data?.value.toString() + " " + balance?.data?.symbol.toString()) | ||
} | ||
|
||
|
||
return ( | ||
<div > | ||
<button onClick={() => open()}>Open</button> | ||
<button onClick={() => disconnect()}>Disconnect</button> | ||
<button onClick={() => switchNetwork(networks[1]) }>Switch</button> | ||
<button onClick={() => signMsg() }>Sign msg</button> | ||
<button onClick={() => sendTx() }>Send tx</button> | ||
<button onClick={() => getBalance()}>Get Balance</button> | ||
</div> | ||
) | ||
} |
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