Skip to content

Commit

Permalink
Merge pull request #272 from reflexer-labs/saviour-fix-v2
Browse files Browse the repository at this point in the history
Saviour fix v2
  • Loading branch information
mstfash authored Oct 26, 2021
2 parents a92355d + a51a702 commit a880bd4
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 26 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ jobs:
with:
cidv0: ${{ steps.upload.outputs.hash }}

# - name: Update DNS with new IPFS hash
# env:
# CLOUDFLARE_TOKEN: ${{ secrets.CLOUDFLARE_TOKEN }}
# RECORD_DOMAIN: 'reflexer.finance'
# RECORD_NAME: '_dnslink.app'
# CLOUDFLARE_ZONE_ID: ${{ secrets.CLOUDFLARE_ZONE_ID }}
# uses: textileio/cloudflare-update-dnslink@0fe7b7a1ffc865db3a4da9773f0f987447ad5848
# with:
# cid: ${{ steps.upload.outputs.hash }}
- name: Update DNS with new IPFS hash
env:
CLOUDFLARE_TOKEN: ${{ secrets.CLOUDFLARE_TOKEN }}
RECORD_DOMAIN: 'reflexer.finance'
RECORD_NAME: '_dnslink.app'
CLOUDFLARE_ZONE_ID: ${{ secrets.CLOUDFLARE_ZONE_ID }}
uses: textileio/cloudflare-update-dnslink@0fe7b7a1ffc865db3a4da9773f0f987447ad5848
with:
cid: ${{ steps.upload.outputs.hash }}

- name: Create GitHub Release
id: create_release
Expand Down
53 changes: 49 additions & 4 deletions src/components/SafeOperations/SafeBody.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useState } from 'react'
import React, { useEffect, useState, useCallback } from 'react'
import { useTranslation } from 'react-i18next'
import { BigNumber } from 'ethers'
import { utils as gebUtils } from 'geb.js'
Expand Down Expand Up @@ -26,6 +26,8 @@ import { DEFAULT_SAFE_STATE, COIN_TICKER } from '../../utils/constants'
import { Info } from 'react-feather'
import ReactTooltip from 'react-tooltip'
import { useIsOwner, useProxyAddress } from '../../hooks/useGeb'
import { useMinSaviourBalance, useSaviourData } from '../../hooks/useSaviour'
import AlertLabel from '../AlertLabel'

export const LIQUIDATION_RATIO = 135 // percent
interface Props {
Expand All @@ -35,6 +37,8 @@ interface Props {
const SafeBody = ({ isChecked }: Props) => {
const { t } = useTranslation()
const proxyAddress = useProxyAddress()
const { getMinSaviourBalance } = useMinSaviourBalance()
const saviourData = useSaviourData()
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [checkUniSwapPool, setCheckUniSwapPool] = useState(isChecked || false)
const [error, setError] = useState('')
Expand Down Expand Up @@ -64,6 +68,10 @@ const SafeBody = ({ isChecked }: Props) => {
globalDebtCeiling,
} = safeState.liquidationData

const raiPrice = singleSafe
? (formatNumber(currentRedemptionPrice, 3) as number)
: 0

const isOwner = useIsOwner(singleSafe?.id as string)
const raiBalance = connectWalletState.raiBalance[NETWORK_ID].toString()

Expand Down Expand Up @@ -160,6 +168,13 @@ const SafeBody = ({ isChecked }: Props) => {
Number(getAvailableRai()) > 0.01
? formatNumber(getAvailableRai(), 2)
: '< 0.01'
} ${
isCreate
? ''
: `≃ $${formatNumber(
String(Number(getAvailableRai()) * raiPrice),
2
)}`
})`
}
if (type === 'repay_withdraw' && isLeft) {
Expand Down Expand Up @@ -487,9 +502,36 @@ const SafeBody = ({ isChecked }: Props) => {
setUniSwapVal(uniSwapPool)
}, [safeData, uniSwapPool])

const returnStatus = useCallback(() => {
if (!saviourData) return 'none'
const minimumBalance = getMinSaviourBalance(
saviourData.saviourRescueRatio,
totalDebt,
totalCollateral
) as number
if (Number(saviourData.saviourBalance) >= minimumBalance) {
return 'Protected'
}
return 'Unprotected'
}, [getMinSaviourBalance, saviourData, totalCollateral, totalDebt])

return (
<>
<Body>
{saviourData && saviourData.hasSaviour ? (
<SaviourLabel>
<AlertLabel
text={`Saviour Status: ${returnStatus()}`}
type={
returnStatus() === 'Protected'
? 'success'
: returnStatus() === 'none'
? 'dimmed'
: 'danger'
}
/>
</SaviourLabel>
) : null}
<DoubleInput
className={type === 'repay_withdraw' ? 'reverse' : ''}
>
Expand Down Expand Up @@ -690,7 +732,6 @@ const DoubleInput = styled.div`
flex: 0 0 50%;
}
}
&.reverse {
> div {
&:first-child {
Expand All @@ -704,7 +745,6 @@ const DoubleInput = styled.div`
}
}
}
${({ theme }) => theme.mediaWidth.upToSmall`
flex-direction: column;
> div {
Expand All @@ -715,7 +755,6 @@ const DoubleInput = styled.div`
margin-top: 20px;
}
}
&.reverse {
> div {
flex: 0 0 100%;
Expand Down Expand Up @@ -834,3 +873,9 @@ const Note = styled.div`
font-size: ${(props) => props.theme.font.extraSmall};
margin-top: 5px;
`

const SaviourLabel = styled.div`
position: absolute;
top: -53px;
right: 20px;
`
33 changes: 20 additions & 13 deletions src/hooks/useSaviour.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,13 @@ export function useMinSaviourBalance() {
// liquidationPrice = -----------------------------------------------
// collateral

const liquidationPrice = !generatedDebt.isZero()
? redemptionPrice
.mul(generatedDebt.mul(WAD_COMPLEMENT))
.mul(accumulatedRate)
.mul(LIQUIDATION_POINT)
.div(HUNDRED)
.div(lockedCollateral.mul(WAD_COMPLEMENT))
.div(RAY)
: BigNumber.from('0')
const liquidationPrice = redemptionPrice
.mul(generatedDebt.mul(WAD_COMPLEMENT))
.mul(accumulatedRate)
.mul(LIQUIDATION_POINT)
.div(HUNDRED)
.div(lockedCollateral.mul(WAD_COMPLEMENT))
.div(RAY)

// The calculation below refers to the formula described at:
// https://docs.reflexer.finance/liquidation-protection/uni-v2-rai-eth-savior-math
Expand All @@ -310,10 +308,19 @@ export function useMinSaviourBalance() {
const pVar = redemptionPrice.mul(RAY).div(liquidationPrice)

// Leave out sqrt(p) from the minimum bal equation because BignNumber doesn't do square root
const minSaviorBalanceRayWithoutSqrtP = lockedCollateral
.mul(WAD_COMPLEMENT)
.sub(generatedDebt.mul(WAD_COMPLEMENT).mul(jVar).div(RAY))
.div(jVar.add(pVar))
const minSaviorBalanceRayWithoutSqrtP =
!jVar.isZero() && !pVar.isZero()
? lockedCollateral
.mul(WAD_COMPLEMENT)
.sub(
generatedDebt
.mul(WAD_COMPLEMENT)
.mul(jVar)
.div(RAY)
)
.div(jVar.add(pVar))
: BigNumber.from('0')

// TODO: Find a better way doing square root if there is
const minSaviorBalanceNumber =
Math.sqrt(Number(pVar.toString()) / 1e27) *
Expand Down

0 comments on commit a880bd4

Please sign in to comment.