Browse Source

Fix Issue 1448

Renaming checkCanBeSpent to checkValidTransaction, dropping isValidTransaction

Added the isZero check from the removed method, removed the dead code from bridges
gre-patch-1
Juan Cortes Ross 7 years ago
parent
commit
132e8ee686
No known key found for this signature in database GPG Key ID: 34A99C03E9455EB8
  1. 6
      src/bridge/EthereumJSBridge.js
  2. 10
      src/bridge/LibcoreBridge.js
  3. 6
      src/bridge/RippleJSBridge.js
  4. 4
      src/bridge/UnsupportedBridge.js
  5. 8
      src/bridge/makeMockBridge.js
  6. 7
      src/bridge/types.js
  7. 8
      src/components/RequestAmount/index.js
  8. 16
      src/components/modals/Send/fields/AmountField.js
  9. 10
      src/components/modals/Send/steps/01-step-amount.js

6
src/bridge/EthereumJSBridge.js

@ -420,15 +420,13 @@ const EthereumBridge: WalletBridge<Transaction> = {
getTransactionRecipient: (a, t) => t.recipient, getTransactionRecipient: (a, t) => t.recipient,
isValidTransaction: (a, t) => (!t.amount.isZero() && t.recipient && true) || false,
EditFees, EditFees,
EditAdvancedOptions, EditAdvancedOptions,
checkCanBeSpent: (a, t) => checkValidTransaction: (a, t) =>
t.amount.isLessThanOrEqualTo(a.balance) t.amount.isLessThanOrEqualTo(a.balance)
? Promise.resolve() ? Promise.resolve(true)
: Promise.reject(new NotEnoughBalance()), : Promise.reject(new NotEnoughBalance()),
getTotalSpent: (a, t) => getTotalSpent: (a, t) =>

10
src/bridge/LibcoreBridge.js

@ -95,11 +95,11 @@ const getFees = async (a, transaction) => {
return promise return promise
} }
const checkCanBeSpent = (a, t) => const checkValidTransaction = (a, t) =>
!t.amount !t.amount
? Promise.resolve() ? Promise.resolve(true)
: getFees(a, t) : getFees(a, t)
.then(() => {}) .then(() => true)
.catch(e => { .catch(e => {
if (e.code === NOT_ENOUGH_FUNDS) { if (e.code === NOT_ENOUGH_FUNDS) {
throw new NotEnoughBalance() throw new NotEnoughBalance()
@ -192,9 +192,7 @@ const LibcoreBridge: WalletBridge<Transaction> = {
// EditAdvancedOptions, // EditAdvancedOptions,
isValidTransaction: (a, t) => (!t.amount.isZero() && t.recipient && true) || false, checkValidTransaction,
checkCanBeSpent,
getTotalSpent: (a, t) => getTotalSpent: (a, t) =>
t.amount.isZero() t.amount.isZero()

6
src/bridge/RippleJSBridge.js

@ -474,9 +474,7 @@ const RippleJSBridge: WalletBridge<Transaction> = {
getTransactionRecipient: (a, t) => t.recipient, getTransactionRecipient: (a, t) => t.recipient,
isValidTransaction: (a, t) => (!t.amount.isZero() && t.recipient && true) || false, checkValidTransaction: async (a, t) => {
checkCanBeSpent: async (a, t) => {
const r = await getServerInfo(a.endpointConfig) const r = await getServerInfo(a.endpointConfig)
if ( if (
t.amount t.amount
@ -484,7 +482,7 @@ const RippleJSBridge: WalletBridge<Transaction> = {
.plus(parseAPIValue(r.validatedLedger.reserveBaseXRP)) .plus(parseAPIValue(r.validatedLedger.reserveBaseXRP))
.isLessThanOrEqualTo(a.balance) .isLessThanOrEqualTo(a.balance)
) { ) {
return return true
} }
throw new NotEnoughBalance() throw new NotEnoughBalance()
}, },

4
src/bridge/UnsupportedBridge.js

@ -27,13 +27,11 @@ const UnsupportedBridge: WalletBridge<*> = {
getTransactionAmount: () => BigNumber(0), getTransactionAmount: () => BigNumber(0),
isValidTransaction: () => false,
editTransactionRecipient: () => null, editTransactionRecipient: () => null,
getTransactionRecipient: () => '', getTransactionRecipient: () => '',
checkCanBeSpent: () => Promise.resolve(), checkValidTransaction: () => Promise.resolve(false),
getTotalSpent: () => Promise.resolve(BigNumber(0)), getTotalSpent: () => Promise.resolve(BigNumber(0)),

8
src/bridge/makeMockBridge.js

@ -18,7 +18,7 @@ const defaultOpts = {
scanAccountDeviceSuccessRate: 0.8, scanAccountDeviceSuccessRate: 0.8,
transactionsSizeTarget: 100, transactionsSizeTarget: 100,
extraInitialTransactionProps: () => null, extraInitialTransactionProps: () => null,
checkCanBeSpent: () => Promise.resolve(), checkValidTransaction: () => Promise.resolve(),
getTotalSpent: (a, t) => Promise.resolve(t.amount), getTotalSpent: (a, t) => Promise.resolve(t.amount),
getMaxAmount: a => Promise.resolve(a.balance), getMaxAmount: a => Promise.resolve(a.balance),
} }
@ -36,7 +36,7 @@ function makeMockBridge(opts?: Opts): WalletBridge<*> {
extraInitialTransactionProps, extraInitialTransactionProps,
getTotalSpent, getTotalSpent,
getMaxAmount, getMaxAmount,
checkCanBeSpent, checkValidTransaction,
} = { } = {
...defaultOpts, ...defaultOpts,
...opts, ...opts,
@ -155,9 +155,7 @@ function makeMockBridge(opts?: Opts): WalletBridge<*> {
EditAdvancedOptions, EditAdvancedOptions,
isValidTransaction: (a, t) => (t.amount > 0 && t.recipient && true) || false, checkValidTransaction,
checkCanBeSpent,
getTotalSpent, getTotalSpent,

7
src/bridge/types.js

@ -76,15 +76,16 @@ export interface WalletBridge<Transaction> {
getTransactionRecipient(account: Account, transaction: Transaction): string; getTransactionRecipient(account: Account, transaction: Transaction): string;
isValidTransaction(account: Account, transaction: Transaction): boolean;
// render the whole Fees section of the form // render the whole Fees section of the form
EditFees?: *; // React$ComponentType<EditProps<Transaction>>; EditFees?: *; // React$ComponentType<EditProps<Transaction>>;
// render the whole advanced part of the form // render the whole advanced part of the form
EditAdvancedOptions?: *; // React$ComponentType<EditProps<Transaction>>; EditAdvancedOptions?: *; // React$ComponentType<EditProps<Transaction>>;
checkCanBeSpent(account: Account, transaction: Transaction): Promise<void>; // validate the transaction and all currency specific validations here, we can return false
// to disable the button without throwing an error if we are handling the error on a different
// input or throw an error that will highlight the issue on the amount field
checkValidTransaction(account: Account, transaction: Transaction): Promise<boolean>;
getTotalSpent(account: Account, transaction: Transaction): Promise<BigNumber>; getTotalSpent(account: Account, transaction: Transaction): Promise<BigNumber>;

8
src/components/RequestAmount/index.js

@ -48,7 +48,7 @@ type OwnProps = {
// left value (always the one which is returned) // left value (always the one which is returned)
value: BigNumber, value: BigNumber,
canBeSpentError: ?Error, validTransactionError: ?Error,
// max left value // max left value
max: BigNumber, max: BigNumber,
@ -113,7 +113,7 @@ const mapStateToProps = (state: State, props: OwnProps) => {
export class RequestAmount extends PureComponent<Props> { export class RequestAmount extends PureComponent<Props> {
static defaultProps = { static defaultProps = {
max: BigNumber(Infinity), max: BigNumber(Infinity),
canBeSpent: true, validTransaction: true,
withMax: true, withMax: true,
} }
@ -139,14 +139,14 @@ export class RequestAmount extends PureComponent<Props> {
renderInputs(containerProps: Object) { renderInputs(containerProps: Object) {
// TODO move this inlined into render() for less spaghetti // TODO move this inlined into render() for less spaghetti
const { value, account, rightCurrency, getCounterValue, canBeSpentError } = this.props const { value, account, rightCurrency, getCounterValue, validTransactionError } = this.props
const right = getCounterValue(value) || BigNumber(0) const right = getCounterValue(value) || BigNumber(0)
const rightUnit = rightCurrency.units[0] const rightUnit = rightCurrency.units[0]
// FIXME: no way InputCurrency pure can work here. inlined InputRight (should be static func?), inline containerProps object.. // FIXME: no way InputCurrency pure can work here. inlined InputRight (should be static func?), inline containerProps object..
return ( return (
<Box horizontal grow shrink> <Box horizontal grow shrink>
<InputCurrency <InputCurrency
error={canBeSpentError} error={validTransactionError}
containerProps={containerProps} containerProps={containerProps}
defaultUnit={account.unit} defaultUnit={account.unit}
value={value} value={value}

16
src/components/modals/Send/fields/AmountField.js

@ -4,9 +4,9 @@ import Box from 'components/base/Box'
import Label from 'components/base/Label' import Label from 'components/base/Label'
import RequestAmount from 'components/RequestAmount' import RequestAmount from 'components/RequestAmount'
class AmountField extends Component<*, { canBeSpentError: ?Error }> { class AmountField extends Component<*, { validTransactionError: ?Error }> {
state = { state = {
canBeSpentError: null, validTransactionError: null,
} }
componentDidMount() { componentDidMount() {
this.resync() this.resync()
@ -27,11 +27,11 @@ class AmountField extends Component<*, { canBeSpentError: ?Error }> {
const { account, bridge, transaction } = this.props const { account, bridge, transaction } = this.props
const syncId = ++this.syncId const syncId = ++this.syncId
try { try {
await bridge.checkCanBeSpent(account, transaction) await bridge.checkValidTransaction(account, transaction)
if (this.syncId !== syncId) return if (this.syncId !== syncId) return
this.setState({ canBeSpentError: null }) this.setState({ validTransactionError: null })
} catch (canBeSpentError) { } catch (validTransactionError) {
this.setState({ canBeSpentError }) this.setState({ validTransactionError })
} }
} }
@ -42,14 +42,14 @@ class AmountField extends Component<*, { canBeSpentError: ?Error }> {
render() { render() {
const { bridge, account, transaction, t } = this.props const { bridge, account, transaction, t } = this.props
const { canBeSpentError } = this.state const { validTransactionError } = this.state
return ( return (
<Box flow={1}> <Box flow={1}>
<Label>{t('app:send.steps.amount.amount')}</Label> <Label>{t('app:send.steps.amount.amount')}</Label>
<RequestAmount <RequestAmount
withMax={false} withMax={false}
account={account} account={account}
canBeSpentError={canBeSpentError} validTransactionError={validTransactionError}
onChange={this.onChange} onChange={this.onChange}
value={bridge.getTransactionAmount(account, transaction)} value={bridge.getTransactionAmount(account, transaction)}
/> />

10
src/components/modals/Send/steps/01-step-amount.js

@ -134,11 +134,13 @@ export class StepAmountFooter extends PureComponent<
bridge.getTransactionRecipient(account, transaction), bridge.getTransactionRecipient(account, transaction),
) )
if (syncId !== this.syncId) return if (syncId !== this.syncId) return
const canBeSpent = await bridge const isValidTransaction = await bridge
.checkCanBeSpent(account, transaction) .checkValidTransaction(account, transaction)
.then(() => true, () => false) .then(result => result, () => false)
if (syncId !== this.syncId) return if (syncId !== this.syncId) return
const canNext = isRecipientValid && canBeSpent && totalSpent.gt(0) const canNext =
!transaction.amount.isZero() && isRecipientValid && isValidTransaction && totalSpent.gt(0)
this.setState({ totalSpent, canNext, isSyncing: false }) this.setState({ totalSpent, canNext, isSyncing: false })
} catch (err) { } catch (err) {
logger.critical(err) logger.critical(err)

Loading…
Cancel
Save