|
| 1 | +import type { ConnectionOptions } from "@thirdweb-dev/wagmi-adapter"; |
| 2 | +import { ConnectButton } from "thirdweb/react"; |
| 3 | +import { |
| 4 | + useAccount, |
| 5 | + useConnect, |
| 6 | + useDisconnect, |
| 7 | + useSendTransaction, |
| 8 | +} from "wagmi"; |
| 9 | +import { chain, client } from "./wagmi.js"; |
| 10 | + |
| 11 | +function App() { |
| 12 | + const account = useAccount(); |
| 13 | + const { connectors, connect, status, error } = useConnect(); |
| 14 | + const { disconnect } = useDisconnect(); |
| 15 | + const { |
| 16 | + sendTransaction, |
| 17 | + isPending, |
| 18 | + isSuccess, |
| 19 | + isError, |
| 20 | + error: sendTxError, |
| 21 | + data: sendTxData, |
| 22 | + } = useSendTransaction(); |
| 23 | + return ( |
| 24 | + <> |
| 25 | + <div> |
| 26 | + <h2>Account</h2> |
| 27 | + |
| 28 | + <div> |
| 29 | + status: {account.status} |
| 30 | + <br /> |
| 31 | + addresses: {JSON.stringify(account.addresses)} |
| 32 | + <br /> |
| 33 | + chainId: {account.chainId} |
| 34 | + </div> |
| 35 | + |
| 36 | + {account.status === "connected" && ( |
| 37 | + <button type="button" onClick={() => disconnect()}> |
| 38 | + Disconnect |
| 39 | + </button> |
| 40 | + )} |
| 41 | + </div> |
| 42 | + |
| 43 | + <div> |
| 44 | + <h2>Connect</h2> |
| 45 | + <ConnectButton |
| 46 | + client={client} |
| 47 | + onConnect={(wallet) => { |
| 48 | + // auto connect to wagmi on tw connect |
| 49 | + const twConnector = connectors.find( |
| 50 | + (c) => c.id === "in-app-wallet", |
| 51 | + ); |
| 52 | + if (twConnector) { |
| 53 | + const options = { |
| 54 | + wallet, |
| 55 | + } satisfies ConnectionOptions; |
| 56 | + connect({ |
| 57 | + connector: twConnector, |
| 58 | + chainId: chain.id, |
| 59 | + ...options, |
| 60 | + }); |
| 61 | + } |
| 62 | + }} |
| 63 | + /> |
| 64 | + {connectors.map((connector) => ( |
| 65 | + <button |
| 66 | + key={connector.uid} |
| 67 | + onClick={() => { |
| 68 | + if (connector.id === "in-app-wallet") { |
| 69 | + const connectOptions = { |
| 70 | + strategy: "google", |
| 71 | + } satisfies ConnectionOptions; |
| 72 | + connect({ |
| 73 | + connector, |
| 74 | + chainId: chain.id, |
| 75 | + ...connectOptions, |
| 76 | + }); |
| 77 | + } else { |
| 78 | + connect({ connector, chainId: chain.id }); |
| 79 | + } |
| 80 | + }} |
| 81 | + type="button" |
| 82 | + > |
| 83 | + {connector.name} |
| 84 | + </button> |
| 85 | + ))} |
| 86 | + <div>{status}</div> |
| 87 | + <div>{error?.message}</div> |
| 88 | + </div> |
| 89 | + {account && ( |
| 90 | + <div> |
| 91 | + <h2>Transact</h2> |
| 92 | + <button |
| 93 | + onClick={() => sendTransaction({ to: account.address, value: 0n })} |
| 94 | + type="button" |
| 95 | + > |
| 96 | + Send Tx |
| 97 | + </button> |
| 98 | + <div>{isPending ? "Sending..." : ""}</div> |
| 99 | + <div> |
| 100 | + {isSuccess |
| 101 | + ? `Success: ${sendTxData}` |
| 102 | + : isError |
| 103 | + ? sendTxError?.message |
| 104 | + : ""} |
| 105 | + </div> |
| 106 | + </div> |
| 107 | + )} |
| 108 | + </> |
| 109 | + ); |
| 110 | +} |
| 111 | + |
| 112 | +export default App; |
0 commit comments