helpers.ts
1 import { AppState } from './reducers'; 2 import * as configSelectors from './config/selectors'; 3 import { ICurrentTo, ICurrentValue, TransactionFieldValues, TransactionFields } from './types'; 4 5 export const reduceToValues = (transactionFields: AppState['transaction']['fields']) => 6 Object.keys(transactionFields).reduce<TransactionFieldValues>( 7 (obj, currFieldName: keyof TransactionFields) => { 8 const currField = transactionFields[currFieldName]; 9 return { ...obj, [currFieldName]: currField.value }; 10 }, 11 {} as TransactionFieldValues 12 ); 13 14 export const isFullTx = ( 15 state: AppState, 16 transactionFields: AppState['transaction']['fields'], 17 currentTo: ICurrentTo, 18 currentValue: ICurrentValue, 19 dataExists: boolean, 20 validGasCost: boolean, 21 unit: string // if its ether, we can have empty data, if its a token, we cant have value 22 ) => { 23 const { data, value, to, ...rest } = transactionFields; 24 const partialParamsToCheck = { ...rest }; 25 26 const validPartialParams = Object.values(partialParamsToCheck).reduce<boolean>( 27 (isValid, v: AppState['transaction']['fields'] & ICurrentTo & ICurrentValue) => 28 isValid && !!v.value, 29 true 30 ); 31 32 if (configSelectors.isNetworkUnit(state, unit)) { 33 // if theres data we can have no current value, and we dont have to check for a to address 34 if (dataExists && validGasCost && !currentValue.value && currentValue.raw === '') { 35 return validPartialParams; 36 } else if (dataExists && validGasCost && !to.value && to.raw === '') { 37 // same goes for value transactions to 0x 38 return !!(validPartialParams && currentValue.value); 39 } else { 40 // otherwise we require value 41 return !!(validPartialParams && currentValue.value && to.value && currentTo.value); 42 } 43 } else { 44 return !!( 45 validPartialParams && 46 data.value && 47 !value.value && 48 currentValue.value && 49 to.value && 50 currentTo.value 51 ); 52 } 53 };