diff --git a/packages/thirdweb/src/extensions/prebuilts/deploy-vote.test.ts b/packages/thirdweb/src/extensions/prebuilts/deploy-vote.test.ts index bb58455932a..7e3a5995e1f 100644 --- a/packages/thirdweb/src/extensions/prebuilts/deploy-vote.test.ts +++ b/packages/thirdweb/src/extensions/prebuilts/deploy-vote.test.ts @@ -3,57 +3,12 @@ import { ANVIL_CHAIN } from "~test/chains.js"; import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js"; import { TEST_CLIENT } from "~test/test-clients.js"; import { TEST_ACCOUNT_B } from "~test/test-wallets.js"; -import { isAddress } from "../../utils/address.js"; -import { deployERC20Contract } from "./deploy-erc20.js"; import { deployVoteContract } from "./deploy-vote.js"; const account = TEST_ACCOUNT_B; describe.runIf(process.env.TW_SECRET_KEY)("deploy-voteERC20 contract", () => { - it("should deploy Vote contract", async () => { - const tokenAddress = await deployERC20Contract({ - client: TEST_CLIENT, - chain: ANVIL_CHAIN, - account, - type: "TokenERC20", - params: { - name: "Token", - contractURI: TEST_CONTRACT_URI, - }, - }); - const address = await deployVoteContract({ - account, - client: TEST_CLIENT, - chain: ANVIL_CHAIN, - params: { - name: "", - contractURI: TEST_CONTRACT_URI, - tokenAddress: tokenAddress, - // user needs 0.5 to create proposal - initialProposalThreshold: "0.5", - // vote expires 10 blocks later - initialVotingPeriod: 10, - // Requires 51% of users who voted, voted "For", for this proposal to pass - minVoteQuorumRequiredPercent: 51, - }, - }); - expect(address).toBeDefined(); - expect(isAddress(address)).toBe(true); - // Further tests to verify the functionality of this contract - // are done in other Vote tests - }); - it("should throw if passed an non-integer-like value to minVoteQuorumRequiredPercent", async () => { - const tokenAddress = await deployERC20Contract({ - client: TEST_CLIENT, - chain: ANVIL_CHAIN, - account, - type: "TokenERC20", - params: { - name: "Token", - contractURI: TEST_CONTRACT_URI, - }, - }); await expect(() => deployVoteContract({ account, @@ -62,7 +17,7 @@ describe.runIf(process.env.TW_SECRET_KEY)("deploy-voteERC20 contract", () => { params: { name: "", contractURI: TEST_CONTRACT_URI, - tokenAddress: tokenAddress, + tokenAddress: "doesnt matter here, code wont be reached", initialProposalThreshold: "0.5", initialVotingPeriod: 10, minVoteQuorumRequiredPercent: 51.12, diff --git a/packages/thirdweb/src/extensions/prebuilts/deploy-vote.ts b/packages/thirdweb/src/extensions/prebuilts/deploy-vote.ts index ba8a1b0df5c..0db5d206ba4 100644 --- a/packages/thirdweb/src/extensions/prebuilts/deploy-vote.ts +++ b/packages/thirdweb/src/extensions/prebuilts/deploy-vote.ts @@ -136,6 +136,27 @@ async function getInitializeTransaction(options: { external_link, social_urls, } = params; + + // Validate initialVoteQuorumFraction + const _num = Number(minVoteQuorumRequiredPercent); + if (Number.isNaN(_num)) { + throw new Error( + `${minVoteQuorumRequiredPercent} is not a valid minVoteQuorumRequiredPercent`, + ); + } + if (_num < 0 || _num > 100) { + throw new Error("minVoteQuorumRequiredPercent must be >= 0 and <= 100"); + } + + // Make sure if user is passing a float, it should only have 2 digit after the decimal point + if (!Number.isInteger(_num)) { + throw new Error( + `${_num} is an invalid value. Only integer-like values accepted`, + ); + } + + const initialVoteQuorumFraction = BigInt(_num); + const tokenErc20Contract = getContract({ address: tokenAddress, client, @@ -177,26 +198,6 @@ async function getInitializeTransaction(options: { })) || ""; - // Validate initialVoteQuorumFraction - const _num = Number(minVoteQuorumRequiredPercent); - if (Number.isNaN(_num)) { - throw new Error( - `${minVoteQuorumRequiredPercent} is not a valid minVoteQuorumRequiredPercent`, - ); - } - if (_num < 0 || _num > 100) { - throw new Error("minVoteQuorumRequiredPercent must be >= 0 and <= 100"); - } - - // Make sure if user is passing a float, it should only have 2 digit after the decimal point - if (!Number.isInteger(_num)) { - throw new Error( - `${_num} is an invalid value. Only integer-like values accepted`, - ); - } - - const initialVoteQuorumFraction = BigInt(_num); - return initialize({ contract: implementationContract, name, diff --git a/packages/thirdweb/src/extensions/vote/read/proposalExists.test.ts b/packages/thirdweb/src/extensions/vote/vote.test.ts similarity index 54% rename from packages/thirdweb/src/extensions/vote/read/proposalExists.test.ts rename to packages/thirdweb/src/extensions/vote/vote.test.ts index af9bfdf2343..8c5053eedfc 100644 --- a/packages/thirdweb/src/extensions/vote/read/proposalExists.test.ts +++ b/packages/thirdweb/src/extensions/vote/vote.test.ts @@ -3,22 +3,22 @@ import { ANVIL_CHAIN } from "~test/chains.js"; import { TEST_CONTRACT_URI } from "~test/ipfs-uris.js"; import { TEST_CLIENT } from "~test/test-clients.js"; import { TEST_ACCOUNT_C } from "~test/test-wallets.js"; -import { getContract } from "../../../contract/contract.js"; -import { delegate } from "../../../extensions/erc20/__generated__/IVotes/write/delegate.js"; -import { mintTo } from "../../../extensions/erc20/write/mintTo.js"; -import { deployERC20Contract } from "../../../extensions/prebuilts/deploy-erc20.js"; -import { deployVoteContract } from "../../../extensions/prebuilts/deploy-vote.js"; -import { sendAndConfirmTransaction } from "../../../transaction/actions/send-and-confirm-transaction.js"; -import { propose } from "../__generated__/Vote/write/propose.js"; -import { getAll } from "./getAll.js"; -import { proposalExists } from "./proposalExists.js"; +import { getContract } from "../../contract/contract.js"; +import { sendAndConfirmTransaction } from "../../transaction/actions/send-and-confirm-transaction.js"; +import { delegate } from "../erc20/__generated__/IVotes/write/delegate.js"; +import { mintTo } from "../erc20/write/mintTo.js"; +import { deployERC20Contract } from "../prebuilts/deploy-erc20.js"; +import { deployVoteContract } from "../prebuilts/deploy-vote.js"; +import { propose } from "./__generated__/Vote/write/propose.js"; +import { getAll } from "./read/getAll.js"; +import { proposalExists } from "./read/proposalExists.js"; const account = TEST_ACCOUNT_C; const client = TEST_CLIENT; const chain = ANVIL_CHAIN; describe.runIf(process.env.TW_SECRET_KEY)("proposal exists", () => { - it("should return false if Vote doesn't have any proposal", async () => { + it("`proposalExists` and `propose` should work", async () => { const tokenAddress = await deployERC20Contract({ client: TEST_CLIENT, chain: ANVIL_CHAIN, @@ -42,47 +42,16 @@ describe.runIf(process.env.TW_SECRET_KEY)("proposal exists", () => { minVoteQuorumRequiredPercent: 51, }, }); - - const contract = getContract({ + const voteContract = getContract({ address, chain, client, }); - - const result = await proposalExists({ contract, proposalId: 0n }); - expect(result).toBe(false); - }); - - it("should return true if Vote has the proposal (id)", async () => { - const tokenAddress = await deployERC20Contract({ - client: TEST_CLIENT, - chain: ANVIL_CHAIN, - account, - type: "TokenERC20", - params: { - name: "Token", - contractURI: TEST_CONTRACT_URI, - }, - }); - const address = await deployVoteContract({ - account, - client: TEST_CLIENT, - chain: ANVIL_CHAIN, - params: { - name: "", - contractURI: TEST_CONTRACT_URI, - tokenAddress: tokenAddress, - initialProposalThreshold: "0.5", - initialVotingPeriod: 10, - minVoteQuorumRequiredPercent: 51, - }, - }); - - const contract = getContract({ - address, - chain, - client, + const result = await proposalExists({ + contract: voteContract, + proposalId: 0n, }); + expect(result).toBe(false); const tokenContract = getContract({ address: tokenAddress, @@ -105,19 +74,19 @@ describe.runIf(process.env.TW_SECRET_KEY)("proposal exists", () => { // step 3: create a proposal const transaction = propose({ - contract, + contract: voteContract, description: "first proposal", - targets: [contract.address], + targets: [voteContract.address], values: [0n], calldatas: ["0x"], }); await sendAndConfirmTransaction({ transaction, account }); - const allProposals = await getAll({ contract }); + const allProposals = await getAll({ contract: voteContract }); expect(allProposals.length).toBe(1); - const result = await proposalExists({ - contract, + const exists = await proposalExists({ + contract: voteContract, proposalId: allProposals[0]?.proposalId || -1n, }); - expect(result).toBe(true); + expect(exists).toBe(true); }); });