Skip to content

Commit 52abd1a

Browse files
committed
chore: Update ERC20 and ERC1155 mintTo to thirdweb v5
1 parent 1505c30 commit 52abd1a

File tree

3 files changed

+99
-40
lines changed

3 files changed

+99
-40
lines changed

src/server/routes/contract/extensions/erc1155/write/mintTo.ts

Lines changed: 57 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
import { Type, type Static } from "@sinclair/typebox";
22
import type { FastifyInstance } from "fastify";
33
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";
610
import { AddressSchema } from "../../../../../schemas/address";
711
import { nftAndSupplySchema } from "../../../../../schemas/nft";
812
import {
@@ -12,10 +16,13 @@ import {
1216
transactionWritesResponseSchema,
1317
} from "../../../../../schemas/sharedApiSchemas";
1418
import { txOverridesWithValueSchema } from "../../../../../schemas/txOverrides";
15-
import { walletWithAAHeaderSchema } from "../../../../../schemas/wallet";
19+
import {
20+
maybeAddress,
21+
requiredAddress,
22+
walletWithAAHeaderSchema,
23+
} from "../../../../../schemas/wallet";
1624
import { getChainIdFromChain } from "../../../../../utils/chain";
1725

18-
// INPUTS
1926
const requestSchema = erc1155ContractParamSchema;
2027
const 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({

src/server/routes/contract/extensions/erc20/write/mintTo.ts

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import { Type, type Static } from "@sinclair/typebox";
22
import type { FastifyInstance } from "fastify";
33
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/erc20";
6+
import { getChain } from "../../../../../../utils/chain";
7+
import { thirdwebClient } from "../../../../../../utils/sdk";
8+
import { queueTransaction } from "../../../../../../utils/transaction/queueTransation";
69
import { AddressSchema } from "../../../../../schemas/address";
710
import {
811
erc20ContractParamSchema,
@@ -11,7 +14,11 @@ import {
1114
transactionWritesResponseSchema,
1215
} from "../../../../../schemas/sharedApiSchemas";
1316
import { txOverridesWithValueSchema } from "../../../../../schemas/txOverrides";
14-
import { walletWithAAHeaderSchema } from "../../../../../schemas/wallet";
17+
import {
18+
maybeAddress,
19+
requiredAddress,
20+
walletWithAAHeaderSchema,
21+
} from "../../../../../schemas/wallet";
1522
import { getChainIdFromChain } from "../../../../../utils/chain";
1623

1724
// INPUTS
@@ -59,31 +66,46 @@ export async function erc20mintTo(fastify: FastifyInstance) {
5966
},
6067
},
6168
handler: async (request, reply) => {
62-
const { chain, contractAddress } = request.params;
69+
const { chain: _chain, contractAddress } = request.params;
6370
const { simulateTx } = request.query;
6471
const { toAddress, amount, txOverrides } = request.body;
6572
const {
66-
"x-backend-wallet-address": walletAddress,
73+
"x-backend-wallet-address": fromAddress,
6774
"x-account-address": accountAddress,
6875
"x-idempotency-key": idempotencyKey,
76+
"x-account-factory-address": accountFactoryAddress,
77+
"x-account-salt": accountSalt,
6978
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
7079

71-
const chainId = await getChainIdFromChain(chain);
72-
const contract = await getContract({
73-
chainId,
74-
contractAddress,
75-
walletAddress,
76-
accountAddress,
80+
const chainId = await getChainIdFromChain(_chain);
81+
const chain = await getChain(chainId);
82+
83+
const contract = getContract({
84+
chain,
85+
client: thirdwebClient,
86+
address: contractAddress,
87+
});
88+
const transaction = mintTo({
89+
contract,
90+
to: toAddress,
91+
amount,
7792
});
78-
const tx = await contract.erc20.mintTo.prepare(toAddress, amount);
7993

80-
const queueId = await queueTx({
81-
tx,
82-
chainId,
83-
simulateTx,
84-
extension: "erc20",
85-
idempotencyKey,
94+
const queueId = await queueTransaction({
95+
transaction,
96+
fromAddress: requiredAddress(fromAddress, "x-backend-wallet-address"),
97+
toAddress: maybeAddress(contractAddress, "to"),
98+
accountAddress: maybeAddress(accountAddress, "x-account-address"),
99+
accountFactoryAddress: maybeAddress(
100+
accountFactoryAddress,
101+
"x-account-factory-address",
102+
),
103+
accountSalt,
86104
txOverrides,
105+
idempotencyKey,
106+
extension: "erc20",
107+
functionName: "mintTo",
108+
shouldSimulate: simulateTx,
87109
});
88110

89111
reply.status(StatusCodes.OK).send({

src/server/routes/contract/extensions/erc721/write/mintTo.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,8 @@ export async function erc721mintTo(fastify: FastifyInstance) {
120120
accountSalt,
121121
txOverrides,
122122
idempotencyKey,
123+
extension: "erc721",
124+
functionName: "mintTo",
123125
shouldSimulate: simulateTx,
124126
});
125127

0 commit comments

Comments
 (0)