Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auctions rpc #325

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/containers/Auctions/AuctionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
228 changes: 225 additions & 3 deletions src/hooks/useAuctions.ts
Original file line number Diff line number Diff line change
@@ -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<IAuction>()
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<Array<IAuction>>()

const {
auctionsModel: auctionsState,
connectWalletModel: connectWalletState,
Expand Down Expand Up @@ -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
}