Developer Hub
🔮 For applications
SDK
Write Hooks
usePrepareBet

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

Usage

import { usePrepareBet } 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,
} = usePrepareBet({
  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 {...} = usePrepareBet({
  betAmount: '10',
  slippage: 5,
  affiliate: '0x...',
  selections: [
    {
      conditionId: '486903008559711340',
      outcomeId: '29',
      coreAddress: '0x34nsf41f...',
    },
    {
      conditionId: '486903008559711341',
      outcomeId: '30',
      coreAddress: '0x34nsf41f...',
    },
  ],
  odds,
  totalOdds,
})
ℹ️

To perform batch bet you need to provide object with amounts for each selection, key is concatenation of conditionId and outcomeId:

const {...} = usePrepareBet({
  betAmount: {
    '486903008559711340-29': 1,
    '486903008559711341-30': 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 choise 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 FreeBet = {
  id: number
  contractAddress: Address
  signature: Hex
  expiresAt: number
  amount: string
  rawAmount: bigint
  minOdds: string
  rawMinOdds: bigint
  campaign: string
  chainId: ChainId
}

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
  }
}