Place a Wager
Bet USDC on a prediction market outcome.
What You'll Do
Place a wager on a bet, choosing a side (For/Against) and amount.
Prerequisites
- SDK initialized with a wallet
- Program user account created
- USDC in your wallet
- A bet address to wager on
Place a Wager
Use the placeWagerV2 method:
typescript
import { SDK, Outcome } from "@solworks/poll-sdk";
const txHash = await sdk.placeWagerV2({
bet: betPublicKey,
amount: 25, // 25 USDC
side: Outcome.For, // Betting "Yes"
signers: [wallet],
});
console.log("Wager placed:", txHash);Parameters
| Parameter | Type | Description |
|---|---|---|
bet | PublicKey | The bet account address |
amount | number | USDC amount (human-readable, e.g., 25 for 25 USDC) |
rawAmount | number | Alternative: raw lamports (25_000_000 for 25 USDC) |
side | Outcome | Outcome.For or Outcome.Against |
signers | Keypair[] | Transaction signers |
Outcome Options
typescript
import { Outcome } from "@solworks/poll-sdk";
Outcome.For // 1 - Betting "Yes" / the affirmative
Outcome.Against // 2 - Betting "No" / the negativeCheck Wager in Bet
After placing, verify your wager:
typescript
const bet = await sdk.accounts.betV2.single(betPublicKey);
const myWager = bet.wagers.find(
(w) => w.user.equals(wallet.publicKey)
);
if (myWager) {
console.log("My Wager:");
console.log("- Amount:", myWager.amount.toNumber() / 1e6, "USDC");
console.log("- Side:", myWager.outcome === 1 ? "For" : "Against");
console.log("- Status:", myWager.status);
}Calculate Potential Payout
Estimate your potential winnings:
typescript
function calculatePotentialPayout(
bet: BetAccount,
myWager: { amount: BN; outcome: number }
): number {
const totalPool =
bet.totalOiFor.toNumber() + bet.totalOiAgainst.toNumber();
const myAmount = myWager.amount.toNumber();
// If my side wins
const mySideTotal =
myWager.outcome === 1
? bet.totalOiFor.toNumber()
: bet.totalOiAgainst.toNumber();
// Proportional share of the pot
const winShare = (myAmount / mySideTotal) * totalPool;
// Minus protocol fee (typically 3-5%)
const feeRate = 0.03;
const netWinnings = winShare * (1 - feeRate);
return netWinnings / 1e6; // Convert to USDC
}Wager Status
Wagers have the following statuses:
| Status | Description |
|---|---|
| Open | Wager is active, bet not resolved |
| SettledWin | Bet resolved, this wager won |
| SettledLoss | Bet resolved, this wager lost |
Multiple Wagers
Users can place multiple wagers on the same bet:
typescript
// First wager
await sdk.placeWagerV2({
bet: betPublicKey,
amount: 10,
side: Outcome.For,
signers: [wallet],
});
// Add to position
await sdk.placeWagerV2({
bet: betPublicKey,
amount: 15,
side: Outcome.For,
signers: [wallet],
});What Happens
When you place a wager:
1. USDC is transferred from your wallet to the bet's escrow pool
2. A wager record is added to the bet account
3. totalOiFor or totalOiAgainst is updated
4. Your user account's inWagerAmount increases
5. Your user account's totalWageredAmount increases
Error Handling
typescript
try {
await sdk.placeWagerV2({
bet: betPublicKey,
amount: 100,
side: Outcome.For,
signers: [wallet],
});
} catch (error) {
if (error.message.includes("insufficient funds")) {
console.log("Not enough USDC");
} else if (error.message.includes("bet is not pending")) {
console.log("Bet is no longer accepting wagers");
} else {
throw error;
}
}Next Steps
Once enough participants have wagered, learn how to initiate the voting process.