Cast a Vote

Submit your vote for the bet outcome.

What You'll Do

Cast your vote to help determine the winning outcome of a bet.

Prerequisites

  • Voting has been initiated on the bet
  • You have a wager on the bet

Cast Your Vote

Use the placeVoteV2 method:

typescript
import { SDK, Outcome } from "@solworks/poll-sdk";

const txHash = await sdk.placeVoteV2({
  bet: betPublicKey,
  outcome: Outcome.For, // Vote that "For" wins
  signers: [wallet],
});

console.log("Vote cast:", txHash);

Vote Options

typescript
import { Outcome } from "@solworks/poll-sdk";

// Vote options
Outcome.For      // 1 - "Yes" won
Outcome.Against  // 2 - "No" won
Outcome.Tied     // 3 - Push/tie (everyone gets refunded)

Check Vote Status

View all votes on a bet:

typescript
const bet = await sdk.accounts.betV2.single(betPublicKey);

console.log("Votes Cast:", bet.votes.length);
console.log("Minimum Required:", bet.minimumVoteCount);

bet.votes.forEach((vote, i) => {
  console.log(`Vote ${i + 1}:`, {
    voter: vote.user.toBase58(),
    outcome: vote.outcome === 1 ? "For" : vote.outcome === 2 ? "Against" : "Tied",
    timestamp: new Date(vote.createdAt.toNumber() * 1000),
  });
});

Check If You've Voted

typescript
const bet = await sdk.accounts.betV2.single(betPublicKey);

const myVote = bet.votes.find((v) => v.user.equals(wallet.publicKey));

if (myVote) {
  console.log("You already voted:", myVote.outcome);
} else {
  console.log("You haven't voted yet");
}

Voting Rules

  • Each participant gets one vote
  • Votes cannot be changed after submission
  • When minimumVoteCount is reached with consensus, outcome is determined
  • If no consensus, more votes are needed

Consensus Logic

typescript
function checkConsensus(votes: Vote[], minimumVoteCount: number) {
  if (votes.length < minimumVoteCount) {
    return { hasConsensus: false, outcome: null };
  }

  const forVotes = votes.filter((v) => v.outcome === 1).length;
  const againstVotes = votes.filter((v) => v.outcome === 2).length;

  // Simple majority with minimum votes met
  if (forVotes > againstVotes && forVotes >= minimumVoteCount) {
    return { hasConsensus: true, outcome: Outcome.For };
  }
  if (againstVotes > forVotes && againstVotes >= minimumVoteCount) {
    return { hasConsensus: true, outcome: Outcome.Against };
  }

  return { hasConsensus: false, outcome: null };
}

What Happens

When you cast a vote:

1. Your vote is recorded in the bet's votes array

2. If consensus is reached:

  • Bet status changes to Resolved
  • resolvedOutcome is set

3. If no consensus yet, voting continues

After Voting

Check if the bet has resolved:

typescript
import { MarketStatus } from "@solworks/poll-sdk";

const bet = await sdk.accounts.betV2.single(betPublicKey);

if (bet.status === MarketStatus.Resolved) {
  console.log("Bet resolved!");
  console.log("Winning outcome:", bet.resolvedOutcome);
  console.log("Ready for settlement");
}

Error Handling

typescript
try {
  await sdk.placeVoteV2({
    bet: betPublicKey,
    outcome: Outcome.For,
    signers: [wallet],
  });
} catch (error) {
  if (error.message.includes("already voted")) {
    console.log("You've already cast your vote");
  } else if (error.message.includes("not resolving")) {
    console.log("Voting hasn't started yet");
  } else if (error.message.includes("no wager")) {
    console.log("Only participants can vote");
  } else {
    throw error;
  }
}

Next Steps

Once voting reaches consensus and the bet is resolved, learn how to settle the bet and distribute winnings.