Skip to content

Commit

Permalink
use writeContract for useHats
Browse files Browse the repository at this point in the history
  • Loading branch information
yu23ki14 committed Dec 16, 2024
1 parent 83ea203 commit f00c613
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 41 deletions.
80 changes: 44 additions & 36 deletions pkgs/frontend/hooks/useHats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useCallback, useEffect, useState } from "react";
import { Address, decodeEventLog, encodeFunctionData } from "viem";
import { base, optimism, sepolia } from "viem/chains";
import { HATS_ADDRESS } from "./useContracts";
import { useSmartAccountClient } from "./useWallet";
import { useActiveWallet, useSmartAccountClient } from "./useWallet";
import { currentChain, publicClient } from "./useViem";

// ###############################################################
Expand Down Expand Up @@ -59,7 +59,7 @@ export const useTreeInfo = (treeId: number) => {
* @returns
*/
export const useHats = () => {
const smartAccountClient = useSmartAccountClient();
const { wallet } = useActiveWallet();

const [isLoading, setIsLoading] = useState(false);

Expand Down Expand Up @@ -243,30 +243,44 @@ export const useHats = () => {
mutable?: boolean;
imageURI?: string;
}) => {
if (!smartAccountClient) return;
if (!wallet) return;

setIsLoading(true);

try {
const txHash = await smartAccountClient.sendTransaction({
calls: [
{
to: HATS_ADDRESS,
data: encodeFunctionData({
abi: HATS_ABI,
functionName: "createHat",
args: [
params.parentHatId,
params.details || "",
params.maxSupply || 5,
params.eligibility ||
"0x0000000000000000000000000000000000004a75",
params.toggle || "0x0000000000000000000000000000000000004a75",
params.mutable || true,
params.imageURI || "",
],
}),
},
// const txHash = await wallet.sendTransaction({
// calls: [
// {
// to: HATS_ADDRESS,
// data: encodeFunctionData({
// abi: HATS_ABI,
// functionName: "createHat",
// args: [
// params.parentHatId,
// params.details || "",
// params.maxSupply || 5,
// params.eligibility ||
// "0x0000000000000000000000000000000000004a75",
// params.toggle || "0x0000000000000000000000000000000000004a75",
// params.mutable || true,
// params.imageURI || "",
// ],
// }),
// },
// ],
// });
const txHash = await wallet.writeContract({
abi: HATS_ABI,
address: HATS_ADDRESS,
functionName: "createHat",
args: [
params.parentHatId,
params.details || "",
params.maxSupply || 5,
params.eligibility || "0x0000000000000000000000000000000000004a75",
params.toggle || "0x0000000000000000000000000000000000004a75",
params.mutable || true,
params.imageURI || "",
],
});

Expand Down Expand Up @@ -302,7 +316,7 @@ export const useHats = () => {
setIsLoading(false);
}
},
[smartAccountClient]
[wallet]
);

/**
Expand All @@ -312,22 +326,16 @@ export const useHats = () => {
*/
const mintHat = useCallback(
async (params: { hatId: bigint; wearer: Address }) => {
if (!smartAccountClient) return;
if (!wallet) return;

setIsLoading(true);

try {
const txHash = await smartAccountClient.sendTransaction({
calls: [
{
to: HATS_ADDRESS,
data: encodeFunctionData({
abi: HATS_ABI,
functionName: "mintHat",
args: [params.hatId, params.wearer],
}),
},
],
const txHash = await wallet.writeContract({
abi: HATS_ABI,
address: HATS_ADDRESS,
functionName: "mintHat",
args: [params.hatId, params.wearer],
});

const receipt = await publicClient.waitForTransactionReceipt({
Expand All @@ -343,7 +351,7 @@ export const useHats = () => {
setIsLoading(false);
}
},
[smartAccountClient]
[wallet]
);

return {
Expand Down
31 changes: 26 additions & 5 deletions pkgs/frontend/hooks/useWallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,21 @@ import { createSmartAccountClient, SmartAccountClient } from "permissionless";
import { toSimpleSmartAccount } from "permissionless/accounts";
import { createPimlicoClient } from "permissionless/clients/pimlico";
import { useCallback, useEffect, useMemo, useState } from "react";
import { Address, createWalletClient, custom, http, WalletClient } from "viem";
import { entryPoint07Address } from "viem/account-abstraction";
import {
Account,
Address,
createWalletClient,
custom,
CustomTransport,
http,
Transport,
WalletClient,
} from "viem";
import {
entryPoint07Address,
SmartAccount,
SmartAccountImplementation,
} from "viem/account-abstraction";
import { currentChain, publicClient } from "./useViem";

// Pimlico API endpoint URL
Expand All @@ -27,7 +40,14 @@ export const pimlicoClient = createPimlicoClient({
* Pimlico 向けの React Hooks
*/
export const useSmartAccountClient = () => {
const [client, setClient] = useState<SmartAccountClient>();
const [client, setClient] =
useState<
SmartAccountClient<
Transport,
typeof currentChain,
SmartAccount<SmartAccountImplementation>
>
>();
const { wallets } = useWallets();

useEffect(() => {
Expand Down Expand Up @@ -65,7 +85,7 @@ export const useSmartAccountClient = () => {
},
});

setClient(smartAccountClient);
setClient(smartAccountClient as any);
};
create();
}, [wallets]);
Expand All @@ -76,7 +96,8 @@ export const useSmartAccountClient = () => {
export const useAccountClient = () => {
const { wallets } = useWallets();

const [client, setClient] = useState<WalletClient>();
const [client, setClient] =
useState<WalletClient<CustomTransport, typeof currentChain, Account>>();

useEffect(() => {
const create = async () => {
Expand Down

0 comments on commit f00c613

Please sign in to comment.