diff --git a/app/components/Dashboard/SimpleDepositWithdraw.jsx b/app/components/Dashboard/SimpleDepositWithdraw.jsx index ffc6c64194..e129abe893 100644 --- a/app/components/Dashboard/SimpleDepositWithdraw.jsx +++ b/app/components/Dashboard/SimpleDepositWithdraw.jsx @@ -19,6 +19,7 @@ import AssetName from "../Utility/AssetName"; import { ChainStore } from "bitsharesjs/es"; import { debounce } from "lodash"; import {DecimalChecker} from "../Exchange/ExchangeInput"; +import { blockTradesAPIs } from "api/apiConfig"; // import DepositFiatOpenLedger from "components/DepositWithdraw/openledger/DepositFiatOpenLedger"; // import WithdrawFiatOpenLedger from "components/DepositWithdraw/openledger/WithdrawFiatOpenLedger"; @@ -39,13 +40,12 @@ class DepositWithdrawContent extends DecimalChecker { constructor(props) { super(); + console.log("constructor"); this.state = { toAddress: WithdrawAddresses.getLast(props.walletType), withdrawValue:"", amountError: null, symbol:props.asset.get("symbol"), - intermediateAccount: props.asset.get("intermediateAccount"), - gateFee: props.asset.get("gateFee"), to_withdraw: new Asset({ asset_id: props.asset.get("id"), precision: props.asset.get("precision") @@ -73,7 +73,7 @@ class DepositWithdrawContent extends DecimalChecker { } componentWillReceiveProps(np) { - if (np.asset && np.asset !== this.props.asset) { + if ((np.asset && this.props.asset) && np.asset.get("id") !== this.props.asset.get("id")) { this.setState({ to_withdraw: new Asset({ asset_id: np.asset.get("id"), @@ -146,7 +146,21 @@ class DepositWithdrawContent extends DecimalChecker { if (!this.props.intermediateAccount) return; - let fee = this._getFee(); + const fee = this._getFee(); + const gateFee = this._getGateFee(); + + let sendAmount = this.state.to_withdraw.clone(); + + let balanceAmount = sendAmount.clone(this._getCurrentBalance().get("balance")); + + sendAmount.plus(gateFee); + + /* Insufficient balance */ + if (balanceAmount.lt(sendAmount)) { + // Send the originally entered amount + sendAmount = this.state.to_withdraw.clone(); + } + AccountActions.transfer( this.props.sender.get("id"), this.props.intermediateAccount, @@ -244,9 +258,11 @@ class DepositWithdrawContent extends DecimalChecker { const {asset} = this.props; const balance = this._getCurrentBalance(); if (!balance || !feeAmount) return; - const hasBalance = checkBalance(to_withdraw.getAmount({real: true}), asset, feeAmount, balance); + const hasBalance = checkBalance(to_withdraw.getAmount({real: true}), asset, this._getFee(), balance, this._getGateFee()); if (hasBalance === null) return; - this.setState({balanceError: !hasBalance}); + if (this.state.balanceError !== !hasBalance) + this.setState({balanceError: !hasBalance}); + return hasBalance; } @@ -257,7 +273,9 @@ class DepositWithdrawContent extends DecimalChecker { const coreStatus = this.state.feeStatus["1.3.0"]; const withdrawAssetStatus = this.state.feeStatus[this.state.to_withdraw.asset_id]; + // Use core asset to pay the fees if present and balance is sufficient since it's cheapest if (coreStatus && coreStatus.hasBalance) return coreStatus.fee; + // Use same asset as withdraw if not if (coreStatus && !coreStatus.hasBalance && withdrawAssetStatus && withdrawAssetStatus.hasBalance) { return withdrawAssetStatus.fee; } @@ -296,12 +314,12 @@ class DepositWithdrawContent extends DecimalChecker { } _validateAddress(address, props = this.props) { - validateAddress({walletType: props.walletType, newAddress: address}) + validateAddress({url: blockTradesAPIs.BASE_OL, walletType: props.walletType, newAddress: address}) .then(isValid => { if (this.state.toAddress === address) { this.setState({ withdraw_address_check_in_progress: false, - validAddress: isValid + validAddress: !!isValid }); } }).catch(err => { @@ -315,6 +333,15 @@ class DepositWithdrawContent extends DecimalChecker { newWnd.opener = null; } + _getGateFee() { + const {gateFee, asset} = this.props; + return new Asset({ + real: parseFloat(gateFee ? gateFee.replace(",", "") : 0), + asset_id: asset.get("id"), + precision: asset.get("precision") + }); + } + _renderWithdraw() { const {amountError} = this.state; const {name: assetName} = utils.replaceName(this.props.asset.get("symbol"), !!this.props.asset.get("bitasset")); @@ -336,9 +363,16 @@ class DepositWithdrawContent extends DecimalChecker { // } const currentFee = this._getFee(); + const gateFee = this._getGateFee(); const feeStatus = this.state.feeStatus[currentFee.asset_id]; const feeAsset = ChainStore.getAsset(currentFee.asset_id); + const disableSubmit = + (feeStatus && !feeStatus.hasBalance) || + this.state.balanceError || + !this.state.toAddress || + !this.state.withdrawValue; + return (