Skip to content

Commit 0a1fd55

Browse files
joaquim-vergesd4mr
andauthored
Refactor e2e tests (#667)
* e2e tests refactor * fix userop types error --------- Co-authored-by: Prithvish Baidya <deformercoding@gmail.com>
1 parent a34fbf9 commit 0a1fd55

File tree

22 files changed

+5989
-233
lines changed

22 files changed

+5989
-233
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.56.0",
72+
"thirdweb": "^5.58.4",
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ export async function erc1155claimTo(fastify: FastifyInstance) {
7777
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
7878

7979
const chainId = await getChainIdFromChain(chain);
80-
const contract = getContractV5({
80+
const contract = await getContractV5({
8181
chainId,
8282
contractAddress,
8383
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export async function erc20claimTo(fastify: FastifyInstance) {
7474
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
7575

7676
const chainId = await getChainIdFromChain(chain);
77-
const contract = getContractV5({
77+
const contract = await getContractV5({
7878
chainId,
7979
contractAddress,
8080
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export async function erc721claimTo(fastify: FastifyInstance) {
7373
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
7474

7575
const chainId = await getChainIdFromChain(chain);
76-
const contract = getContractV5({
76+
const contract = await getContractV5({
7777
chainId,
7878
contractAddress,
7979
});

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@ import { SignedPayload721WithQuantitySignature } from "@thirdweb-dev/sdk";
33
import { BigNumber } from "ethers";
44
import { FastifyInstance } from "fastify";
55
import { StatusCodes } from "http-status-codes";
6-
import { Address, Hex, getContract as getContractV5 } from "thirdweb";
6+
import { Address, Hex } from "thirdweb";
77
import { mintWithSignature } from "thirdweb/extensions/erc721";
88
import { resolvePromisedValue } from "thirdweb/utils";
99
import { queueTx } from "../../../../../../db/transactions/queueTx";
1010
import { getContract } from "../../../../../../utils/cache/getContract";
11-
import { getChain } from "../../../../../../utils/chain";
11+
import { getContractV5 } from "../../../../../../utils/cache/getContractv5";
1212
import { maybeBigInt } from "../../../../../../utils/primitiveTypes";
13-
import { thirdwebClient } from "../../../../../../utils/sdk";
1413
import { insertTransaction } from "../../../../../../utils/transaction/insertTransaction";
1514
import { thirdwebSdkVersionSchema } from "../../../../../schemas/httpHeaders/thirdwebSdkVersion";
1615
import { signature721OutputSchema } from "../../../../../schemas/nft";
@@ -97,10 +96,9 @@ export async function erc721SignatureMint(fastify: FastifyInstance) {
9796
let queueId: string;
9897
if (sdkVersion === "5") {
9998
const payloadV5 = payload as Static<typeof signature721OutputSchemaV5>;
100-
const contract = getContractV5({
101-
client: thirdwebClient,
102-
chain: await getChain(chainId),
103-
address: contractAddress,
99+
const contract = await getContractV5({
100+
chainId,
101+
contractAddress,
104102
});
105103
const transaction = mintWithSignature({
106104
contract,

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { prepareContractCall, resolveMethod } from "thirdweb";
55
import { getContractV5 } from "../../../../utils/cache/getContractv5";
66
import { maybeBigInt } from "../../../../utils/primitiveTypes";
77
import { queueTransaction } from "../../../../utils/transaction/queueTransation";
8+
import { createCustomError } from "../../../middleware/error";
89
import { abiSchema } from "../../../schemas/contract";
910
import {
1011
contractParamSchema,
@@ -76,7 +77,7 @@ export async function writeToContract(fastify: FastifyInstance) {
7677
} = request.headers as Static<typeof walletWithAAHeaderSchema>;
7778

7879
const chainId = await getChainIdFromChain(chain);
79-
const contract = getContractV5({
80+
const contract = await getContractV5({
8081
chainId,
8182
contractAddress,
8283
abi,
@@ -87,9 +88,15 @@ export async function writeToContract(fastify: FastifyInstance) {
8788
// 2. functionName passed as function name + passed in ABI
8889
// 3. functionName passed as function name + inferred ABI (fetched at encode time)
8990
// this is all handled inside the `resolveMethod` function
91+
let method;
92+
try {
93+
method = await resolveMethod(functionName)(contract);
94+
} catch (e: any) {
95+
throw createCustomError(`${e}`, StatusCodes.BAD_REQUEST, "BAD_REQUEST");
96+
}
9097
const transaction = prepareContractCall({
9198
contract,
92-
method: resolveMethod(functionName),
99+
method,
93100
params: args,
94101
gas: maybeBigInt(txOverrides?.gas),
95102
value: maybeBigInt(txOverrides?.value),

src/server/schemas/contract/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export interface readSchema extends contractSchemaTypes {
2424
const abiTypeSchema = Type.Object({
2525
type: Type.Optional(Type.String()),
2626
name: Type.Optional(Type.String()),
27+
internalType: Type.Optional(Type.String()),
2728
stateMutability: Type.Optional(Type.String()),
2829
components: Type.Optional(
2930
Type.Array(
@@ -56,6 +57,7 @@ export const abiSchema = Type.Object({
5657
type: Type.String(),
5758
name: Type.Optional(Type.String()),
5859
inputs: Type.Array(abiTypeSchema),
60+
outputs: Type.Array(abiTypeSchema),
5961
stateMutability: Type.Optional(Type.String()),
6062
});
6163

src/utils/cache/getContractv5.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { defineChain, getContract } from "thirdweb";
1+
import { getContract } from "thirdweb";
22
import { thirdwebClient } from "../../utils/sdk";
3+
import { getChain } from "../chain";
34

45
interface GetContractParams {
56
chainId: number;
@@ -8,12 +9,12 @@ interface GetContractParams {
89
}
910

1011
// Using new v5 SDK
11-
export const getContractV5 = ({
12+
export const getContractV5 = async ({
1213
chainId,
1314
contractAddress,
1415
abi,
1516
}: GetContractParams) => {
16-
const definedChain = defineChain(chainId);
17+
const definedChain = await getChain(chainId);
1718

1819
// get a contract
1920
return getContract({

src/utils/transaction/userOperation.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,13 @@ export const generateSignedUserOperation = async (
8888

8989
// Create Unsigned UserOperation
9090
// Todo: Future expose a way to skip paymaster
91-
let unsignedOp = await createUnsignedUserOp({
91+
let unsignedOp = (await createUnsignedUserOp({
9292
transaction: userOpCall,
9393
accountContract: smartAccountContract,
9494
sponsorGas: true,
9595
factoryContract: accountFactoryContract,
9696
adminAddress: signerAddress,
97-
});
97+
})) as UserOperation; // TODO support entrypoint v0.7 accounts
9898

9999
// Pass custom gas limit for UserOperation
100100
if (gas) {
@@ -109,7 +109,10 @@ export const generateSignedUserOperation = async (
109109
client: thirdwebClient,
110110
});
111111

112-
const paymasterAndData = paymasterResult.paymasterAndData;
112+
const paymasterAndData =
113+
"paymasterAndData" in paymasterResult
114+
? paymasterResult.paymasterAndData
115+
: "0x";
113116
if (paymasterAndData && paymasterAndData !== "0x") {
114117
unsignedOp.paymasterAndData = paymasterAndData as Hex;
115118
}
@@ -122,11 +125,12 @@ export const generateSignedUserOperation = async (
122125
});
123126

124127
// Sign UserOperation
125-
const signedUserOp = await signUserOp({
128+
const signedUserOp = (await signUserOp({
129+
client: thirdwebClient,
126130
userOp: unsignedOp,
127131
adminAccount,
128132
chain,
129-
});
133+
})) as UserOperation; // TODO support entrypoint v0.7 accounts
130134

131135
// Return Signed UserOperation
132136
return signedUserOp;

test/e2e/bun.lockb

-51.2 KB
Binary file not shown.

test/e2e/config.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,21 @@
11
import assert from "assert";
2-
import { type Chain } from "viem";
3-
import { anvil } from "viem/chains";
2+
import { anvil, type Chain } from "thirdweb/chains";
43

54
assert(process.env.ENGINE_URL, "ENGINE_URL is required");
65
assert(process.env.ENGINE_ACCESS_TOKEN, "ENGINE_ACCESS_TOKEN is required");
76

87
export const CONFIG: Config = {
98
ACCESS_TOKEN: process.env.ENGINE_ACCESS_TOKEN,
109
URL: process.env.ENGINE_URL,
11-
1210
USE_LOCAL_CHAIN: true,
13-
CHAIN: anvil,
14-
// CHAIN: defineChain({
15-
// name: "B3 Sepolia",
16-
// id: 1993,
17-
// rpcUrls: {
18-
// default: {
19-
// http: ["https://sepolia.b3.fun/http"],
20-
// webSocket: ["wss://sepolia.b3.fun/ws"],
21-
// },
22-
// },
23-
// nativeCurrency: {
24-
// name: "Ether",
25-
// symbol: "ETH",
26-
// decimals: 18,
27-
// },
28-
// }),
29-
11+
CHAIN: {
12+
...anvil,
13+
rpc: "http://127.0.0.1:8545/1",
14+
},
3015
TRANSACTION_COUNT: 500,
3116
TRANSACTIONS_PER_BATCH: 100,
3217
POLLING_INTERVAL: 1000,
33-
3418
STAGGER_MAX: 500,
35-
INITIAL_BALANCE: "10000", // in Ether
3619
};
3720

3821
type Config = {
@@ -42,8 +25,6 @@ type Config = {
4225
TRANSACTIONS_PER_BATCH: number;
4326
POLLING_INTERVAL: number;
4427
STAGGER_MAX: number;
45-
INITIAL_BALANCE: string;
46-
4728
CHAIN: Chain;
4829
USE_LOCAL_CHAIN?: boolean;
4930
};

test/e2e/package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@
1010
},
1111
"type": "module",
1212
"dependencies": {
13-
"@thirdweb-dev/sdk": "^4.0.99",
14-
"ethers": "5",
15-
"thirdweb": "^5.43.2",
16-
"viem": "^2.18.1"
13+
"prool": "^0.0.16",
14+
"thirdweb": "^5.43.2"
1715
}
1816
}

test/e2e/tests/extensions.test.ts

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,20 @@
11
import assert from "assert";
22
import { sleep } from "bun";
3-
import { describe, expect, test } from "bun:test";
4-
import { erc721Abi, getAddress, type Address } from "viem";
3+
import { beforeAll, describe, expect, test } from "bun:test";
4+
import { getAddress, type Address } from "viem";
55
import { CONFIG } from "../config";
6+
import type { setupEngine } from "../utils/engine";
67
import { setup } from "./setup";
78

89
describe("Extensions", () => {
910
let nftContractAddress: Address | undefined;
11+
let engine: ReturnType<typeof setupEngine>;
12+
let backendWallet: Address;
1013

11-
test("Deploy ERC721 Contract", async () => {
12-
const { engine, backendWallet } = await setup();
14+
beforeAll(async () => {
15+
const setupRes = await setup();
16+
engine = setupRes.engine;
17+
backendWallet = setupRes.backendWallet;
1318

1419
const res = await engine.deploy.deployNftCollection(
1520
CONFIG.CHAIN.id.toString(),
@@ -44,8 +49,6 @@ describe("Extensions", () => {
4449
});
4550

4651
test("Mint NFT", async () => {
47-
const { engine, testClient, publicClient, backendWallet } = await setup();
48-
4952
expect(nftContractAddress).toBeDefined();
5053
assert(nftContractAddress, "NFT contract address is not defined");
5154

@@ -79,14 +82,6 @@ describe("Extensions", () => {
7982
nftContractAddress,
8083
);
8184

82-
const viemBalanceOfBackendWallet = await publicClient.readContract({
83-
abi: erc721Abi,
84-
address: nftContractAddress,
85-
functionName: "balanceOf",
86-
args: [backendWallet],
87-
});
88-
8985
expect(engineBalanceOfBackendWallet.result).toEqual("1");
90-
expect(viemBalanceOfBackendWallet).toEqual(1n);
9186
});
9287
});

test/e2e/tests/setup.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,16 @@
11
import { sleep } from "bun";
2-
import { beforeAll } from "bun:test";
2+
import { afterAll, beforeAll } from "bun:test";
33
import {
44
createChain,
55
getEngineBackendWallet,
6-
setWalletBalance,
76
setupEngine,
87
} from "../utils/engine";
98

109
import type { Address } from "viem";
1110
import { CONFIG } from "../config";
12-
import { setupAnvil } from "../utils/anvil";
13-
import { setupPublicClient, setupTestClient } from "../utils/viem";
11+
import { startAnvil, stopAnvil } from "../utils/anvil";
1412

1513
type SetupResult = {
16-
testClient?: ReturnType<typeof setupTestClient>;
17-
publicClient: ReturnType<typeof setupPublicClient>;
1814
engine: ReturnType<typeof setupEngine>;
1915
backendWallet: Address;
2016
};
@@ -26,25 +22,26 @@ export const setup = async (): Promise<SetupResult> => {
2622
return cachedSetup;
2723
}
2824

29-
const publicClient = setupPublicClient();
3025
const engine = setupEngine();
3126
const backendWallet = await getEngineBackendWallet(engine);
32-
let testClient;
27+
await engine.backendWallet.resetNonces();
3328

3429
if (CONFIG.USE_LOCAL_CHAIN) {
35-
setupAnvil();
30+
startAnvil();
3631

37-
testClient = setupTestClient();
3832
await createChain(engine);
39-
await setWalletBalance(backendWallet, CONFIG.INITIAL_BALANCE);
4033
await sleep(1000); // wait for chain to start producing blocks
4134
}
4235

43-
cachedSetup = { testClient, publicClient, engine, backendWallet };
36+
cachedSetup = { engine, backendWallet };
4437
return cachedSetup;
4538
};
4639

4740
// Run setup once before all tests
4841
beforeAll(async () => {
4942
await setup();
5043
});
44+
45+
afterAll(async () => {
46+
await stopAnvil();
47+
});

0 commit comments

Comments
 (0)