⚠️
Important! We’ve moved to V3! This documentation is for V2 only and will be updated in May.
Developer Hub
🔮 For applications
SDK
Write Hooks
useBet

useBet

The useBet hook is used for users to place bets and approve the use tokens for payment when needed.

Usage

import { useBet } from '@azuro-org/sdk'
 
// using BetslipProvider
import { useBaseBetslip, useDetailedBetslip } from '@azuro-org/sdk'
 
const { items, clear } = useBaseBetslip()
const { betAmount, odds, totalOdds } = useDetailedBetslip()
 
// using useOdds
import { useOdds } from '@azuro-org/sdk'
 
const [ betAmount, setBetAmount ] = useState()
const items = [...]
const { odds, totalOdds } = useOdds({ betAmount, selections })
 
const {
  isAllowanceLoading,
  isApproveRequired,
  submit,
  approveTx,
  betTx,
} = useBet({
  betAmount,
  slippage: 10, // 10%
  affiliate: '0x...', // your affiliate address
  selections: items,
  odds,
  totalOdds,
  onSuccess: () => {
    clear()
  },
})
 
submit()
ℹ️

When allowing users to place express (or combo) bets, you can pass multiple selections, each for different outcomes across various events, and combine them into one larger bet.

const {...} = useBet({
  betAmount: '10',
  slippage: 5,
  affiliate: '0x...',
  selections: [
    {
      conditionId: '486903008559711340',
      outcomeId: '29',
      coreAddress: '0x34nsf41f...',
    },
    {
      conditionId: '486903008559711341',
      outcomeId: '30',
      coreAddress: '0x34nsf41f...',
    },
  ],
  odds,
  totalOdds,
})

Props

{
  betAmount: string | Record<string, string> // bet amount
  slippage: number // in percent
  affiliate: Address // your affiliate address
  odds: Record<string, number>, // odds
  totalOdds: number, // total odds
  freebet?: FreeBet // freeBet info
  betGas?: Pick<SendTransactionParameters, 'gas' | 'gasPrice' | 'maxFeePerGas' | 'maxFeePerBlobGas' | 'maxPriorityFeePerGas'> // transaction gas parameters, prematch only
  selections: Selection[] // user's choice for bet
  liveEIP712Attention?: string // attention text for eip-712 sign (up to 160 chars)
  deadline?: number // duration after which the bet will be canceled if it does not get included in a block
  onSuccess?(receipt: TransactionReceipt): void // callback function triggered after a successful bet
  onError?(err?: Error): void // callback function triggered upon encountering an error
}
import { type SendTransactionParameters } from 'viem'
 
type Selection = {
  conditionId: string
  outcomeId: string
  coreAddress: string
}
 
type BonusBase = {
  id: string
  type: BonusType,
  amount: string
  status: BonusStatus
  chainId: ChainId
  expiresAt: number
  usedAt: number
  createdAt: number
}
 
type Freebet = {
  type: BonusType.FreeBet,
  params: {
    isBetSponsored: boolean,
    isFeeSponsored: boolean,
    isSponsoredBetReturnable: boolean
  }
} & BonusBase

Return Value

{
  isAllowanceLoading: boolean // flag indicates the fetching of allowance
  isApproveRequired: boolean // flag indicates the necessity for token spending approval
  isRelayerFeeLoading: boolean // flag indicates the fetching of relayer fee for live bet
  submit: () => Promise<TransactionReceipt> // function used to trigger the bet action or token spending approval
  relayerFeeAmount: string | undefined // relayer fee amount for live bet
  approveTx: { // approve transaction state
    isPending: boolean
    isProcessing: boolean
  }
  betTx: { // bet transaction state
    data: Hex | undefined
    isPending: boolean
    isProcessing: boolean
  }
}