Documentation
Creating a bet

Bets

Creating a bet

Every bet starts with the client calling Gamba's play method with a list of potential outcomes. The program will pick a random item from the list and that number will be multiplied by the wagered amount to determine how much the player won.

In the following example we will simulate a coin toss of 1 SOL by calling the method with [2, 0] (2x or 0x).

 
const gamba = useGamba()
 
const tossCoin = () => {
  gamba.play({
    bet: [2, 0],
    wager: solToLamports(.1),
  })
}

To give the user an option between heads or tails, we can simply swap the order of the array, so that their choice influence the outcome:

const gamba = useGamba()
 
const tossCoin = (side: 'heads' | 'tails') => {
  gamba.play({
    bet: side === 'heads' ? [2, 0] : [0, 2],
    wager: solToLamports(.1),
  })
}

We can create longer arrays of different lengths and values to simulate more complex games. The sum of the bet array has to be less or equal to its length so that the odds don't put the player at an unfair advantage. Examples:

  • [0, 2] equal odds = allowed ✅
  • [1.5, .5] equal odds = allowed ✅
  • [1, 1, 1] equal odds = allowed ✅
  • [0, 4] player has an edge = not allowed ❌
  • [1, 1, 1, 1] player has an edge = not allowed ❌

Note that the actual program does not actually deal with floating point numbers. The gamba library automatically applies a scaling factor of 1000 to the numbers. So [2, 0] actually becomes [2000, 0]. You only need to care about this if you're making method calls to the program directly, otherwise it's handled by the npm package.

Handling results

In most apps we want to display the result of the player's bet when it's done, and a loading indicator while the result is being generated. This example does that:

const [loading, setLoading] = useState(false)
 
const play = async () => {
  try {
    const request = await gamba.play({
      bet: [2, 0],
      wager: solToLamports(.1),
    })
    setLoading(true)
    const result = await request.result()
    // ... do something with the result
  } catch (err) {
    // ... do something with the error
  } finally {
    setLoading(false)
  }
}

Here we await the response of gamba.play, which will finish once the user has confirmed the transaction with their wallet.

To wait for the actual random result, we also have to await the result promise of the request object, which will finish once the bet has been settled on-chain, or throw an error if the bet for some reason failed.

Claiming profits

Every time the player wins a bet, the payout will be added to the player's user account balance. The withdraw method can be called to claim any or all funds currently in the Gamba account.

The following example withdraws all the SOL available in the player's user account:

const gamba = useGamba()
 
const claim = async () => {
  await gamba.withdraw()
  alert('Claimed!')
}

Bet examples

Bets can be orchestrated in different ways to simulate everything from a coin toss to a roulette spin. This section will be updated soon with more examples. In the meantime, checkout our open-source demo platform to see the variety of games that can be achieved with a simple array: https://play.gamba.so/ (opens in a new tab)