Skip to main content

createB3Client

Initialize the B3 client in your backend to interact with Upside’s API. Framework Support: Currently supports Hono with Cloudflare Workers.
Parameters:
  • context (Hono Context): The Hono context object containing:
    • req.header(name: string): Method to extract request headers
    • env (optional): Cloudflare Workers environment
Returns:
  • B3Client instance with authentication automatically configured from the Authorization header
Authentication: The Authorization header is automatically extracted from the incoming request:

Core Functions

placeBet

Start a game session by placing a bet. This locks in the wager and creates a game session.
Parameters:
  • gameType (string): Your game’s identifier (e.g., “coin-flip”, “dice-roll”)
  • betAmount (string): Bet amount in wei (1 token = 10^18 wei, e.g., “100000000000000000”)
Returns:
  • sessionId: Unique identifier for this game session
  • gameId: Game record identifier
  • status: Current session status (“active”, “completed”, “failed”)
  • createdAt: ISO timestamp of bet creation
Errors:
  • Insufficient player balance
  • Invalid game type
  • Game not active/enabled
  • Invalid bet amount

processPayout

Complete the game and credit the player’s winnings.
Parameters:
  • gameType (string): Same game type from placeBet
  • sessionId (string): Session ID from placeBet response
  • payoutAmount (string): WIN tokens to credit in wei (0 for losses, e.g., “150000000000000000” = 1.5 tokens)
  • metadata (object):
    • playerChoice: What the player chose/predicted
    • result: The actual game outcome
    • outcome: “win” or “loss”
Returns:
  • status: “completed”, “failed”, etc.
  • payoutAmount: Amount credited in wei
  • newBalance: Player’s updated WIN balance in wei
  • updatedAt: ISO timestamp of completion
Errors:
  • Session not found
  • Session already completed (duplicate request)
  • Payout exceeds pool limits
  • Invalid bet amount format

Backend Example

Key Differences from Express:
  • createB3Client(c) extracts auth automatically from Hono context
  • Runs on Cloudflare Workers (serverless)
  • No need for manual middleware - Hono context handles everything
  • Response uses c.json() instead of res.json()
Environment Setup (wrangler.toml):

Best Practices

Bet Placement

  • Always validate amounts: Check bet is within player balance
  • Use idempotency: Retry failed placeBet calls with the same sessionId
  • Lock immediately: Once placeBet succeeds, prevent player from placing another bet

Game Logic

  • Backend is source of truth: Never trust client-side game outcomes
  • Store everything: Log all game events for audits and disputes
  • Validate results: Ensure game outcome matches expected range
  • Timeout games: Cancel bets if no payout is processed within 5 minutes

Payout Processing

  • Process once: Only call processPayout once per game session
  • Use correct amounts: Verify payout calculation before sending
  • Handle duplicates: If processPayout returns “already completed”, that’s OK
  • Handle failures: Retry failed payouts, but check if already paid first

Security

  • Verify tokens: Always validate JWT in every backend request
  • Use HTTPS: All communication must be encrypted
  • Validate game types: Only allow known, approved game types
  • Rate limit: Implement rate limiting to prevent abuse

Error Handling