From 51ce6af921f6048a1ba373503399ffa5420acfb8 Mon Sep 17 00:00:00 2001 From: Sigve Kvalsvik Date: Wed, 1 Nov 2017 13:12:36 +0100 Subject: [PATCH] Withdraw gatefee in simpledepositwithdraw modal, fix some fee and other minor issues #530 --- .../Dashboard/SimpleDepositWithdraw.jsx | 66 ++++++++++++++++--- .../DepositWithdraw/WithdrawModal.jsx | 3 +- .../blocktrades/WithdrawModalBlocktrades.jsx | 15 +++-- app/lib/common/MarketClasses.js | 3 +- app/test/marketTests.js | 5 ++ 5 files changed, 75 insertions(+), 17 deletions(-) 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 (

@@ -380,6 +414,20 @@ class DepositWithdrawContent extends DecimalChecker { {feeStatus && !feeStatus.hasBalance ?

:null}
+
+ +
+ + +
+
+
+
+
+
+ {feeStatus && !feeStatus.hasBalance ?

:null} +
+
@@ -407,7 +455,7 @@ class DepositWithdrawContent extends DecimalChecker { ) : null}
-
diff --git a/app/components/DepositWithdraw/WithdrawModal.jsx b/app/components/DepositWithdraw/WithdrawModal.jsx index 050614a700..947b6f233d 100644 --- a/app/components/DepositWithdraw/WithdrawModal.jsx +++ b/app/components/DepositWithdraw/WithdrawModal.jsx @@ -28,7 +28,7 @@ class WithdrawModal extends React.Component { }; } - onWithdrawAmountChange( {amount, asset} ) { + onWithdrawAmountChange( {amount} ) { this.setState( {withdraw_amount:amount} ); } @@ -40,7 +40,6 @@ class WithdrawModal extends React.Component { let asset = this.props.asset; let precision = utils.get_asset_precision(asset.get("precision")); let amount = this.state.withdraw_amount.replace( /,/g, "" ); - console.log( "withdraw_amount: ", amount ); AccountActions.transfer( this.props.account.get("id"), this.props.issuer.get("id"), diff --git a/app/components/DepositWithdraw/blocktrades/WithdrawModalBlocktrades.jsx b/app/components/DepositWithdraw/blocktrades/WithdrawModalBlocktrades.jsx index ea1b45d94d..4bdd92bb02 100644 --- a/app/components/DepositWithdraw/blocktrades/WithdrawModalBlocktrades.jsx +++ b/app/components/DepositWithdraw/blocktrades/WithdrawModalBlocktrades.jsx @@ -221,8 +221,8 @@ class WithdrawModalBlocktrades extends React.Component { const {feeAmount} = this.state; - let amount = parseFloat(String.prototype.replace.call(this.state.withdraw_amount, /,/g, "")); - let gateFee = parseFloat(String.prototype.replace.call(this.props.gateFee, /,/g, "")); + const amount = parseFloat(String.prototype.replace.call(this.state.withdraw_amount, /,/g, "")); + const gateFee = parseFloat(String.prototype.replace.call(this.props.gateFee, /,/g, "")); let sendAmount = new Asset({ asset_id: asset.get("id"), @@ -232,7 +232,7 @@ class WithdrawModalBlocktrades extends React.Component { let balanceAmount = sendAmount.clone(this.props.balance.get("balance")); - let gateFeeAmount = new Asset({ + const gateFeeAmount = new Asset({ asset_id: asset.get("id"), precision: asset.get("precision"), real: gateFee @@ -244,7 +244,7 @@ class WithdrawModalBlocktrades extends React.Component { if (balanceAmount.lt(sendAmount)) { sendAmount = balanceAmount; } - + AccountActions.transfer( this.props.account.get("id"), this.props.issuer.get("id"), @@ -475,6 +475,11 @@ class WithdrawModalBlocktrades extends React.Component { balance = "No funds"; } + const disableSubmit = + this.state.error || + this.state.balanceError || + !this.state.withdraw_amount; + return (
@@ -547,7 +552,7 @@ class WithdrawModalBlocktrades extends React.Component { {/* Withdraw/Cancel buttons */}
-
+
diff --git a/app/lib/common/MarketClasses.js b/app/lib/common/MarketClasses.js index eb2e1b0744..ff1e3329f6 100644 --- a/app/lib/common/MarketClasses.js +++ b/app/lib/common/MarketClasses.js @@ -48,7 +48,8 @@ class Asset { } toSats(amount = 1) { // Return the full integer amount in 'satoshis' - return Math.floor(amount * this.satoshi); + // Round to prevent floating point math errors + return Math.round(amount * this.satoshi); } setAmount({sats, real}) { diff --git a/app/test/marketTests.js b/app/test/marketTests.js index 44d170c784..e8fdd29e86 100644 --- a/app/test/marketTests.js +++ b/app/test/marketTests.js @@ -52,6 +52,11 @@ describe("Asset", function() { assert.equal(asset.asset_id, "1.3.0", "Asset should be 1.3.0"); assert.equal(asset.amount, 100000, "Amount should be 242"); assert.equal(asset.satoshi, 100000, "Satoshi should be 10000"); + + let asset2 = new Asset({asset_id: "1.3.861", real: "0.00030", precision: 8}); + assert.equal(asset2.asset_id, "1.3.861", "Asset should be 1.3.861"); + assert.equal(asset2.amount, 30000, "Amount should be 30000"); + assert.equal(asset2.satoshi, 100000000, "Satoshi should be 100000000"); }); it("Can be added", function() {