11import { Type , type Static } from "@sinclair/typebox" ;
22import type { FastifyInstance } from "fastify" ;
33import { StatusCodes } from "http-status-codes" ;
4- import { queueTx } from "../../../../../../db/transactions/queueTx" ;
5- import { getContract } from "../../../../../../utils/cache/getContract" ;
4+ import { getContract } from "thirdweb" ;
5+ import { mintTo } from "thirdweb/extensions/erc1155" ;
6+ import type { NFTInput } from "thirdweb/utils" ;
7+ import { getChain } from "../../../../../../utils/chain" ;
8+ import { thirdwebClient } from "../../../../../../utils/sdk" ;
9+ import { queueTransaction } from "../../../../../../utils/transaction/queueTransation" ;
610import { AddressSchema } from "../../../../../schemas/address" ;
711import { nftAndSupplySchema } from "../../../../../schemas/nft" ;
812import {
@@ -12,10 +16,13 @@ import {
1216 transactionWritesResponseSchema ,
1317} from "../../../../../schemas/sharedApiSchemas" ;
1418import { txOverridesWithValueSchema } from "../../../../../schemas/txOverrides" ;
15- import { walletWithAAHeaderSchema } from "../../../../../schemas/wallet" ;
19+ import {
20+ maybeAddress ,
21+ requiredAddress ,
22+ walletWithAAHeaderSchema ,
23+ } from "../../../../../schemas/wallet" ;
1624import { getChainIdFromChain } from "../../../../../utils/chain" ;
1725
18- // INPUTS
1926const requestSchema = erc1155ContractParamSchema ;
2027const requestBodySchema = Type . Object ( {
2128 receiver : {
@@ -66,34 +73,62 @@ export async function erc1155mintTo(fastify: FastifyInstance) {
6673 } ,
6774 } ,
6875 handler : async ( request , reply ) => {
69- const { chain, contractAddress } = request . params ;
76+ const { chain : _chain , contractAddress } = request . params ;
7077 const { simulateTx } = request . query ;
7178 const { receiver, metadataWithSupply, txOverrides } = request . body ;
7279 const {
73- "x-backend-wallet-address" : walletAddress ,
80+ "x-backend-wallet-address" : fromAddress ,
7481 "x-account-address" : accountAddress ,
7582 "x-idempotency-key" : idempotencyKey ,
83+ "x-account-factory-address" : accountFactoryAddress ,
84+ "x-account-salt" : accountSalt ,
7685 } = request . headers as Static < typeof walletWithAAHeaderSchema > ;
7786
78- const chainId = await getChainIdFromChain ( chain ) ;
79- const contract = await getContract ( {
80- chainId,
81- contractAddress,
82- walletAddress,
83- accountAddress,
87+ const chainId = await getChainIdFromChain ( _chain ) ;
88+ const chain = await getChain ( chainId ) ;
89+
90+ const contract = getContract ( {
91+ chain,
92+ client : thirdwebClient ,
93+ address : contractAddress ,
8494 } ) ;
85- const tx = await contract . erc1155 . mintTo . prepare (
86- receiver ,
87- metadataWithSupply ,
88- ) ;
8995
90- const queueId = await queueTx ( {
91- tx,
92- chainId,
93- simulateTx,
94- extension : "erc1155" ,
95- idempotencyKey,
96+ // Backward compatibility: Transform the request body's v4 shape to v5.
97+ const { metadata, supply } = metadataWithSupply ;
98+ const nft : NFTInput | string =
99+ typeof metadata === "string"
100+ ? metadata
101+ : {
102+ name : metadata . name ?. toString ( ) ?? undefined ,
103+ description : metadata . description ?? undefined ,
104+ image : metadata . image ?? undefined ,
105+ animation_url : metadata . animation_url ?? undefined ,
106+ external_url : metadata . external_url ?? undefined ,
107+ background_color : metadata . background_color ?? undefined ,
108+ properties : metadata . properties ,
109+ } ;
110+ const transaction = mintTo ( {
111+ contract,
112+ to : receiver ,
113+ nft,
114+ supply : BigInt ( supply ) ,
115+ } ) ;
116+
117+ const queueId = await queueTransaction ( {
118+ transaction,
119+ fromAddress : requiredAddress ( fromAddress , "x-backend-wallet-address" ) ,
120+ toAddress : maybeAddress ( contractAddress , "to" ) ,
121+ accountAddress : maybeAddress ( accountAddress , "x-account-address" ) ,
122+ accountFactoryAddress : maybeAddress (
123+ accountFactoryAddress ,
124+ "x-account-factory-address" ,
125+ ) ,
126+ accountSalt,
96127 txOverrides,
128+ idempotencyKey,
129+ extension : "erc1155" ,
130+ functionName : "mintTo" ,
131+ shouldSimulate : simulateTx ,
97132 } ) ;
98133
99134 reply . status ( StatusCodes . OK ) . send ( {
0 commit comments