-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(warning): Add generic gas fee warning component (#6385)
### Description Generic gas warning create in `GasFeeWarning.tsx`. Used in `EarnEnterAmount`. For cases 2-5 `rounded n` is omitted, since it is difficult to find for cases 2-4, and in case 5 there is no cta so no immediate action to be taken (slack thread discussing [here](https://valora-app.slack.com/archives/C03D3K379FE/p1734981974845839)). Analytics added for when the error shows, analytics for cta press are to be included in the `onPressCta` prop. ### Test plan - `EarnEnterAmount` unit test updated - `GasFeeWarning` unit tests created **NEW Manual Testing** https://github.com/user-attachments/assets/13dac169-14cc-402e-85a6-adc7d33f3cbd https://github.com/user-attachments/assets/660b0353-8f1e-49d9-92ea-2a52123366ac **OLD Manual Testing:** Not enough gas currency: | <img src="https://github.com/user-attachments/assets/775bbabc-8434-401f-a73e-465d1f6d627f" width="250" /> | <img src="https://github.com/user-attachments/assets/89969a0e-141f-4a1f-b967-61086af39f01" width="250" /> | Transacting with max amount: | <img src="https://github.com/user-attachments/assets/0b2cdca5-4d5c-4e38-a14b-a163808e8638" width="250" /> | <img src="https://github.com/user-attachments/assets/a1afef7b-407f-45b4-9b93-98c2093045f3" width="250" /> | Dapp: <img src="https://github.com/user-attachments/assets/a4de5d16-6536-4701-a6aa-ac331bac8016" width="250" /> Tapping CTA: https://github.com/user-attachments/assets/cbd04993-a63f-400a-a620-060fa73eae9b https://github.com/user-attachments/assets/f4a8dfa5-7352-46a4-8a84-515baca065e5 ### Related issues - Fixes ACT-1482 ### 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
1 parent
f284f7c
commit ee71b25
Showing
10 changed files
with
356 additions
and
69 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,126 @@ | ||
import { fireEvent, render } from '@testing-library/react-native' | ||
import BigNumber from 'bignumber.js' | ||
import React from 'react' | ||
import { Provider } from 'react-redux' | ||
import AppAnalytics from 'src/analytics/AppAnalytics' | ||
import { FeeEvents } from 'src/analytics/Events' | ||
import GasFeeWarning from 'src/components/GasFeeWarning' | ||
import { NetworkId } from 'src/transactions/types' | ||
import { | ||
PreparedTransactionsNeedDecreaseSpendAmountForGas, | ||
PreparedTransactionsNotEnoughBalanceForGas, | ||
PreparedTransactionsPossible, | ||
} from 'src/viem/prepareTransactions' | ||
import { createMockStore } from 'test/utils' | ||
import { mockArbEthTokenId, mockCeloTokenId, mockTokenBalances } from 'test/values' | ||
|
||
const mockPreparedTransactionPossible: PreparedTransactionsPossible = { | ||
type: 'possible' as const, | ||
transactions: [], | ||
feeCurrency: { | ||
...mockTokenBalances[mockArbEthTokenId], | ||
isNative: true, | ||
balance: new BigNumber(10), | ||
priceUsd: new BigNumber(1), | ||
lastKnownPriceUsd: new BigNumber(1), | ||
}, | ||
} | ||
|
||
const mockPreparedTransactionNotEnoughCelo: PreparedTransactionsNotEnoughBalanceForGas = { | ||
type: 'not-enough-balance-for-gas' as const, | ||
feeCurrencies: [ | ||
{ | ||
...mockTokenBalances[mockCeloTokenId], | ||
isNative: true, | ||
balance: new BigNumber(0), | ||
priceUsd: new BigNumber(1500), | ||
lastKnownPriceUsd: new BigNumber(1500), | ||
}, | ||
], | ||
} | ||
|
||
const mockPreparedTransactionNeedDecreaseEth: PreparedTransactionsNeedDecreaseSpendAmountForGas = { | ||
type: 'need-decrease-spend-amount-for-gas' as const, | ||
feeCurrency: { | ||
...mockTokenBalances[mockArbEthTokenId], | ||
isNative: true, | ||
balance: new BigNumber(0), | ||
priceUsd: new BigNumber(1500), | ||
lastKnownPriceUsd: new BigNumber(1500), | ||
}, | ||
maxGasFeeInDecimal: new BigNumber(1), | ||
estimatedGasFeeInDecimal: new BigNumber(1), | ||
decreasedSpendAmount: new BigNumber(1), | ||
} | ||
|
||
describe('GasFeeWarning', () => { | ||
beforeEach(() => { | ||
jest.clearAllMocks() | ||
}) | ||
it('should return null if prepareTransactionsResult is undefined', () => { | ||
const store = createMockStore() | ||
const { queryByTestId } = render( | ||
<Provider store={store}> | ||
<GasFeeWarning flow={'Send'} /> | ||
</Provider> | ||
) | ||
expect(queryByTestId('GasFeeWarning')).toBeFalsy() | ||
}) | ||
it('should return null if prepareTransactionsResult.type is possible', () => { | ||
const store = createMockStore() | ||
const { queryByTestId } = render( | ||
<Provider store={store}> | ||
<GasFeeWarning flow={'Send'} prepareTransactionsResult={mockPreparedTransactionPossible} /> | ||
</Provider> | ||
) | ||
expect(queryByTestId('GasFeeWarning')).toBeFalsy() | ||
}) | ||
it.each` | ||
scenario | flow | prepareTransactionsResult | feeCurrencyTokenId | title | description | ctaLabel | ||
${'sending max amount of ETH'} | ${'Send'} | ${mockPreparedTransactionNeedDecreaseEth} | ${mockArbEthTokenId} | ${'gasFeeWarning.title, {"context":"Send","tokenSymbol":"ETH"}'} | ${'gasFeeWarning.descriptionMaxAmount, {"context":"Send","tokenSymbol":"ETH"}'} | ${'gasFeeWarning.ctaAction, {"context":"Send"}'} | ||
${'sending with insufficient CELO'} | ${'Send'} | ${mockPreparedTransactionNotEnoughCelo} | ${mockCeloTokenId} | ${'gasFeeWarning.title, {"context":"Send","tokenSymbol":"CELO"}'} | ${undefined} | ${'gasFeeWarning.ctaBuy, {"tokenSymbol":"CELO"}'} | ||
${'swapping max amount of ETH'} | ${'Swap'} | ${mockPreparedTransactionNeedDecreaseEth} | ${mockArbEthTokenId} | ${'gasFeeWarning.title, {"context":"Swap","tokenSymbol":"ETH"}'} | ${'gasFeeWarning.descriptionMaxAmount, {"context":"Swap","tokenSymbol":"ETH"}'} | ${'gasFeeWarning.ctaAction, {"context":"Swap"}'} | ||
${'swapping with insufficient CELO'} | ${'Swap'} | ${mockPreparedTransactionNotEnoughCelo} | ${mockCeloTokenId} | ${'gasFeeWarning.title, {"context":"Swap","tokenSymbol":"CELO"}'} | ${undefined} | ${'gasFeeWarning.ctaBuy, {"tokenSymbol":"CELO"}'} | ||
${'withdrawing max amount of ETH'} | ${'Withdraw'} | ${mockPreparedTransactionNeedDecreaseEth} | ${mockArbEthTokenId} | ${'gasFeeWarning.title, {"context":"Withdraw","tokenSymbol":"ETH"}'} | ${'gasFeeWarning.descriptionMaxAmount, {"context":"Withdraw","tokenSymbol":"ETH"}'} | ${'gasFeeWarning.ctaAction, {"context":"Withdraw"}'} | ||
${'withdrawing with insufficient CELO'} | ${'Withdraw'} | ${mockPreparedTransactionNotEnoughCelo} | ${mockCeloTokenId} | ${'gasFeeWarning.title, {"context":"Withdraw","tokenSymbol":"CELO"}'} | ${undefined} | ${'gasFeeWarning.ctaBuy, {"tokenSymbol":"CELO"}'} | ||
${'depositing max amount of ETH'} | ${'Deposit'} | ${mockPreparedTransactionNeedDecreaseEth} | ${mockArbEthTokenId} | ${'gasFeeWarning.title, {"context":"Deposit","tokenSymbol":"ETH"}'} | ${'gasFeeWarning.descriptionMaxAmount, {"context":"Deposit","tokenSymbol":"ETH"}'} | ${'gasFeeWarning.ctaAction, {"context":"Deposit"}'} | ||
${'depositing with insufficient CELO'} | ${'Deposit'} | ${mockPreparedTransactionNotEnoughCelo} | ${mockCeloTokenId} | ${'gasFeeWarning.title, {"context":"Deposit","tokenSymbol":"CELO"}'} | ${undefined} | ${'gasFeeWarning.ctaBuy, {"tokenSymbol":"CELO"}'} | ||
${'dapp transaction with max amount of ETH'} | ${'Dapp'} | ${mockPreparedTransactionNeedDecreaseEth} | ${mockArbEthTokenId} | ${'gasFeeWarning.title, {"context":"Dapp","tokenSymbol":"ETH"}'} | ${'gasFeeWarning.descriptionDapp, {"tokenSymbol":"ETH"}'} | ${undefined} | ||
${'dapp transaction with insufficient CELO'} | ${'Dapp'} | ${mockPreparedTransactionNotEnoughCelo} | ${mockCeloTokenId} | ${'gasFeeWarning.title, {"context":"Dapp","tokenSymbol":"CELO"}'} | ${'gasFeeWarning.descriptionDapp, {"tokenSymbol":"CELO"}'} | ${undefined} | ||
`( | ||
'renders error correctly when $scenario', | ||
({ flow, prepareTransactionsResult, feeCurrencyTokenId, title, description, ctaLabel }) => { | ||
const store = createMockStore() | ||
const onPressSmallerAmount = jest.fn() | ||
const { getByTestId, getByText } = render( | ||
<Provider store={store}> | ||
<GasFeeWarning | ||
flow={flow} | ||
prepareTransactionsResult={prepareTransactionsResult} | ||
onPressSmallerAmount={onPressSmallerAmount} | ||
/> | ||
</Provider> | ||
) | ||
expect(getByTestId('GasFeeWarning')).toBeTruthy() | ||
expect(AppAnalytics.track).toHaveBeenCalledTimes(1) | ||
expect(AppAnalytics.track).toHaveBeenCalledWith(FeeEvents.gas_fee_warning_impression, { | ||
flow, | ||
errorType: prepareTransactionsResult.type, | ||
tokenId: feeCurrencyTokenId, | ||
networkId: | ||
feeCurrencyTokenId === mockArbEthTokenId | ||
? NetworkId['arbitrum-sepolia'] | ||
: NetworkId['celo-alfajores'], | ||
}) | ||
expect(getByText(title)).toBeTruthy() | ||
expect(description ? getByText(description) : true).toBeTruthy() | ||
expect(ctaLabel ? getByText(ctaLabel) : true).toBeTruthy() | ||
if (ctaLabel) { | ||
fireEvent.press(getByText(ctaLabel)) | ||
} | ||
expect(onPressSmallerAmount).toHaveBeenCalledTimes( | ||
ctaLabel && ctaLabel.includes('ctaAction') ? 1 : 0 | ||
) | ||
} | ||
) | ||
}) |
This file contains 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,124 @@ | ||
import React, { useEffect, useMemo } from 'react' | ||
import { useTranslation } from 'react-i18next' | ||
import { StyleSheet } from 'react-native' | ||
import AppAnalytics from 'src/analytics/AppAnalytics' | ||
import { FeeEvents } from 'src/analytics/Events' | ||
import InLineNotification, { NotificationVariant } from 'src/components/InLineNotification' | ||
import { CICOFlow } from 'src/fiatExchanges/types' | ||
import { navigate } from 'src/navigator/NavigationService' | ||
import { Screens } from 'src/navigator/Screens' | ||
import { Spacing } from 'src/styles/styles' | ||
import { PreparedTransactionsResult } from 'src/viem/prepareTransactions' | ||
|
||
export type GasFeeWarningFlow = 'Send' | 'Swap' | 'Withdraw' | 'Deposit' | 'Dapp' | ||
|
||
function GasFeeWarning({ | ||
prepareTransactionsResult, | ||
flow, | ||
onPressSmallerAmount, | ||
}: { | ||
prepareTransactionsResult?: PreparedTransactionsResult | ||
flow: GasFeeWarningFlow | ||
onPressSmallerAmount?: (amount: string) => void | ||
}) { | ||
const { t } = useTranslation() | ||
|
||
const feeCurrency = prepareTransactionsResult | ||
? prepareTransactionsResult.type === 'not-enough-balance-for-gas' | ||
? prepareTransactionsResult.feeCurrencies[0] | ||
: prepareTransactionsResult.feeCurrency | ||
: undefined | ||
|
||
useEffect(() => { | ||
if (feeCurrency && prepareTransactionsResult && prepareTransactionsResult.type !== 'possible') { | ||
AppAnalytics.track(FeeEvents.gas_fee_warning_impression, { | ||
flow, | ||
errorType: prepareTransactionsResult.type, | ||
tokenId: feeCurrency.tokenId, | ||
networkId: feeCurrency.networkId, | ||
}) | ||
} | ||
}, [flow, prepareTransactionsResult, feeCurrency]) | ||
|
||
const { title, description, ctaLabel, onPressCta } = useMemo(() => { | ||
if ( | ||
!feeCurrency || | ||
!prepareTransactionsResult || | ||
prepareTransactionsResult.type === 'possible' | ||
) { | ||
return {} | ||
} | ||
const title = t('gasFeeWarning.title', { | ||
context: flow, | ||
tokenSymbol: feeCurrency.symbol, | ||
}) | ||
const trackCtaAnalytics = () => { | ||
AppAnalytics.track(FeeEvents.gas_fee_warning_cta_press, { | ||
flow, | ||
tokenId: feeCurrency.tokenId, | ||
errorType: prepareTransactionsResult.type, | ||
networkId: feeCurrency.networkId, | ||
}) | ||
} | ||
if (flow === 'Dapp') { | ||
return { | ||
title, | ||
description: t('gasFeeWarning.descriptionDapp', { tokenSymbol: feeCurrency.symbol }), | ||
ctaLabel: undefined, | ||
onPressCta: undefined, | ||
} | ||
} else if (prepareTransactionsResult.type === 'not-enough-balance-for-gas') { | ||
return { | ||
title, | ||
ctaLabel: t('gasFeeWarning.ctaBuy', { tokenSymbol: feeCurrency.symbol }), | ||
onPressCta: () => { | ||
trackCtaAnalytics() | ||
navigate(Screens.FiatExchangeAmount, { | ||
tokenId: feeCurrency.tokenId, | ||
flow: CICOFlow.CashIn, | ||
tokenSymbol: feeCurrency.symbol, | ||
}) | ||
}, | ||
} | ||
} else { | ||
return { | ||
title, | ||
description: t('gasFeeWarning.descriptionMaxAmount', { | ||
context: flow, | ||
tokenSymbol: feeCurrency.symbol, | ||
}), | ||
ctaLabel: t('gasFeeWarning.ctaAction', { context: flow }), | ||
onPressCta: () => { | ||
trackCtaAnalytics() | ||
onPressSmallerAmount?.(prepareTransactionsResult.decreasedSpendAmount.toString()) | ||
}, | ||
} | ||
} | ||
}, [flow, prepareTransactionsResult, feeCurrency]) | ||
|
||
if (!title) { | ||
return false | ||
} | ||
|
||
return ( | ||
<InLineNotification | ||
variant={NotificationVariant.Warning} | ||
title={title} | ||
description={description ?? null} | ||
ctaLabel={ctaLabel} | ||
onPressCta={onPressCta} | ||
style={styles.warning} | ||
testID={'GasFeeWarning'} | ||
/> | ||
) | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
warning: { | ||
marginTop: Spacing.Regular16, | ||
paddingHorizontal: Spacing.Regular16, | ||
borderRadius: 16, | ||
}, | ||
}) | ||
|
||
export default GasFeeWarning |
This file contains 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.