From 9c9f3c3611aef60f497f5ad7b5cac28861810f8f Mon Sep 17 00:00:00 2001 From: Danny Skubak Date: Wed, 13 Jun 2018 18:54:05 -0400 Subject: [PATCH] [Fix] Estimate Gas on Value Field Change (#1942) * estimate gas on value set * update test, add test, update snapshot --- .../sagas/transaction/current/currentValue.ts | 23 ++++++++++- common/sagas/transaction/network/gas.ts | 9 ++++- .../transaction/current/currentValue.spec.ts | 39 ++++++++++++++++++- spec/sagas/transaction/network/gas.spec.ts | 1 + 4 files changed, 67 insertions(+), 5 deletions(-) diff --git a/common/sagas/transaction/current/currentValue.ts b/common/sagas/transaction/current/currentValue.ts index 56c1f087..a46d2011 100644 --- a/common/sagas/transaction/current/currentValue.ts +++ b/common/sagas/transaction/current/currentValue.ts @@ -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 { diff --git a/common/sagas/transaction/network/gas.ts b/common/sagas/transaction/network/gas.ts index 422a4bf4..fc43c42f 100644 --- a/common/sagas/transaction/network/gas.ts +++ b/common/sagas/transaction/network/gas.ts @@ -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 !== ''; diff --git a/spec/sagas/transaction/current/currentValue.spec.ts b/spec/sagas/transaction/current/currentValue.spec.ts index 7439f50d..f2749914 100644 --- a/spec/sagas/transaction/current/currentValue.spec.ts +++ b/spec/sagas/transaction/current/currentValue.spec.ts @@ -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; diff --git a/spec/sagas/transaction/network/gas.spec.ts b/spec/sagas/transaction/network/gas.spec.ts index 156c2189..0f300518 100644 --- a/spec/sagas/transaction/network/gas.spec.ts +++ b/spec/sagas/transaction/network/gas.spec.ts @@ -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,