[Fix] Estimate Gas on Value Field Change (#1942)

* estimate gas on value set

* update test, add test, update snapshot
This commit is contained in:
Danny Skubak 2018-06-13 18:54:05 -04:00 committed by Daniel Ternyak
parent 36faee932f
commit 9c9f3c3611
4 changed files with 67 additions and 5 deletions

View File

@ -1,3 +1,5 @@
import BN from 'bn.js';
import {
isEtherTransaction,
getUnit,
@ -50,7 +52,26 @@ export function* revalidateCurrentValue(): SagaIterator {
return yield put(setter({ raw: currVal.raw, value: null }));
}
const isValid: boolean = yield call(validateInput, reparsedValue.value, unit);
yield put(setter({ raw: reparsedValue.raw, value: isValid ? reparsedValue.value : null }));
const newVal = { raw: reparsedValue.raw, value: isValid ? reparsedValue.value : null };
if (isValueDifferent(currVal, newVal)) {
yield put(setter(newVal));
}
}
export function isValueDifferent(curVal: ICurrentValue, newVal: ICurrentValue) {
const val1 = curVal.value as BN;
const val2 = newVal.value as BN;
if (curVal.raw !== newVal.raw) {
return true;
}
if (BN.isBN(val1) && BN.isBN(val2)) {
return !val1.eq(val2);
}
if (curVal.value !== newVal.value) {
return true;
}
}
export function* reparseCurrentValue(value: IInput): SagaIterator {

View File

@ -24,6 +24,7 @@ import {
estimateGasRequested,
SetToFieldAction,
SetDataFieldAction,
SetValueFieldAction,
SwapEtherToTokenAction,
SwapTokenToTokenAction,
SwapTokenToEtherAction,
@ -44,8 +45,10 @@ export function* shouldEstimateGas(): SagaIterator {
| SwapEtherToTokenAction
| SwapTokenToTokenAction
| SwapTokenToEtherAction
| ToggleAutoGasLimitAction = yield take([
| ToggleAutoGasLimitAction
| SetValueFieldAction = yield take([
TypeKeys.TO_FIELD_SET,
TypeKeys.VALUE_FIELD_SET,
TypeKeys.DATA_FIELD_SET,
TypeKeys.ETHER_TO_TOKEN_SWAP,
TypeKeys.TOKEN_TO_TOKEN_SWAP,
@ -64,7 +67,9 @@ export function* shouldEstimateGas(): SagaIterator {
// invalid field is a field that the value is null and the input box isnt empty
// reason being is an empty field is valid because it'll be null
const invalidField =
(action.type === TypeKeys.TO_FIELD_SET || action.type === TypeKeys.DATA_FIELD_SET) &&
(action.type === TypeKeys.TO_FIELD_SET ||
action.type === TypeKeys.DATA_FIELD_SET ||
action.type === TypeKeys.VALUE_FIELD_SET) &&
!action.payload.value &&
action.payload.raw !== '';

View File

@ -1,4 +1,12 @@
import { isEtherTransaction, getUnit, getDecimal, getCurrentValue } from 'selectors/transaction';
import BN from 'bn.js';
import {
isEtherTransaction,
getUnit,
getDecimal,
getCurrentValue,
ICurrentValue
} from 'selectors/transaction';
import { select, call, put } from 'redux-saga/effects';
import { setTokenValue, setValueField } from 'actions/transaction/actionCreators';
import { toTokenBase } from 'libs/units';
@ -7,7 +15,8 @@ import {
setCurrentValue,
revalidateCurrentValue,
reparseCurrentValue,
valueHandler
valueHandler,
isValueDifferent
} from 'sagas/transaction/current/currentValue';
import { cloneableGenerator, SagaIteratorClone } from 'redux-saga/utils';
import { SagaIterator } from 'redux-saga';
@ -224,6 +233,32 @@ describe('revalidateCurrentValue*', () => {
});
});
describe('isValueDifferent', () => {
it('should be truthy when raw differs', () => {
const curVal: ICurrentValue = { raw: 'a', value: new BN(0) };
const newVal: ICurrentValue = { raw: 'b', value: new BN(0) };
expect(isValueDifferent(curVal, newVal)).toBeTruthy();
});
it('should be falsy when value is the same BN', () => {
const curVal: ICurrentValue = { raw: '', value: new BN(1) };
const newVal: ICurrentValue = { raw: '', value: new BN(1) };
expect(isValueDifferent(curVal, newVal)).toBeFalsy();
});
it('should be truthy when value is a different BN', () => {
const curVal: ICurrentValue = { raw: '', value: new BN(1) };
const newVal: ICurrentValue = { raw: '', value: new BN(2) };
expect(isValueDifferent(curVal, newVal)).toBeTruthy();
});
it('should be truthy when value is not the same and not both BNs', () => {
const curVal: ICurrentValue = { raw: '', value: new BN(1) };
const newVal: ICurrentValue = { raw: '', value: null };
expect(isValueDifferent(curVal, newVal)).toBeTruthy();
});
});
describe('reparseCurrentValue*', () => {
const decimal = 5;

View File

@ -56,6 +56,7 @@ describe('shouldEstimateGas*', () => {
expect(gen.next().value).toEqual(
take([
TypeKeys.TO_FIELD_SET,
TypeKeys.VALUE_FIELD_SET,
TypeKeys.DATA_FIELD_SET,
TypeKeys.ETHER_TO_TOKEN_SWAP,
TypeKeys.TOKEN_TO_TOKEN_SWAP,