Utility Functions
This page lists all utility functions available in the BetSwirl SDK core. These functions provide helper utilities for formatting, calculations, and common operations.
These utility functions are standalone and don’t require a BetSwirl client instance.
Formatting Functions
formatAmount
formattingDescription: Formats a number or string representation of a number into a readable string format.
💡Tips: Supports large numbers with suffixes (M, B, T) and predefined format types.
// Format large numbers
const large = formatAmount(123456789, FORMAT_TYPE.STANDARD); // "123.4568M"
// Use predefined format types
const minify = formatAmount(123.456, FORMAT_TYPE.MINIFY); // "123.46"
const standard = formatAmount(123.456, FORMAT_TYPE.STANDARD); // "123.456"
const precise = formatAmount(123.456, FORMAT_TYPE.PRECISE); // "123.456"
const precise2 = formatAmount(123.45678999111, FORMAT_TYPE.PRECISE); // "123.456789991"
const full = formatAmount(123.456, FORMAT_TYPE.FULL_PRECISE); // "123.456"
const precise2 = formatAmount(123.45678999111, FORMAT_TYPE.PRECISE); // "123.45678999111"
// Format small numbers
const small = formatAmount(0.000004, FORMAT_TYPE.STANDARD); // "<0.0001"
formatRawAmount
formattingDescription: Formats a raw bigint amount with specified decimals into a readable string.
💡Tips: Perfect for formatting blockchain amounts from wei to human-readable values. See "formatAmount" code examples for more details.
// Format wei to ETH
const formatted = formatRawAmount(
parseEther("1.55555"),
18,
FORMAT_TYPE.STANDARD
); // "1.5556"
// Format with custom decimals
const custom = formatRawAmount(
BigInt("1500000000000000000"),
18,
FORMAT_TYPE.MINIFY
); // "1.5"truncate
formattingDescription: Truncates a string to a specified length with a separator in the middle.
💡Tips: Useful for displaying long addresses or hashes in a compact format.
// Truncate address
const short = truncate("0x1234567890abcdef1234567890abcdef12345678", 10);
// "0x123...45678"
formatAccountUrl
formattingDescription: Generates a blockchain explorer URL for a given account address.
💡Tips: Useful for creating links to view account details on block explorers.
// Generate account URL
const url = formatAccountUrl(
"0x1234567890abcdef1234567890abcdef12345678",
137
); // "https://polygonscan.com/address/0x1234..."formatTxnUrl
formattingDescription: Generates a blockchain explorer URL for a given transaction hash.
💡Tips: Useful for creating links to view transaction details on block explorers.
// Generate transaction URL
const url = formatTxnUrl(
"0xabcd...",
137
); // "https://polygonscan.com/tx/0xabcd..."getBetSwirlBetUrl
formattingDescription: Generates a BetSwirl bet URL for viewing bet details.
💡Tips: Useful for creating links to view bet information directly on the BetSwirl platform.
// Generate bet URL
const url = getBetSwirlBetUrl(
"123",
CASINO_GAME_TYPE.DICE,
137
); // "https://betswirl.com/polygon/casino/dice/123"Calculation Functions
getBetSwirlFees
calculationDescription: Calculates the BetSwirl fees (affiliate + dividends + treasury + team + bankroll fees) for a given payout amount and house edge.
// Calculate fees
const fees = getBetSwirlFees(
parseEther("2"), // payout amount
400 // house edge (4%)
); // Returns fees in wei (800000000000000000n)getGrossPayout
calculationDescription: Calculates the gross payout amount for a bet with given parameters.
💡Tips: This is the payout before house edge is applied.
// Calculate gross payout
const grossPayout = getGrossPayout(
parseEther("2"), // bet amount
2, // bet count
20000 // gross multiplier (2x)
); // Returns gross payout in wei (4000000000000000000n)getNetMultiplier
calculationDescription: Calculates the net multiplier after applying house edge.
💡Tips: Use this to show players the actual multiplier they'll receive after fees.
// Calculate net multiplier
const netMultiplier = getNetMultiplier(
20000, // gross multiplier (2x)
400 // house edge (4%)
); // Returns net multiplier in basis points (19200)getFormattedNetMultiplier
calculationDescription: Calculates and formats the net multiplier as a decimal number.
💡Tips: Perfect for displaying multipliers in user interfaces.
// Get formatted net multiplier
const formatted = getFormattedNetMultiplier(
20000, // gross multiplier (2x)
400 // house edge (4%)
); // Returns 1.92Game Utility Functions
Game.getMultiplier
game-utilityDescription: Calculates the multiplier for a bet based on the selected number.
💡Tips: All games have these functions.
// Get dice multiplier
const multiplier = Dice.getMultiplier(35); // Returns gross multiplier in basis points (15385)
// Get formatted multiplier
const formatted = Roulette.getFormattedMultiplier([1,25,14]); // Returns gross multiplier in decimal (12.333)Game.getWinChancePercent
game-utilityDescription: Calculates the win chance percentage for a bet.
💡Tips: Use this to display win probabilities to players. All games have this function.
// Get win chance
const diceChance = Dice.getWinChancePercent(75); // Returns percentage (25)
const rouletteChance = Roulette.getWinChancePercent([1,14,25,26,27,28]) // Returns percentage (16.2)WeightedGame.getUniqueOutputs
game-utilityDescription: Gets unique outputs for a weighted game configuration with house edge applied.
💡Tips: Perfect for displaying wheel/plinko segments with their probabilities. It is particularly useful because often, configurations have multiple segments with the same multiplier.
// Get unique outputs
const outputs = WeightedGame.getUniqueOutputs(
wheelConfig, // wheel configuration
400 // house edge (4%)
); // Returns array of unique multipliers with probabilitiesPlinko.getSortedPlinkoOutputs
game-utilityDescription: Gets plinko outputs sorted in a bell-shaped order for optimal UI display.
💡Tips: Creates a visually appealing bell curve layout for plinko game segments.
// Get sorted plinko outputs
const outputs = Plinko.getSortedPlinkoOutputs(
plinkoConfig, // plinko configuration
400 // house edge (4%)
); // Returns bell-shaped sorted outputsEncoding/Decoding Functions
Game.encodeInput
encoding-decodingDescription: Encodes bet input for smart contract interaction.
💡Tips: All games have these functions. Use this to prepare bet data before calling the smart contract.
// Encode dice input
const diceEncoded = Dice.encodeInput(50); // Returns encoded input for contract
// Encode roulette input
const rouletteEncoded = Roulette.encodeInput([1, 2, 3]); // Returns encoded input for numbersGame.decodeInput
encoding-decodingDescription: Decodes bet input from smart contract data.
💡Tips: All games have these functions. Use this to interpret bet data returned from smart contracts.
// Decode dice input
const diceDecoded = Dice.decodeInput(encodedInput); // Returns dice number
// Decode roulette input
const rouletteDecoded = Roulette.decodeInput(encodedInput); // Returns array of numbersGame.decodeRolled
encoding-decodingDescription: Decodes the rolled result from smart contract data.
💡Tips: All games have these functions. Use this to interpret the actual result returned from smart contracts.
// Decode dice result
const diceResult = Dice.decodeRolled(encodedResult); // Returns rolled dice number
// Decode roulette result
const rouletteResult = Roulette.decodeRolled(encodedResult); // Returns winning numberToken Utility Functions
getTokenMetadata
token-utilityDescription: Retrieves token metadata (symbol, decimals) from the blockchain.
💡Tips: Use this to get token information without relying on external APIs.
// Get token metadata
const metadata = await getTokenMetadata(
wallet,
"0x1234...", // token address
137 // chain ID
); // Returns { symbol, decimals, address }chainNativeCurrencyToToken
token-utilityDescription: Converts a chain's native currency to a Token object.
💡Tips: Useful for handling native tokens like ETH, MATIC, etc.
// Get native currency token
const nativeToken = chainNativeCurrencyToToken(
chainById[137].nativeCurrency // polygon native currency
); // Returns Token object for MATICgetGasPrices
token-utilityDescription: Retrieves current gas prices for a specific chain.
💡Tips: Use this to get real-time gas price information for transaction optimization.
// Get gas prices
const gasPrices = await getGasPrices(
wallet,
137, // chain ID
); // Returns gas prices in wei (normal, fast, instant)Function Data Functions
getBetRequirementsFunctionData
function-dataDescription: Generates function data for checking bet requirements.
💡Tips: Use this to prepare contract calls for checking betting restrictions. Use the biggest raw multiplier for games like Keno or Wheel where you have multiple different possible outputs.
// Get function data
const functionData = getBetRequirementsFunctionData(
"0x1234...", // token address
20000, // raw multiplier
137 // chain ID
); // Returns function data for contract callgetAffiliateHouseEdgeFunctionData
function-dataDescription: Generates function data for getting affiliate house edge.
💡Tips: Use this to prepare contract calls for affiliate house edge information.
// Get function data
const functionData = getAffiliateHouseEdgeFunctionData(
CASINO_GAME_TYPE.DICE, // game type
"0x1234...", // token address
"0xabcd...", // affiliate address
137 // chain ID
); // Returns function data for contract callgetKenoConfigurationFunctionData
function-dataDescription: Generates function data for getting keno configuration.
💡Tips: Use this to prepare contract calls for keno game settings.
// Get function data
const functionData = getKenoConfigurationFunctionData(
"0x1234...", // token address
137 // chain ID
); // Returns function data for contract callgetWeightedGameConfigurationFunctionData
function-dataDescription: Generates function data for getting weighted game configuration.
💡Tips: Use this to prepare contract calls for wheel/plinko configurations.
// Get function data
const functionData = getWeightedGameConfigurationFunctionData(
"0x1234...", // contract address
1, // config ID
137 // chain ID
); // Returns function data for contract callgetGamePausedFunctionData
function-dataDescription: Generates function data for checking if a game is paused.
💡Tips: Use this to prepare contract calls for checking game status.
// Get function data
const functionData = getGamePausedFunctionData(
CASINO_GAME_TYPE.DICE, // game type
137 // chain ID
); // Returns function data for contract callgetTokenInfoFunctionData
function-dataDescription: Generates function data for getting token information.
💡Tips: Use this to prepare contract calls for token metadata.
// Get function data
const functionData = getTokenInfoFunctionData(
CASINO_GAME_TYPE.DICE, // game type
"0x1234...", // token address
137 // chain ID
); // Returns function data for contract callgetCasinoTokensFunctionData
function-dataDescription: Generates function data for getting casino tokens.
💡Tips: Use this to prepare contract calls for casino token list.
// Get function data
const functionData = getCasinoTokensFunctionData(
137 // chain ID
); // Returns function data for contract callgetTokenDecimalsFunctionData
function-dataDescription: Generates function data for getting token decimals.
💡Tips: Use this to prepare contract calls for token decimal information.
// Get function data
const functionData = getTokenDecimalsFunctionData(
"0x1234..." // token address
); // Returns function data for contract callgetTokenSymbolFunctionData
function-dataDescription: Generates function data for getting token symbol.
💡Tips: Use this to prepare contract calls for token symbol information.
// Get function data
const functionData = getTokenSymbolFunctionData(
"0x1234..." // token address
); // Returns function data for contract callgetChainlinkVrfCostFunctionData
function-dataDescription: Generates function data for getting Chainlink VRF cost.
💡Tips: Use this to prepare contract calls for VRF cost calculation.
// Get function data
const functionData = getChainlinkVrfCostFunctionData(
CASINO_GAME_TYPE.DICE, // game type
"0x1234...", // token address
1, // bet count
137 // chain ID
); // Returns function data for contract callgetPlaceBetFunctionData
function-dataDescription: Generates function data for placing a bet.
💡Tips: Use this to prepare contract calls for placing casino bets.
// Get function data
const functionData = getPlaceBetFunctionData(
betParams, // bet parameters
137 // chain ID
); // Returns function data for contract callgetPlaceFreebetFunctionData
function-dataDescription: Generates function data for placing a freebet.
💡Tips: Use this to prepare contract calls for placing freebets.
// Get function data
const functionData = getPlaceFreebetFunctionData(
freebetParams, // freebet parameters
137 // chain ID
); // Returns function data for contract callgetAllowanceFunctionData
function-dataDescription: Generates function data for checking token allowance.
💡Tips: Use this to prepare contract calls for allowance checking.
// Get function data
const functionData = getAllowanceFunctionData(
"0x1234...", // token address
"0x5678...", // owner address
"0xabcd..." // spender address
); // Returns function data for contract callgetApproveFunctionData
function-dataDescription: Generates function data for approving token spending.
💡Tips: Use this to prepare contract calls for token approval.
// Get function data
const functionData = getApproveFunctionData(
"0x1234...", // token address
"0xabcd...", // spender address
parseEther("100") // amount to approve
); // Returns function data for contract callgetClaimableAmountFunctionData
function-dataDescription: Generates function data for getting claimable amount.
💡Tips: Use this to prepare contract calls for claimable rewards.
// Get function data
const functionData = getClaimableAmountFunctionData(
leaderboard, // leaderboard address
"0xabcd...", // user address
137 // chain ID
); // Returns function data for contract callgetClaimRewardsLeaderboardFunctionData
function-dataDescription: Generates function data for claiming leaderboard rewards.
💡Tips: Use this to prepare contract calls for claiming rewards.
// Get function data
const functionData = getClaimRewardsLeaderboardFunctionData(
leaderboard, // leaderboard address
"0xabcd..." // receiver address
); // Returns function data for contract call