From cb5f8aca031ffbac11d2df9d6bede0419503e4dd Mon Sep 17 00:00:00 2001 From: Phillip Ho Date: Sun, 8 Dec 2024 15:03:56 +0800 Subject: [PATCH 1/4] chore: Update erc721 mintTo to thirdweb v5 sdk --- .../extensions/erc721/write/mintTo.ts | 61 +++++++++++++------ 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/src/server/routes/contract/extensions/erc721/write/mintTo.ts b/src/server/routes/contract/extensions/erc721/write/mintTo.ts index 8099991eb..c68da3f15 100644 --- a/src/server/routes/contract/extensions/erc721/write/mintTo.ts +++ b/src/server/routes/contract/extensions/erc721/write/mintTo.ts @@ -1,8 +1,11 @@ -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 { 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 +15,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 +68,45 @@ 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, + const transaction = mintTo({ + contract, + to: receiver, + nft: typeof metadata === "string" ? metadata : { nft: metadata }, + }); + + 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({ From a3b1ef5f42a80d278521d2db0690a4439d452d31 Mon Sep 17 00:00:00 2001 From: Phillip Ho Date: Sun, 8 Dec 2024 15:19:21 +0800 Subject: [PATCH 2/4] fix metadata --- .../contract/extensions/erc721/write/mintTo.ts | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/server/routes/contract/extensions/erc721/write/mintTo.ts b/src/server/routes/contract/extensions/erc721/write/mintTo.ts index c68da3f15..6a7e72463 100644 --- a/src/server/routes/contract/extensions/erc721/write/mintTo.ts +++ b/src/server/routes/contract/extensions/erc721/write/mintTo.ts @@ -3,6 +3,7 @@ import type { FastifyInstance } from "fastify"; import { StatusCodes } from "http-status-codes"; import { getContract } from "thirdweb"; import { mintTo } from "thirdweb/extensions/erc721"; +import { NFTInput } from "thirdweb/utils"; import { getChain } from "../../../../../../utils/chain"; import { thirdwebClient } from "../../../../../../utils/sdk"; import { queueTransaction } from "../../../../../../utils/transaction/queueTransation"; @@ -88,10 +89,24 @@ export async function erc721mintTo(fastify: FastifyInstance) { address: contractAddress, }); + // Backward compatibility: This endpoint body uses the v4 SDK shape. + // Transform to v5 SDK shape to use the v5 endpoint. + const nft: NFTInput | string = + typeof metadata === "string" + ? metadata + : { + name: metadata.name, + description: metadata.description, + image: metadata.image, + animation_url: metadata.animation_url, + external_url: metadata.external_url, + background_color: metadata.background_color, + properties: metadata.properties, + }; const transaction = mintTo({ contract, to: receiver, - nft: typeof metadata === "string" ? metadata : { nft: metadata }, + nft, }); const queueId = await queueTransaction({ From 8d8b1274c7553146617feb2430252af76e50fc6e Mon Sep 17 00:00:00 2001 From: Phillip Ho Date: Sun, 8 Dec 2024 16:24:19 +0800 Subject: [PATCH 3/4] fit type --- .../contract/extensions/erc721/write/mintTo.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/server/routes/contract/extensions/erc721/write/mintTo.ts b/src/server/routes/contract/extensions/erc721/write/mintTo.ts index 6a7e72463..f18b3037d 100644 --- a/src/server/routes/contract/extensions/erc721/write/mintTo.ts +++ b/src/server/routes/contract/extensions/erc721/write/mintTo.ts @@ -3,7 +3,7 @@ import type { FastifyInstance } from "fastify"; import { StatusCodes } from "http-status-codes"; import { getContract } from "thirdweb"; import { mintTo } from "thirdweb/extensions/erc721"; -import { NFTInput } from "thirdweb/utils"; +import type { NFTInput } from "thirdweb/utils"; import { getChain } from "../../../../../../utils/chain"; import { thirdwebClient } from "../../../../../../utils/sdk"; import { queueTransaction } from "../../../../../../utils/transaction/queueTransation"; @@ -94,15 +94,15 @@ export async function erc721mintTo(fastify: FastifyInstance) { const nft: NFTInput | string = typeof metadata === "string" ? metadata - : { - name: metadata.name, - description: metadata.description, - image: metadata.image, - animation_url: metadata.animation_url, - external_url: metadata.external_url, - background_color: metadata.background_color, + : ({ + 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, - }; + } satisfies NFTInput); const transaction = mintTo({ contract, to: receiver, From 6ee85af0f39076b75314be5a758ca8c8842799c5 Mon Sep 17 00:00:00 2001 From: Phillip Ho Date: Sun, 8 Dec 2024 16:26:06 +0800 Subject: [PATCH 4/4] comment --- .../routes/contract/extensions/erc721/write/mintTo.ts | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/server/routes/contract/extensions/erc721/write/mintTo.ts b/src/server/routes/contract/extensions/erc721/write/mintTo.ts index f18b3037d..22e6345ca 100644 --- a/src/server/routes/contract/extensions/erc721/write/mintTo.ts +++ b/src/server/routes/contract/extensions/erc721/write/mintTo.ts @@ -89,12 +89,11 @@ export async function erc721mintTo(fastify: FastifyInstance) { address: contractAddress, }); - // Backward compatibility: This endpoint body uses the v4 SDK shape. - // Transform to v5 SDK shape to use the v5 endpoint. + // 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, @@ -102,7 +101,7 @@ export async function erc721mintTo(fastify: FastifyInstance) { external_url: metadata.external_url ?? undefined, background_color: metadata.background_color ?? undefined, properties: metadata.properties, - } satisfies NFTInput); + }; const transaction = mintTo({ contract, to: receiver,