Skip to content

Commit

Permalink
Withdraw gatefee in simpledepositwithdraw modal, fix some fee and oth…
Browse files Browse the repository at this point in the history
…er minor issues bitshares#530
  • Loading branch information
svk31 committed Nov 1, 2017
1 parent aa13e9d commit 51ce6af
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 17 deletions.
66 changes: 57 additions & 9 deletions app/components/Dashboard/SimpleDepositWithdraw.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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")
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}
Expand Down Expand Up @@ -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 => {
Expand All @@ -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"));
Expand All @@ -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 (
<div>
<p><Translate content="gateway.withdraw_funds" asset={assetName} /></p>
Expand Down Expand Up @@ -380,6 +414,20 @@ class DepositWithdrawContent extends DecimalChecker {
{feeStatus && !feeStatus.hasBalance ? <p className="has-error no-margin" style={{paddingTop: 10}}><Translate content="transfer.errors.insufficient" /></p>:null}
</div>

<div className="SimpleTrade__withdraw-row">
<label className="left-label">{counterpart.translate("gateway.fee")}</label>
<div className="inline-label input-wrapper">
<input type="text" disabled value={gateFee.getAmount({real: true})} />

<div className="form-label select floating-dropdown">
<div className="dropdown-wrapper inactive">
<div><AssetName name={this.props.asset.get("symbol")} /></div>
</div>
</div>
</div>
{feeStatus && !feeStatus.hasBalance ? <p className="has-error no-margin" style={{paddingTop: 10}}><Translate content="transfer.errors.insufficient" /></p>:null}
</div>

<div className="SimpleTrade__withdraw-row">
<label className="left-label">{counterpart.translate("modal.withdraw.address")}</label>
<div className="inline-label input-wrapper">
Expand Down Expand Up @@ -407,7 +455,7 @@ class DepositWithdrawContent extends DecimalChecker {
) : null}

<div className="button-group SimpleTrade__withdraw-row">
<button tabIndex={tabIndex++} className={"button" + ((feeStatus && !feeStatus.hasBalance) || this.state.balanceError ? " disabled" : "")} onClick={this.onSubmit.bind(this)} type="submit" >
<button tabIndex={tabIndex++} className={"button" + (disableSubmit ? " disabled" : "")} onClick={this.onSubmit.bind(this)} type="submit" >
<Translate content="gateway.withdraw_now" />
</button>
</div>
Expand Down
3 changes: 1 addition & 2 deletions app/components/DepositWithdraw/WithdrawModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class WithdrawModal extends React.Component {
};
}

onWithdrawAmountChange( {amount, asset} ) {
onWithdrawAmountChange( {amount} ) {
this.setState( {withdraw_amount:amount} );
}

Expand All @@ -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"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand All @@ -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
Expand All @@ -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"),
Expand Down Expand Up @@ -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 (<form className="grid-block vertical full-width-content">
<div className="grid-container">
<div className="content-block">
Expand Down Expand Up @@ -547,7 +552,7 @@ class WithdrawModalBlocktrades extends React.Component {
{/* Withdraw/Cancel buttons */}
<div className="button-group">

<div onClick={this.onSubmit.bind(this)} className={"button" + (this.state.error || this.state.balanceError ? (" disabled") : "")}>
<div onClick={this.onSubmit.bind(this)} className={"button" + (disableSubmit ? (" disabled") : "")}>
<Translate content="modal.withdraw.submit" />
</div>

Expand Down
3 changes: 2 additions & 1 deletion app/lib/common/MarketClasses.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}) {
Expand Down
5 changes: 5 additions & 0 deletions app/test/marketTests.js
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 51ce6af

Please sign in to comment.