UNPKG

@sx-bet/sportx-js

Version:

Provides an easy to use API to interact with the SportX relayer.

37 lines 1.98 kB
import { BigNumber as EthBigNumber } from "@ethersproject/bignumber"; import { formatUnits, parseUnits } from "@ethersproject/units"; import { BigNumber } from "bignumber.js"; import { FRACTION_DENOMINATOR, PERCENTAGE_PRECISION_EXPONENT, TokenDecimalMapping, } from "../constants"; export function convertToAPIPercentageOdds(decimal) { if (decimal < 0 || decimal > 1) { throw new Error(`${decimal} not in valid range. Must be between 0 and 1`); } const protocolBigNum = decimal * Math.pow(10, 20); return EthBigNumber.from(protocolBigNum.toString()); } export function convertFromAPIPercentageOdds(odds) { const apiPercentageOdds = EthBigNumber.from(odds); if (apiPercentageOdds.gt(FRACTION_DENOMINATOR)) { throw new Error(`Invalid api percentage odds. ${apiPercentageOdds} greater than ${EthBigNumber.from(10) .pow(PERCENTAGE_PRECISION_EXPONENT) .toString()}`); } const bigNumWithDecimals = new BigNumber(apiPercentageOdds.toString()); const impliedOddsWithDecimals = bigNumWithDecimals.dividedBy(new BigNumber(10).exponentiatedBy(PERCENTAGE_PRECISION_EXPONENT)); return impliedOddsWithDecimals.toNumber(); } export function convertToContractOrder(order) { return Object.assign(Object.assign({}, order), { totalBetSize: EthBigNumber.from(order.totalBetSize), percentageOdds: EthBigNumber.from(order.percentageOdds), expiry: EthBigNumber.from(order.expiry), baseToken: order.baseToken, salt: EthBigNumber.from(order.salt) }); } export function convertToTrueTokenAmount(amount, baseToken) { return parseUnits(amount.toString(), TokenDecimalMapping[baseToken]).toString(); } export function convertToDisplayAmount(amount, baseToken) { return formatUnits(amount, TokenDecimalMapping[baseToken]); } export function convertToTakerPayAmount(amount, odds) { return EthBigNumber.from(amount) .mul(odds) .div(FRACTION_DENOMINATOR.sub(odds)); } //# sourceMappingURL=convert.js.map