-
Notifications
You must be signed in to change notification settings - Fork 464
Fix/filtering by apys on Market page #2755
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
Open
AGMASO
wants to merge
5
commits into
main
Choose a base branch
from
fix/filtering-by-apy
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
17566fa
feat: create hook to isolate displayapy calculation
AGMASO eb403fd
chore: style
AGMASO 5aa8385
feat: added filtering for apys accounting extra incentives on market …
AGMASO 7099a0f
Merge branch 'main' into fix/filtering-by-apy
AGMASO 732aa2d
fix: simplified dependencies to avoid re-renders
AGMASO File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -42,6 +42,7 @@ yarn-error.log* | |
| package-lock.json | ||
|
|
||
| .eslintcache | ||
| .yarn-cache | ||
|
|
||
| # IDE specific | ||
| .idea | ||
|
|
||
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ import { ProtocolAction } from '@aave/contract-helpers'; | |
| import { ReserveIncentiveResponse } from '@aave/math-utils/dist/esm/formatters/incentive/calculate-reserve-incentives'; | ||
| import { Box, Typography } from '@mui/material'; | ||
| import { useRouter } from 'next/router'; | ||
| import { ReactNode } from 'react'; | ||
| import { ReactNode, useMemo } from 'react'; | ||
| import { ENABLE_SELF_CAMPAIGN, useMeritIncentives } from 'src/hooks/useMeritIncentives'; | ||
| import { useMerklIncentives } from 'src/hooks/useMerklIncentives'; | ||
| import { useMerklPointsIncentives } from 'src/hooks/useMerklPointsIncentives'; | ||
|
|
@@ -32,6 +32,7 @@ interface IncentivesCardProps { | |
| protocolAction?: ProtocolAction; | ||
| align?: 'center' | 'flex-end'; | ||
| inlineIncentives?: boolean; | ||
| displayAPY?: number | 'Infinity'; | ||
| } | ||
|
|
||
| export const IncentivesCard = ({ | ||
|
|
@@ -47,21 +48,10 @@ export const IncentivesCard = ({ | |
| market, | ||
| protocolAction, | ||
| inlineIncentives = false, | ||
| displayAPY, | ||
| }: IncentivesCardProps) => { | ||
| const router = useRouter(); | ||
| const protocolAPY = typeof value === 'string' ? parseFloat(value) : value; | ||
|
|
||
| const protocolIncentivesAPR = | ||
| incentives?.reduce((sum, inc) => { | ||
| if (inc.incentiveAPR === 'Infinity' || sum === 'Infinity') { | ||
| return 'Infinity'; | ||
| } | ||
| return sum + +inc.incentiveAPR; | ||
| }, 0 as number | 'Infinity') || 0; | ||
|
|
||
| const protocolIncentivesAPY = convertAprToApy( | ||
| protocolIncentivesAPR === 'Infinity' ? 0 : protocolIncentivesAPR | ||
| ); | ||
| const { data: meritIncentives } = useMeritIncentives({ | ||
| symbol, | ||
| market, | ||
|
|
@@ -86,27 +76,48 @@ export const IncentivesCard = ({ | |
| protocolIncentives: incentives || [], | ||
| }); | ||
|
|
||
| const meritIncentivesAPR = meritIncentives?.breakdown?.meritIncentivesAPR || 0; | ||
|
|
||
| // TODO: This is a one-off for the Self campaign. | ||
| // Remove once the Self incentives are finished. | ||
| const selfAPY = ENABLE_SELF_CAMPAIGN ? meritIncentives?.variants?.selfAPY ?? 0 : 0; | ||
| const totalMeritAPY = meritIncentivesAPR + selfAPY; | ||
|
|
||
| const merklIncentivesAPR = merklPointsIncentives?.breakdown?.points | ||
| ? merklPointsIncentives.breakdown.merklIncentivesAPR || 0 | ||
| : merklIncentives?.breakdown?.merklIncentivesAPR || 0; | ||
|
|
||
| const isBorrow = protocolAction === ProtocolAction.borrow; | ||
|
|
||
| // If any incentive is infinite, the total should be infinite | ||
| const hasInfiniteIncentives = protocolIncentivesAPR === 'Infinity'; | ||
|
|
||
| const displayAPY = hasInfiniteIncentives | ||
| ? 'Infinity' | ||
| : isBorrow | ||
| ? protocolAPY - (protocolIncentivesAPY as number) - totalMeritAPY - merklIncentivesAPR | ||
| : protocolAPY + (protocolIncentivesAPY as number) + totalMeritAPY + merklIncentivesAPR; | ||
| const computedDisplayAPY = useMemo(() => { | ||
| if (displayAPY !== undefined) { | ||
| return displayAPY; | ||
| } | ||
|
|
||
| const protocolIncentivesAPR = | ||
| incentives?.reduce((sum, inc) => { | ||
| if (inc.incentiveAPR === 'Infinity' || sum === 'Infinity') { | ||
| return 'Infinity'; | ||
| } | ||
| return sum + +inc.incentiveAPR; | ||
| }, 0 as number | 'Infinity') || 0; | ||
|
|
||
| const protocolIncentivesAPY = convertAprToApy( | ||
| protocolIncentivesAPR === 'Infinity' ? 0 : protocolIncentivesAPR | ||
| ); | ||
|
|
||
| const meritIncentivesAPR = meritIncentives?.breakdown?.meritIncentivesAPR || 0; | ||
| const selfAPY = ENABLE_SELF_CAMPAIGN ? meritIncentives?.variants?.selfAPY ?? 0 : 0; | ||
| const totalMeritAPY = meritIncentivesAPR + selfAPY; | ||
|
|
||
| const merklIncentivesAPR = merklPointsIncentives?.breakdown?.points | ||
| ? merklPointsIncentives.breakdown.merklIncentivesAPR || 0 | ||
| : merklIncentives?.breakdown?.merklIncentivesAPR || 0; | ||
|
|
||
| const isBorrow = protocolAction === ProtocolAction.borrow; | ||
| const hasInfiniteIncentives = protocolIncentivesAPR === 'Infinity'; | ||
|
|
||
| return hasInfiniteIncentives | ||
| ? 'Infinity' | ||
| : isBorrow | ||
| ? protocolAPY - (protocolIncentivesAPY as number) - totalMeritAPY - merklIncentivesAPR | ||
| : protocolAPY + (protocolIncentivesAPY as number) + totalMeritAPY + merklIncentivesAPR; | ||
|
Comment on lines
+107
to
+111
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe we can remove this one because we have something similar in useIncentivizedApy.ts? |
||
| }, [ | ||
| displayAPY, | ||
| incentives, | ||
| meritIncentives, | ||
| merklIncentives, | ||
| merklPointsIncentives, | ||
| protocolAPY, | ||
| protocolAction, | ||
| ]); | ||
|
|
||
| const isSghoPage = | ||
| typeof router?.asPath === 'string' && router.asPath.toLowerCase().startsWith('/sgho'); | ||
|
|
@@ -156,14 +167,14 @@ export const IncentivesCard = ({ | |
| > | ||
| {value.toString() !== '-1' ? ( | ||
| <Box sx={{ display: 'flex', alignItems: 'center', gap: 1 }}> | ||
| {displayAPY === 'Infinity' ? ( | ||
| {computedDisplayAPY === 'Infinity' ? ( | ||
| <Typography variant={variant} color={color || 'text.secondary'}> | ||
| ∞ % | ||
| </Typography> | ||
| ) : ( | ||
| <FormattedNumber | ||
| data-cy={`apy`} | ||
| value={displayAPY} | ||
| value={computedDisplayAPY} | ||
| percent | ||
| variant={variant} | ||
| symbolsVariant={symbolsVariant} | ||
|
|
||
This file contains hidden or 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,92 @@ | ||
| import { ProtocolAction } from '@aave/contract-helpers'; | ||
| import { ReserveIncentiveResponse } from '@aave/math-utils/dist/esm/formatters/incentive/calculate-reserve-incentives'; | ||
| import { ENABLE_SELF_CAMPAIGN, useMeritIncentives } from 'src/hooks/useMeritIncentives'; | ||
| import { convertAprToApy } from 'src/utils/utils'; | ||
|
|
||
| import { useMerklIncentives } from './useMerklIncentives'; | ||
| import { useMerklPointsIncentives } from './useMerklPointsIncentives'; | ||
|
|
||
| interface IncentivizedApyParams { | ||
| symbol: string; | ||
| market: string; | ||
| rewardedAsset: string; | ||
| protocolAction?: ProtocolAction; | ||
| protocolAPY: number | string; | ||
| protocolIncentives?: ReserveIncentiveResponse[]; | ||
| } | ||
| type UseIncentivizedApyResult = { | ||
| displayAPY: number | 'Infinity'; | ||
| hasInfiniteIncentives: boolean; | ||
| isLoading: boolean; | ||
| }; | ||
| export const useIncentivizedApy = ({ | ||
| symbol, | ||
| market, | ||
| rewardedAsset: address, | ||
| protocolAction, | ||
| protocolAPY: value, | ||
| protocolIncentives: incentives = [], | ||
| }: IncentivizedApyParams): UseIncentivizedApyResult => { | ||
| const protocolAPY = typeof value === 'string' ? parseFloat(value) : value; | ||
|
|
||
| const protocolIncentivesAPR = | ||
| incentives?.reduce((sum, inc) => { | ||
| if (inc.incentiveAPR === 'Infinity' || sum === 'Infinity') { | ||
| return 'Infinity'; | ||
| } | ||
| return sum + +inc.incentiveAPR; | ||
| }, 0 as number | 'Infinity') || 0; | ||
|
|
||
| const protocolIncentivesAPY = convertAprToApy( | ||
| protocolIncentivesAPR === 'Infinity' ? 0 : protocolIncentivesAPR | ||
| ); | ||
| const { data: meritIncentives, isLoading: meritLoading } = useMeritIncentives({ | ||
| symbol, | ||
| market, | ||
| protocolAction, | ||
| protocolAPY, | ||
| protocolIncentives: incentives || [], | ||
| }); | ||
|
|
||
| const { data: merklIncentives, isLoading: merklLoading } = useMerklIncentives({ | ||
| market, | ||
| rewardedAsset: address, | ||
| protocolAction, | ||
| protocolAPY, | ||
| protocolIncentives: incentives || [], | ||
| }); | ||
|
|
||
| const { data: merklPointsIncentives, isLoading: merklPointsLoading } = useMerklPointsIncentives({ | ||
| market, | ||
| rewardedAsset: address, | ||
| protocolAction, | ||
| protocolAPY, | ||
| protocolIncentives: incentives || [], | ||
| }); | ||
|
|
||
| const isLoading = meritLoading || merklLoading || merklPointsLoading; | ||
|
|
||
| const meritIncentivesAPR = meritIncentives?.breakdown?.meritIncentivesAPR || 0; | ||
|
|
||
| // TODO: This is a one-off for the Self campaign. | ||
| // Remove once the Self incentives are finished. | ||
| const selfAPY = ENABLE_SELF_CAMPAIGN ? meritIncentives?.variants?.selfAPY ?? 0 : 0; | ||
| const totalMeritAPY = meritIncentivesAPR + selfAPY; | ||
|
|
||
| const merklIncentivesAPR = merklPointsIncentives?.breakdown?.points | ||
| ? merklPointsIncentives.breakdown.merklIncentivesAPR || 0 | ||
| : merklIncentives?.breakdown?.merklIncentivesAPR || 0; | ||
|
|
||
| const isBorrow = protocolAction === ProtocolAction.borrow; | ||
|
|
||
| // If any incentive is infinite, the total should be infinite | ||
| const hasInfiniteIncentives = protocolIncentivesAPR === 'Infinity'; | ||
|
|
||
| const displayAPY = hasInfiniteIncentives | ||
| ? 'Infinity' | ||
| : isBorrow | ||
| ? protocolAPY - (protocolIncentivesAPY as number) - totalMeritAPY - merklIncentivesAPR | ||
| : protocolAPY + (protocolIncentivesAPY as number) + totalMeritAPY + merklIncentivesAPR; | ||
|
|
||
| return { displayAPY, hasInfiniteIncentives, isLoading }; | ||
| }; |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is APR + APY okay?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For some reason,

meritIncentivesAPRhas been kept with this name (probably to avoid conflicts between files), but it actually represents the APY.