diff --git a/src/server/routes/contract/extensions/erc721/write/mintTo.ts b/src/server/routes/contract/extensions/erc721/write/mintTo.ts index 8099991eb..22e6345ca 100644 --- a/src/server/routes/contract/extensions/erc721/write/mintTo.ts +++ b/src/server/routes/contract/extensions/erc721/write/mintTo.ts @@ -1,8 +1,12 @@ -import { Static, Type } from "@sinclair/typebox"; -import { FastifyInstance } from "fastify"; +import { Type, type Static } from "@sinclair/typebox"; +import type { FastifyInstance } from "fastify"; import { StatusCodes } from "http-status-codes"; -import { queueTx } from "../../../../../../db/transactions/queueTx"; -import { getContract } from "../../../../../../utils/cache/getContract"; +import { getContract } from "thirdweb"; +import { mintTo } from "thirdweb/extensions/erc721"; +import type { NFTInput } from "thirdweb/utils"; +import { getChain } from "../../../../../../utils/chain"; +import { thirdwebClient } from "../../../../../../utils/sdk"; +import { queueTransaction } from "../../../../../../utils/transaction/queueTransation"; import { AddressSchema } from "../../../../../schemas/address"; import { nftOrInputSchema } from "../../../../../schemas/nft"; import { @@ -12,7 +16,11 @@ import { transactionWritesResponseSchema, } from "../../../../../schemas/sharedApiSchemas"; import { txOverridesWithValueSchema } from "../../../../../schemas/txOverrides"; -import { walletWithAAHeaderSchema } from "../../../../../schemas/wallet"; +import { + maybeAddress, + requiredAddress, + walletWithAAHeaderSchema, +} from "../../../../../schemas/wallet"; import { getChainIdFromChain } from "../../../../../utils/chain"; // INPUTS @@ -61,31 +69,58 @@ export async function erc721mintTo(fastify: FastifyInstance) { }, }, handler: async (request, reply) => { - const { chain, contractAddress } = request.params; + const { chain: _chain, contractAddress } = request.params; const { simulateTx } = request.query; const { receiver, metadata, txOverrides } = request.body; const { - "x-backend-wallet-address": walletAddress, + "x-backend-wallet-address": fromAddress, "x-account-address": accountAddress, "x-idempotency-key": idempotencyKey, + "x-account-factory-address": accountFactoryAddress, + "x-account-salt": accountSalt, } = request.headers as Static; - const chainId = await getChainIdFromChain(chain); - const contract = await getContract({ - chainId, - contractAddress, - walletAddress, - accountAddress, + const chainId = await getChainIdFromChain(_chain); + const chain = await getChain(chainId); + + const contract = getContract({ + chain, + client: thirdwebClient, + address: contractAddress, }); - const tx = await contract.erc721.mintTo.prepare(receiver, metadata); - const queueId = await queueTx({ - tx, - chainId, - simulateTx, - extension: "erc721", - idempotencyKey, + // Backward compatibility: Transform the request body's v4 shape to v5. + const nft: NFTInput | string = + typeof metadata === "string" + ? metadata + : { + name: metadata.name?.toString() ?? undefined, + description: metadata.description ?? undefined, + image: metadata.image ?? undefined, + animation_url: metadata.animation_url ?? undefined, + external_url: metadata.external_url ?? undefined, + background_color: metadata.background_color ?? undefined, + properties: metadata.properties, + }; + const transaction = mintTo({ + contract, + to: receiver, + nft, + }); + + const queueId = await queueTransaction({ + transaction, + fromAddress: requiredAddress(fromAddress, "x-backend-wallet-address"), + toAddress: maybeAddress(contractAddress, "to"), + accountAddress: maybeAddress(accountAddress, "x-account-address"), + accountFactoryAddress: maybeAddress( + accountFactoryAddress, + "x-account-factory-address", + ), + accountSalt, txOverrides, + idempotencyKey, + shouldSimulate: simulateTx, }); reply.status(StatusCodes.OK).send({