Skip to content

Commit

Permalink
Merge pull request #338 from reflexer-labs/develop
Browse files Browse the repository at this point in the history
Surplus auctions addons
  • Loading branch information
mstfash authored May 28, 2023
2 parents 3d22582 + f3b63dd commit 9ba1f69
Show file tree
Hide file tree
Showing 3 changed files with 203 additions and 3 deletions.
102 changes: 102 additions & 0 deletions src/containers/Auctions/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import Modal from '../../components/Modals/Modal'
import { useActiveWeb3React } from '../../hooks'
import { useStoreActions } from '../../store'
import AuctionsList from './AuctionsList'
import { useStartSurplusAuction } from 'src/hooks/useAuctions'
import { handleTransactionError } from 'src/hooks/TransactionHooks'
import { formatNumber } from 'src/utils/helper'

export type AuctionEventType = 'DEBT' | 'SURPLUS' | 'STAKED_TOKEN'

Expand All @@ -22,6 +25,31 @@ const Auctions = ({
const [showFaqs, setShowFaqs] = useState(false)
const [type, setType] = useState<AuctionEventType>('SURPLUS')
const [error, setError] = useState('')
const [isLoading, setIsLoading] = useState(false)
const {
startSurplusAcution,
surplusAmountToSell,
accountingEngineSurplus,
allowStartSurplusAuction,
deltaToStartSurplusAuction,
} = useStartSurplusAuction()

const handleStartSurplusAuction = async () => {
setIsLoading(true)
try {
popupsActions.setIsWaitingModalOpen(true)
popupsActions.setWaitingPayload({
title: 'Waiting For Confirmation',
hint: 'Confirm this transaction in your wallet',
status: 'loading',
})
await startSurplusAcution()
} catch (e) {
handleTransactionError(e)
} finally {
setIsLoading(false)
}
}

useEffect(() => {
async function init() {
Expand Down Expand Up @@ -118,6 +146,57 @@ const Auctions = ({
</Tab>
</Switcher>
)}
{type === 'SURPLUS' && account ? (
<StartAuctionContainer>
<Box style={{ justifyContent: 'space-between' }}>
<div>
<Box>
<SurplusTitle>
System Surplus:{' '}
</SurplusTitle>
<span>
{formatNumber(
accountingEngineSurplus as string,
2
)}{' '}
RAI
</span>
</Box>
<Box>
<SurplusTitle>
Surplus Amount to Sell:{' '}
</SurplusTitle>
<span>
{formatNumber(
surplusAmountToSell as string,
2
)}{' '}
RAI
</span>
</Box>

{allowStartSurplusAuction ? null : (
<Box>
(
{formatNumber(
String(deltaToStartSurplusAuction),
2
)}{' '}
RAI) to start an auction
</Box>
)}
</div>
<Button
text={'Start Surplus Auction'}
onClick={handleStartSurplusAuction}
isLoading={isLoading}
disabled={
isLoading || !allowStartSurplusAuction
}
/>
</Box>
</StartAuctionContainer>
) : null}

<AuctionsList type={type} />
</Container>
Expand Down Expand Up @@ -187,3 +266,26 @@ const BtnContainer = styled.div`
padding-top: 20px;
text-align: center;
`

const Box = styled.div`
display: flex;
align-items: center;
span {
font-weight: bold;
}
@media (max-width: 767px) {
flex-direction: column;
margin-bottom: 15px;
}
`
const SurplusTitle = styled.h3`
font-size: 16px;
margin-right: 10px;
font-weight: normal;
`

const StartAuctionContainer = styled.div`
padding: 10px 20px;
border-radius: 15px;
background: ${(props) => props.theme.colors.colorSecondary};
`
102 changes: 100 additions & 2 deletions src/hooks/useAuctions.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import { useEffect, useState } from 'react'
import { useStoreState } from '../store'
import { useEffect, useMemo, useState } from 'react'
import { useStoreActions, useStoreState } from '../store'
import { IAuction } from '../utils/interfaces'
import _ from '../utils/lodash'
import { Geb } from 'geb.js'
import { useActiveWeb3React } from '.'
import { ETH_NETWORK } from 'src/utils/constants'
import { handlePreTxGasEstimate } from './TransactionHooks'
import { parseRad } from 'src/utils/gebManager'
import { BigNumber } from 'ethers'
import { toFixedString } from 'src/utils/helper'

// list auctions data
export default function useAuctions() {
Expand Down Expand Up @@ -83,3 +90,94 @@ export default function useAuctions() {

return state
}

// start surplus auction
export function useStartSurplusAuction() {
const {
transactionsModel: transactionsActions,
popupsModel: popupsActions,
} = useStoreActions((store) => store)

const { account, library } = useActiveWeb3React()
const [surplusAmountToSell, setSurplusAmountToSell] = useState<string>()
const [accountingEngineSurplus, setAccountingEngineSurplus] =
useState<string>()

useEffect(() => {
if (!library || !account) return
const signer = library.getSigner(account)
const geb = new Geb(ETH_NETWORK, signer.provider)
geb.multiCall([
geb.contracts.accountingEngine.surplusAuctionAmountToSell(true),
geb.contracts.safeEngine.coinBalance(
geb.contracts.accountingEngine.address,
true
),
]).then((res) => {
setSurplusAmountToSell(parseRad(res[0]))
setAccountingEngineSurplus(parseRad(res[1]))
})
}, [account, library])

const allowStartSurplusAuction = useMemo(() => {
if (!surplusAmountToSell || !accountingEngineSurplus) return false
const surplusBuffer = 500_000
const surplusAmountToSellWithBuffer = BigNumber.from(
toFixedString(
String(Number(surplusAmountToSell) + surplusBuffer),
'RAD'
)
)
const accountingEngineSurplusBN = BigNumber.from(
toFixedString(accountingEngineSurplus, 'RAD')
)
return surplusAmountToSellWithBuffer.lte(accountingEngineSurplusBN)
}, [surplusAmountToSell, accountingEngineSurplus])

const deltaToStartSurplusAuction = useMemo(() => {
if (!surplusAmountToSell || !accountingEngineSurplus) return 0
const surplusBuffer = 500_000
const surplusAmountToSellWithBuffer =
Number(surplusAmountToSell) + surplusBuffer
const accountingEngineSurplusN = Number(accountingEngineSurplus)
return surplusAmountToSellWithBuffer - accountingEngineSurplusN
}, [surplusAmountToSell, accountingEngineSurplus])

const startSurplusAcution = async function () {
if (!library || !account) throw new Error('No library or account')
const signer = library.getSigner(account)
const geb = new Geb(ETH_NETWORK, signer.provider)

const txData = geb.contracts.accountingEngine.auctionSurplus()

if (!txData) throw new Error('No transaction request!')
const tx = await handlePreTxGasEstimate(signer, txData)
const txResponse = await signer.sendTransaction(tx)
if (txResponse) {
const { hash, chainId } = txResponse
transactionsActions.addTransaction({
chainId,
hash,
from: txResponse.from,
summary: 'Starting surplus auction',
addedTime: new Date().getTime(),
originalTx: txResponse,
})
popupsActions.setIsWaitingModalOpen(true)
popupsActions.setWaitingPayload({
title: 'Transaction Submitted',
hash: txResponse.hash,
status: 'success',
})
await txResponse.wait()
}
}

return {
startSurplusAcution,
surplusAmountToSell,
accountingEngineSurplus,
allowStartSurplusAuction,
deltaToStartSurplusAuction,
}
}
2 changes: 1 addition & 1 deletion src/utils/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@
"debt_auction_claim_tokens": "What does the Claim Tokens button do?",
"debt_auction_claim_tokens_desc": "In case someone outbids you in a debt auction, your RAI bid will be reimbursed to your Reflexer Account.Claim Tokens can be used to get back RAI (and FLX) that is kept in your Account.",
"surplus_auction_minting_flx_header": "Selling Surplus RAI",
"surplus_auction_minting_flx_desc": "Surplus auctions are meant to sell RAI that accrued inside the protocol in exchange for FLX. The FLX that is received by an auction will be burned.",
"surplus_auction_minting_flx_desc": "Surplus auctions are meant to sell RAI that accrued inside the protocol in exchange for FLX. The FLX that is received by an auction will be burned. Auction will start when the surplus amount plus 500k (fixed surplus buffer) be less than or equal to System Surplus ",
"surplus_auction_how_to_bid": "How do I bid?",
"surplus_auction_how_to_bid_desc": "During a surplus auction, you will bid an increasing amount of FLX for a fixed amount of RAI. A practical example: if the current FLX bid is 100 for 20K RAI (resulting in a price of 200 RAI/FLX), the next bidder must bid more FLX for the same amount of RAI. The auction also makes sure that each bid must be at least a certain percentage higher than the previous one (e.g the next bid must be at least 3% higher).",
"surplus_auction_claim_tokens": "What does the Claim Tokens button do?",
Expand Down

0 comments on commit 9ba1f69

Please sign in to comment.