Skip to content

Commit

Permalink
chore: Update ERC20 and ERC1155 mintTo to thirdweb v5
Browse files Browse the repository at this point in the history
  • Loading branch information
arcoraven committed Dec 8, 2024
1 parent 1505c30 commit 52abd1a
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 40 deletions.
79 changes: 57 additions & 22 deletions src/server/routes/contract/extensions/erc1155/write/mintTo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
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/erc1155";
import type { NFTInput } from "thirdweb/utils";
import { getChain } from "../../../../../../utils/chain";
import { thirdwebClient } from "../../../../../../utils/sdk";
import { queueTransaction } from "../../../../../../utils/transaction/queueTransation";
import { AddressSchema } from "../../../../../schemas/address";
import { nftAndSupplySchema } from "../../../../../schemas/nft";
import {
Expand All @@ -12,10 +16,13 @@ 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
const requestSchema = erc1155ContractParamSchema;
const requestBodySchema = Type.Object({
receiver: {
Expand Down Expand Up @@ -66,34 +73,62 @@ export async function erc1155mintTo(fastify: FastifyInstance) {
},
},
handler: async (request, reply) => {
const { chain, contractAddress } = request.params;
const { chain: _chain, contractAddress } = request.params;
const { simulateTx } = request.query;
const { receiver, metadataWithSupply, 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<typeof walletWithAAHeaderSchema>;

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.erc1155.mintTo.prepare(
receiver,
metadataWithSupply,
);

const queueId = await queueTx({
tx,
chainId,
simulateTx,
extension: "erc1155",
idempotencyKey,
// Backward compatibility: Transform the request body's v4 shape to v5.
const { metadata, supply } = metadataWithSupply;
const nft: NFTInput | string =
typeof metadata === "string"
? metadata
: {
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,
};
const transaction = mintTo({
contract,
to: receiver,
nft,
supply: BigInt(supply),
});

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,
extension: "erc1155",
functionName: "mintTo",
shouldSimulate: simulateTx,
});

reply.status(StatusCodes.OK).send({
Expand Down
58 changes: 40 additions & 18 deletions src/server/routes/contract/extensions/erc20/write/mintTo.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
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/erc20";
import { getChain } from "../../../../../../utils/chain";
import { thirdwebClient } from "../../../../../../utils/sdk";
import { queueTransaction } from "../../../../../../utils/transaction/queueTransation";
import { AddressSchema } from "../../../../../schemas/address";
import {
erc20ContractParamSchema,
Expand All @@ -11,7 +14,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
Expand Down Expand Up @@ -59,31 +66,46 @@ export async function erc20mintTo(fastify: FastifyInstance) {
},
},
handler: async (request, reply) => {
const { chain, contractAddress } = request.params;
const { chain: _chain, contractAddress } = request.params;
const { simulateTx } = request.query;
const { toAddress, amount, 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<typeof walletWithAAHeaderSchema>;

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 transaction = mintTo({
contract,
to: toAddress,
amount,
});
const tx = await contract.erc20.mintTo.prepare(toAddress, amount);

const queueId = await queueTx({
tx,
chainId,
simulateTx,
extension: "erc20",
idempotencyKey,
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,
extension: "erc20",
functionName: "mintTo",
shouldSimulate: simulateTx,
});

reply.status(StatusCodes.OK).send({
Expand Down
2 changes: 2 additions & 0 deletions src/server/routes/contract/extensions/erc721/write/mintTo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ export async function erc721mintTo(fastify: FastifyInstance) {
accountSalt,
txOverrides,
idempotencyKey,
extension: "erc721",
functionName: "mintTo",
shouldSimulate: simulateTx,
});

Expand Down

0 comments on commit 52abd1a

Please sign in to comment.