Skip to content

Commit

Permalink
fix(earn): Show correct pool amount when LP to deposit is not 1:1 (#6121
Browse files Browse the repository at this point in the history
)

### Description

Also fix TVL, now assume that TVL is always in USD (will add note in
hooks).

### Test plan

Manual test:
<img
src="https://github.com/user-attachments/assets/3fe943f0-ffe9-446a-afd1-dc47d7ea9ed1"
width="250" />
<img
src="https://github.com/user-attachments/assets/6661ba15-e8ce-40e2-ac74-e693061e3ca6"
width="250" />

### Related issues

- Fixes ACT-1397

### Backwards compatibility

Yes

### Network scalability

If a new NetworkId and/or Network are added in the future, the changes
in this PR will:

- [X] Continue to work without code changes, OR trigger a compilation
error (guaranteeing we find it when a new network is added)
  • Loading branch information
finnian0826 authored Oct 2, 2024
1 parent 73ce253 commit c0a9178
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 17 deletions.
10 changes: 5 additions & 5 deletions src/earn/EarnPoolInfoScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ describe('EarnPoolInfoScreen', () => {
)
).toBeTruthy()
expect(within(getByTestId('TvlCard')).getByText('earnFlow.poolInfoScreen.tvl')).toBeTruthy()
expect(within(getByTestId('TvlCard')).getByText('₱2,170,560.00')).toBeTruthy()
expect(within(getByTestId('TvlCard')).getByText('₱1,808,800.00')).toBeTruthy()
expect(
within(getByTestId('AgeCard')).getByText('duration, {"context":"month","count":5}')
).toBeTruthy()
Expand Down Expand Up @@ -142,7 +142,7 @@ describe('EarnPoolInfoScreen', () => {
).toBeTruthy()
expect(
within(getByTestId('DepositAndEarningsCard')).getByText(
' earnFlow.poolInfoScreen.titleLocalAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"154.28"}'
' earnFlow.poolInfoScreen.titleLocalAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"180.88"}'
)
).toBeTruthy()
expect(
Expand Down Expand Up @@ -182,7 +182,7 @@ describe('EarnPoolInfoScreen', () => {
).toBeTruthy()
expect(
within(getByTestId('DepositAndEarningsCard')).getByText(
' earnFlow.poolInfoScreen.titleLocalAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"133.00"}'
' earnFlow.poolInfoScreen.titleLocalAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"159.60"}'
)
).toBeTruthy()
expect(
Expand Down Expand Up @@ -219,7 +219,7 @@ describe('EarnPoolInfoScreen', () => {
).toBeTruthy()
expect(
within(getByTestId('DepositAndEarningsCard')).getByText(
'earnFlow.poolInfoScreen.titleLocalAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"152.95"}'
'earnFlow.poolInfoScreen.titleLocalAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"179.55"}'
)
).toBeTruthy()
expect(
Expand Down Expand Up @@ -266,7 +266,7 @@ describe('EarnPoolInfoScreen', () => {
).toBeTruthy()
expect(
within(getByTestId('DepositAndEarningsCard')).getByText(
'earnFlow.poolInfoScreen.titleLocalAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"152.95"}'
'earnFlow.poolInfoScreen.titleLocalAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"179.55"}'
)
).toBeTruthy()
expect(
Expand Down
13 changes: 5 additions & 8 deletions src/earn/EarnPoolInfoScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,14 +153,14 @@ function DepositAndEarningsCard({
onInfoIconPress: () => void
}) {
const { t } = useTranslation()
const { balance } = earnPosition
const { balance, priceUsd, pricePerShare } = earnPosition
const { earningItems, depositTokenId, cantSeparateCompoundedInterest } = earnPosition.dataProps
const depositTokenInfo = useTokenInfo(depositTokenId)
const localCurrencySymbol = useSelector(getLocalCurrencySymbol)
const localCurrencyExchangeRate = useSelector(usdToLocalCurrencyRateSelector)

// Deposit items used to calculate the total balance and total deposited
const depositBalanceInUsd = depositTokenInfo?.priceUsd?.multipliedBy(balance)
const depositBalanceInUsd = new BigNumber(priceUsd).multipliedBy(balance)
const depositBalanceInLocalCurrency = new BigNumber(localCurrencyExchangeRate ?? 0).multipliedBy(
depositBalanceInUsd ?? 0
)
Expand Down Expand Up @@ -190,7 +190,7 @@ function DepositAndEarningsCard({
])

const totalDepositBalanceInCrypto = useMemo(() => {
return new BigNumber(balance).minus(
return new BigNumber(balance).multipliedBy(new BigNumber(pricePerShare[0]) ?? 1).minus(
earningItems
.filter((item) => item.includedInPoolBalance)
.reduce((acc, item) => {
Expand All @@ -202,7 +202,7 @@ function DepositAndEarningsCard({
)
}, new BigNumber(0))
)
}, [balance, earningItems, earningItemsTokenInfo, depositTokenInfo])
}, [balance, pricePerShare, earningItems, earningItemsTokenInfo, depositTokenInfo])

const totalDepositBalanceInLocalCurrency =
useDollarsToLocalAmount(
Expand Down Expand Up @@ -328,10 +328,7 @@ function TvlCard({
const localCurrencySymbol = useSelector(getLocalCurrencySymbol)
const { t } = useTranslation()
const tvl = earnPosition.dataProps.tvl
const priceUsd = earnPosition.priceUsd
const tvlInFiat = useDollarsToLocalAmount(
tvl ? new BigNumber(tvl).times(new BigNumber(priceUsd)) : null
)
const tvlInFiat = useDollarsToLocalAmount(tvl ?? null)
const tvlString = useMemo(() => {
return `${localCurrencySymbol}${tvlInFiat ? formatValueToDisplay(tvlInFiat) : '--'}`
}, [localCurrencySymbol, tvlInFiat])
Expand Down
2 changes: 1 addition & 1 deletion src/earn/PoolCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ describe('PoolCard', () => {
getByText('earnFlow.poolCard.onNetwork, {"networkName":"Arbitrum Sepolia"}')
).toBeTruthy()
expect(getByText('earnFlow.poolCard.percentage, {"percentage":"1.92"}')).toBeTruthy()
expect(getByText('₱2,170,560.00')).toBeTruthy()
expect(getByText('₱1,808,800.00')).toBeTruthy()
})

it('correct behavior when tapping pool card', () => {
Expand Down
4 changes: 1 addition & 3 deletions src/earn/PoolCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,7 @@ export default function PoolCard({
[localCurrencySymbol, poolBalanceInFiat, rewardAmountInFiat]
)

const tvlInFiat = useDollarsToLocalAmount(
tvl ? new BigNumber(tvl).times(new BigNumber(priceUsd)) : null
)
const tvlInFiat = useDollarsToLocalAmount(tvl ?? null)
const tvlString = useMemo(() => {
return `${localCurrencySymbol}${tvlInFiat ? formatValueToDisplay(tvlInFiat) : '--'}`
}, [localCurrencySymbol, tvlInFiat])
Expand Down

0 comments on commit c0a9178

Please sign in to comment.