Skip to main content

Vote

Learn how to interact with your Vote contract in the SDK.

Create a Vote Contract

const sdk = useSDK();

const contractAddress = await sdk.deployer.deployVote({
name: "My Vote",
primary_sale_recipient: "your-address",
voting_token_address: "your-token-contract-address",
});

Getting the contract in your application

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

import { useVote } from '@thirdweb-dev/react'

export default function Component() {
const vote = useVote("<YOUR-CONTRACT-ADDRESS>")

// Now you can use the vote contract in the rest of the component
}

Creating A Proposal

// 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);

Viewing Proposals

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

const proposals = await contract.getAll();
console.log(proposals);

Check if a wallet has voted

Check if a wallet address has voted on 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);

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);

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);

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);