Skip to content

Commit

Permalink
Merge pull request #500 from interlay/rui/vault-dashboard-null-vault
Browse files Browse the repository at this point in the history
fix(VaultDashboard): vault remaining capacity calculation
  • Loading branch information
tomjeatt authored Sep 14, 2022
2 parents 8181795 + b05ad10 commit d247d1f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/pages/Vaults/Vault/VaultDashboard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ const VaultDashboard = (): JSX.Element => {
<ProgressCircle
aria-label='BTC remaining capacity'
diameter='65'
value={(1 - Number(vault.remainingCapacity.percentage)) * 100}
value={(1 - Number(vault.remainingCapacity.ratio)) * 100}
/>
);

Expand All @@ -66,7 +66,7 @@ const VaultDashboard = (): JSX.Element => {
{
title: 'Remaining kBTC capacity',
label: vault.remainingCapacity.amount.toBig().toString(),
sublabel: formatPercentage(vault.remainingCapacity.percentage, {
sublabel: formatPercentage(vault.remainingCapacity.ratio, {
maximumFractionDigits: 2,
minimumFractionDigits: 2
}),
Expand Down
21 changes: 14 additions & 7 deletions src/utils/hooks/api/vaults/get-vault-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,22 @@ interface VaultData {
secureThreshold: Big;
remainingCapacity: {
amount: MonetaryAmount<CollateralCurrencyExt>;
percentage: number;
ratio: number;
};
}

const getRemainingCapacity = (issuableTokens: Big, vaultExt: VaultExt): number => {
if (!issuableTokens.gt(0)) return 0;

const backedTokens = vaultExt.getBackedTokens().toBig();

if (!backedTokens.gt(0)) return 1;

const totalTokens = issuableTokens.add(backedTokens);

return issuableTokens.div(totalTokens).toNumber();
};

const getVaultData = async (vault: VaultExt, accountId: AccountId, prices: Prices | undefined): Promise<VaultData> => {
const collateralTokenIdLiteral = vault.backingCollateral.currency.ticker as CollateralIdLiteral;
const collateralTokenPrice = getTokenPrice(prices, collateralTokenIdLiteral);
Expand Down Expand Up @@ -144,11 +156,6 @@ const getVaultData = async (vault: VaultExt, accountId: AccountId, prices: Price

const pendingRequests = issuesCount.data.issuesConnection.totalCount + redeemsCount.data.redeemsConnection.totalCount;

// Calculate remaning capacity
const backedTokens = vaultExt.getBackedTokens();
const divisor = issuableTokens.add(backedTokens).toBig();
const remainingCapacity = issuableTokens.div(divisor);

return {
apy,
collateralization,
Expand Down Expand Up @@ -184,7 +191,7 @@ const getVaultData = async (vault: VaultExt, accountId: AccountId, prices: Price
secureThreshold,
remainingCapacity: {
amount: issuableTokens,
percentage: remainingCapacity.toBig().toNumber()
ratio: getRemainingCapacity(issuableTokens.toBig(), vaultExt)
}
};
};
Expand Down

2 comments on commit d247d1f

@vercel
Copy link

@vercel vercel bot commented on d247d1f Sep 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vercel
Copy link

@vercel vercel bot commented on d247d1f Sep 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.