Skip to content

Commit

Permalink
fix position total value display (#6185)
Browse files Browse the repository at this point in the history
* fix

* totalLocked
  • Loading branch information
greg-schrammel authored Oct 22, 2024
1 parent b5f08ea commit 646902f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/hooks/useWalletBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { useAddysSummary } from '@/resources/summary/summary';
import { useQueries } from '@tanstack/react-query';
import { fetchPositions, positionsQueryKey } from '@/resources/defi/PositionsQuery';
import { RainbowPositions } from '@/resources/defi/types';
import { add, convertAmountToNativeDisplay } from '@/helpers/utilities';
import { add, convertAmountToNativeDisplay, subtract } from '@/helpers/utilities';
import { queryClient } from '@/react-query';

const QUERY_CONFIG = {
Expand Down Expand Up @@ -65,7 +65,7 @@ const useWalletBalances = (wallets: AllRainbowWallets): WalletBalanceResult => {
const assetBalance = summaryData?.data?.addresses?.[lowerCaseAddress]?.summary?.asset_value?.toString() || '0';

const positionData = queryClient.getQueryData<RainbowPositions | undefined>(positionsQueryKey({ address, currency: nativeCurrency }));
const positionsBalance = positionData?.totals?.total?.amount || '0';
const positionsBalance = positionData ? subtract(positionData.totals.total.amount, positionData.totals.totalLocked) : '0';
const totalAccountBalance = add(assetBalance, positionsBalance);

result[lowerCaseAddress] = {
Expand Down
2 changes: 2 additions & 0 deletions src/resources/defi/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export type PositionDapp = {

export type PositionsTotals = {
totals: NativeDisplay;
totalLocked: string;
borrows: NativeDisplay;
claimables: NativeDisplay;
deposits: NativeDisplay;
Expand Down Expand Up @@ -112,6 +113,7 @@ export type RainbowPosition = {
export type RainbowPositions = {
totals: {
total: NativeDisplay;
totalLocked: string;
borrows: NativeDisplay;
claimables: NativeDisplay;
deposits: NativeDisplay;
Expand Down
35 changes: 23 additions & 12 deletions src/resources/defi/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import { ethereumUtils } from '@/utils';
import { chainsIdByName } from '@/chains';

export const parsePosition = (position: Position, currency: NativeCurrencyKey): RainbowPosition => {
let totalLocked = '0';

let totalDeposits = '0';
const parsedDeposits = position.deposits?.map((deposit): RainbowDeposit => {
return {
Expand All @@ -28,9 +30,11 @@ export const parsePosition = (position: Position, currency: NativeCurrencyKey):
underlying.asset.price?.value!,
currency
);
if (!deposit.omit_from_total) {
totalDeposits = add(totalDeposits, nativeDisplay.amount);

if (deposit.omit_from_total) {
totalLocked = add(totalLocked, nativeDisplay.amount);
}
totalDeposits = add(totalDeposits, nativeDisplay.amount);

return {
...underlying,
Expand All @@ -52,10 +56,13 @@ export const parsePosition = (position: Position, currency: NativeCurrencyKey):
underlying.asset.price?.value!,
currency
);
if (!borrow.omit_from_total) {
totalBorrows = add(totalBorrows, nativeDisplay.amount);

if (borrow.omit_from_total) {
totalLocked = subtract(totalLocked, nativeDisplay.amount);
}

totalBorrows = add(totalBorrows, nativeDisplay.amount);

return {
...underlying,
native: nativeDisplay,
Expand All @@ -67,9 +74,12 @@ export const parsePosition = (position: Position, currency: NativeCurrencyKey):
let totalClaimables = '0';
const parsedClaimables = position.claimables?.map((claim: Claimable): RainbowClaimable => {
const nativeDisplay = convertRawAmountToNativeDisplay(claim.quantity, claim.asset.decimals, claim.asset.price?.value!, currency);
if (!claim.omit_from_total) {
totalClaimables = add(totalClaimables, nativeDisplay.amount);

if (claim.omit_from_total) {
totalLocked = add(totalLocked, nativeDisplay.amount);
}
totalClaimables = add(totalClaimables, nativeDisplay.amount);

return {
asset: claim.asset,
quantity: claim.quantity,
Expand All @@ -82,6 +92,7 @@ export const parsePosition = (position: Position, currency: NativeCurrencyKey):
amount: subtract(add(totalDeposits, totalClaimables), totalBorrows),
display: convertAmountToNativeDisplay(subtract(add(totalDeposits, totalClaimables), totalBorrows), currency),
},
totalLocked,
borrows: {
amount: totalBorrows,
display: convertAmountToNativeDisplay(totalBorrows, currency),
Expand Down Expand Up @@ -142,6 +153,7 @@ export const parsePositions = (data: AddysPositionsResponse, currency: NativeCur

const positionsTotals = parsedPositions.reduce(
(acc, position) => ({
totalLocked: add(acc.totalLocked, position.totals.totalLocked),
borrows: {
amount: add(acc.borrows.amount, position.totals.borrows.amount),
display: convertAmountToNativeDisplay(add(acc.borrows.amount, position.totals.borrows.amount), currency),
Expand All @@ -156,6 +168,7 @@ export const parsePositions = (data: AddysPositionsResponse, currency: NativeCur
},
}),
{
totalLocked: '0',
borrows: { amount: '0', display: '0' },
claimables: { amount: '0', display: '0' },
deposits: { amount: '0', display: '0' },
Expand All @@ -164,14 +177,12 @@ export const parsePositions = (data: AddysPositionsResponse, currency: NativeCur

const totalAmount = subtract(add(positionsTotals.deposits.amount, positionsTotals.claimables.amount), positionsTotals.borrows.amount);

const totalDisplay = {
amount: totalAmount,
display: convertAmountToNativeDisplay(totalAmount, currency),
};

return {
totals: {
total: totalDisplay,
total: {
amount: totalAmount,
display: convertAmountToNativeDisplay(totalAmount, currency),
},
...positionsTotals,
},
positions: parsedPositions,
Expand Down

0 comments on commit 646902f

Please sign in to comment.