1
1
import { Type , type Static } from "@sinclair/typebox" ;
2
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/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" ;
6
10
import { AddressSchema } from "../../../../../schemas/address" ;
7
11
import { nftAndSupplySchema } from "../../../../../schemas/nft" ;
8
12
import {
@@ -12,10 +16,13 @@ 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
- // INPUTS
19
26
const requestSchema = erc1155ContractParamSchema ;
20
27
const requestBodySchema = Type . Object ( {
21
28
receiver : {
@@ -66,34 +73,62 @@ export async function erc1155mintTo(fastify: FastifyInstance) {
66
73
} ,
67
74
} ,
68
75
handler : async ( request , reply ) => {
69
- const { chain, contractAddress } = request . params ;
76
+ const { chain : _chain , contractAddress } = request . params ;
70
77
const { simulateTx } = request . query ;
71
78
const { receiver, metadataWithSupply, txOverrides } = request . body ;
72
79
const {
73
- "x-backend-wallet-address" : walletAddress ,
80
+ "x-backend-wallet-address" : fromAddress ,
74
81
"x-account-address" : accountAddress ,
75
82
"x-idempotency-key" : idempotencyKey ,
83
+ "x-account-factory-address" : accountFactoryAddress ,
84
+ "x-account-salt" : accountSalt ,
76
85
} = request . headers as Static < typeof walletWithAAHeaderSchema > ;
77
86
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 ,
84
94
} ) ;
85
- const tx = await contract . erc1155 . mintTo . prepare (
86
- receiver ,
87
- metadataWithSupply ,
88
- ) ;
89
95
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,
96
127
txOverrides,
128
+ idempotencyKey,
129
+ extension : "erc1155" ,
130
+ functionName : "mintTo" ,
131
+ shouldSimulate : simulateTx ,
97
132
} ) ;
98
133
99
134
reply . status ( StatusCodes . OK ) . send ( {
0 commit comments