|
1 | | -import { useEffect, useState } from 'react' |
2 | | -import { useStoreState } from '../store' |
| 1 | +import { useEffect, useMemo, useState } from 'react' |
| 2 | +import { useStoreActions, useStoreState } from '../store' |
3 | 3 | import { IAuction } from '../utils/interfaces' |
4 | 4 | import _ from '../utils/lodash' |
| 5 | +import { Geb } from 'geb.js' |
| 6 | +import { useActiveWeb3React } from '.' |
| 7 | +import { ETH_NETWORK } from 'src/utils/constants' |
| 8 | +import { handlePreTxGasEstimate } from './TransactionHooks' |
| 9 | +import { parseRad } from 'src/utils/gebManager' |
| 10 | +import { BigNumber } from 'ethers' |
| 11 | +import { toFixedString } from 'src/utils/helper' |
5 | 12 |
|
6 | 13 | // list auctions data |
7 | 14 | export default function useAuctions() { |
@@ -83,3 +90,94 @@ export default function useAuctions() { |
83 | 90 |
|
84 | 91 | return state |
85 | 92 | } |
| 93 | + |
| 94 | +// start surplus auction |
| 95 | +export function useStartSurplusAuction() { |
| 96 | + const { |
| 97 | + transactionsModel: transactionsActions, |
| 98 | + popupsModel: popupsActions, |
| 99 | + } = useStoreActions((store) => store) |
| 100 | + |
| 101 | + const { account, library } = useActiveWeb3React() |
| 102 | + const [surplusAmountToSell, setSurplusAmountToSell] = useState<string>() |
| 103 | + const [accountingEngineSurplus, setAccountingEngineSurplus] = |
| 104 | + useState<string>() |
| 105 | + |
| 106 | + useEffect(() => { |
| 107 | + if (!library || !account) return |
| 108 | + const signer = library.getSigner(account) |
| 109 | + const geb = new Geb(ETH_NETWORK, signer.provider) |
| 110 | + geb.multiCall([ |
| 111 | + geb.contracts.accountingEngine.surplusAuctionAmountToSell(true), |
| 112 | + geb.contracts.safeEngine.coinBalance( |
| 113 | + geb.contracts.accountingEngine.address, |
| 114 | + true |
| 115 | + ), |
| 116 | + ]).then((res) => { |
| 117 | + setSurplusAmountToSell(parseRad(res[0])) |
| 118 | + setAccountingEngineSurplus(parseRad(res[1])) |
| 119 | + }) |
| 120 | + }, [account, library]) |
| 121 | + |
| 122 | + const allowStartSurplusAuction = useMemo(() => { |
| 123 | + if (!surplusAmountToSell || !accountingEngineSurplus) return false |
| 124 | + const surplusBuffer = 500_000 |
| 125 | + const surplusAmountToSellWithBuffer = BigNumber.from( |
| 126 | + toFixedString( |
| 127 | + String(Number(surplusAmountToSell) + surplusBuffer), |
| 128 | + 'RAD' |
| 129 | + ) |
| 130 | + ) |
| 131 | + const accountingEngineSurplusBN = BigNumber.from( |
| 132 | + toFixedString(accountingEngineSurplus, 'RAD') |
| 133 | + ) |
| 134 | + return surplusAmountToSellWithBuffer.lte(accountingEngineSurplusBN) |
| 135 | + }, [surplusAmountToSell, accountingEngineSurplus]) |
| 136 | + |
| 137 | + const deltaToStartSurplusAuction = useMemo(() => { |
| 138 | + if (!surplusAmountToSell || !accountingEngineSurplus) return 0 |
| 139 | + const surplusBuffer = 500_000 |
| 140 | + const surplusAmountToSellWithBuffer = |
| 141 | + Number(surplusAmountToSell) + surplusBuffer |
| 142 | + const accountingEngineSurplusN = Number(accountingEngineSurplus) |
| 143 | + return surplusAmountToSellWithBuffer - accountingEngineSurplusN |
| 144 | + }, [surplusAmountToSell, accountingEngineSurplus]) |
| 145 | + |
| 146 | + const startSurplusAcution = async function () { |
| 147 | + if (!library || !account) throw new Error('No library or account') |
| 148 | + const signer = library.getSigner(account) |
| 149 | + const geb = new Geb(ETH_NETWORK, signer.provider) |
| 150 | + |
| 151 | + const txData = geb.contracts.accountingEngine.auctionSurplus() |
| 152 | + |
| 153 | + if (!txData) throw new Error('No transaction request!') |
| 154 | + const tx = await handlePreTxGasEstimate(signer, txData) |
| 155 | + const txResponse = await signer.sendTransaction(tx) |
| 156 | + if (txResponse) { |
| 157 | + const { hash, chainId } = txResponse |
| 158 | + transactionsActions.addTransaction({ |
| 159 | + chainId, |
| 160 | + hash, |
| 161 | + from: txResponse.from, |
| 162 | + summary: 'Starting surplus auction', |
| 163 | + addedTime: new Date().getTime(), |
| 164 | + originalTx: txResponse, |
| 165 | + }) |
| 166 | + popupsActions.setIsWaitingModalOpen(true) |
| 167 | + popupsActions.setWaitingPayload({ |
| 168 | + title: 'Transaction Submitted', |
| 169 | + hash: txResponse.hash, |
| 170 | + status: 'success', |
| 171 | + }) |
| 172 | + await txResponse.wait() |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + return { |
| 177 | + startSurplusAcution, |
| 178 | + surplusAmountToSell, |
| 179 | + accountingEngineSurplus, |
| 180 | + allowStartSurplusAuction, |
| 181 | + deltaToStartSurplusAuction, |
| 182 | + } |
| 183 | +} |
0 commit comments