1
- import { Static , Type } from "@sinclair/typebox" ;
2
- import { FastifyInstance } from "fastify" ;
1
+ import { Type , type Static } from "@sinclair/typebox" ;
2
+ import type { FastifyInstance } from "fastify" ;
3
3
import { 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/erc721" ;
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" ;
6
10
import { AddressSchema } from "../../../../../schemas/address" ;
7
11
import { nftOrInputSchema } from "../../../../../schemas/nft" ;
8
12
import {
@@ -12,7 +16,11 @@ import {
12
16
transactionWritesResponseSchema ,
13
17
} from "../../../../../schemas/sharedApiSchemas" ;
14
18
import { txOverridesWithValueSchema } from "../../../../../schemas/txOverrides" ;
15
- import { walletWithAAHeaderSchema } from "../../../../../schemas/wallet" ;
19
+ import {
20
+ maybeAddress ,
21
+ requiredAddress ,
22
+ walletWithAAHeaderSchema ,
23
+ } from "../../../../../schemas/wallet" ;
16
24
import { getChainIdFromChain } from "../../../../../utils/chain" ;
17
25
18
26
// INPUTS
@@ -61,31 +69,58 @@ export async function erc721mintTo(fastify: FastifyInstance) {
61
69
} ,
62
70
} ,
63
71
handler : async ( request , reply ) => {
64
- const { chain, contractAddress } = request . params ;
72
+ const { chain : _chain , contractAddress } = request . params ;
65
73
const { simulateTx } = request . query ;
66
74
const { receiver, metadata, txOverrides } = request . body ;
67
75
const {
68
- "x-backend-wallet-address" : walletAddress ,
76
+ "x-backend-wallet-address" : fromAddress ,
69
77
"x-account-address" : accountAddress ,
70
78
"x-idempotency-key" : idempotencyKey ,
79
+ "x-account-factory-address" : accountFactoryAddress ,
80
+ "x-account-salt" : accountSalt ,
71
81
} = request . headers as Static < typeof walletWithAAHeaderSchema > ;
72
82
73
- const chainId = await getChainIdFromChain ( chain ) ;
74
- const contract = await getContract ( {
75
- chainId,
76
- contractAddress,
77
- walletAddress,
78
- accountAddress,
83
+ const chainId = await getChainIdFromChain ( _chain ) ;
84
+ const chain = await getChain ( chainId ) ;
85
+
86
+ const contract = getContract ( {
87
+ chain,
88
+ client : thirdwebClient ,
89
+ address : contractAddress ,
79
90
} ) ;
80
- const tx = await contract . erc721 . mintTo . prepare ( receiver , metadata ) ;
81
91
82
- const queueId = await queueTx ( {
83
- tx,
84
- chainId,
85
- simulateTx,
86
- extension : "erc721" ,
87
- idempotencyKey,
92
+ // Backward compatibility: Transform the request body's v4 shape to v5.
93
+ const nft : NFTInput | string =
94
+ typeof metadata === "string"
95
+ ? metadata
96
+ : {
97
+ name : metadata . name ?. toString ( ) ?? undefined ,
98
+ description : metadata . description ?? undefined ,
99
+ image : metadata . image ?? undefined ,
100
+ animation_url : metadata . animation_url ?? undefined ,
101
+ external_url : metadata . external_url ?? undefined ,
102
+ background_color : metadata . background_color ?? undefined ,
103
+ properties : metadata . properties ,
104
+ } ;
105
+ const transaction = mintTo ( {
106
+ contract,
107
+ to : receiver ,
108
+ nft,
109
+ } ) ;
110
+
111
+ const queueId = await queueTransaction ( {
112
+ transaction,
113
+ fromAddress : requiredAddress ( fromAddress , "x-backend-wallet-address" ) ,
114
+ toAddress : maybeAddress ( contractAddress , "to" ) ,
115
+ accountAddress : maybeAddress ( accountAddress , "x-account-address" ) ,
116
+ accountFactoryAddress : maybeAddress (
117
+ accountFactoryAddress ,
118
+ "x-account-factory-address" ,
119
+ ) ,
120
+ accountSalt,
88
121
txOverrides,
122
+ idempotencyKey,
123
+ shouldSimulate : simulateTx ,
89
124
} ) ;
90
125
91
126
reply . status ( StatusCodes . OK ) . send ( {
0 commit comments