From cabc461275bc1e2862e8b049126a6b953b182531 Mon Sep 17 00:00:00 2001 From: thomas jin Date: Tue, 14 Oct 2025 04:12:08 +1000 Subject: [PATCH] integrate new endpoint --- .../smartwallet/sendUserOperation/route.ts | 42 -------------- hooks/useZoraMintComment.ts | 56 ++++--------------- lib/smartwallets/mintOnSmartWallet.ts | 34 +++++++++++ 3 files changed, 46 insertions(+), 86 deletions(-) delete mode 100644 app/api/smartwallet/sendUserOperation/route.ts create mode 100644 lib/smartwallets/mintOnSmartWallet.ts diff --git a/app/api/smartwallet/sendUserOperation/route.ts b/app/api/smartwallet/sendUserOperation/route.ts deleted file mode 100644 index 74910312..00000000 --- a/app/api/smartwallet/sendUserOperation/route.ts +++ /dev/null @@ -1,42 +0,0 @@ -import { smartWalletABI } from "@/lib/abis/smartWalletABI"; -import { CHAIN } from "@/lib/consts"; -import getSmartWallet from "@/lib/getSmartWallet"; -import { NextRequest } from "next/server"; -import { Address, createWalletClient, encodeFunctionData, http } from "viem"; -import { privateKeyToAccount } from "viem/accounts"; - -export async function POST(req: NextRequest) { - const body = await req.json(); - const { parameters } = body; - const { abi, functionName, address, args } = parameters; - try { - const smartWallet = await getSmartWallet(); - if (!smartWallet) throw new Error(); - const admin = privateKeyToAccount(process.env.PRIVATE_KEY as Address); - const createContractCalldata = encodeFunctionData({ - abi, - functionName, - args, - }); - const walletClient = createWalletClient({ - account: admin, - transport: http(CHAIN.rpcUrls.default.http[0]), - }); - const hash = await walletClient.writeContract({ - address: smartWallet.address, - abi: smartWalletABI, - functionName: "execute", - args: [address, 0, createContractCalldata], - chain: CHAIN, - }); - return Response.json({ transactionHash: hash }); - } catch (e: any) { - console.log(e); - const message = e?.message ?? "failed to create collection"; - return Response.json({ message }, { status: 500 }); - } -} - -export const dynamic = "force-dynamic"; -export const fetchCache = "force-no-store"; -export const revalidate = 0; diff --git a/hooks/useZoraMintComment.ts b/hooks/useZoraMintComment.ts index 748f9e2c..0777d29c 100644 --- a/hooks/useZoraMintComment.ts +++ b/hooks/useZoraMintComment.ts @@ -1,41 +1,16 @@ import { useEffect, useState } from "react"; -import { useAccount } from "wagmi"; -import { zoraCreator1155ImplABI } from "@zoralabs/protocol-deployments"; -import { zoraCreatorFixedPriceSaleStrategyAddress } from "@/lib/protocolSdk/constants"; -import { CHAIN } from "@/lib/consts"; -import { Address, encodeAbiParameters, parseAbiParameters } from "viem"; +import { Address } from "viem"; import { useTokenProvider } from "@/providers/TokenProvider"; import { useUserProvider } from "@/providers/UserProvider"; import { useCrossmintCheckout } from "@crossmint/client-sdk-react-ui"; -import useConnectedWallet from "./useConnectedWallet"; -import { useFrameProvider } from "@/providers/FrameProvider"; import { toast } from "sonner"; import { MintType } from "@/types/zora"; import useUsdcMint from "./useUsdcMint"; import useNativeMint from "./useNativeMint"; - -const mintOnSmartWallet = async (parameters: any) => { - const response = await fetch(`/api/smartwallet/sendUserOperation`, { - method: "POST", - headers: { - "content-type": "application/json", - accept: "application/json", - }, - body: JSON.stringify({ - parameters, - }), - }); - - const data = await response.json(); - - return data.transactionHash; -}; +import mintOnSmartWallet from "@/lib/smartwallets/mintOnSmartWallet"; const useZoraMintComment = () => { const [isOpenCrossmint, setIsOpenCrossmint] = useState(false); - const { connectedWallet } = useConnectedWallet(); - const { address } = useAccount(); - const { context } = useFrameProvider(); const [isLoading, setIsLoading] = useState(false); const { token, @@ -47,7 +22,8 @@ const useZoraMintComment = () => { setCollected, mintCount, } = useTokenProvider(); - const { isPrepared } = useUserProvider(); + const { isPrepared, externalWallet, connectedAddress } = useUserProvider(); + const account = externalWallet || connectedAddress; const { order } = useCrossmintCheckout(); const { mintWithUsdc } = useUsdcMint(); const { mintWithNativeToken } = useNativeMint(); @@ -62,24 +38,16 @@ const useZoraMintComment = () => { if (!isPrepared()) return; if (!saleConfig) return; setIsLoading(true); - const account = context ? address : connectedWallet; - const minterArguments = encodeAbiParameters(parseAbiParameters("address, string"), [ - account as Address, - comment, - ]); - if (saleConfig.pricePerToken === BigInt(0)) { await mintOnSmartWallet({ - address: token.tokenContractAddress, - abi: zoraCreator1155ImplABI, - functionName: "mint", - args: [ - zoraCreatorFixedPriceSaleStrategyAddress[CHAIN.id], - token.tokenId, - mintCount, - [], - minterArguments, - ], + token: { + tokenId: Number(token.tokenId), + tokenContractAddress: token.tokenContractAddress, + }, + account: connectedAddress as Address, + to: (externalWallet || connectedAddress) as Address, + comment, + amount: mintCount, }); } else { let receipt = null; diff --git a/lib/smartwallets/mintOnSmartWallet.ts b/lib/smartwallets/mintOnSmartWallet.ts new file mode 100644 index 00000000..930aeca8 --- /dev/null +++ b/lib/smartwallets/mintOnSmartWallet.ts @@ -0,0 +1,34 @@ +import { Address } from "viem"; + +interface mintCommentInput { + token: { + tokenId: number; + tokenContractAddress: Address; + }; + account: Address; + to: Address; + comment: string; + amount: number; +} +const mintOnSmartWallet = async ({ token, account, comment, amount, to }: mintCommentInput) => { + const response = await fetch(`/api/smartwallet/comment`, { + method: "POST", + headers: { + "content-type": "application/json", + accept: "application/json", + }, + body: JSON.stringify({ + account, + to, + token, + comment, + amount, + }), + }); + + const data = await response.json(); + + return data.transactionHash; +}; + +export default mintOnSmartWallet;