-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ui-ux): allow dusd loops in vaults (#4114)
* feat(ui-ux): allow dusd loops in vaults * feat(ui-ux): keep dusd loan requirement of 50% dfi * chore(translations): update error message texts and add translation placeholder * feat(ui-ux): allow removal of dfi asset if vault will be left with only dusd * feat(ui-ux): disable load for non-dusd token when 100% col is dusd * feat(ui-ux): use symbol instead of displaySymbol * feat(ui-ux): use symbol instead of displaySymbol * chore(translations): update translation for required collateral error message * feat(ui-ux): feature-gate dusd-loop based on wallet-website api flags * chore(translations): update insufficient dusd error message * Keep previous error message when dusd loop is disabled * update docker-compose * update vault details collateral info message
- Loading branch information
1 parent
269c2e2
commit 9b52cb0
Showing
14 changed files
with
259 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 0 additions & 61 deletions
61
...pp/app/screens/AppNavigator/screens/Loans/hooks/DFIRequirementForDusdLoanAndCollateral.ts
This file was deleted.
Oops, something went wrong.
80 changes: 80 additions & 0 deletions
80
mobile-app/app/screens/AppNavigator/screens/Loans/hooks/ValidateLoanAndCollateral.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { | ||
CollateralToken, | ||
LoanToken, | ||
LoanVaultTokenAmount, | ||
} from "@defichain/whale-api-client/dist/api/loan"; | ||
import { RootState } from "@store"; | ||
import BigNumber from "bignumber.js"; | ||
import { useSelector } from "react-redux"; | ||
import { useFeatureFlagContext } from "@contexts/FeatureFlagContext"; | ||
import { getActivePrice } from "../../Auctions/helpers/ActivePrice"; | ||
|
||
interface useValidateLoanAndCollateralProps { | ||
collateralAmounts: LoanVaultTokenAmount[]; | ||
loanAmounts: LoanVaultTokenAmount[]; | ||
collateralValue: BigNumber; | ||
loanValue: BigNumber; | ||
loanToken: LoanToken; | ||
minColRatio: string; | ||
} | ||
|
||
/** | ||
* To check if current vault has either at least 50% of DFI or 100% of DUSD as total collateral when taking DUSD loan | ||
* | ||
* Modified formula from [DeFiCh/ain] to include new loan amount to be taken during `Borrow` flow | ||
* Source: https://github.com/DeFiCh/ain/blob/07ba855f73c7fdfb0b2f10fc3b31fe73c17b1630/src/dfi/consensus/txvisitor.cpp#L270 | ||
* | ||
* Note: DUSD loops in vaults are now allowed - https://github.com/DeFiCh/ain/pull/1971 | ||
* | ||
* @returns | ||
*/ | ||
export function useValidateLoanAndCollateral( | ||
props: useValidateLoanAndCollateralProps, | ||
) { | ||
const { isFeatureAvailable } = useFeatureFlagContext(); | ||
const collateralTokens: CollateralToken[] = useSelector( | ||
(state: RootState) => state.loans.collateralTokens, | ||
); | ||
const isTakingDUSDLoan = props.loanToken.token.symbol === "DUSD"; | ||
|
||
const dfiCollateralToken = collateralTokens.find( | ||
(col) => col.token.symbol === "DFI", | ||
); | ||
|
||
const dfiActivePrice = getActivePrice( | ||
"DFI", | ||
dfiCollateralToken?.activePrice, | ||
dfiCollateralToken?.factor, | ||
"ACTIVE", | ||
"COLLATERAL", | ||
); | ||
|
||
const dfiCollateralValue = new BigNumber(dfiActivePrice).multipliedBy( | ||
props.collateralAmounts.find((col) => col.symbol === "DFI")?.amount ?? 0, | ||
); | ||
const totalRequiredCollateral = new BigNumber(props.loanValue) | ||
.multipliedBy(props.minColRatio) | ||
.dividedBy(100); | ||
|
||
const isDFIGreaterThanHalfOfRequiredCollateral = | ||
dfiCollateralValue.isGreaterThanOrEqualTo( | ||
totalRequiredCollateral.dividedBy(2), | ||
); | ||
|
||
// check if all collaterals is of DUSD token | ||
const isDUSD100PercentOfCollateral = props.collateralAmounts.every( | ||
(col) => col.symbol === "DUSD", | ||
); | ||
|
||
const isNonDUSDLoanAllowed = | ||
!isTakingDUSDLoan && isDFIGreaterThanHalfOfRequiredCollateral; | ||
|
||
const isDUSDLoanAllowed = | ||
isTakingDUSDLoan && | ||
(isDFIGreaterThanHalfOfRequiredCollateral || | ||
(isFeatureAvailable("loop_dusd") && isDUSD100PercentOfCollateral)); | ||
|
||
return { | ||
isLoanAllowed: isNonDUSDLoanAllowed || isDUSDLoanAllowed, | ||
}; | ||
} |
Oops, something went wrong.