Skip to content

Commit

Permalink
fix(earn): Show correct fiat and token amount on earn collect screen (#…
Browse files Browse the repository at this point in the history
…6130)

### Description

withdrawToken is not 1:1 with depositToken, need to convert
withdrawToken balance to depositToken using pricePerShare

### Test plan

Without change (mismatch):


https://github.com/user-attachments/assets/160417bf-9047-4a63-ae16-5568c08a6861



With change (no mismatch):


https://github.com/user-attachments/assets/f4e01cd0-9005-4c99-a9b8-2a4d7e013fad



### Related issues

- Fixes N/A

### 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 3, 2024
1 parent f49b00e commit 36a6b3e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 12 deletions.
10 changes: 5 additions & 5 deletions src/earn/EarnCollectScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,9 @@ describe('EarnCollectScreen', () => {
expect(getByText('earnFlow.collect.title')).toBeTruthy()
expect(getByText('earnFlow.collect.total')).toBeTruthy()
expect(getByTestId(`EarnCollect/${mockArbUsdcTokenId}/CryptoAmount`)).toHaveTextContent(
'10.75 USDC'
'11.83 USDC'
)
expect(getByTestId(`EarnCollect/${mockArbUsdcTokenId}/FiatAmount`)).toHaveTextContent('₱14.30')
expect(getByTestId(`EarnCollect/${mockArbUsdcTokenId}/FiatAmount`)).toHaveTextContent('₱15.73')
expect(queryByTestId('EarnCollect/ApyLoading')).toBeFalsy()
expect(getByTestId('EarnCollect/GasLoading')).toBeTruthy()
expect(getByTestId('EarnCollectScreen/CTA')).toBeDisabled()
Expand Down Expand Up @@ -173,9 +173,9 @@ describe('EarnCollectScreen', () => {
expect(getByText('earnFlow.collect.title')).toBeTruthy()
expect(getByText('earnFlow.collect.total')).toBeTruthy()
expect(getByTestId(`EarnCollect/${mockArbUsdcTokenId}/CryptoAmount`)).toHaveTextContent(
'10.75 USDC'
'11.83 USDC'
)
expect(getByTestId(`EarnCollect/${mockArbUsdcTokenId}/FiatAmount`)).toHaveTextContent('₱14.30')
expect(getByTestId(`EarnCollect/${mockArbUsdcTokenId}/FiatAmount`)).toHaveTextContent('₱15.73')
expect(getByTestId('EarnCollectScreen/CTA')).toBeDisabled()

expect(queryByText('earnFlow.collect.plus')).toBeFalsy()
Expand Down Expand Up @@ -278,7 +278,7 @@ describe('EarnCollectScreen', () => {

expect(AppAnalytics.track).toHaveBeenCalledWith(EarnEvents.earn_collect_earnings_press, {
depositTokenId: mockArbUsdcTokenId,
tokenAmount: '10.75',
tokenAmount: '11.825',
networkId: NetworkId['arbitrum-sepolia'],
providerId: mockEarnPositions[0].appId,
rewards: [{ amount: '0.01', tokenId: mockArbArbTokenId }],
Expand Down
8 changes: 6 additions & 2 deletions src/earn/EarnCollectScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ export default function EarnCollectScreen({ route }: Props) {
rewardsPositions,
})

const withdrawAmountInDepositToken = useMemo(() => {
return withdrawToken.balance.multipliedBy(pool.pricePerShare[0] ?? 1)
}, [withdrawToken, pool.pricePerShare])

const onPress = () => {
if (prepareTransactionsResult?.type !== 'possible') {
// should never happen because button is disabled if withdraw is not possible
Expand All @@ -98,7 +102,7 @@ export default function EarnCollectScreen({ route }: Props) {

AppAnalytics.track(EarnEvents.earn_collect_earnings_press, {
depositTokenId,
tokenAmount: withdrawToken.balance.toString(),
tokenAmount: withdrawAmountInDepositToken.toString(),
networkId: withdrawToken.networkId,
providerId: pool.appId,
poolId: pool.positionId,
Expand Down Expand Up @@ -139,7 +143,7 @@ export default function EarnCollectScreen({ route }: Props) {
<CollectItem
title={t('earnFlow.collect.total')}
tokenInfo={depositToken}
rewardAmount={withdrawToken.balance}
rewardAmount={withdrawAmountInDepositToken}
/>
{rewardsTokens.map((token, index) => (
<CollectItem
Expand Down
8 changes: 4 additions & 4 deletions src/earn/EarnPoolInfoScreen.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ describe('EarnPoolInfoScreen', () => {
).toBeTruthy()
expect(
within(getByTestId('DepositAndEarningsCard')).getByText(
'earnFlow.poolInfoScreen.lineItemAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"133.00","cryptoAmount":"100.00","cryptoSymbol":"USDC"}'
'earnFlow.poolInfoScreen.lineItemAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"146.30","cryptoAmount":"110.00","cryptoSymbol":"USDC"}'
)
).toBeTruthy()
expect(getAllByTestId('EarningItemLineItem')).toHaveLength(2)
Expand Down Expand Up @@ -187,7 +187,7 @@ describe('EarnPoolInfoScreen', () => {
).toBeTruthy()
expect(
within(getByTestId('DepositAndEarningsCard')).getByText(
'earnFlow.poolInfoScreen.lineItemAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"133.00","cryptoAmount":"100.00","cryptoSymbol":"USDC"}'
'earnFlow.poolInfoScreen.lineItemAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"146.30","cryptoAmount":"110.00","cryptoSymbol":"USDC"}'
)
).toBeTruthy()
})
Expand Down Expand Up @@ -224,7 +224,7 @@ describe('EarnPoolInfoScreen', () => {
).toBeTruthy()
expect(
within(getByTestId('DepositAndEarningsCard')).getByText(
'earnFlow.poolInfoScreen.lineItemAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"131.67","cryptoAmount":"99.00","cryptoSymbol":"USDC"}'
'earnFlow.poolInfoScreen.lineItemAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"144.97","cryptoAmount":"109.00","cryptoSymbol":"USDC"}'
)
).toBeTruthy()
expect(getAllByTestId('EarningItemLineItem')).toHaveLength(2)
Expand Down Expand Up @@ -271,7 +271,7 @@ describe('EarnPoolInfoScreen', () => {
).toBeTruthy()
expect(
within(getByTestId('DepositAndEarningsCard')).getByText(
'earnFlow.poolInfoScreen.lineItemAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"131.01","cryptoAmount":"98.50","cryptoSymbol":"USDC"}'
'earnFlow.poolInfoScreen.lineItemAmountDisplay, {"localCurrencySymbol":"₱","localCurrencyAmount":"144.31","cryptoAmount":"108.50","cryptoSymbol":"USDC"}'
)
).toBeTruthy()
expect(getAllByTestId('EarningItemLineItem')).toHaveLength(2)
Expand Down
2 changes: 1 addition & 1 deletion test/values.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1782,7 +1782,7 @@ export const mockEarnPositions: EarnPosition[] = [
balance: '0',
},
],
pricePerShare: ['1'],
pricePerShare: ['1.1'],
priceUsd: '1.2',
balance: '0',
supply: '190288.768509',
Expand Down

0 comments on commit 36a6b3e

Please sign in to comment.