Accounts

Fetch and decode on-chain account data using the sdk.accounts object.

Overview

The sdk.accounts object provides typed methods to fetch on-chain account data. Mapper functions convert raw Anchor account structs (which use BN and Rust enum objects) into plain TypeScript objects.

Bet accounts

sdk.accounts.betV2.single(address)

Fetch a single V2 bet by its public key.

typescript
const bet = await sdk.accounts.betV2.single(betAddress);
console.log("Question:", bet.question);

Returns Promise<ProgramBetV2>

sdk.accounts.betV2.all(filters?)

Fetch all V2 bets, with optional memcmp filters.

typescript
const allBets = await sdk.accounts.betV2.all();

// Filter by creator
const myBets = await sdk.accounts.betV2.all([
  { memcmp: { offset: 8, bytes: wallet.publicKey.toBase58() } }
]);

Returns Promise<ProgramAccount<ProgramBetV2>[]>

User accounts

sdk.accounts.user.single(address)

Fetches a program user account.

typescript
const userAddress = sdk.addresses.user.get(wallet.publicKey);
const user = await sdk.accounts.user.single(userAddress);

console.log("Total wagers:", user.totalWagersCount);
console.log("Wagered (USDC):", user.totalWageredAmount.toNumber() / 1e6);

Protocol account

sdk.accounts.protocol.single(address)

Fetches the protocol configuration account.

typescript
const protocolAddress = sdk.addresses.protocol.get();
const protocol = await sdk.accounts.protocol.single(protocolAddress);

console.log("Fee %:", protocol.feePercentage);
console.log("Total bets:", protocol.totalUniqueBets);

Mapper functions

Raw on-chain accounts use BN for big numbers and Rust enum objects like { pending: {} }. Mapper functions convert these to plain numbers and strings:

sdk.accounts.mapBetV2(rawBet)

Maps a V2 bet to a plain TypeScript object.

typescript
const rawBet = await sdk.accounts.betV2.single(address);
const bet = sdk.accounts.mapBetV2(rawBet);

console.log("Status:", bet.status);        // "Pending" (not { pending: {} })
console.log("For total:", bet.totalOiFor); // number (not BN)

sdk.accounts.mapUser(rawUser)

Maps a user account to a plain object.

typescript
const raw = await sdk.accounts.user.single(userAddress);
const user = sdk.accounts.mapUser(raw);

console.log("Wagered:", user.totalWageredAmount); // plain number

sdk.accounts.mapProtocol(rawProtocol)

Maps the protocol account to a plain object.

typescript
const raw = await sdk.accounts.protocol.single(protocolAddress);
const protocol = sdk.accounts.mapProtocol(raw);

Raw structures

The raw on-chain structures below are returned by account fetch methods before mapping:

ProgramBetV2
interface ProgramBetV2 {
  creator: PublicKey;
  createdAt: BN;
  updatedAt: BN;
  resolvedAt: BN;
  distributedAt: BN;
  voteInitiatedAt: BN;
  status: { pending: {} } | { resolving: {} } | { resolved: {} } | { distributed: {} };
  resolvedOutcome: { for: {} } | { against: {} } | { tied: {} };
  question: string;
  expectedUserCount: number;
  minimumVoteCount: number;
  totalOiFor: BN;
  totalOiAgainst: BN;
  poolAddress: PublicKey;
  wagers: Wager[];
  votes: Vote[];
  wagerId: BN;
  isCreatorResolver: boolean;
  settledUserCount: number;
  totalBatchesRequired: number;
}
Wager
interface Wager {
  user: PublicKey;
  amount: BN;
  outcome: { for: {} } | { against: {} };
  updatedAt: BN;
  status: { open: {} } | { settledWin: {} } | { settledLoss: {} };
  isVoteInitiator: boolean;
}