How to use the web3/lib/utils/utils.js.toBigNumber function in web3

To help you get started, we’ve selected a few web3 examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github gnosis / dx-react / src / actions / blockchain.ts View on Github external
{ fee },
  ] = await Promise.all([
    dxAPI(),
    promisedContractsMap(),
    calculateSellAmountAfterFee(sellAmount, userAccount),
  ])

  const [ethUSDPrice, owlBalance, owlAllowance] = await Promise.all([
    PriceOracle.getUSDETHPrice(),
    getTokenBalance(TokenOWL.address, userAccount),
    getTokenAllowance(TokenOWL.address, userAccount),
  ])
  const feeInUSD = fee.mul(ethUSDPrice)
  // // return lesser of the 2
  // return Math.min(owlBalance.toNumber(), Math.min(owlAllowance.toNumber(), feeInUSD.div(2).toNumber()))
  const owlsBurned =  toBigNumber(Math.min(owlBalance.toNumber(), Math.min(owlAllowance.toNumber(), feeInUSD.div(2).toNumber())))
  const adjustment = owlsBurned.gt(0) ? (owlsBurned.mul(fee)).div(feeInUSD) : toBigNumber(0)

  // console.warn('getFeeReductionFromOWL ==> ', { TokenOWLaddr: TokenOWL.address, fee, ethUSDPrice, owlBalance, owlAllowance, feeInUSD, owlsBurned, adjustment })
  return { adjustment, ethUSDPrice }
}
github gnosis / dx-react / src / components / AuctionStateHOC / index.tsx View on Github external
currentAuctionIndex,
        outstandingVolume,
      })
      console.log('status: ', status)

      const account = await promisedAccount
      const sellerBalance = await getSellerBalance(pair, index, account)
      console.log('sellerBalance: ', sellerBalance.toString())

      // TODO: calculate differently for PLANNED auctions (currently is NaN)
      // ALSO: consider calculating not using price but rather sellerBalance/totalSellVolume*totalBuyVolume,
      // as price calculation returns a slightly larger figure than buyerVolume even (price is too optimistic)
      const userGetting = sellerBalance.mul(price[0]).div(price[1])

      let userCanClaim = sellerBalance.greaterThan(0) && closingPrice[0].gte(0) ?
        (await getUnclaimedSellerFunds(pair, index, account)) : toBigNumber(0)

        // if theoretically closed, then calculate differently as fraction of current volume
      if (theoretically) userCanClaim = buyVolume.div(sellVolume).mul(sellerBalance)

      const timeToCompletion = status === AuctionStatus.ACTIVE ? auctionStart.plus(86400 - now).mul(1000).toNumber() : 0

      const progress = getProgressStep({
        status,
        sellerBalance,
      })

      if (this.interval === null) return true

      this.setState({
        completed: status === AuctionStatus.ENDED,
        theoreticallyCompleted: theoretically,
github gnosis / dx-react / src / actions / blockchain.ts View on Github external
] = await Promise.all([
    dxAPI(),
    promisedContractsMap(),
    calculateSellAmountAfterFee(sellAmount, userAccount),
  ])

  const [ethUSDPrice, owlBalance, owlAllowance] = await Promise.all([
    PriceOracle.getUSDETHPrice(),
    getTokenBalance(TokenOWL.address, userAccount),
    getTokenAllowance(TokenOWL.address, userAccount),
  ])
  const feeInUSD = fee.mul(ethUSDPrice)
  // // return lesser of the 2
  // return Math.min(owlBalance.toNumber(), Math.min(owlAllowance.toNumber(), feeInUSD.div(2).toNumber()))
  const owlsBurned =  toBigNumber(Math.min(owlBalance.toNumber(), Math.min(owlAllowance.toNumber(), feeInUSD.div(2).toNumber())))
  const adjustment = owlsBurned.gt(0) ? (owlsBurned.mul(fee)).div(feeInUSD) : toBigNumber(0)

  // console.warn('getFeeReductionFromOWL ==> ', { TokenOWLaddr: TokenOWL.address, fee, ethUSDPrice, owlBalance, owlAllowance, feeInUSD, owlsBurned, adjustment })
  return { adjustment, ethUSDPrice }
}
github gnosis / dx-react / src / api / index.ts View on Github external
balanceNormal: ${balanceNormal}
        balanceInverse: ${balanceInverse}

        currAuctionNeverRanDir: ${currAuctionNeverRanDir}
        currAuctionNeverRanOpp: ${currAuctionNeverRanOpp}

        indicesWithSellerBalance: ${indicesWithSellerBalance}
        indicesWithSellerBalanceInverse: ${indicesWithSellerBalanceInverse}
      `)

      if (  // if there are truly no auctions with sellBalance
        !committedToNextNormal && !committedToNextInverse &&
        indicesWithSellerBalance.length === 0 && indicesWithSellerBalanceInverse.length === 0
      ) return accum

      let totalPastBalanceNormal: BigNumber    = toBigNumber(0), totalPastBalanceInverse: BigNumber    = toBigNumber(0)
      let totalPastClaimableNormal: BigNumber  = toBigNumber(0), totalPastClaimableInverse: BigNumber  = toBigNumber(0)

      let currentClaimableBalanceNormal: BigNumber    = toBigNumber(0), currentClaimableBalanceInverse: BigNumber    = toBigNumber(0)

      const pastIndicesNormal: string[] = [], pastIndicesInverse: string[] = []
      const pastBalancesPerIndexNormal: BigNumber[] = [], pastBalancesPerIndexInverse: BigNumber[] = []
      const pastClaimablePerIndexNormal: BigNumber[] = [], pastClaimablePerIndexInverse: BigNumber[] = []

      for (let i = 0; i < indicesWithSellerBalance.length; ++i) {
        const ind = indicesWithSellerBalance[i]
        const bal = balancePerIndex[i]
        const claim = claimablePerIndex[i]

        if (lastIndex.eq(ind)) {
          currentClaimableBalanceNormal = claim
github gnosis / dx-react / src / actions / blockchain.ts View on Github external
export async function calculateSellAmountAfterFee(sellAmount: string | BigNumber, userAccount: Account) {
  const selling: BigNumber = toBigNumber(sellAmount)

  const feeRatio = await getFeeRatio(userAccount)
  const fee = selling.times(feeRatio)

  return { selling, fee }
}
github gnosis / dx-react / stories / AuctionStatus.tsx View on Github external
const constructKnobs = (status: string) => ({
  buyToken: object('buyToken', { name: 'GNOSIS', symbol: 'GNO', address: '', decimals: 18 }),
  sellToken: object('sellToken', { name: 'ETHER', symbol: 'ETH', address: '', decimals: 18 }),
  buyAmount: toBigNumber(number('buyAmount', 2.55203, {
    range: true,
    min: 0,
    max: 100,
    step: 0.00000001,
  })),
  timeLeft: 73414,
  status: text('status', status),
}) as AuctionStatusProps
github gnosis / dx-react / stories / AuctionFooter.tsx View on Github external
const constructKnobs = (auctionEnded: boolean) => {
  return ({
    buyTokenSymbol: text('buyTokenSymbol', 'GNO'),
    sellTokenSymbol: text('sellTokenSymbol', 'ETH'),
    sellAmount: toBigNumber(number('sellAmt', 100)),
    buyAmount: toBigNumber(number('buyAmt', 100)),
    sellDecimal: number('sellDecimal', 18),
    buyDecimal: number('buyDecimal', 18),
    auctionEnded: boolean('auctionEnded', auctionEnded),
  }) as AuctionFooterProps
}
github gnosis / dx-react / stories / TokenItem.tsx View on Github external
const constructKnobs = (name: TokenName, code: TokenCode, balance: Balance, mod?: TokenMod) => ({
  name: text('name', name),
  symbol: text('code', code),
  balance: toBigNumber(number('balance', +balance, {
    range: true,
    min: 0,
    max: 100,
    step: 0.00000001,
  })),
  mod: text('mod', mod),
}) as TokenItemProps
github gnosis / dx-react / stories / AuctionSellingGetting.tsx View on Github external
.add('AuctionSellingGetting', () => {
    const sellAmount = number('sellAmount', 0).toString()
    const ratio = number('sellRatio', 1.5)
    const buyAmount = (+sellAmount * ratio).toString()

    return (
      {}}
      />
    )
  })