Skip to content

Commit

Permalink
chore: port /contract/write to V5 (#661)
Browse files Browse the repository at this point in the history
* Port /contract/write to V5

* reuse maybeAddress
  • Loading branch information
joaquim-verges authored Sep 21, 2024
1 parent 9d21d26 commit 044f922
Show file tree
Hide file tree
Showing 10 changed files with 353 additions and 224 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
"prisma": "^5.14.0",
"prom-client": "^15.1.3",
"superjson": "^2.2.1",
"thirdweb": "^5.45.1",
"thirdweb": "^5.56.0",
"uuid": "^9.0.1",
"winston": "^3.14.1",
"zod": "^3.23.8"
Expand Down
69 changes: 22 additions & 47 deletions src/server/routes/contract/extensions/erc1155/write/claimTo.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { Address, Hex, encode } from "thirdweb";
import { Address } from "thirdweb";
import { claimTo } from "thirdweb/extensions/erc1155";
import { getContractV5 } from "../../../../../../utils/cache/getContractv5";
import { maybeBigInt } from "../../../../../../utils/primitiveTypes";
import { insertTransaction } from "../../../../../../utils/transaction/insertTransaction";
import { createCustomError } from "../../../../../middleware/error";
import { queueTransaction } from "../../../../../../utils/transaction/queueTransation";
import {
erc1155ContractParamSchema,
requestQuerystringSchema,
standardResponseSchema,
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 @@ -71,10 +73,11 @@ export async function erc1155claimTo(fastify: FastifyInstance) {
"x-backend-wallet-address": fromAddress,
"x-account-address": accountAddress,
"x-idempotency-key": idempotencyKey,
"x-account-factory-address": accountFactoryAddress,
} = request.headers as Static<typeof walletWithAAHeaderSchema>;

const chainId = await getChainIdFromChain(chain);
const contract = await getContractV5({
const contract = getContractV5({
chainId,
contractAddress,
});
Expand All @@ -87,47 +90,19 @@ export async function erc1155claimTo(fastify: FastifyInstance) {
tokenId: BigInt(tokenId),
});

let data: Hex;
try {
data = await encode(transaction);
} catch (e) {
throw createCustomError(`${e}`, StatusCodes.BAD_REQUEST, "BAD_REQUEST");
}

let queueId: string;
const insertedTransaction = {
chainId,
from: fromAddress as Address,
to: contractAddress as Address | undefined,
data,
value: maybeBigInt(txOverrides?.value),
gas: maybeBigInt(txOverrides?.gas),
maxFeePerGas: maybeBigInt(txOverrides?.maxFeePerGas),
maxPriorityFeePerGas: maybeBigInt(txOverrides?.maxPriorityFeePerGas),
};

if (accountAddress) {
queueId = await insertTransaction({
insertedTransaction: {
...insertedTransaction,
isUserOp: true,
accountAddress: accountAddress as Address,
signerAddress: fromAddress as Address,
target: contractAddress as Address | undefined,
},
shouldSimulate: simulateTx,
idempotencyKey,
});
} else {
queueId = await insertTransaction({
insertedTransaction: {
...insertedTransaction,
isUserOp: false,
},
shouldSimulate: simulateTx,
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",
),
txOverrides,
idempotencyKey,
shouldSimulate: simulateTx,
});

reply.status(StatusCodes.OK).send({
result: {
Expand Down
69 changes: 22 additions & 47 deletions src/server/routes/contract/extensions/erc20/write/claimTo.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { Address, Hex, encode } from "thirdweb";
import { Address } from "thirdweb";
import { claimTo } from "thirdweb/extensions/erc20";
import { getContractV5 } from "../../../../../../utils/cache/getContractv5";
import { maybeBigInt } from "../../../../../../utils/primitiveTypes";
import { insertTransaction } from "../../../../../../utils/transaction/insertTransaction";
import { createCustomError } from "../../../../../middleware/error";
import { queueTransaction } from "../../../../../../utils/transaction/queueTransation";
import {
erc20ContractParamSchema,
requestQuerystringSchema,
standardResponseSchema,
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 @@ -68,10 +70,11 @@ export async function erc20claimTo(fastify: FastifyInstance) {
"x-backend-wallet-address": fromAddress,
"x-account-address": accountAddress,
"x-idempotency-key": idempotencyKey,
"x-account-factory-address": accountFactoryAddress,
} = request.headers as Static<typeof walletWithAAHeaderSchema>;

const chainId = await getChainIdFromChain(chain);
const contract = await getContractV5({
const contract = getContractV5({
chainId,
contractAddress,
});
Expand All @@ -82,47 +85,19 @@ export async function erc20claimTo(fastify: FastifyInstance) {
quantity: amount,
});

let data: Hex;
try {
data = await encode(transaction);
} catch (e) {
throw createCustomError(`${e}`, StatusCodes.BAD_REQUEST, "BAD_REQUEST");
}

let queueId: string;
const insertedTransaction = {
chainId,
from: fromAddress as Address,
to: contractAddress as Address | undefined,
data,
value: maybeBigInt(txOverrides?.value),
gas: maybeBigInt(txOverrides?.gas),
maxFeePerGas: maybeBigInt(txOverrides?.maxFeePerGas),
maxPriorityFeePerGas: maybeBigInt(txOverrides?.maxPriorityFeePerGas),
};

if (accountAddress) {
queueId = await insertTransaction({
insertedTransaction: {
...insertedTransaction,
isUserOp: true,
accountAddress: accountAddress as Address,
signerAddress: fromAddress as Address,
target: contractAddress as Address | undefined,
},
shouldSimulate: simulateTx,
idempotencyKey,
});
} else {
queueId = await insertTransaction({
insertedTransaction: {
...insertedTransaction,
isUserOp: false,
},
shouldSimulate: simulateTx,
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",
),
txOverrides,
idempotencyKey,
shouldSimulate: simulateTx,
});

reply.status(StatusCodes.OK).send({
result: {
Expand Down
69 changes: 22 additions & 47 deletions src/server/routes/contract/extensions/erc721/write/claimTo.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { Address, Hex, encode } from "thirdweb";
import { Address } from "thirdweb";
import { claimTo } from "thirdweb/extensions/erc721";
import { getContractV5 } from "../../../../../../utils/cache/getContractv5";
import { maybeBigInt } from "../../../../../../utils/primitiveTypes";
import { insertTransaction } from "../../../../../../utils/transaction/insertTransaction";
import { createCustomError } from "../../../../../middleware/error";
import { queueTransaction } from "../../../../../../utils/transaction/queueTransation";
import {
contractParamSchema,
requestQuerystringSchema,
standardResponseSchema,
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 @@ -67,10 +69,11 @@ export async function erc721claimTo(fastify: FastifyInstance) {
"x-backend-wallet-address": fromAddress,
"x-account-address": accountAddress,
"x-idempotency-key": idempotencyKey,
"x-account-factory-address": accountFactoryAddress,
} = request.headers as Static<typeof walletWithAAHeaderSchema>;

const chainId = await getChainIdFromChain(chain);
const contract = await getContractV5({
const contract = getContractV5({
chainId,
contractAddress,
});
Expand All @@ -81,47 +84,19 @@ export async function erc721claimTo(fastify: FastifyInstance) {
quantity: BigInt(quantity),
});

let data: Hex;
try {
data = await encode(transaction);
} catch (e) {
throw createCustomError(`${e}`, StatusCodes.BAD_REQUEST, "BAD_REQUEST");
}

let queueId: string;
const insertedTransaction = {
chainId,
from: fromAddress as Address,
to: contractAddress as Address | undefined,
data,
value: maybeBigInt(txOverrides?.value),
gas: maybeBigInt(txOverrides?.gas),
maxFeePerGas: maybeBigInt(txOverrides?.maxFeePerGas),
maxPriorityFeePerGas: maybeBigInt(txOverrides?.maxPriorityFeePerGas),
};

if (accountAddress) {
queueId = await insertTransaction({
insertedTransaction: {
...insertedTransaction,
isUserOp: true,
accountAddress: accountAddress as Address,
signerAddress: fromAddress as Address,
target: contractAddress as Address | undefined,
},
shouldSimulate: simulateTx,
idempotencyKey,
});
} else {
queueId = await insertTransaction({
insertedTransaction: {
...insertedTransaction,
isUserOp: false,
},
shouldSimulate: simulateTx,
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",
),
txOverrides,
idempotencyKey,
shouldSimulate: simulateTx,
});

reply.status(StatusCodes.OK).send({
result: {
Expand Down
46 changes: 28 additions & 18 deletions src/server/routes/contract/write/write.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Static, Type } from "@sinclair/typebox";
import { FastifyInstance } from "fastify";
import { StatusCodes } from "http-status-codes";
import { queueTx } from "../../../../db/transactions/queueTx";
import { getContract } from "../../../../utils/cache/getContract";
import { prepareContractCall, resolveMethod } from "thirdweb";
import { getContractV5 } from "../../../../utils/cache/getContractv5";
import { maybeBigInt } from "../../../../utils/primitiveTypes";
import { queueTransaction } from "../../../../utils/transaction/queueTransation";
import { abiSchema } from "../../../schemas/contract";
import {
contractParamSchema,
Expand All @@ -13,6 +15,7 @@ import {
import { txOverridesWithValueSchema } from "../../../schemas/txOverrides";
import {
maybeAddress,
requiredAddress,
walletWithAAHeaderSchema,
} from "../../../schemas/wallet";
import { getChainIdFromChain } from "../../../utils/chain";
Expand Down Expand Up @@ -66,39 +69,46 @@ export async function writeToContract(fastify: FastifyInstance) {
const { simulateTx } = request.query;
const { functionName, args, txOverrides, abi } = 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,
} = request.headers as Static<typeof walletWithAAHeaderSchema>;

const chainId = await getChainIdFromChain(chain);
const contract = await getContract({
const contract = getContractV5({
chainId,
contractAddress,
walletAddress,
accountAddress,
abi,
});

const tx = contract.prepare(functionName, args, {
value: txOverrides?.value,
gasLimit: txOverrides?.gas,
maxFeePerGas: txOverrides?.maxFeePerGas,
maxPriorityFeePerGas: txOverrides?.maxPriorityFeePerGas,
// 3 possible ways to get function from abi:
// 1. functionName passed as solidity signature
// 2. functionName passed as function name + passed in ABI
// 3. functionName passed as function name + inferred ABI (fetched at encode time)
// this is all handled inside the `resolveMethod` function
const transaction = prepareContractCall({
contract,
method: resolveMethod(functionName),
params: args,
gas: maybeBigInt(txOverrides?.gas),
value: maybeBigInt(txOverrides?.value),
maxFeePerGas: maybeBigInt(txOverrides?.maxFeePerGas),
maxPriorityFeePerGas: maybeBigInt(txOverrides?.maxPriorityFeePerGas),
});

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

reply.status(StatusCodes.OK).send({
Expand Down
Loading

0 comments on commit 044f922

Please sign in to comment.