Skip to content

Commit 5dc1899

Browse files
committed
[SDK] Improve Vote contract tests (#5156)
## Problem solved Short description of the bug fixed or feature added <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on enhancing validation for the `minVoteQuorumRequiredPercent` parameter in the `deploy-vote.ts` file and updating corresponding tests to ensure correct functionality. ### Detailed summary - Added validation for `minVoteQuorumRequiredPercent` in `deploy-vote.ts`. - Included checks for NaN, range (0-100), and integer-like values. - Updated tests in `deploy-vote.test.ts` to handle new validation logic. - Adjusted test descriptions and assertions in `vote.test.ts`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent 4b18351 commit 5dc1899

File tree

3 files changed

+43
-118
lines changed

3 files changed

+43
-118
lines changed

packages/thirdweb/src/extensions/prebuilts/deploy-vote.test.ts

Lines changed: 1 addition & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -3,57 +3,12 @@ import { ANVIL_CHAIN } from "~test/chains.js";
33
import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js";
44
import { TEST_CLIENT } from "~test/test-clients.js";
55
import { TEST_ACCOUNT_B } from "~test/test-wallets.js";
6-
import { isAddress } from "../../utils/address.js";
7-
import { deployERC20Contract } from "./deploy-erc20.js";
86
import { deployVoteContract } from "./deploy-vote.js";
97

108
const account = TEST_ACCOUNT_B;
119

1210
describe.runIf(process.env.TW_SECRET_KEY)("deploy-voteERC20 contract", () => {
13-
it("should deploy Vote contract", async () => {
14-
const tokenAddress = await deployERC20Contract({
15-
client: TEST_CLIENT,
16-
chain: ANVIL_CHAIN,
17-
account,
18-
type: "TokenERC20",
19-
params: {
20-
name: "Token",
21-
contractURI: TEST_CONTRACT_URI,
22-
},
23-
});
24-
const address = await deployVoteContract({
25-
account,
26-
client: TEST_CLIENT,
27-
chain: ANVIL_CHAIN,
28-
params: {
29-
name: "",
30-
contractURI: TEST_CONTRACT_URI,
31-
tokenAddress: tokenAddress,
32-
// user needs 0.5 <token> to create proposal
33-
initialProposalThreshold: "0.5",
34-
// vote expires 10 blocks later
35-
initialVotingPeriod: 10,
36-
// Requires 51% of users who voted, voted "For", for this proposal to pass
37-
minVoteQuorumRequiredPercent: 51,
38-
},
39-
});
40-
expect(address).toBeDefined();
41-
expect(isAddress(address)).toBe(true);
42-
// Further tests to verify the functionality of this contract
43-
// are done in other Vote tests
44-
});
45-
4611
it("should throw if passed an non-integer-like value to minVoteQuorumRequiredPercent", async () => {
47-
const tokenAddress = await deployERC20Contract({
48-
client: TEST_CLIENT,
49-
chain: ANVIL_CHAIN,
50-
account,
51-
type: "TokenERC20",
52-
params: {
53-
name: "Token",
54-
contractURI: TEST_CONTRACT_URI,
55-
},
56-
});
5712
await expect(() =>
5813
deployVoteContract({
5914
account,
@@ -62,7 +17,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("deploy-voteERC20 contract", () => {
6217
params: {
6318
name: "",
6419
contractURI: TEST_CONTRACT_URI,
65-
tokenAddress: tokenAddress,
20+
tokenAddress: "doesnt matter here, code wont be reached",
6621
initialProposalThreshold: "0.5",
6722
initialVotingPeriod: 10,
6823
minVoteQuorumRequiredPercent: 51.12,

packages/thirdweb/src/extensions/prebuilts/deploy-vote.ts

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,27 @@ async function getInitializeTransaction(options: {
136136
external_link,
137137
social_urls,
138138
} = params;
139+
140+
// Validate initialVoteQuorumFraction
141+
const _num = Number(minVoteQuorumRequiredPercent);
142+
if (Number.isNaN(_num)) {
143+
throw new Error(
144+
`${minVoteQuorumRequiredPercent} is not a valid minVoteQuorumRequiredPercent`,
145+
);
146+
}
147+
if (_num < 0 || _num > 100) {
148+
throw new Error("minVoteQuorumRequiredPercent must be >= 0 and <= 100");
149+
}
150+
151+
// Make sure if user is passing a float, it should only have 2 digit after the decimal point
152+
if (!Number.isInteger(_num)) {
153+
throw new Error(
154+
`${_num} is an invalid value. Only integer-like values accepted`,
155+
);
156+
}
157+
158+
const initialVoteQuorumFraction = BigInt(_num);
159+
139160
const tokenErc20Contract = getContract({
140161
address: tokenAddress,
141162
client,
@@ -177,26 +198,6 @@ async function getInitializeTransaction(options: {
177198
})) ||
178199
"";
179200

180-
// Validate initialVoteQuorumFraction
181-
const _num = Number(minVoteQuorumRequiredPercent);
182-
if (Number.isNaN(_num)) {
183-
throw new Error(
184-
`${minVoteQuorumRequiredPercent} is not a valid minVoteQuorumRequiredPercent`,
185-
);
186-
}
187-
if (_num < 0 || _num > 100) {
188-
throw new Error("minVoteQuorumRequiredPercent must be >= 0 and <= 100");
189-
}
190-
191-
// Make sure if user is passing a float, it should only have 2 digit after the decimal point
192-
if (!Number.isInteger(_num)) {
193-
throw new Error(
194-
`${_num} is an invalid value. Only integer-like values accepted`,
195-
);
196-
}
197-
198-
const initialVoteQuorumFraction = BigInt(_num);
199-
200201
return initialize({
201202
contract: implementationContract,
202203
name,

packages/thirdweb/src/extensions/vote/read/proposalExists.test.ts renamed to packages/thirdweb/src/extensions/vote/vote.test.ts

Lines changed: 21 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@ import { ANVIL_CHAIN } from "~test/chains.js";
33
import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js";
44
import { TEST_CLIENT } from "~test/test-clients.js";
55
import { TEST_ACCOUNT_C } from "~test/test-wallets.js";
6-
import { getContract } from "../../../contract/contract.js";
7-
import { delegate } from "../../../extensions/erc20/__generated__/IVotes/write/delegate.js";
8-
import { mintTo } from "../../../extensions/erc20/write/mintTo.js";
9-
import { deployERC20Contract } from "../../../extensions/prebuilts/deploy-erc20.js";
10-
import { deployVoteContract } from "../../../extensions/prebuilts/deploy-vote.js";
11-
import { sendAndConfirmTransaction } from "../../../transaction/actions/send-and-confirm-transaction.js";
12-
import { propose } from "../__generated__/Vote/write/propose.js";
13-
import { getAll } from "./getAll.js";
14-
import { proposalExists } from "./proposalExists.js";
6+
import { getContract } from "../../contract/contract.js";
7+
import { sendAndConfirmTransaction } from "../../transaction/actions/send-and-confirm-transaction.js";
8+
import { delegate } from "../erc20/__generated__/IVotes/write/delegate.js";
9+
import { mintTo } from "../erc20/write/mintTo.js";
10+
import { deployERC20Contract } from "../prebuilts/deploy-erc20.js";
11+
import { deployVoteContract } from "../prebuilts/deploy-vote.js";
12+
import { propose } from "./__generated__/Vote/write/propose.js";
13+
import { getAll } from "./read/getAll.js";
14+
import { proposalExists } from "./read/proposalExists.js";
1515

1616
const account = TEST_ACCOUNT_C;
1717
const client = TEST_CLIENT;
1818
const chain = ANVIL_CHAIN;
1919

2020
describe.runIf(process.env.TW_SECRET_KEY)("proposal exists", () => {
21-
it("should return false if Vote doesn't have any proposal", async () => {
21+
it("`proposalExists` and `propose` should work", async () => {
2222
const tokenAddress = await deployERC20Contract({
2323
client: TEST_CLIENT,
2424
chain: ANVIL_CHAIN,
@@ -42,47 +42,16 @@ describe.runIf(process.env.TW_SECRET_KEY)("proposal exists", () => {
4242
minVoteQuorumRequiredPercent: 51,
4343
},
4444
});
45-
46-
const contract = getContract({
45+
const voteContract = getContract({
4746
address,
4847
chain,
4948
client,
5049
});
51-
52-
const result = await proposalExists({ contract, proposalId: 0n });
53-
expect(result).toBe(false);
54-
});
55-
56-
it("should return true if Vote has the proposal (id)", async () => {
57-
const tokenAddress = await deployERC20Contract({
58-
client: TEST_CLIENT,
59-
chain: ANVIL_CHAIN,
60-
account,
61-
type: "TokenERC20",
62-
params: {
63-
name: "Token",
64-
contractURI: TEST_CONTRACT_URI,
65-
},
66-
});
67-
const address = await deployVoteContract({
68-
account,
69-
client: TEST_CLIENT,
70-
chain: ANVIL_CHAIN,
71-
params: {
72-
name: "",
73-
contractURI: TEST_CONTRACT_URI,
74-
tokenAddress: tokenAddress,
75-
initialProposalThreshold: "0.5",
76-
initialVotingPeriod: 10,
77-
minVoteQuorumRequiredPercent: 51,
78-
},
79-
});
80-
81-
const contract = getContract({
82-
address,
83-
chain,
84-
client,
50+
const result = await proposalExists({
51+
contract: voteContract,
52+
proposalId: 0n,
8553
});
54+
expect(result).toBe(false);
8655

8756
const tokenContract = getContract({
8857
address: tokenAddress,
@@ -105,19 +74,19 @@ describe.runIf(process.env.TW_SECRET_KEY)("proposal exists", () => {
10574

10675
// step 3: create a proposal
10776
const transaction = propose({
108-
contract,
77+
contract: voteContract,
10978
description: "first proposal",
110-
targets: [contract.address],
79+
targets: [voteContract.address],
11180
values: [0n],
11281
calldatas: ["0x"],
11382
});
11483
await sendAndConfirmTransaction({ transaction, account });
115-
const allProposals = await getAll({ contract });
84+
const allProposals = await getAll({ contract: voteContract });
11685
expect(allProposals.length).toBe(1);
117-
const result = await proposalExists({
118-
contract,
86+
const exists = await proposalExists({
87+
contract: voteContract,
11988
proposalId: allProposals[0]?.proposalId || -1n,
12089
});
121-
expect(result).toBe(true);
90+
expect(exists).toBe(true);
12291
});
12392
});

0 commit comments

Comments
 (0)