Skip to content

Commit a34fbf9

Browse files
authored
Revert "Revert "chore: port /contract/write to V5 (#661)" (#670)" (#671)
This reverts commit 468dea0.
1 parent 035cb16 commit a34fbf9

File tree

10 files changed

+353
-224
lines changed

10 files changed

+353
-224
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@
6969
"prisma": "^5.14.0",
7070
"prom-client": "^15.1.3",
7171
"superjson": "^2.2.1",
72-
"thirdweb": "^5.45.1",
72+
"thirdweb": "^5.56.0",
7373
"uuid": "^9.0.1",
7474
"winston": "^3.14.1",
7575
"zod": "^3.23.8"

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

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import { Static, Type } from "@sinclair/typebox";
22
import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4-
import { Address, Hex, encode } from "thirdweb";
4+
import { Address } from "thirdweb";
55
import { claimTo } from "thirdweb/extensions/erc1155";
66
import { getContractV5 } from "../../../../../../utils/cache/getContractv5";
7-
import { maybeBigInt } from "../../../../../../utils/primitiveTypes";
8-
import { insertTransaction } from "../../../../../../utils/transaction/insertTransaction";
9-
import { createCustomError } from "../../../../../middleware/error";
7+
import { queueTransaction } from "../../../../../../utils/transaction/queueTransation";
108
import {
119
erc1155ContractParamSchema,
1210
requestQuerystringSchema,
1311
standardResponseSchema,
1412
transactionWritesResponseSchema,
1513
} from "../../../../../schemas/sharedApiSchemas";
1614
import { txOverridesWithValueSchema } from "../../../../../schemas/txOverrides";
17-
import { walletWithAAHeaderSchema } from "../../../../../schemas/wallet";
15+
import {
16+
maybeAddress,
17+
requiredAddress,
18+
walletWithAAHeaderSchema,
19+
} from "../../../../../schemas/wallet";
1820
import { getChainIdFromChain } from "../../../../../utils/chain";
1921

2022
// INPUTS
@@ -71,10 +73,11 @@ export async function erc1155claimTo(fastify: FastifyInstance) {
7173
"x-backend-wallet-address": fromAddress,
7274
"x-account-address": accountAddress,
7375
"x-idempotency-key": idempotencyKey,
76+
"x-account-factory-address": accountFactoryAddress,
7477
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
7578

7679
const chainId = await getChainIdFromChain(chain);
77-
const contract = await getContractV5({
80+
const contract = getContractV5({
7881
chainId,
7982
contractAddress,
8083
});
@@ -87,47 +90,19 @@ export async function erc1155claimTo(fastify: FastifyInstance) {
8790
tokenId: BigInt(tokenId),
8891
});
8992

90-
let data: Hex;
91-
try {
92-
data = await encode(transaction);
93-
} catch (e) {
94-
throw createCustomError(`${e}`, StatusCodes.BAD_REQUEST, "BAD_REQUEST");
95-
}
96-
97-
let queueId: string;
98-
const insertedTransaction = {
99-
chainId,
100-
from: fromAddress as Address,
101-
to: contractAddress as Address | undefined,
102-
data,
103-
value: maybeBigInt(txOverrides?.value),
104-
gas: maybeBigInt(txOverrides?.gas),
105-
maxFeePerGas: maybeBigInt(txOverrides?.maxFeePerGas),
106-
maxPriorityFeePerGas: maybeBigInt(txOverrides?.maxPriorityFeePerGas),
107-
};
108-
109-
if (accountAddress) {
110-
queueId = await insertTransaction({
111-
insertedTransaction: {
112-
...insertedTransaction,
113-
isUserOp: true,
114-
accountAddress: accountAddress as Address,
115-
signerAddress: fromAddress as Address,
116-
target: contractAddress as Address | undefined,
117-
},
118-
shouldSimulate: simulateTx,
119-
idempotencyKey,
120-
});
121-
} else {
122-
queueId = await insertTransaction({
123-
insertedTransaction: {
124-
...insertedTransaction,
125-
isUserOp: false,
126-
},
127-
shouldSimulate: simulateTx,
128-
idempotencyKey,
129-
});
130-
}
93+
const queueId = await queueTransaction({
94+
transaction,
95+
fromAddress: requiredAddress(fromAddress, "x-backend-wallet-address"),
96+
toAddress: maybeAddress(contractAddress, "to"),
97+
accountAddress: maybeAddress(accountAddress, "x-account-address"),
98+
accountFactoryAddress: maybeAddress(
99+
accountFactoryAddress,
100+
"x-account-factory-address",
101+
),
102+
txOverrides,
103+
idempotencyKey,
104+
shouldSimulate: simulateTx,
105+
});
131106

132107
reply.status(StatusCodes.OK).send({
133108
result: {

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

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import { Static, Type } from "@sinclair/typebox";
22
import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4-
import { Address, Hex, encode } from "thirdweb";
4+
import { Address } from "thirdweb";
55
import { claimTo } from "thirdweb/extensions/erc20";
66
import { getContractV5 } from "../../../../../../utils/cache/getContractv5";
7-
import { maybeBigInt } from "../../../../../../utils/primitiveTypes";
8-
import { insertTransaction } from "../../../../../../utils/transaction/insertTransaction";
9-
import { createCustomError } from "../../../../../middleware/error";
7+
import { queueTransaction } from "../../../../../../utils/transaction/queueTransation";
108
import {
119
erc20ContractParamSchema,
1210
requestQuerystringSchema,
1311
standardResponseSchema,
1412
transactionWritesResponseSchema,
1513
} from "../../../../../schemas/sharedApiSchemas";
1614
import { txOverridesWithValueSchema } from "../../../../../schemas/txOverrides";
17-
import { walletWithAAHeaderSchema } from "../../../../../schemas/wallet";
15+
import {
16+
maybeAddress,
17+
requiredAddress,
18+
walletWithAAHeaderSchema,
19+
} from "../../../../../schemas/wallet";
1820
import { getChainIdFromChain } from "../../../../../utils/chain";
1921

2022
// INPUTS
@@ -68,10 +70,11 @@ export async function erc20claimTo(fastify: FastifyInstance) {
6870
"x-backend-wallet-address": fromAddress,
6971
"x-account-address": accountAddress,
7072
"x-idempotency-key": idempotencyKey,
73+
"x-account-factory-address": accountFactoryAddress,
7174
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
7275

7376
const chainId = await getChainIdFromChain(chain);
74-
const contract = await getContractV5({
77+
const contract = getContractV5({
7578
chainId,
7679
contractAddress,
7780
});
@@ -82,47 +85,19 @@ export async function erc20claimTo(fastify: FastifyInstance) {
8285
quantity: amount,
8386
});
8487

85-
let data: Hex;
86-
try {
87-
data = await encode(transaction);
88-
} catch (e) {
89-
throw createCustomError(`${e}`, StatusCodes.BAD_REQUEST, "BAD_REQUEST");
90-
}
91-
92-
let queueId: string;
93-
const insertedTransaction = {
94-
chainId,
95-
from: fromAddress as Address,
96-
to: contractAddress as Address | undefined,
97-
data,
98-
value: maybeBigInt(txOverrides?.value),
99-
gas: maybeBigInt(txOverrides?.gas),
100-
maxFeePerGas: maybeBigInt(txOverrides?.maxFeePerGas),
101-
maxPriorityFeePerGas: maybeBigInt(txOverrides?.maxPriorityFeePerGas),
102-
};
103-
104-
if (accountAddress) {
105-
queueId = await insertTransaction({
106-
insertedTransaction: {
107-
...insertedTransaction,
108-
isUserOp: true,
109-
accountAddress: accountAddress as Address,
110-
signerAddress: fromAddress as Address,
111-
target: contractAddress as Address | undefined,
112-
},
113-
shouldSimulate: simulateTx,
114-
idempotencyKey,
115-
});
116-
} else {
117-
queueId = await insertTransaction({
118-
insertedTransaction: {
119-
...insertedTransaction,
120-
isUserOp: false,
121-
},
122-
shouldSimulate: simulateTx,
123-
idempotencyKey,
124-
});
125-
}
88+
const queueId = await queueTransaction({
89+
transaction,
90+
fromAddress: requiredAddress(fromAddress, "x-backend-wallet-address"),
91+
toAddress: maybeAddress(contractAddress, "to"),
92+
accountAddress: maybeAddress(accountAddress, "x-account-address"),
93+
accountFactoryAddress: maybeAddress(
94+
accountFactoryAddress,
95+
"x-account-factory-address",
96+
),
97+
txOverrides,
98+
idempotencyKey,
99+
shouldSimulate: simulateTx,
100+
});
126101

127102
reply.status(StatusCodes.OK).send({
128103
result: {

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

Lines changed: 22 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,22 @@
11
import { Static, Type } from "@sinclair/typebox";
22
import { FastifyInstance } from "fastify";
33
import { StatusCodes } from "http-status-codes";
4-
import { Address, Hex, encode } from "thirdweb";
4+
import { Address } from "thirdweb";
55
import { claimTo } from "thirdweb/extensions/erc721";
66
import { getContractV5 } from "../../../../../../utils/cache/getContractv5";
7-
import { maybeBigInt } from "../../../../../../utils/primitiveTypes";
8-
import { insertTransaction } from "../../../../../../utils/transaction/insertTransaction";
9-
import { createCustomError } from "../../../../../middleware/error";
7+
import { queueTransaction } from "../../../../../../utils/transaction/queueTransation";
108
import {
119
contractParamSchema,
1210
requestQuerystringSchema,
1311
standardResponseSchema,
1412
transactionWritesResponseSchema,
1513
} from "../../../../../schemas/sharedApiSchemas";
1614
import { txOverridesWithValueSchema } from "../../../../../schemas/txOverrides";
17-
import { walletWithAAHeaderSchema } from "../../../../../schemas/wallet";
15+
import {
16+
maybeAddress,
17+
requiredAddress,
18+
walletWithAAHeaderSchema,
19+
} from "../../../../../schemas/wallet";
1820
import { getChainIdFromChain } from "../../../../../utils/chain";
1921

2022
// INPUTS
@@ -67,10 +69,11 @@ export async function erc721claimTo(fastify: FastifyInstance) {
6769
"x-backend-wallet-address": fromAddress,
6870
"x-account-address": accountAddress,
6971
"x-idempotency-key": idempotencyKey,
72+
"x-account-factory-address": accountFactoryAddress,
7073
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
7174

7275
const chainId = await getChainIdFromChain(chain);
73-
const contract = await getContractV5({
76+
const contract = getContractV5({
7477
chainId,
7578
contractAddress,
7679
});
@@ -81,47 +84,19 @@ export async function erc721claimTo(fastify: FastifyInstance) {
8184
quantity: BigInt(quantity),
8285
});
8386

84-
let data: Hex;
85-
try {
86-
data = await encode(transaction);
87-
} catch (e) {
88-
throw createCustomError(`${e}`, StatusCodes.BAD_REQUEST, "BAD_REQUEST");
89-
}
90-
91-
let queueId: string;
92-
const insertedTransaction = {
93-
chainId,
94-
from: fromAddress as Address,
95-
to: contractAddress as Address | undefined,
96-
data,
97-
value: maybeBigInt(txOverrides?.value),
98-
gas: maybeBigInt(txOverrides?.gas),
99-
maxFeePerGas: maybeBigInt(txOverrides?.maxFeePerGas),
100-
maxPriorityFeePerGas: maybeBigInt(txOverrides?.maxPriorityFeePerGas),
101-
};
102-
103-
if (accountAddress) {
104-
queueId = await insertTransaction({
105-
insertedTransaction: {
106-
...insertedTransaction,
107-
isUserOp: true,
108-
accountAddress: accountAddress as Address,
109-
signerAddress: fromAddress as Address,
110-
target: contractAddress as Address | undefined,
111-
},
112-
shouldSimulate: simulateTx,
113-
idempotencyKey,
114-
});
115-
} else {
116-
queueId = await insertTransaction({
117-
insertedTransaction: {
118-
...insertedTransaction,
119-
isUserOp: false,
120-
},
121-
shouldSimulate: simulateTx,
122-
idempotencyKey,
123-
});
124-
}
87+
const queueId = await queueTransaction({
88+
transaction,
89+
fromAddress: requiredAddress(fromAddress, "x-backend-wallet-address"),
90+
toAddress: maybeAddress(contractAddress, "to"),
91+
accountAddress: maybeAddress(accountAddress, "x-account-address"),
92+
accountFactoryAddress: maybeAddress(
93+
accountFactoryAddress,
94+
"x-account-factory-address",
95+
),
96+
txOverrides,
97+
idempotencyKey,
98+
shouldSimulate: simulateTx,
99+
});
125100

126101
reply.status(StatusCodes.OK).send({
127102
result: {

src/server/routes/contract/write/write.ts

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import { Static, Type } from "@sinclair/typebox";
22
import { 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 { prepareContractCall, resolveMethod } from "thirdweb";
5+
import { getContractV5 } from "../../../../utils/cache/getContractv5";
6+
import { maybeBigInt } from "../../../../utils/primitiveTypes";
7+
import { queueTransaction } from "../../../../utils/transaction/queueTransation";
68
import { abiSchema } from "../../../schemas/contract";
79
import {
810
contractParamSchema,
@@ -13,6 +15,7 @@ import {
1315
import { txOverridesWithValueSchema } from "../../../schemas/txOverrides";
1416
import {
1517
maybeAddress,
18+
requiredAddress,
1619
walletWithAAHeaderSchema,
1720
} from "../../../schemas/wallet";
1821
import { getChainIdFromChain } from "../../../utils/chain";
@@ -66,39 +69,46 @@ export async function writeToContract(fastify: FastifyInstance) {
6669
const { simulateTx } = request.query;
6770
const { functionName, args, txOverrides, abi } = request.body;
6871
const {
69-
"x-backend-wallet-address": walletAddress,
72+
"x-backend-wallet-address": fromAddress,
7073
"x-account-address": accountAddress,
7174
"x-idempotency-key": idempotencyKey,
7275
"x-account-factory-address": accountFactoryAddress,
7376
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
7477

7578
const chainId = await getChainIdFromChain(chain);
76-
const contract = await getContract({
79+
const contract = getContractV5({
7780
chainId,
7881
contractAddress,
79-
walletAddress,
80-
accountAddress,
8182
abi,
8283
});
8384

84-
const tx = contract.prepare(functionName, args, {
85-
value: txOverrides?.value,
86-
gasLimit: txOverrides?.gas,
87-
maxFeePerGas: txOverrides?.maxFeePerGas,
88-
maxPriorityFeePerGas: txOverrides?.maxPriorityFeePerGas,
85+
// 3 possible ways to get function from abi:
86+
// 1. functionName passed as solidity signature
87+
// 2. functionName passed as function name + passed in ABI
88+
// 3. functionName passed as function name + inferred ABI (fetched at encode time)
89+
// this is all handled inside the `resolveMethod` function
90+
const transaction = prepareContractCall({
91+
contract,
92+
method: resolveMethod(functionName),
93+
params: args,
94+
gas: maybeBigInt(txOverrides?.gas),
95+
value: maybeBigInt(txOverrides?.value),
96+
maxFeePerGas: maybeBigInt(txOverrides?.maxFeePerGas),
97+
maxPriorityFeePerGas: maybeBigInt(txOverrides?.maxPriorityFeePerGas),
8998
});
9099

91-
const queueId = await queueTx({
92-
tx,
93-
chainId,
94-
simulateTx,
95-
extension: "none",
96-
idempotencyKey,
97-
txOverrides,
100+
const queueId = await queueTransaction({
101+
transaction,
102+
fromAddress: requiredAddress(fromAddress, "x-backend-wallet-address"),
103+
toAddress: maybeAddress(contractAddress, "to"),
104+
accountAddress: maybeAddress(accountAddress, "x-account-address"),
98105
accountFactoryAddress: maybeAddress(
99106
accountFactoryAddress,
100107
"x-account-factory-address",
101108
),
109+
txOverrides,
110+
idempotencyKey,
111+
shouldSimulate: simulateTx,
102112
});
103113

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

0 commit comments

Comments
 (0)