Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wallet] Doesn't calculate fee on empty balance #1652

Merged
merged 3 commits into from
Nov 11, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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') {
martinvol marked this conversation as resolved.
Show resolved Hide resolved
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