Skip to content
Merged
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
75 changes: 55 additions & 20 deletions src/server/routes/contract/extensions/erc721/write/mintTo.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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<typeof walletWithAAHeaderSchema>;

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({
Expand Down
Loading