config#AUCTION_BIDDERS_TO_FETCH TypeScript Examples

The following examples show how to use config#AUCTION_BIDDERS_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: useAuctionHistory.tsx    From glide-frontend with GNU General Public License v3.0 6 votes vote down vote up
useAuctionHistory = (auctionId: number) => {
  const [auctionHistory, setAuctionHistory] = useState<AuctionHistoryMap>({})

  const farmAuctionContract = useFarmAuctionContract()

  // Get past auction data
  useEffect(() => {
    const fetchAuction = async () => {
      try {
        const auctionData = await farmAuctionContract.auctions(auctionId)
        const processedAuctionData = await processAuctionData(auctionId, auctionData)

        const [auctionBidders] = await farmAuctionContract.viewBidsPerAuction(auctionId, 0, AUCTION_BIDDERS_TO_FETCH)
        const sortedBidders = sortAuctionBidders(auctionBidders, processedAuctionData)
        setAuctionHistory((prev) => ({
          ...prev,
          [auctionId]: { auction: processedAuctionData, bidders: sortedBidders },
        }))
      } catch (error) {
        console.error('Failed to fetch auction history', error)
      }
    }
    if (!auctionHistory[auctionId] && auctionId > 0) {
      fetchAuction()
    }
  }, [farmAuctionContract, auctionHistory, auctionId])

  return auctionHistory
}
Example #2
Source File: useCurrentFarmAuction.ts    From glide-frontend with GNU General Public License v3.0 4 votes vote down vote up
useCurrentFarmAuction = (account: string) => {
  const [currentAuction, setCurrentAuction] = useState<Auction | null>(null)
  const [bidders, setBidders] = useState<Bidder[] | null>(null)
  const [conncetedBidder, setConncetedBidder] = useState<ConnectedBidder | null>(null)
  // Used to force-refresh bidders after successful bid
  const { lastUpdated, setLastUpdated } = useLastUpdated()

  const { fastRefresh } = useRefresh()

  const farmAuctionContract = useFarmAuctionContract()

  // Get latest auction id and its data
  useEffect(() => {
    const fetchCurrentAuction = async () => {
      try {
        const auctionId = await farmAuctionContract.currentAuctionId()
        const auctionData = await farmAuctionContract.auctions(auctionId)
        const processedAuctionData = await processAuctionData(auctionId.toNumber(), auctionData)
        setCurrentAuction(processedAuctionData)
      } catch (error) {
        console.error('Failed to fetch current auction', error)
      }
    }
    fetchCurrentAuction()
  }, [farmAuctionContract, fastRefresh])

  // Fetch bidders for current auction
  useEffect(() => {
    const fetchBidders = async () => {
      try {
        const [currentAuctionBidders] = await farmAuctionContract.viewBidsPerAuction(
          currentAuction.id,
          0,
          AUCTION_BIDDERS_TO_FETCH,
        )
        const sortedBidders = sortAuctionBidders(currentAuctionBidders, currentAuction)
        setBidders(sortedBidders)
      } catch (error) {
        console.error('Failed to fetch bidders', error)
      }
    }
    if (currentAuction) {
      fetchBidders()
    }
  }, [currentAuction, farmAuctionContract, lastUpdated, fastRefresh])

  // Check if connected wallet is whitelisted
  useEffect(() => {
    const checkAccount = async () => {
      try {
        const whitelistedStatus = await farmAuctionContract.whitelisted(account)
        setConncetedBidder({
          account,
          isWhitelisted: whitelistedStatus,
        })
      } catch (error) {
        console.error('Failed to check if account is whitelisted', error)
      }
    }
    if (account && (!conncetedBidder || conncetedBidder.account !== account)) {
      checkAccount()
    }
    // Refresh UI if user logs out
    if (!account) {
      setConncetedBidder(null)
    }
  }, [account, conncetedBidder, farmAuctionContract])

  // Attach bidder data to conncetedBidder object
  useEffect(() => {
    const getBidderData = () => {
      if (bidders && bidders.length > 0) {
        const bidderData = bidders.find((bidder) => bidder.account === account)
        if (bidderData) {
          return bidderData
        }
      }
      const bidderInfo = getBidderInfo(account)
      const defaultBidderData = {
        position: null,
        samePositionAsAbove: false,
        isTopPosition: false,
        amount: BIG_ZERO,
        ...bidderInfo,
      }
      return defaultBidderData
    }
    if (conncetedBidder && conncetedBidder.isWhitelisted) {
      const bidderData = getBidderData()
      if (!isEqual(bidderData, conncetedBidder.bidderData)) {
        setConncetedBidder({
          account,
          isWhitelisted: true,
          bidderData,
        })
      }
    }
  }, [account, conncetedBidder, bidders])

  return {
    currentAuction,
    bidders,
    conncetedBidder,
    refreshBidders: setLastUpdated,
  }
}
Example #3
Source File: useCurrentFarmAuction.ts    From vvs-ui with GNU General Public License v3.0 4 votes vote down vote up
useCurrentFarmAuction = (account: string) => {
  const [currentAuction, setCurrentAuction] = useState<Auction | null>(null)
  const [bidders, setBidders] = useState<Bidder[] | null>(null)
  const [connectedBidder, setConnectedBidder] = useState<ConnectedBidder | null>(null)
  // Used to force-refresh bidders after successful bid
  const { lastUpdated, setLastUpdated } = useLastUpdated()

  const { fastRefresh } = useRefresh()

  const farmAuctionContract = useFarmAuctionContract()

  // Get latest auction id and its data
  useEffect(() => {
    const fetchCurrentAuction = async () => {
      try {
        const auctionId = await farmAuctionContract.currentAuctionId()
        const auctionData = await farmAuctionContract.auctions(auctionId)
        const processedAuctionData = await processAuctionData(auctionId.toNumber(), auctionData)
        setCurrentAuction(processedAuctionData)
      } catch (error) {
        console.error('Failed to fetch current auction', error)
      }
    }
    fetchCurrentAuction()
  }, [farmAuctionContract, fastRefresh])

  // Fetch bidders for current auction
  useEffect(() => {
    const fetchBidders = async () => {
      try {
        const [currentAuctionBidders] = await farmAuctionContract.viewBidsPerAuction(
          currentAuction.id,
          0,
          AUCTION_BIDDERS_TO_FETCH,
        )
        const sortedBidders = sortAuctionBidders(currentAuctionBidders, currentAuction)
        setBidders(sortedBidders)
      } catch (error) {
        console.error('Failed to fetch bidders', error)
      }
    }
    if (currentAuction) {
      fetchBidders()
    }
  }, [currentAuction, farmAuctionContract, lastUpdated, fastRefresh])

  // Check if connected wallet is whitelisted
  useEffect(() => {
    const checkAccount = async () => {
      try {
        const whitelistedStatus = await farmAuctionContract.whitelisted(account)
        setConnectedBidder({
          account,
          isWhitelisted: whitelistedStatus,
        })
      } catch (error) {
        console.error('Failed to check if account is whitelisted', error)
      }
    }
    if (account && (!connectedBidder || connectedBidder.account !== account)) {
      checkAccount()
    }
    // Refresh UI if user logs out
    if (!account) {
      setConnectedBidder(null)
    }
  }, [account, connectedBidder, farmAuctionContract])

  // Attach bidder data to connectedBidder object
  useEffect(() => {
    const getBidderData = () => {
      if (bidders && bidders.length > 0) {
        const bidderData = bidders.find((bidder) => bidder.account === account)
        if (bidderData) {
          return bidderData
        }
      }
      const bidderInfo = getBidderInfo(account)
      const defaultBidderData = {
        position: null,
        samePositionAsAbove: false,
        isTopPosition: false,
        amount: BIG_ZERO,
        ...bidderInfo,
      }
      return defaultBidderData
    }
    if (connectedBidder && connectedBidder.isWhitelisted) {
      const bidderData = getBidderData()
      if (!isEqual(bidderData, connectedBidder.bidderData)) {
        setConnectedBidder({
          account,
          isWhitelisted: true,
          bidderData,
        })
      }
    }
  }, [account, connectedBidder, bidders])

  return {
    currentAuction,
    bidders,
    connectedBidder,
    refreshBidders: setLastUpdated,
  }
}