From e6af8ec3c37aaab6b413c2ab025fdae1476dd85e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Volpe?= Date: Mon, 11 Nov 2019 12:35:31 +0100 Subject: [PATCH 1/2] Doesn't calculate fee on empty balance --- packages/mobile/src/fees/saga.test.ts | 17 ++++++++++++++++- packages/mobile/src/fees/saga.ts | 13 ++++++++++++- packages/mobile/src/stableToken/reducer.ts | 3 +++ 3 files changed, 31 insertions(+), 2 deletions(-) diff --git a/packages/mobile/src/fees/saga.test.ts b/packages/mobile/src/fees/saga.test.ts index 0d2b3d9dc7d..f772b16801c 100644 --- a/packages/mobile/src/fees/saga.test.ts +++ b/packages/mobile/src/fees/saga.test.ts @@ -1,12 +1,13 @@ import BigNumber from 'bignumber.js' import { expectSaga } from 'redux-saga-test-plan' import * as matchers from 'redux-saga-test-plan/matchers' -import { call } from 'redux-saga/effects' +import { call, select } from 'redux-saga/effects' import { getReclaimEscrowGas } from 'src/escrow/saga' import { feeEstimated, FeeType } from 'src/fees/actions' import { estimateFeeSaga } from 'src/fees/saga' import { getInvitationVerificationFeeInWei, getInviteTxGas } from 'src/invite/saga' import { getSendTxGas } from 'src/send/saga' +import { stableTokenBalanceSelector } from 'src/stableToken/reducer' import { getConnectedAccount } from 'src/web3/saga' import { mockAccount } from 'test/values' @@ -32,6 +33,8 @@ describe(estimateFeeSaga, () => { .provide([ [call(getConnectedAccount), mockAccount], [matchers.call.fn(getInviteTxGas), new BigNumber(GAS_AMOUNT)], + [matchers.call.fn(getInviteTxGas), new BigNumber(GAS_AMOUNT)], + [select(stableTokenBalanceSelector), '1'], ]) .put( feeEstimated( @@ -50,6 +53,7 @@ describe(estimateFeeSaga, () => { .provide([ [call(getConnectedAccount), mockAccount], [matchers.call.fn(getSendTxGas), new BigNumber(GAS_AMOUNT)], + [select(stableTokenBalanceSelector), '1'], ]) .put(feeEstimated(FeeType.SEND, new BigNumber(10000).times(GAS_AMOUNT).toString())) .run() @@ -60,8 +64,19 @@ describe(estimateFeeSaga, () => { .provide([ [call(getConnectedAccount), mockAccount], [matchers.call.fn(getReclaimEscrowGas), new BigNumber(GAS_AMOUNT)], + [select(stableTokenBalanceSelector), '1'], ]) .put(feeEstimated(FeeType.SEND, new BigNumber(10000).times(GAS_AMOUNT).toString())) .run() }) + + it("doesn't calculates fee if the balance is zero", async () => { + await expectSaga(estimateFeeSaga, { feeType: FeeType.SEND }) + .provide([ + [select(stableTokenBalanceSelector), '0'], + [matchers.call.fn(getSendTxGas), new BigNumber(GAS_AMOUNT)], + ]) + .put(feeEstimated(FeeType.SEND, '0')) + .run() + }) }) diff --git a/packages/mobile/src/fees/saga.ts b/packages/mobile/src/fees/saga.ts index 15e6ad2d86a..e861a5d90fb 100644 --- a/packages/mobile/src/fees/saga.ts +++ b/packages/mobile/src/fees/saga.ts @@ -1,6 +1,6 @@ import { getStableTokenContract } from '@celo/walletkit' import BigNumber from 'bignumber.js' -import { call, CallEffect, put, takeLatest } from 'redux-saga/effects' +import { call, CallEffect, put, select, takeLatest } from 'redux-saga/effects' import { showError } from 'src/alert/actions' import { ErrorMessages } from 'src/app/ErrorMessages' import { getReclaimEscrowGas } from 'src/escrow/saga' @@ -8,6 +8,7 @@ import { Actions, EstimateFeeAction, feeEstimated, FeeType } from 'src/fees/acti import { getInvitationVerificationFeeInWei, getInviteTxGas } from 'src/invite/saga' import { getSendTxGas } from 'src/send/saga' import { CeloDefaultRecipient } from 'src/send/Send' +import { stableTokenBalanceSelector } from 'src/stableToken/reducer' import { BasicTokenTransfer } from 'src/tokens/saga' import Logger from 'src/utils/Logger' import { web3 } from 'src/web3/contracts' @@ -27,6 +28,16 @@ const placeholderSendTx: BasicTokenTransfer = { export function* estimateFeeSaga({ feeType }: EstimateFeeAction) { Logger.debug(`${TAG}/estimateFeeSaga`, `updating for ${feeType}`) + + const balance = yield select(stableTokenBalanceSelector) + if (balance === '0') { + Logger.warn(`${TAG}/estimateFeeSaga`, 'Error estimating fee') + yield put(feeEstimated(feeType, '0')) + return + } + + Logger.debug(`${TAG}/estimateFeeSaga`, `balance is ${balance}`) + try { const account = yield call(getConnectedAccount) diff --git a/packages/mobile/src/stableToken/reducer.ts b/packages/mobile/src/stableToken/reducer.ts index 7a70e121ebb..af0a1234679 100644 --- a/packages/mobile/src/stableToken/reducer.ts +++ b/packages/mobile/src/stableToken/reducer.ts @@ -1,3 +1,4 @@ +import { RootState } from 'src/redux/reducers' import { Actions, ActionTypes } from 'src/stableToken/actions' export interface State { @@ -29,3 +30,5 @@ export const reducer = (state: State | undefined = initialState, action: ActionT return state } } + +export const stableTokenBalanceSelector = (state: RootState) => state.stableToken.balance From d96849e089666eb8c8fd5f83061987ea95fc54fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Volpe?= Date: Mon, 11 Nov 2019 13:47:11 +0100 Subject: [PATCH 2/2] Changed comment --- packages/mobile/src/fees/saga.ts | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/packages/mobile/src/fees/saga.ts b/packages/mobile/src/fees/saga.ts index e861a5d90fb..cb6587bc1c1 100644 --- a/packages/mobile/src/fees/saga.ts +++ b/packages/mobile/src/fees/saga.ts @@ -30,8 +30,15 @@ export function* estimateFeeSaga({ feeType }: EstimateFeeAction) { Logger.debug(`${TAG}/estimateFeeSaga`, `updating for ${feeType}`) const balance = yield select(stableTokenBalanceSelector) + + if (!balance) { + Logger.warn(`${TAG}/estimateFeeSaga`, 'Balance is null or empty string') + yield put(feeEstimated(feeType, '0')) + return + } + if (balance === '0') { - Logger.warn(`${TAG}/estimateFeeSaga`, 'Error estimating fee') + Logger.warn(`${TAG}/estimateFeeSaga`, "Can't estimate fee with zero balance") yield put(feeEstimated(feeType, '0')) return }