config#CRONOS_BLOCK_TIME TypeScript Examples

The following examples show how to use config#CRONOS_BLOCK_TIME. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: useGetBlockCountdown.ts    From vvs-ui with GNU General Public License v3.0 6 votes vote down vote up
useBlockCountdown = (blockNumber: number) => {
  const timer = useRef<ReturnType<typeof setTimeout>>(null)
  const [secondsRemaining, setSecondsRemaining] = useState(0)

  useEffect(() => {
    const startCountdown = async () => {
      const currentBlock = await simpleRpcProvider.getBlockNumber()

      if (blockNumber > currentBlock) {
        setSecondsRemaining((blockNumber - currentBlock) * CRONOS_BLOCK_TIME)

        // Clear previous interval
        if (timer.current) {
          clearInterval(timer.current)
        }

        timer.current = setInterval(() => {
          setSecondsRemaining((prevSecondsRemaining) => {
            if (prevSecondsRemaining === 1) {
              clearInterval(timer.current)
            }

            return prevSecondsRemaining - 1
          })
        }, 1000)
      }
    }

    startCountdown()

    return () => {
      clearInterval(timer.current)
    }
  }, [setSecondsRemaining, blockNumber, timer])

  return secondsRemaining
}
Example #2
Source File: helpers.tsx    From vvs-ui with GNU General Public License v3.0 6 votes vote down vote up
getDateForBlock = async (currentBlock: number, block: number) => {
  const blocksUntilBlock = block - currentBlock
  const secondsUntilStart = blocksUntilBlock * CRONOS_BLOCK_TIME
  // if block already happened we can get timestamp via .getBlock(block)
  if (currentBlock > block) {
    try {
      const { timestamp } = await simpleRpcProvider.getBlock(block)
      return toDate(timestamp * 1000)
    } catch {
      add(new Date(), { seconds: secondsUntilStart })
    }
  }
  return add(new Date(), { seconds: secondsUntilStart })
}
Example #3
Source File: useGetPublicIfoData.ts    From vvs-ui with GNU General Public License v3.0 5 votes vote down vote up
useGetPublicIfoData = (ifo: Ifo): PublicIfoData => {
  const { address, releaseBlockNumber } = ifo
  const lpTokenPriceInUsd = useLpTokenPrice(ifo.currency.symbol)
  const [state, setState] = useState({
    status: 'idle' as IfoStatus,
    blocksRemaining: 0,
    secondsUntilStart: 0,
    progress: 5,
    secondsUntilEnd: 0,
    startBlockNum: 0,
    endBlockNum: 0,
    numberPoints: null,
    [PoolIds.poolUnlimited]: {
      raisingAmountPool: BIG_ZERO,
      totalAmountPool: BIG_ZERO,
      offeringAmountPool: BIG_ZERO, // Not know
      limitPerUserInLP: BIG_ZERO, //  Not used
      taxRate: 0, //  Not used
      sumTaxesOverflow: BIG_ZERO, //  Not used
    },
  })
  const { currentBlock } = useBlock()
  const fetchIfoData = useCallback(async () => {
    const ifoCalls = ['startBlock', 'endBlock', 'raisingAmount', 'totalAmount'].map((method) => ({
      address,
      name: method,
    }))

    const [startBlock, endBlock, raisingAmount, totalAmount] = await multicallv2(ifoV1Abi, ifoCalls)

    const startBlockNum = startBlock ? startBlock[0].toNumber() : 0
    const endBlockNum = endBlock ? endBlock[0].toNumber() : 0

    const status = getStatus(currentBlock, startBlockNum, endBlockNum)
    const totalBlocks = endBlockNum - startBlockNum
    const blocksRemaining = endBlockNum - currentBlock

    // Calculate the total progress until finished or until start
    const progress =
      currentBlock > startBlockNum
        ? ((currentBlock - startBlockNum) / totalBlocks) * 100
        : ((currentBlock - releaseBlockNumber) / (startBlockNum - releaseBlockNumber)) * 100

    setState((prev) => ({
      status,
      blocksRemaining,
      secondsUntilStart: (startBlockNum - currentBlock) * CRONOS_BLOCK_TIME,
      progress,
      secondsUntilEnd: blocksRemaining * CRONOS_BLOCK_TIME,
      startBlockNum,
      endBlockNum,
      currencyPriceInUSD: null,
      numberPoints: null,
      [PoolIds.poolUnlimited]: {
        ...prev.poolUnlimited,
        raisingAmountPool: raisingAmount ? new BigNumber(raisingAmount[0].toString()) : BIG_ZERO,
        totalAmountPool: totalAmount ? new BigNumber(totalAmount[0].toString()) : BIG_ZERO,
      },
    }))
  }, [address, currentBlock, releaseBlockNumber])

  useEffect(() => {
    fetchIfoData()
  }, [fetchIfoData])

  return { ...state, currencyPriceInUSD: lpTokenPriceInUsd, fetchIfoData }
}
Example #4
Source File: useGetPublicIfoData.ts    From vvs-ui with GNU General Public License v3.0 4 votes vote down vote up
useGetPublicIfoData = (ifo: Ifo): PublicIfoData => {
  const { address, releaseBlockNumber } = ifo
  const lpTokenPriceInUsd = useLpTokenPrice(ifo.currency.symbol)
  const { fastRefresh } = useRefresh()

  const [state, setState] = useState({
    status: 'idle' as IfoStatus,
    blocksRemaining: 0,
    secondsUntilStart: 0,
    progress: 5,
    secondsUntilEnd: 0,
    poolBasic: {
      raisingAmountPool: BIG_ZERO,
      offeringAmountPool: BIG_ZERO,
      limitPerUserInLP: BIG_ZERO,
      taxRate: 0,
      totalAmountPool: BIG_ZERO,
      sumTaxesOverflow: BIG_ZERO,
    },
    poolUnlimited: {
      raisingAmountPool: BIG_ZERO,
      offeringAmountPool: BIG_ZERO,
      limitPerUserInLP: BIG_ZERO,
      taxRate: 0,
      totalAmountPool: BIG_ZERO,
      sumTaxesOverflow: BIG_ZERO,
    },
    startBlockNum: 0,
    endBlockNum: 0,
    numberPoints: 0,
  })
  const { currentBlock } = useBlock()

  const fetchIfoData = useCallback(async () => {
    const ifoCalls = [
      {
        address,
        name: 'startBlock',
      },
      {
        address,
        name: 'endBlock',
      },
      {
        address,
        name: 'viewPoolInformation',
        params: [0],
      },
      {
        address,
        name: 'viewPoolInformation',
        params: [1],
      },
      {
        address,
        name: 'viewPoolTaxRateOverflow',
        params: [1],
      },
      {
        address,
        name: 'numberPoints',
      },
    ]

    const [startBlock, endBlock, poolBasic, poolUnlimited, taxRate, numberPoints] = await multicallv2(
      ifoV2Abi,
      ifoCalls,
    )

    const poolBasicFormatted = formatPool(poolBasic)
    const poolUnlimitedFormatted = formatPool(poolUnlimited)

    const startBlockNum = startBlock ? startBlock[0].toNumber() : 0
    const endBlockNum = endBlock ? endBlock[0].toNumber() : 0
    const taxRateNum = taxRate ? taxRate[0].div(TAX_PRECISION).toNumber() : 0

    const status = getStatus(currentBlock, startBlockNum, endBlockNum)
    const totalBlocks = endBlockNum - startBlockNum
    const blocksRemaining = endBlockNum - currentBlock

    // Calculate the total progress until finished or until start
    const progress =
      currentBlock > startBlockNum
        ? ((currentBlock - startBlockNum) / totalBlocks) * 100
        : ((currentBlock - releaseBlockNumber) / (startBlockNum - releaseBlockNumber)) * 100

    setState((prev) => ({
      ...prev,
      secondsUntilEnd: blocksRemaining * CRONOS_BLOCK_TIME,
      secondsUntilStart: (startBlockNum - currentBlock) * CRONOS_BLOCK_TIME,
      poolBasic: { ...poolBasicFormatted, taxRate: 0 },
      poolUnlimited: { ...poolUnlimitedFormatted, taxRate: taxRateNum },
      status,
      progress,
      blocksRemaining,
      startBlockNum,
      endBlockNum,
      numberPoints: numberPoints ? numberPoints[0].toNumber() : 0,
    }))
  }, [address, currentBlock, releaseBlockNumber])

  useEffect(() => {
    fetchIfoData()
  }, [fetchIfoData, fastRefresh])

  return { ...state, currencyPriceInUSD: lpTokenPriceInUsd, fetchIfoData }
}