Skip to content

Commit

Permalink
US-996 RIF Wallet libs - Sending a transaction should check if the us…
Browse files Browse the repository at this point in the history
…er has the funds + fee before sending (RIF RELAY) (#739)

* feat: handle insuffient funds

* refactor: use feeContract as address to search balances
  • Loading branch information
TravellerOnTheRun authored Sep 15, 2023
1 parent 165a532 commit cb02e27
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/lib/i18n.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ const resources = {
transaction_summary_function_type: 'type',
transaction_summary_plus_fees: '+ fees',
transaction_summary_plus_fees_capitalcase: '+ Fees',
transaction_summary_insufficient_funds: 'Insufficient funds',
profile_screen_title: 'Profile',
profile_contact_details_subtitle: 'Contact Details',
profile_phone_label: 'Phone Number',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ import {
import { TransactionSummaryScreenProps } from '.'

interface Props {
goBack?: () => void
wallet: RIFWallet
goBack?: () => void
}

type TransactionSummaryComponentProps = Omit<
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
import { BigNumber, constants } from 'ethers'
import { useCallback, useEffect, useMemo, useState } from 'react'
import { useTranslation } from 'react-i18next'
import { StyleSheet, View } from 'react-native'
import { Alert, StyleSheet, View } from 'react-native'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { isAddress } from '@rsksmart/rsk-utils'

Expand All @@ -18,15 +18,22 @@ import { TransactionSummaryScreenProps } from 'screens/transactionSummary'
import { TransactionSummaryComponent } from 'screens/transactionSummary/TransactionSummaryComponent'
import { sharedColors } from 'shared/constants'
import { chainTypesById } from 'shared/constants/chainConstants'
import { errorHandler } from 'shared/utils'
import { castStyle, errorHandler } from 'shared/utils'
import { selectWalletState } from 'store/slices/settingsSlice'
import { ChainTypeEnum } from 'store/slices/settingsSlice/types'
import { selectUsdPrices } from 'store/slices/usdPricesSlice'
import { useAppDispatch, useAppSelector } from 'store/storeUtils'
import { addRecentContact } from 'store/slices/contactsSlice'
import { selectBalances } from 'store/slices/balancesSlice'

import useEnhancedWithGas from '../useEnhancedWithGas'

const tokenToBoolMap = new Map([
[TokenSymbol.RIF, true],
[TokenSymbol.TRIF, true],
[undefined, false],
])

interface Props {
request: SendTransactionRequest
onConfirm: () => void
Expand All @@ -43,6 +50,7 @@ export const ReviewTransactionContainer = ({
const tokenPrices = useAppSelector(selectUsdPrices)
// enhance the transaction to understand what it is:
const { wallet, chainId } = useAppSelector(selectWalletState)
const balances = useAppSelector(selectBalances)
const [txCostInRif, setTxCostInRif] = useState<BigNumber>()
const { t } = useTranslation()

Expand Down Expand Up @@ -144,6 +152,22 @@ export const ReviewTransactionContainer = ({
const feeValue = txCostInRif
? `${balanceToDisplay(txCostInRif, 18, 0)}`
: '0'

let insufficientFunds = false

if (tokenToBoolMap.get(symbol as TokenSymbol)) {
insufficientFunds =
Number(value) + Number(feeValue) > Number(balances[feeContract].balance)
} else {
insufficientFunds =
Number(feeValue) > Number(balances[feeContract].balance)
}

if (insufficientFunds) {
Alert.alert(t('transaction_summary_insufficient_funds'))
}

// get usd values
const tokenUsd = convertToUSD(Number(value), tokenQuote)
const feeUsd = convertToUSD(Number(feeValue), feeQuote)

Expand Down Expand Up @@ -174,6 +198,7 @@ export const ReviewTransactionContainer = ({
color: sharedColors.white,
textColor: sharedColors.black,
accessibilityLabel: 'Confirm',
disabled: insufficientFunds,
},
{
style: { marginTop: 10 },
Expand All @@ -186,6 +211,8 @@ export const ReviewTransactionContainer = ({
functionName,
}
}, [
feeContract,
balances,
txCostInRif,
value,
tokenQuote,
Expand All @@ -211,10 +238,10 @@ export const ReviewTransactionContainer = ({
}

const styles = StyleSheet.create({
container: {
container: castStyle.view({
width: '100%',
height: '100%',
zIndex: 999,
position: 'absolute',
},
}),
})

0 comments on commit cb02e27

Please sign in to comment.