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

Peter/feat loans q token handle edge cases #1449

Merged
merged 26 commits into from
Jul 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
440872f
chore: update monetary to latest 0.7.3
peterslany May 19, 2023
0ef748d
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany May 22, 2023
41fc8c9
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany May 23, 2023
26de655
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany May 24, 2023
6ba14a5
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jun 1, 2023
e006f4f
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jun 5, 2023
8362aa6
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jun 8, 2023
d122720
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jun 8, 2023
3b60f16
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jun 12, 2023
e646740
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jun 12, 2023
12e8338
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jun 16, 2023
5e7ea8f
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jun 19, 2023
ecc8f4e
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jun 27, 2023
41b1b16
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jun 27, 2023
991e4f6
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jun 28, 2023
535f2c7
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jun 29, 2023
0f5114b
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jul 3, 2023
3135b6f
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jul 4, 2023
add2aa0
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jul 5, 2023
edfa106
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jul 6, 2023
ab79a7e
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jul 6, 2023
00bb86a
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jul 7, 2023
f64786d
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jul 10, 2023
b0d7786
Merge branch 'master' of github.com:interlay/interbtc-ui
peterslany Jul 13, 2023
c439cf6
feat(loans): handle lend position when qToken is used as vault collat…
peterslany Jul 13, 2023
4ec136e
chore: update lib
peterslany Jul 14, 2023
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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"@headlessui/react": "^1.1.1",
"@heroicons/react": "^2.0.18",
"@interlay/bridge": "^0.3.13",
"@interlay/interbtc-api": "2.3.6",
"@interlay/interbtc-api": "2.3.7",
"@interlay/monetary-js": "0.7.3",
"@polkadot/api": "9.14.2",
"@polkadot/extension-dapp": "0.44.1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { CollateralPosition, LoanAsset } from '@interlay/interbtc-api';
import { CollateralPosition, CurrencyExt, LoanAsset } from '@interlay/interbtc-api';
import { MonetaryAmount } from '@interlay/monetary-js';
import { useEffect, useRef } from 'react';
import { TFunction, useTranslation } from 'react-i18next';

Expand All @@ -19,7 +20,7 @@ import { useGetLTV } from '../../hooks/use-get-ltv';
import { BorrowLimit } from '../BorrowLimit';
import { StyledDescription } from './CollateralModal.style';

type CollateralModalVariant = 'enable' | 'disable' | 'disable-error';
type CollateralModalVariant = 'enable' | 'disable' | 'disable-error' | 'disable-vault-collateral';

const getContentMap = (t: TFunction, variant: CollateralModalVariant, asset: LoanAsset) =>
({
Expand All @@ -40,10 +41,21 @@ const getContentMap = (t: TFunction, variant: CollateralModalVariant, asset: Loa
description:
'This asset is required to support your borrowed assets. Either repay borrowed assets, or supply another asset as collateral.',
buttonLabel: `Dismiss`
},
'disable-vault-collateral': {
title: 'Already used as vault collateral',
description:
'This asset is already used as vault collateral and therefore can not be used as collateral for lending.',
buttonLabel: `Dismiss`
}
}[variant]);

const getModalVariant = (isCollateralActive: boolean, ltvStatus?: Status): CollateralModalVariant => {
const getModalVariant = (
isCollateralActive: boolean,
ltvStatus: Status | undefined,
vaultCollateralAmount: MonetaryAmount<CurrencyExt>
): CollateralModalVariant => {
if (!vaultCollateralAmount.isZero()) return 'disable-vault-collateral';
if (!isCollateralActive) return 'enable';
// User is trying switching off collateral
if (!ltvStatus || ltvStatus !== 'success') return 'disable-error';
Expand Down Expand Up @@ -73,11 +85,11 @@ const CollateralModal = ({ asset, position, onClose, isOpen, ...props }: Collate
onSuccess: refetch
});

const { isCollateral: isCollateralActive, amount: lendPositionAmount } = position;
const { isCollateral: isCollateralActive, amount: lendPositionAmount, vaultCollateralAmount } = position;

const loanAction = isCollateralActive ? 'withdraw' : 'lend';
const currentLTV = getLTV({ type: loanAction, amount: lendPositionAmount });
const variant = getModalVariant(isCollateralActive, currentLTV?.status);
const variant = getModalVariant(isCollateralActive, currentLTV?.status, vaultCollateralAmount);

const handleSubmit = () => {
if (variant === 'enable') {
Expand Down Expand Up @@ -107,7 +119,7 @@ const CollateralModal = ({ asset, position, onClose, isOpen, ...props }: Collate
// Doing this call on mount so that the form becomes dirty
// TODO: find better approach
useEffect(() => {
if (variant === 'disable-error' || !isOpen) return;
if (variant === 'disable-error' || variant === 'disable-vault-collateral' || !isOpen) return;

form.setFieldValue(LOAN_TOGGLE_COLLATERAL_FEE_TOKEN_FIELD, transaction.fee.defaultCurrency.ticker, true);
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand All @@ -128,11 +140,13 @@ const CollateralModal = ({ asset, position, onClose, isOpen, ...props }: Collate
<ModalBody>
<Flex direction='column' gap='spacing8'>
<StyledDescription color='tertiary'>{content.description}</StyledDescription>
<BorrowLimit loanAction={loanAction} asset={asset} actionAmount={lendPositionAmount} prices={prices} />
{variant !== 'disable-vault-collateral' && (
<BorrowLimit loanAction={loanAction} asset={asset} actionAmount={lendPositionAmount} prices={prices} />
)}
</Flex>
</ModalBody>
<ModalFooter>
{variant === 'disable-error' ? (
{variant === 'disable-error' || variant === 'disable-vault-collateral' ? (
<CTA size='large' onPress={onClose}>
{content.buttonLabel}
</CTA>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ const getMaxWithdrawableAmountByBorrowLimit = (
position: CollateralPosition,
lendingStats: LendingStats
): MonetaryAmount<CurrencyExt> => {
const { amount, isCollateral } = position;
const { amount, isCollateral, vaultCollateralAmount } = position;
const { collateralThreshold, exchangeRate, currency } = asset;
const { borrowLimitBtc } = lendingStats;

if (!isCollateral) {
return amount;
// MEMO: If the position is not used as loan collateral it can be used
// as vault collateral.
return amount.sub(vaultCollateralAmount);
}

const positionAmountBtc = exchangeRate.toBase(amount);
Expand Down
6 changes: 4 additions & 2 deletions src/test/mocks/@interlay/interbtc-api/parachain/loans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ const DEFAULT_POSITIONS = {
currency: WRAPPED_TOKEN,
amount: DEFAULT_IBTC.MONETARY.MEDIUM,
isCollateral: true,
earnedInterest: DEFAULT_IBTC.MONETARY.VERY_SMALL
earnedInterest: DEFAULT_IBTC.MONETARY.VERY_SMALL,
vaultCollateralAmount: DEFAULT_IBTC.MONETARY.EMPTY
} as CollateralPosition,
INTR: {
currency: GOVERNANCE_TOKEN,
amount: DEFAULT_INTR.MONETARY.MEDIUM,
isCollateral: true,
earnedInterest: DEFAULT_INTR.MONETARY.SMALL
earnedInterest: DEFAULT_INTR.MONETARY.SMALL,
vaultCollateralAmount: DEFAULT_INTR.MONETARY.EMPTY
} as CollateralPosition
},
BORROW: {
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2125,10 +2125,10 @@
dependencies:
axios "^0.21.1"

"@interlay/interbtc-api@2.3.6":
version "2.3.6"
resolved "https://registry.yarnpkg.com/@interlay/interbtc-api/-/interbtc-api-2.3.6.tgz#2db11e94b495139da87d1052cdde39a7a8dd25ab"
integrity sha512-bfrDE7QqmG25NLeBpVvZb4tKp1Zr2DqG+lWZnU5L27Z0V/zzPdWDbBcBcG1uKEDy4lkxX8K9jYculy/UA/rW1g==
"@interlay/interbtc-api@2.3.7":
version "2.3.7"
resolved "https://registry.yarnpkg.com/@interlay/interbtc-api/-/interbtc-api-2.3.7.tgz#26d4fa574531fe9eea3f0d49364f7476da9713cf"
integrity sha512-w9xPaUa3wTTXOb83pHbSqlE3E8V2iA4WE4IlOu23Zqth4hnG0h819WynlfzUsAPGug6RkZkHWIKnu+85V95g5A==
dependencies:
"@interlay/esplora-btc-api" "0.4.0"
"@interlay/interbtc-types" "1.12.0"
Expand Down