config#RECLAIM_AUCTIONS_TO_FETCH TypeScript Examples

The following examples show how to use config#RECLAIM_AUCTIONS_TO_FETCH. 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: useReclaimAuctionBid.ts    From glide-frontend with GNU General Public License v3.0 5 votes vote down vote up
useReclaimAuctionBid = (): [ReclaimableAuction | null, () => void] => {
  const { account } = useWeb3React()

  const [state, dispatch] = useReducer(reclaimReducer, initialState)

  const farmAuctionContract = useFarmAuctionContract()

  const checkNextAuction = () => {
    dispatch({ type: 'checkNextAuction' })
  }

  // Reset checking if account was switched
  useEffect(() => {
    dispatch({ type: 'reset' })
  }, [account])

  // Fetch auction data for auctions account has participated
  useEffect(() => {
    const fetchBidderAuctions = async () => {
      try {
        dispatch({ type: 'setLoading', payload: { loading: true } })

        const bidderAuctionsResponse = await farmAuctionContract.viewBidderAuctions(
          account,
          state.currentCursor,
          RECLAIM_AUCTIONS_TO_FETCH,
        )

        const { auctions, nextCursor } = processBidderAuctions(bidderAuctionsResponse)
        if (auctions.length > 0) {
          dispatch({ type: 'setAuctions', payload: { auctions, nextCursor } })
        }
      } catch (error) {
        console.error('Failed to fetch auctions for bidder', error)
        dispatch({ type: 'setLoading', payload: { loading: false } })
      }
    }

    if (!state.loading && account && state.currentCursor === state.nextCursor) {
      fetchBidderAuctions()
    }
  }, [account, state, farmAuctionContract])

  useEffect(() => {
    const checkIfAuctionIsClaimable = async (auctionToCheck: BidderAuction) => {
      dispatch({ type: 'setLoading', payload: { loading: true } })
      try {
        const isClaimable = await farmAuctionContract.claimable(auctionToCheck.id, account)
        if (isClaimable) {
          const [auctionBidders] = await farmAuctionContract.viewBidsPerAuction(auctionToCheck.id, 0, 500)
          const sortedBidders = sortAuctionBidders(auctionBidders)
          const accountBidderData = sortedBidders.find((bidder) => bidder.account === account)
          const position = accountBidderData?.position
          const auctionToReclaim = { id: auctionToCheck.id, amount: auctionToCheck.amount, position }
          dispatch({ type: 'setAuctionToReclaim', payload: { auctionToReclaim } })
        } else {
          dispatch({ type: 'checkNextAuction' })
        }
      } catch (error) {
        dispatch({ type: 'setLoading', payload: { loading: false } })
        console.error('Failed to check for unclaim bids', error)
      }
    }
    const { auctions, nextAuctionToCheck, loading } = state
    if (auctions.length > 0 && account && !loading) {
      const auctionToCheck = auctions[nextAuctionToCheck]
      checkIfAuctionIsClaimable(auctionToCheck)
    }
  }, [account, state, farmAuctionContract])

  return [state.auctionToReclaim, checkNextAuction]
}