Skip to main content

Vote

The Vote contract is designed for groups such as DAO's to vote on proposals.

In order to use the Vote contract, you also need to have an existing ERC-20 token, such as our Token Contract to act as the governance token.

You could use the Vote contract to:

  • Vote to decide on organizational changes
  • Vote on managing and distributing funds in a treasury
  • Vote on any other proposal in an organization, such as a DAO

Create a Vote Contract

Using the dashboard

Learn how to create any of thirdweb's pre-built contracts in the Deploying Contracts page.

Getting the contract in your application

To start using your Vote contract inside your application, you'll need to use it's contract address. You can get the contract address from the dashboard.

import { ThirdwebSDK } from "@thirdweb-dev/sdk";

const sdk = new ThirdwebSDK("rinkeby");
const contract = sdk.getVote("{{contract_address}}");

Creating A Proposal

Using the dashboard

Create New Proposal

Using the SDK

// The description of the proposal you want to pass
const description = "This is a great proposal - vote for it!"
// You can (optionally) pass in contract calls that will get executed when the proposal is executed.
const executions = [
{
// The contract you want to make a call to
toAddress: "0x...",
// The amount of the native currency to send in this transaction
nativeTokenValue: 0,
// Transaction data that will be executed when the proposal is executed
// This is an example transfer transaction with a token contract (which you would need to setup in code)
transactionData: tokenContract.encoder.encode(
"transfer", [
fromAddress,
amount,
]
),
}
]

const proposal = await contract.propose(description, executions);
View in Javascript SDK Documentation

Viewing Proposals

View all of the proposals that have been created in this Vote smart contract.

const proposals = await contract.getAll();
console.log(proposals);
View in Javascript SDK Documentation

Check if a wallet has voted

Check if a wallet address has voted in a specific proposal.

// The proposal ID of the proposal you want to check
const proposalId = "0";
// The address of the wallet you want to check to see if they voted
const address = "{{wallet_address}}";

await contract.hasVoted(proposalId, address);
View in Javascript SDK Documentation

Execute A Proposal

Check if a proposal can be executed

Check to see if the amount of votes required for the proposal to be executed has been met

// The proposal ID of the proposal you want to check
const proposalId = "0";
const canExecute = await contract.canExecute(proposalId);
console.log(canExecute);
View in Javascript SDK Documentation

Execute a proposal

Execute a proposal that has a sufficient amount of votes.

// The proposal ID ofthe proposal you want to execute
const proposalId = "0"
await contract.execute(proposalId);
View in Javascript SDK Documentation

Vote on a Proposal

// The proposal ID of the proposal you want to vote on
const proposalId = "0";
// The vote type you want to cast, can be VoteType.Against, VoteType.For, or VoteType.Abstain
const voteType = VoteType.For;
// The (optional) reason for the vote
const reason = "I like this proposal!";

await contract.vote(proposalId, voteType, reason);
View in Javascript SDK Documentation