Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: demo sign and send transaction #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 111 additions & 0 deletions template/src/display/Actions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import {
useAccount,
useSendTransaction,
useSignMessage,
useSignTypedData,
} from "wagmi";

import { Address } from "viem";

const Actions = () => {
const { isConnected, address } = useAccount();

const {
signMessage,
isPending: signMessageIsLoading,
isError: signMessageIsError,
} = useSignMessage();
const {
signTypedData,
isPending: signTypedDataIsLoading,
isError: signTypedDataIsError,
} = useSignTypedData();
const {
sendTransaction,
isPending: sendTransactionIsLoading,
isError: sendTransactionIsError,
} = useSendTransaction();

const testSignMessage = () => {
signMessage({
message: "wen token",
});
};
const testSignTypedData = () => {
signTypedData({
// All properties on a domain are optional
domain: {
name: "Ether Mail",
version: "1",
chainId: 1,
verifyingContract: "0xCcCCccccCCCCcCCCCCCcCcCccCcCCCcCcccccccC",
},
// The named list of all type definitions
types: {
Person: [
{ name: "name", type: "string" },
{ name: "wallet", type: "address" },
],
Mail: [
{ name: "from", type: "Person" },
{ name: "to", type: "Person" },
{ name: "contents", type: "string" },
],
},
primaryType: "Mail",
message: {
from: {
name: "Cow",
wallet: "0xCD2a3d9F938E13CD947Ec05AbC7FE734Df8DD826",
},
to: {
name: "Bob",
wallet: "0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB",
},
contents: "Hello, Bob!",
},
});
};
const testSendTransaction = () => {
sendTransaction({
to: (address as Address) ?? "",
value: 0n,
});
};

return (
<div
style={{
display: "flex",
flexDirection: "column",
gap: 8,
flexWrap: "wrap",
}}
>
<h2>Actions {!isConnected && `(connect to test)`}</h2>
<button disabled={!isConnected} onClick={testSignMessage}>
{signMessageIsError
? "Error. Check console"
: signMessageIsLoading
? "Waiting..."
: "Sign message"}
</button>
<button disabled={!isConnected} onClick={testSignTypedData}>
{signTypedDataIsError
? "Error. Check console"
: signTypedDataIsLoading
? "Waiting..."
: "Sign typed data"}
</button>
<button disabled={!isConnected} onClick={testSendTransaction}>
{sendTransactionIsError
? "Error. Check console"
: sendTransactionIsLoading
? "Waiting..."
: "Send transaction"}
</button>
</div>
);
};

export default Actions;
2 changes: 2 additions & 0 deletions template/src/display/Contracts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import useRootStore from "../store/rootStore.ts";
import Read from "./Read.tsx";
import Write from "./Write.tsx";
import { ContractConfig } from "../types.ts";
import Actions from "./Actions.tsx";

const Contracts = () => {
const [activeChainContracts, activeChain] = useRootStore((state) => [
Expand Down Expand Up @@ -68,6 +69,7 @@ const Contracts = () => {
)
)
)}
<Actions />
</div>
);
};
Expand Down