Skip to content

Commit c508751

Browse files
refactor: optimize encode with async params (#2636)
1 parent fbf4556 commit c508751

File tree

5 files changed

+40
-40
lines changed

5 files changed

+40
-40
lines changed

.changeset/hot-forks-live.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Optimie encode by caching asyncParams

packages/thirdweb/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@
227227
"test:watch": "vitest -c ./test/vitest.config.ts dev",
228228
"test": "vitest run -c ./test/vitest.config.ts --coverage --bail=1",
229229
"test:cov": "vitest dev -c ./test/vitest.config.ts --coverage",
230-
"test:dev": "vitest dev -c ./test/vitest.config.ts",
230+
"test:dev": "vitest run -c ./test/vitest.config.ts",
231231
"typedoc": "node scripts/typedoc.mjs",
232232
"update-version": "node scripts/version.mjs"
233233
},

packages/thirdweb/src/extensions/erc4337/account/permissions.test.ts

Lines changed: 12 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@ import { simulateTransaction } from "../../../transaction/actions/simulate.js";
1717
import { addAdmin } from "./addAdmin.js";
1818
import { getAllAdmins } from "../__generated__/IAccountPermissions/read/getAllAdmins.js";
1919
import { removeAdmin } from "./removeAdmin.js";
20-
import { getAllSigners } from "../__generated__/IAccountPermissions/read/getAllSigners.js";
2120
import { addSessionKey } from "./addSessionKey.js";
2221
import { parseEventLogs } from "../../../event/actions/parse-logs.js";
2322
import { adminUpdatedEvent } from "../__generated__/IAccountPermissions/events/AdminUpdated.js";
2423
import { signerPermissionsUpdatedEvent } from "../__generated__/IAccountPermissions/events/SignerPermissionsUpdated.js";
2524
import { ADDRESS_ZERO } from "../../../constants/addresses.js";
2625

27-
describe.todo("Account Permissions", () => {
26+
describe("Account Permissions", () => {
2827
let accountFactoryContract: ThirdwebContract;
2928
let accountContract: ThirdwebContract;
3029

@@ -57,74 +56,55 @@ describe.todo("Account Permissions", () => {
5756
});
5857
});
5958

60-
it("should fetch the correct inital state", async () => {
61-
const admins = await getAllAdmins({
59+
it("should allow adding admins", async () => {
60+
let admins = await getAllAdmins({
6261
contract: accountContract,
6362
});
6463
expect(admins).toEqual([TEST_ACCOUNT_A.address]);
65-
});
66-
67-
it("should allow adding admins", async () => {
68-
const receipt = await sendAndConfirmTransaction({
64+
let receipt = await sendAndConfirmTransaction({
6965
transaction: addAdmin({
7066
account: TEST_ACCOUNT_A,
7167
contract: accountContract,
7268
adminAddress: TEST_ACCOUNT_B.address,
7369
}),
7470
account: TEST_ACCOUNT_A,
7571
});
76-
const logs = parseEventLogs({
72+
let logs = parseEventLogs({
7773
events: [adminUpdatedEvent()],
7874
logs: receipt.logs,
7975
});
8076
expect(logs.length).toBe(1);
8177
expect(logs[0]?.args.signer).toBe(TEST_ACCOUNT_B.address);
8278
expect(logs[0]?.args.isAdmin).toBe(true);
83-
});
84-
85-
it("should fetch the correct state after adding", async () => {
86-
const admins = await getAllAdmins({
79+
admins = await getAllAdmins({
8780
contract: accountContract,
8881
});
8982
expect(admins.length).toBe(2);
9083
expect(admins).toStrictEqual([
9184
TEST_ACCOUNT_A.address,
9285
TEST_ACCOUNT_B.address,
9386
]);
94-
});
95-
96-
it("should allow removing admins", async () => {
97-
const receipt = await sendAndConfirmTransaction({
87+
receipt = await sendAndConfirmTransaction({
9888
transaction: removeAdmin({
9989
account: TEST_ACCOUNT_B,
10090
contract: accountContract,
10191
adminAddress: TEST_ACCOUNT_A.address,
10292
}),
10393
account: TEST_ACCOUNT_B,
10494
});
105-
const logs = parseEventLogs({
95+
logs = parseEventLogs({
10696
events: [adminUpdatedEvent()],
10797
logs: receipt.logs,
10898
});
10999
expect(logs[0]?.args.signer).toBe(TEST_ACCOUNT_A.address);
110100
expect(logs[0]?.args.isAdmin).toBe(false);
111-
});
112-
113-
it("should fetch the correct state after removing", async () => {
114-
const admins = await getAllAdmins({
101+
admins = await getAllAdmins({
115102
contract: accountContract,
116103
});
117104
expect(admins.length).toBe(1);
118105
expect(admins).toStrictEqual([TEST_ACCOUNT_B.address]);
119106
});
120107

121-
it("should fetch the correct before adding a session key", async () => {
122-
const signers = await getAllSigners({
123-
contract: accountContract,
124-
});
125-
expect(signers).toEqual([]);
126-
});
127-
128108
it("should allow adding session keys", async () => {
129109
const receipt = await sendAndConfirmTransaction({
130110
transaction: addSessionKey({
@@ -143,6 +123,8 @@ describe.todo("Account Permissions", () => {
143123
});
144124
expect(logs[0]?.args.authorizingSigner).toBe(TEST_ACCOUNT_B.address);
145125
expect(logs[0]?.args.targetSigner).toBe(TEST_ACCOUNT_A.address);
146-
expect(logs[0]?.args.permissions.approvedTargets).toBe([ADDRESS_ZERO]);
126+
expect(logs[0]?.args.permissions.approvedTargets).toStrictEqual([
127+
ADDRESS_ZERO,
128+
]);
147129
});
148130
});

packages/thirdweb/src/transaction/actions/encode.ts

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import type { Hex } from "../../utils/encoding/hex.js";
22
import type { PreparedTransaction } from "../prepare-transaction.js";
33
import type { Abi, AbiFunction } from "abitype";
44

5+
const encodeWeakMap = new WeakMap<
6+
PreparedTransaction<Abi, AbiFunction>,
7+
Promise<Hex>
8+
>();
9+
510
/**
611
* Encodes a transaction object into a hexadecimal string representation of the encoded data.
712
* @param transaction - The transaction object to encode.
@@ -16,15 +21,23 @@ import type { Abi, AbiFunction } from "abitype";
1621
export async function encode<abi extends Abi, abiFn extends AbiFunction>(
1722
transaction: PreparedTransaction<abi, abiFn>,
1823
): Promise<Hex> {
19-
if (transaction.data === undefined) {
20-
return "0x";
24+
if (encodeWeakMap.has(transaction)) {
25+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
26+
return encodeWeakMap.get(transaction)!;
2127
}
22-
if (typeof transaction.data === "function") {
23-
const data = await transaction.data();
24-
if (!data) {
28+
const promise = (async () => {
29+
if (transaction.data === undefined) {
2530
return "0x";
2631
}
27-
return data;
28-
}
29-
return transaction.data;
32+
if (typeof transaction.data === "function") {
33+
const data = await transaction.data();
34+
if (!data) {
35+
return "0x";
36+
}
37+
return data;
38+
}
39+
return transaction.data;
40+
})();
41+
encodeWeakMap.set(transaction, promise);
42+
return promise;
3043
}

packages/thirdweb/test/vitest.config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default defineConfig({
1616
all: true,
1717
provider: "v8",
1818
reporter: process.env.CI ? ["lcov"] : ["text", "json", "html"],
19-
exclude: ["**/*.test.ts", "**/test/**"],
19+
exclude: ["**/*.test.ts", "**/test/**", "**/__generated__/**"],
2020
include: ["src/**"],
2121
},
2222
environment: "node",

0 commit comments

Comments
 (0)