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

Add collateral you redeem #428

Closed
wants to merge 25 commits into from
Closed
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
9f0413b
Add collateral you redeem
May 5, 2022
24a8bff
Update dependency logic
May 6, 2022
c122959
Merge branch 'main' into feat/partical-close-target-eth
deepthinker250 May 6, 2022
8441755
chore: remove console log
May 6, 2022
cad2b3b
Merge branch 'feat/partical-close-target-eth' of https://github.com/o…
May 6, 2022
1cecd60
Merge branch 'main' into feat/partical-close-target-eth
deepthinker250 May 6, 2022
8fc4b85
Merge branch 'main' of https://github.com/opynfinance/squeeth-monorep…
May 9, 2022
755eb95
Remove unused hooks
May 9, 2022
5bc3c1d
Add logic for liq price
May 9, 2022
0df4165
Merge branch 'feat/partical-close-target-eth' of https://github.com/o…
May 9, 2022
64e5675
Merge branch 'main' into feat/partical-close-target-eth
deepthinker250 May 9, 2022
b6e54fb
Merge branch 'main' of https://github.com/opynfinance/squeeth-monorep…
May 10, 2022
1f1de08
Update new liquidation price calc
May 10, 2022
93b11b7
Remvoe console log
May 10, 2022
5006b39
Remove unused import
May 10, 2022
1c427b2
Merge branch 'feat/partical-close-target-eth' of https://github.com/o…
May 10, 2022
28c6133
Merge branch 'main' of https://github.com/opynfinance/squeeth-monorep…
May 10, 2022
b4b33c0
Merge branch 'main' into feat/partical-close-target-eth
deepthinker250 May 11, 2022
331564f
Merge branch 'main' of https://github.com/opynfinance/squeeth-monorep…
May 13, 2022
8ebf706
Merge branch 'feat/partical-close-target-eth' of https://github.com/o…
May 13, 2022
205da1f
Set max allowed to slider to ensure eth being greater than 0
May 13, 2022
a37dc8b
Merge branch 'main' into feat/partical-close-target-eth
deepthinker250 May 13, 2022
b783ed0
Merge branch 'main' of https://github.com/opynfinance/squeeth-monorep…
May 23, 2022
f460556
Update UI and logic for partial short close
May 23, 2022
3bdd18d
Merge branch 'main' into feat/partical-close-target-eth
deepthinker250 May 23, 2022
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
153 changes: 130 additions & 23 deletions packages/frontend/src/components/Trade/Short/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import ArrowRightAltIcon from '@material-ui/icons/ArrowRightAlt'
import InfoOutlinedIcon from '@material-ui/icons/InfoOutlined'
import BigNumber from 'bignumber.js'
import React, { memo, useState } from 'react'
import React, { memo, useEffect, useState } from 'react'

import { CloseType, Tooltips, Links } from '@constants/enums'
import useShortHelper from '@hooks/contracts/useShortHelper'
Expand All @@ -34,8 +34,13 @@ import { useETHPrice } from '@hooks/useETHPrice'
import { collatRatioAtom } from 'src/state/ethPriceCharts/atoms'
import { useResetAtom, useUpdateAtom } from 'jotai/utils'
import { useGetBuyQuote, useGetSellQuote, useGetWSqueethPositionValue } from 'src/state/squeethPool/hooks'
import { useGetDebtAmount, useGetShortAmountFromDebt, useUpdateOperator } from 'src/state/controller/hooks'
import { useComputeSwaps, useFirstValidVault, useLPPositionsQuery, useVaultQuery } from 'src/state/positions/hooks'
import {
useGetCollatRatioAndLiqPrice,
useGetDebtAmount,
useGetShortAmountFromDebt,
useUpdateOperator,
} from 'src/state/controller/hooks'
import { useComputeSwaps, useFirstValidVault, useLPPositionsQuery } from 'src/state/positions/hooks'
import {
ethTradeAmountAtom,
quoteAtom,
Expand Down Expand Up @@ -658,8 +663,10 @@ const CloseShort: React.FC<SellType> = ({ open }) => {
const [buyLoading, setBuyLoading] = useState(false)
const [withdrawCollat, setWithdrawCollat] = useState(new BigNumber(0))
const [neededCollat, setNeededCollat] = useState(new BigNumber(0))
const [debtAmount, setDebtAmount] = useState(BIG_ZERO)
const [closeType, setCloseType] = useState(CloseType.FULL)
const [isTxFirstStep, setIsTxFirstStep] = useAtom(isTransactionFirstStepAtom)
const getCollatRatioAndLiqPrice = useGetCollatRatioAndLiqPrice()

const classes = useStyles()
const {
Expand All @@ -671,6 +678,8 @@ const CloseShort: React.FC<SellType> = ({ open }) => {
resetTxCancelled,
} = useTransactionStatus()

const [liqPrice, setLiqPrice] = useState(BIG_ZERO)

const { closeShort } = useShortHelper()
const getWSqueethPositionValue = useGetWSqueethPositionValue()
const { shortHelper } = useAtomValue(addressesAtom)
Expand Down Expand Up @@ -712,6 +721,16 @@ const CloseShort: React.FC<SellType> = ({ open }) => {
}
}, [vault, vault?.shortAmount])

useAppEffect(() => {
getCollatRatioAndLiqPrice(
neededCollat,
finalShortAmount.minus(amount),
vault?.NFTCollateralId ? Number(vault.NFTCollateralId) : undefined,
).then(({ liquidationPrice }) => {
setLiqPrice(liquidationPrice)
})
}, [amount, neededCollat, finalShortAmount, vault?.NFTCollateralId, getCollatRatioAndLiqPrice])

// useAppEffect(() => {
// if (shortVaults[firstValidVault]?.shortAmount && shortVaults[firstValidVault]?.shortAmount.lt(amount)) {
// console.log('looking for something weird')
Expand All @@ -723,29 +742,85 @@ const CloseShort: React.FC<SellType> = ({ open }) => {
if (!vault) return

setIsVaultApproved(vault?.operator?.toLowerCase() === shortHelper?.toLowerCase())
}, [vaultId, shortHelper, vault])
}, [shortHelper, vault])

useAppEffect(() => {
if (amount.isEqualTo(0)) {
setExistingCollat(new BigNumber(0))
setNeededCollat(new BigNumber(0))
setWithdrawCollat(new BigNumber(0))
}
}, [amount])

useAppEffect(() => {
if (vault && !amount.isEqualTo(0)) {
const _collat: BigNumber = vault?.collateralAmount ?? new BigNumber(0)
setExistingCollat(_collat)
const restOfShort = new BigNumber(vault?.shortAmount ?? new BigNumber(0)).minus(amount)
if (amount.isEqualTo(0)) {
setExistingCollat(BIG_ZERO)
} else {
setExistingCollat(vault?.collateralAmount ?? BIG_ZERO)
}
}, [amount, vault?.collateralAmount])

getDebtAmount(new BigNumber(restOfShort)).then((debt) => {
const _neededCollat = debt.times(collatPercent / 100)
setNeededCollat(_neededCollat)
setWithdrawCollat(_neededCollat.gt(0) ? _collat.minus(neededCollat) : _collat)
})
useAppEffect(() => {
const restOfShort = new BigNumber(vault?.shortAmount ?? new BigNumber(0)).minus(amount)

getDebtAmount(restOfShort).then((debt) => {
setDebtAmount(debt)
})
}, [vault?.shortAmount, amount, getDebtAmount])

const handleWithdrawCollatChange = (newWithdrawCollat: BigNumber) => {
setWithdrawCollat(newWithdrawCollat)

if (amount.isZero()) {
return
}
}, [amount, collatPercent, getDebtAmount, neededCollat, vault])

const newNeededCollat = existingCollat.minus(newWithdrawCollat)
setNeededCollat(newNeededCollat)

setCollatPercent(Math.max(0, newNeededCollat.div(debtAmount).toNumber() * 100))
}

const handleCollatPercentChange = useAppCallback(
(newCollatPercent: number) => {
setCollatPercent(newCollatPercent)

if (amount.isZero()) {
return
}

const newNeededCollat = debtAmount.times(newCollatPercent / 100)
setNeededCollat(newNeededCollat)

setWithdrawCollat(newNeededCollat.gt(0) ? existingCollat.minus(newNeededCollat) : existingCollat)
},
[amount, debtAmount, existingCollat],
)

useAppEffect(() => {
handleCollatPercentChange(collatPercent)
}, [debtAmount, collatPercent, handleCollatPercentChange])

// useAppEffect(() => {
// if (shortVaults.length && !amount.isEqualTo(0)) {
// const _collat: BigNumber = vault?.collateralAmount ?? new BigNumber(0)
// setExistingCollat(_collat)
// const restOfShort = new BigNumber(vault?.shortAmount ?? new BigNumber(0)).minus(amount)

// getDebtAmount(new BigNumber(restOfShort)).then((debt) => {
// const _neededCollat = debt.times(collatPercent / 100)
// setNeededCollat(_neededCollat)
// setWithdrawCollat(_neededCollat.gt(0) ? _collat.minus(neededCollat) : _collat)
// })
// }
// }, [
// amount,
// shortVaults?.length,
// collatPercent,
// vault?.collateralAmount,
// vault?.shortAmount,
// getDebtAmount,
// neededCollat,
// ])

useAppEffect(() => {
if (transactionInProgress) {
Expand Down Expand Up @@ -812,6 +887,10 @@ const CloseShort: React.FC<SellType> = ({ open }) => {
}
}, [finalShortAmount, setSqthTradeAmount])

useEffect(() => {
setShortCloseMax()
}, [setShortCloseMax])

// let openError: string | undefined
let closeError: string | undefined
let existingLongError: string | undefined
Expand Down Expand Up @@ -984,7 +1063,7 @@ const CloseShort: React.FC<SellType> = ({ open }) => {
} else {
setSqthTradeAmount('0')
}
setCollatPercent(200)
handleCollatPercentChange(200)
return setCloseType(event.target.value as CloseType)
}}
displayEmpty
Expand All @@ -1004,10 +1083,10 @@ const CloseShort: React.FC<SellType> = ({ open }) => {
<div className={classes.thirdHeading}>
<TextField
size="small"
value={collatPercent}
value={Number(collatPercent.toFixed(2))}
type="number"
style={{ width: 300 }}
onChange={(event) => setCollatPercent(Number(event.target.value))}
onChange={(event) => handleCollatPercentChange(Number(event.target.value))}
id="filled-basic"
label="Collateral Ratio"
variant="outlined"
Expand All @@ -1028,12 +1107,38 @@ const CloseShort: React.FC<SellType> = ({ open }) => {
/>
<CollatRange
className={classes.thirdHeading}
onCollatValueChange={(val) => setCollatPercent(val)}
onCollatValueChange={(val) => handleCollatPercentChange(val)}
collatValue={collatPercent}
id="close-short-collat-ratio"
/>
</div>
)}

<div className={classes.thirdHeading}>
<TextField
size="small"
disabled={closeType === CloseType.FULL}
value={Number(withdrawCollat.toFixed(4))}
type="number"
style={{ width: 300 }}
onChange={(event) => handleWithdrawCollatChange(new BigNumber(event.target.value))}
id="filled-basic"
label="Collateral you redeem"
variant="outlined"
FormHelperTextProps={{ classes: { root: classes.formHelperText } }}
InputProps={{
endAdornment: (
<InputAdornment position="end">
<Typography variant="caption">ETH</Typography>
</InputAdornment>
),
}}
inputProps={{
min: '0',
}}
/>
</div>

<TradeDetails
actionTitle="Spend"
amount={sellCloseQuote.amountIn.toFixed(6)}
Expand Down Expand Up @@ -1069,10 +1174,12 @@ const CloseShort: React.FC<SellType> = ({ open }) => {
/>
<div className={classes.divider}>
<TradeInfoItem
label="Collateral you redeem"
value={withdrawCollat.isPositive() ? withdrawCollat.toFixed(4) : 0}
unit="ETH"
id="close-short-collateral-to-redeem"
label="New Liquidation Price"
value={liqPrice.toFixed(2)}
unit="USDC"
tooltip={`${Tooltips.LiquidationPrice}. ${Tooltips.Twap}`}
priceType="twap"
id="close-short-liquidation-price"
/>
<TradeInfoItem
label="Current Collateral ratio"
Expand Down