Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 1 addition & 46 deletions packages/thirdweb/src/extensions/prebuilts/deploy-vote.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 <token> 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,
Expand All @@ -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,
Expand Down
41 changes: 21 additions & 20 deletions packages/thirdweb/src/extensions/prebuilts/deploy-vote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,27 @@
external_link,
social_urls,
} = params;

// Validate initialVoteQuorumFraction
const _num = Number(minVoteQuorumRequiredPercent);
if (Number.isNaN(_num)) {
throw new Error(
`${minVoteQuorumRequiredPercent} is not a valid minVoteQuorumRequiredPercent`,
);
}

Check warning on line 146 in packages/thirdweb/src/extensions/prebuilts/deploy-vote.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/prebuilts/deploy-vote.ts#L143-L146

Added lines #L143 - L146 were not covered by tests
if (_num < 0 || _num > 100) {
throw new Error("minVoteQuorumRequiredPercent must be >= 0 and <= 100");
}

Check warning on line 149 in packages/thirdweb/src/extensions/prebuilts/deploy-vote.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/extensions/prebuilts/deploy-vote.ts#L148-L149

Added lines #L148 - L149 were not covered by tests

// 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,
Expand Down Expand Up @@ -177,26 +198,6 @@
})) ||
"";

// 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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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);
});
});
Loading