Skip to content

Commit

Permalink
Peter/loans fix subsidy rewards (#1276)
Browse files Browse the repository at this point in the history
* chore: update monetary to latest 0.7.3

* fix(loans): display correct subsidy rewards accrued amount and APY

* chore: console log cleanup

* chore: replace GOVERNANCE_TOKEN_SYMBOL with GOVERNANCE_TOKEN.ticker
  • Loading branch information
peterslany authored Jun 9, 2023
1 parent 36c41c6 commit 4bf3e13
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 30 deletions.
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.11",
"@interlay/interbtc-api": "2.3.0",
"@interlay/interbtc-api": "2.3.1",
"@interlay/monetary-js": "0.7.3",
"@polkadot/api": "9.14.2",
"@polkadot/extension-dapp": "0.44.1",
Expand Down
9 changes: 3 additions & 6 deletions src/components/LoanApyTooltip/LoanApyTooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,19 @@ import { TooltipProps } from '@reach/tooltip';
import Big from 'big.js';

import { Dd, Dl, DlGroup } from '@/component-library';
import { GOVERNANCE_TOKEN } from '@/config/relay-chains';
import { Prices } from '@/utils/hooks/api/use-get-prices';

import { AssetGroup } from './AssetGroup';
import { BreakdownGroup } from './BreakdownGroup';
import { StyledApyTooltipTitle, StyledTooltip } from './LoanApyTooltip.style';
import { RewardsGroup } from './RewardsGroup';

type Props = {
apy: Big;
currency: CurrencyExt;
earnedInterest?: MonetaryAmount<CurrencyExt>;
accumulatedDebt?: MonetaryAmount<CurrencyExt>;
rewardsApy?: Big;
rewards: MonetaryAmount<CurrencyExt> | null;
prices: Prices;
isBorrow: boolean;
};
Expand All @@ -32,20 +31,19 @@ const LoanApyTooltip = ({
earnedInterest,
accumulatedDebt,
rewardsApy,
rewards,
prices,
isBorrow,
...props
}: LoanApyTooltipProps): JSX.Element => {
const showEarnedRewards = !!rewards || !!earnedInterest;
const showEarnedRewards = !!earnedInterest;

const label = (
<Dl direction='column' gap='spacing2'>
<BreakdownGroup
apy={apy}
isBorrow={isBorrow}
rewardsApy={rewardsApy}
rewardsTicker={rewards?.currency.ticker}
rewardsTicker={GOVERNANCE_TOKEN.ticker}
ticker={currency.ticker}
/>
{accumulatedDebt && (
Expand All @@ -64,7 +62,6 @@ const LoanApyTooltip = ({
<Dd>
<Dl direction='column' alignItems='flex-start' gap='spacing0'>
{earnedInterest && <AssetGroup amount={earnedInterest} prices={prices} />}
{!!rewards && <RewardsGroup rewards={rewards} prices={prices} />}
</Dl>
</Dd>
</DlGroup>
Expand Down
3 changes: 0 additions & 3 deletions src/components/LoanPositionsTable/ApyCell.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ type ApyCellProps = {
earnedInterest?: MonetaryAmount<CurrencyExt>;
accumulatedDebt?: MonetaryAmount<CurrencyExt>;
rewardsPerYear: MonetaryAmount<CurrencyExt> | null;
accruedRewards: MonetaryAmount<CurrencyExt> | null;
prices?: Prices;
isBorrow?: boolean;
onClick?: () => void;
Expand All @@ -25,7 +24,6 @@ const ApyCell = ({
apy,
currency,
rewardsPerYear,
accruedRewards,
accumulatedDebt,
earnedInterest,
prices,
Expand Down Expand Up @@ -55,7 +53,6 @@ const ApyCell = ({
apy={apy}
currency={currency}
prices={prices}
rewards={accruedRewards}
rewardsApy={rewardsApy}
isBorrow={isBorrow}
accumulatedDebt={accumulatedDebt}
Expand Down
8 changes: 2 additions & 6 deletions src/components/LoanPositionsTable/LoanPositionsTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { convertMonetaryAmountToValueInUSD } from '@/common/utils/utils';
import { Switch } from '@/component-library';
import { LoanType } from '@/types/loans';
import { getTokenPrice } from '@/utils/helpers/prices';
import { useGetAccountSubsidyRewards } from '@/utils/hooks/api/loans/use-get-account-subsidy-rewards';
import { useGetPrices } from '@/utils/hooks/api/use-get-prices';

import { AssetCell, BalanceCell, Table, TableProps } from '../DataGrid';
Expand Down Expand Up @@ -53,7 +52,6 @@ const LoanPositionsTable = ({
const titleId = useId();
const { t } = useTranslation();
const prices = useGetPrices();
const { data: subsidyRewards } = useGetAccountSubsidyRewards();

const isLending = variant === 'lend';
const showCollateral = !!onPressCollateralSwitch && isLending;
Expand Down Expand Up @@ -91,13 +89,11 @@ const LoanPositionsTable = ({
const apyCellProps = isLending
? {
apy: lendApy,
rewardsPerYear: lendReward,
accruedRewards: subsidyRewards ? subsidyRewards.perMarket[currency.ticker].lend : null
rewardsPerYear: lendReward
}
: {
apy: borrowApy,
rewardsPerYear: borrowReward,
accruedRewards: subsidyRewards ? subsidyRewards.perMarket[currency.ticker].borrow : null,
accumulatedDebt: (position as BorrowPosition).accumulatedDebt,
isBorrow: true
};
Expand Down Expand Up @@ -140,7 +136,7 @@ const LoanPositionsTable = ({
collateral
};
}),
[assets, isLending, onPressCollateralSwitch, onRowAction, positions, prices, showCollateral, subsidyRewards]
[assets, isLending, onPressCollateralSwitch, onRowAction, positions, prices, showCollateral]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { Cell, Table, TableProps } from '@/components';
import { ApyCell } from '@/components/LoanPositionsTable/ApyCell';
import { LoanTablePlaceholder } from '@/components/LoanPositionsTable/LoanTablePlaceholder';
import { getTokenPrice } from '@/utils/helpers/prices';
import { useGetAccountSubsidyRewards } from '@/utils/hooks/api/loans/use-get-account-subsidy-rewards';
import { useGetPrices } from '@/utils/hooks/api/use-get-prices';

import { StyledAssetCell } from './BorrowAssetsTable.style';
Expand Down Expand Up @@ -48,20 +47,17 @@ const BorrowAssetsTable = ({ assets, onRowAction, ...props }: BorrowAssetsTableP
const titleId = useId();
const { t } = useTranslation();
const prices = useGetPrices();
const { data: subsidyRewards } = useGetAccountSubsidyRewards();

const rows: BorrowAssetsTableRow[] = useMemo(
() =>
Object.values(assets).map(({ borrowApy, currency, availableCapacity, totalBorrows, borrowReward }) => {
const asset = <StyledAssetCell ticker={currency.ticker} />;
const accruedRewards = subsidyRewards ? subsidyRewards.perMarket[currency.ticker].borrow : null;

const apy = (
<ApyCell
apy={borrowApy}
currency={currency}
rewardsPerYear={borrowReward}
accruedRewards={accruedRewards}
prices={prices}
isBorrow
// TODO: temporary until we find why row click is being ignored
Expand Down Expand Up @@ -89,7 +85,7 @@ const BorrowAssetsTable = ({ assets, onRowAction, ...props }: BorrowAssetsTableP
totalBorrowed
};
}),
[assets, prices, onRowAction, subsidyRewards]
[assets, prices, onRowAction]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { AssetCell, BalanceCell, Cell, Table, TableProps } from '@/components';
import { ApyCell } from '@/components/LoanPositionsTable/ApyCell';
import { LoanTablePlaceholder } from '@/components/LoanPositionsTable/LoanTablePlaceholder';
import { getTokenPrice } from '@/utils/helpers/prices';
import { useGetAccountSubsidyRewards } from '@/utils/hooks/api/loans/use-get-account-subsidy-rewards';
import { useGetBalances } from '@/utils/hooks/api/tokens/use-get-balances';
import { useGetPrices } from '@/utils/hooks/api/use-get-prices';

Expand Down Expand Up @@ -48,20 +47,17 @@ const LendAssetsTable = ({ assets, onRowAction, ...props }: LendAssetsTableProps
const { t } = useTranslation();
const prices = useGetPrices();
const { data: balances } = useGetBalances();
const { data: subsidyRewards } = useGetAccountSubsidyRewards();

const rows: LendAssetsTableRow[] = useMemo(
() =>
Object.values(assets).map(({ lendApy, currency, totalLiquidity, lendReward }) => {
const asset = <AssetCell ticker={currency.ticker} />;
const accruedRewards = subsidyRewards ? subsidyRewards.perMarket[currency.ticker].lend : null;

const apy = (
<ApyCell
apy={lendApy}
currency={currency}
rewardsPerYear={lendReward}
accruedRewards={accruedRewards}
prices={prices}
// TODO: temporary until we find why row click is being ignored
onClick={() => onRowAction?.(currency.ticker as Key)}
Expand All @@ -87,7 +83,7 @@ const LendAssetsTable = ({ assets, onRowAction, ...props }: LendAssetsTableProps
totalSupply
};
}),
[assets, balances, onRowAction, prices, subsidyRewards]
[assets, balances, onRowAction, prices]
);

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const getNetAPY = (
const totalBorrowApy = borrowPositions.reduce((total, position) => {
const { currency } = position.amount;
const { borrowApy, borrowReward } = assets[currency.ticker];

const rewardsApy = getSubsidyRewardApy(currency, borrowReward, prices);
const positionApy = borrowApy.sub(rewardsApy || 0);
const positionUSDValue = convertMonetaryAmountToValueInUSD(
Expand Down
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2120,10 +2120,10 @@
dependencies:
axios "^0.21.1"

"@interlay/[email protected].0":
version "2.3.0"
resolved "https://registry.yarnpkg.com/@interlay/interbtc-api/-/interbtc-api-2.3.0.tgz#54ca0a80550c1b8ec93224d5e29b960b0d607d74"
integrity sha512-r+GU1jfTpapjG5bgdJMXAVWok1msSqJPA/ldcPfN1d7v8BnY26F2LekeJjO77Birc5tDAfeGMqYvRkNcmcqkUA==
"@interlay/[email protected].1":
version "2.3.1"
resolved "https://registry.yarnpkg.com/@interlay/interbtc-api/-/interbtc-api-2.3.1.tgz#99bd9058453f6125d0fe1aa7bae208acda3191ed"
integrity sha512-XTbFNz0W/ev9cLfO0hKOfoPa79ARUrhKItrNolA98n055DMWHS7Lu9P1HUBG9KfKvgoiB5hQBo1Gc4hG+oPKQg==
dependencies:
"@interlay/esplora-btc-api" "0.4.0"
"@interlay/interbtc-types" "1.12.0"
Expand Down

2 comments on commit 4bf3e13

@vercel
Copy link

@vercel vercel bot commented on 4bf3e13 Jun 9, 2023

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 4bf3e13 Jun 9, 2023

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.