diff --git a/src/containers/Auctions/AuctionsList.tsx b/src/containers/Auctions/AuctionsList.tsx index 57119f42..73a3aafc 100644 --- a/src/containers/Auctions/AuctionsList.tsx +++ b/src/containers/Auctions/AuctionsList.tsx @@ -27,7 +27,7 @@ const AuctionsList = ({ type }: Props) => { const { proxyAddress } = connectWalletState // auctions list - const auctions = useAuctions() + const auctions = useAuctions(type) // handle clicking to claim const handleClick = (modalType: string) => { diff --git a/src/hooks/useAuctions.ts b/src/hooks/useAuctions.ts index 8c69c61d..cdcf910b 100644 --- a/src/hooks/useAuctions.ts +++ b/src/hooks/useAuctions.ts @@ -1,11 +1,217 @@ -import { useEffect, useState } from 'react' +import { ethers } from 'ethers' +import { useCallback, useEffect, useMemo, useState } from 'react' import { useStoreState } from '../store' +import { utils as gebUtils } from 'geb.js' import { IAuction } from '../utils/interfaces' import _ from '../utils/lodash' +import useGeb from './useGeb' +import dayjs from 'dayjs' -// list auctions data -export default function useAuctions() { +const TYPES = { + SURPLUS: 'surplusAuctionHouse', + DEBT: 'debtAuctionHouse', + STAKED_TOKEN: 'stakingAuctionHouse', +} as const + +export function useRPCAuctions( + type: keyof typeof TYPES = 'SURPLUS', + id?: number +) { + const [state, setState] = useState() + const geb = useGeb() + const fetchRPCAuctions = useCallback( + ({ + bids, + bidIncrease, + bidDuration, + amountSoldIncrease, + totalLength, + auctionId, + type, + }) => { + const { bidAmount, amountToSell, highBidder, auctionDeadline } = + bids + + const auction = { + auctionDeadline: String(auctionDeadline), + auctionId: String(auctionId), + buyAmount: ethers.utils.formatEther(bidAmount), + buyInitialAmount: ethers.utils.formatEther(bidAmount), + buyToken: type === 'SURPLUS' ? 'PROTOCOL_TOKEN' : 'COIN', + createdAt: String(Date.now()), + createdAtTransaction: '0x0', + englishAuctionBids: [ + { + bidder: highBidder, + buyAmount: ethers.utils.formatEther(bidAmount), + createdAt: String(dayjs(new Date()).unix()), + createdAtTransaction: '0x0', + sellAmount: ethers.utils.formatEther(amountToSell), + }, + ], + biddersList: [ + { + bidder: highBidder, + buyAmount: ethers.utils.formatEther(bidAmount), + createdAt: String(dayjs(new Date()).unix()), + createdAtTransaction: '0x0', + sellAmount: ethers.utils.formatEther(amountToSell), + }, + ], + englishAuctionConfiguration: { + DEBT_amountSoldIncrease: + ethers.utils.formatEther(amountSoldIncrease), + bidDuration: String(bidDuration), + bidIncrease: ethers.utils.formatEther(bidIncrease), + totalAuctionLength: String(totalLength), + }, + englishAuctionType: type, + isClaimed: bidAmount.isZero(), + sellAmount: ethers.utils.formatEther(amountToSell), + sellInitialAmount: ethers.utils.formatEther(amountToSell), + sellToken: + type === 'SURPLUS' + ? 'COIN' + : type === 'DEBT' + ? 'PROTOCOL_TOKEN' + : 'PROTOCOL_TOKEN_LP', + startedBy: highBidder, + winner: + auctionDeadline > 0 && auctionDeadline * 1000 > Date.now() + ? highBidder + : '', + } + + setState(auction) + }, + [] + ) + + useEffect(() => { + if (!geb) return + if (type === 'SURPLUS') { + geb.contracts.surplusAuctionHouse + .auctionsStarted() + .then((totalLength) => { + const auctionId = id ? id : totalLength + geb.multiCall([ + geb.contracts.surplusAuctionHouse.bids(auctionId, true), + geb.contracts.surplusAuctionHouse.bidIncrease(true), + geb.contracts.surplusAuctionHouse.bidDuration(true), + geb.contracts.debtAuctionHouse.amountSoldIncrease(true), + ]) + .then( + ([ + bids, + bidIncrease, + bidDuration, + amountSoldIncrease, + ]) => { + const modBids = { + ...bids, + amountToSell: gebUtils.decimalShift( + bids.amountToSell.div(gebUtils.WAD), + -9 + ), + } + fetchRPCAuctions({ + bids: modBids, + bidIncrease, + bidDuration, + totalLength, + auctionId, + amountSoldIncrease, + type, + }) + } + ) + .catch((e) => console.log(e)) + }) + } + if (type === 'DEBT') { + geb.contracts.debtAuctionHouse + .auctionsStarted() + .then((totalLength) => { + const auctionId = id ? id : totalLength.toNumber() + geb.multiCall([ + geb.contracts.debtAuctionHouse.bids(auctionId, true), + geb.contracts.surplusAuctionHouse.bidIncrease(true), + geb.contracts.debtAuctionHouse.bidDuration(true), + geb.contracts.debtAuctionHouse.amountSoldIncrease(true), + ]) + .then( + ([ + bids, + bidIncrease, + bidDuration, + amountSoldIncrease, + ]) => { + const modBids = { + ...bids, + bidAmount: gebUtils.decimalShift( + bids.bidAmount.div(gebUtils.WAD), + -9 + ), + } + + fetchRPCAuctions({ + bids: modBids, + bidIncrease, + bidDuration, + amountSoldIncrease, + totalLength, + auctionId, + type, + }) + } + ) + .catch((e) => console.log(e)) + }) + } + + if (type === 'STAKED_TOKEN') { + geb.contracts.stakingAuctionHouse + .auctionsStarted() + .then((totalLength) => { + const auctionId = id ? id : totalLength + geb.multiCall([ + geb.contracts.stakingAuctionHouse.bids(auctionId, true), + geb.contracts.stakingAuctionHouse.bidIncrease(true), + geb.contracts.stakingAuctionHouse.bidDuration(true), + geb.contracts.debtAuctionHouse.amountSoldIncrease(true), + ]) + .then( + ([ + bids, + bidIncrease, + bidDuration, + amountSoldIncrease, + ]) => { + const modBids = { + ...bids, + } + fetchRPCAuctions({ + bids: modBids, + bidIncrease, + bidDuration, + totalLength, + auctionId, + amountSoldIncrease, + type, + }) + } + ) + .catch((e) => console.log(e)) + }) + } + }, [fetchRPCAuctions, geb, id, type]) + + return useMemo(() => state, [state]) +} + +export function useGraphAuctions() { const [state, setState] = useState>() + const { auctionsModel: auctionsState, connectWalletModel: connectWalletState, @@ -91,3 +297,19 @@ export default function useAuctions() { return state } + +// list auctions data +export default function useAuctions( + type?: 'DEBT' | 'SURPLUS' | 'STAKED_TOKEN', + id?: string +) { + const graphAuctions = useGraphAuctions() + const rpcAuction = useRPCAuctions(type, id ? Number(id) : undefined) + + const auctions = rpcAuction + ? [rpcAuction].filter((auction: IAuction) => { + return Number(auction.auctionDeadline) * 1000 > Date.now() + }) + : graphAuctions + return auctions +}