Skip to content

Commit

Permalink
[wallet] Doesn't calculate fee on empty balance (#1652)
Browse files Browse the repository at this point in the history
  • Loading branch information
martinvol committed Nov 11, 2019
1 parent 95f7e35 commit 9d315b5
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
17 changes: 16 additions & 1 deletion packages/mobile/src/fees/saga.test.ts
Original file line number Diff line number Diff line change
@@ -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'

Expand All @@ -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(
Expand All @@ -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()
Expand All @@ -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()
})
})
20 changes: 19 additions & 1 deletion packages/mobile/src/fees/saga.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
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'
import { Actions, EstimateFeeAction, feeEstimated, FeeType } from 'src/fees/actions'
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'
Expand All @@ -27,6 +28,23 @@ const placeholderSendTx: BasicTokenTransfer = {

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`, "Can't estimate fee with zero balance")
yield put(feeEstimated(feeType, '0'))
return
}

Logger.debug(`${TAG}/estimateFeeSaga`, `balance is ${balance}`)

try {
const account = yield call(getConnectedAccount)

Expand Down
3 changes: 3 additions & 0 deletions packages/mobile/src/stableToken/reducer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { RootState } from 'src/redux/reducers'
import { Actions, ActionTypes } from 'src/stableToken/actions'

export interface State {
Expand Down Expand Up @@ -29,3 +30,5 @@ export const reducer = (state: State | undefined = initialState, action: ActionT
return state
}
}

export const stableTokenBalanceSelector = (state: RootState) => state.stableToken.balance

0 comments on commit 9d315b5

Please sign in to comment.