-
Notifications
You must be signed in to change notification settings - Fork 569
/
Transfer.jsx
430 lines (379 loc) · 17.9 KB
/
Transfer.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
import React from "react";
import BalanceComponent from "../Utility/BalanceComponent";
import AccountActions from "actions/AccountActions";
import Translate from "react-translate-component";
import AccountSelect from "../Forms/AccountSelect";
import AccountSelector from "../Account/AccountSelector";
import AccountStore from "stores/AccountStore";
import AmountSelector from "../Utility/AmountSelector";
import utils from "common/utils";
import counterpart from "counterpart";
import TransactionConfirmStore from "stores/TransactionConfirmStore";
import { RecentTransactions } from "../Account/RecentTransactions";
import Immutable from "immutable";
import {ChainStore} from "bitsharesjs/es";
import {connect} from "alt-react";
class Transfer extends React.Component {
constructor(props) {
super(props);
this.state = Transfer.getInitialState();
let {query} = this.props.location;
if(query.from) {
this.state.from_name = query.from;
ChainStore.getAccount(query.from);
}
if(query.to) {
this.state.to_name = query.to;
ChainStore.getAccount(query.to);
}
if(query.amount) this.state.amount = query.amount;
if(query.asset) {
this.state.asset_id = query.asset;
this.state.asset = ChainStore.getAsset(query.asset);
}
if(query.memo) this.state.memo = query.memo;
let currentAccount = AccountStore.getState().currentAccount;
if (!this.state.from_name) this.state.from_name = currentAccount;
this.onTrxIncluded = this.onTrxIncluded.bind(this);
}
static getInitialState() {
return {
from_name: "",
to_name: "",
from_account: null,
to_account: null,
amount: "",
asset_id: null,
asset: null,
memo: "",
error: null,
propose: false,
propose_account: "",
feeAsset: null,
fee_asset_id: "1.3.0"
};
};
componentWillMount() {
this.nestedRef = null;
}
shouldComponentUpdate(np, ns) {
let { asset_types: current_types } = this._getAvailableAssets();
let { asset_types } = this._getAvailableAssets(ns);
if (asset_types.length === 1) {
let asset = ChainStore.getAsset(asset_types[0]);
if (current_types.length !== 1) {
this.onAmountChanged({amount: ns.amount, asset});
}
if (asset_types[0] !== this.state.fee_asset_id) {
if (asset && this.state.fee_asset_id !== asset_types[0]) {
this.setState({
feeAsset: asset,
fee_asset_id: asset_types[0]
});
}
}
}
return true;
}
componentWillReceiveProps(np) {
if (np.currentAccount !== this.state.from_name && np.currentAccount !== this.props.currentAccount) {
this.setState({
from_name: np.currentAccount,
from_account: ChainStore.getAccount(np.currentAccount)
});
}
}
fromChanged(from_name) {
if (!from_name) this.setState({from_account: null});
this.setState({from_name, error: null, propose: false, propose_account: ""});
}
toChanged(to_name) {
this.setState({to_name, error: null});
}
onFromAccountChanged(from_account) {
this.setState({from_account, error: null});
}
onToAccountChanged(to_account) {
this.setState({to_account, error: null});
}
onAmountChanged({amount, asset}) {
if (!asset) {
return;
}
this.setState({amount, asset, asset_id: asset.get("id"), error: null});
}
onFeeChanged({asset}) {
this.setState({feeAsset: asset, error: null});
}
onMemoChanged(e) {
this.setState({memo: e.target.value});
}
onTrxIncluded(confirm_store_state) {
if(confirm_store_state.included && confirm_store_state.broadcasted_transaction) {
// this.setState(Transfer.getInitialState());
TransactionConfirmStore.unlisten(this.onTrxIncluded);
TransactionConfirmStore.reset();
} else if (confirm_store_state.closed) {
TransactionConfirmStore.unlisten(this.onTrxIncluded);
TransactionConfirmStore.reset();
}
}
onPropose(propose, e) {
e.preventDefault();
this.setState({ propose, propose_account: null });
}
onProposeAccount(propose_account) {
this.setState({ propose_account });
}
onSubmit(e) {
e.preventDefault();
this.setState({error: null});
let asset = this.state.asset;
let precision = utils.get_asset_precision(asset.get("precision"));
let amount = this.state.amount.replace( /,/g, "" );
AccountActions.transfer(
this.state.from_account.get("id"),
this.state.to_account.get("id"),
parseInt(amount * precision, 10),
asset.get("id"),
this.state.memo ? new Buffer(this.state.memo, "utf-8") : this.state.memo,
this.state.propose ? this.state.propose_account : null,
this.state.feeAsset ? this.state.feeAsset.get("id") : "1.3.0"
).then( () => {
TransactionConfirmStore.unlisten(this.onTrxIncluded);
TransactionConfirmStore.listen(this.onTrxIncluded);
}).catch( e => {
let msg = e.message ? e.message.split( '\n' )[1] : null;
console.log( "error: ", e, msg);
this.setState({error: msg});
} );
}
setNestedRef(ref) {
this.nestedRef = ref;
}
_setTotal(asset_id, balance_id, fee, fee_asset_id) {
let balanceObject = ChainStore.getObject(balance_id);
let transferAsset = ChainStore.getObject(asset_id);
if (balanceObject) {
let amount = (utils.get_asset_amount(balanceObject.get("balance"), transferAsset) - (asset_id === fee_asset_id ? fee : 0)).toString();
this.setState({amount});
}
}
_getAvailableAssets(state = this.state) {
const { from_account, from_error } = state;
let asset_types = [], fee_asset_types = [];
if (!(from_account && from_account.get("balances") && !from_error)) {
return {asset_types, fee_asset_types};
}
let account_balances = state.from_account.get("balances").toJS();
asset_types = Object.keys(account_balances).sort(utils.sortID);
fee_asset_types = Object.keys(account_balances).sort(utils.sortID);
for (let key in account_balances) {
let asset = ChainStore.getObject(key);
let balanceObject = ChainStore.getObject(account_balances[key]);
if (balanceObject && balanceObject.get("balance") === 0) {
asset_types.splice(asset_types.indexOf(key), 1);
if (fee_asset_types.indexOf(key) !== -1) {
fee_asset_types.splice(fee_asset_types.indexOf(key), 1);
}
}
if (asset) {
if (asset.get("id") !== "1.3.0" && !utils.isValidPrice(asset.getIn(["options", "core_exchange_rate"]))) {
fee_asset_types.splice(fee_asset_types.indexOf(key), 1);
}
}
}
return {asset_types, fee_asset_types};
}
_onAccountDropdown(account) {
let newAccount = ChainStore.getAccount(account);
if (newAccount) {
this.setState({
from_name: account,
from_account: ChainStore.getAccount(account)
});
}
}
render() {
let from_error = null;
let {propose, from_account, to_account, asset, asset_id, propose_account,
amount, error, to_name, from_name, memo, feeAsset, fee_asset_id} = this.state;
let from_my_account = AccountStore.isMyAccount(from_account) || from_name === this.props.passwordAccount;
if(from_account && ! from_my_account && ! propose ) {
from_error = <span>
{counterpart.translate("account.errors.not_yours")}
(<a onClick={this.onPropose.bind(this, true)}>{counterpart.translate("propose")}</a>)
</span>;
}
let { asset_types, fee_asset_types } = this._getAvailableAssets();
let balance = null;
// Estimate fee
let globalObject = ChainStore.getObject("2.0.0");
let fee = utils.estimateFee(propose ? "proposal_create" : "transfer", null, globalObject);
if (from_account && from_account.get("balances") && !from_error) {
let account_balances = from_account.get("balances").toJS();
// Finish fee estimation
let core = ChainStore.getObject("1.3.0");
if (feeAsset && feeAsset.get("id") !== "1.3.0" && core) {
let price = utils.convertPrice(core, feeAsset.getIn(["options", "core_exchange_rate"]).toJS(), null, feeAsset.get("id"));
fee = utils.convertValue(price, fee, core, feeAsset);
if (parseInt(fee, 10) !== fee) {
fee += 1; // Add 1 to round up;
}
}
if (core) {
fee = utils.limitByPrecision(utils.get_asset_amount(fee, feeAsset || core), feeAsset ? feeAsset.get("precision") : core.get("precision"));
}
if (asset_types.length === 1) asset = ChainStore.getAsset(asset_types[0]);
if (asset_types.length > 0) {
let current_asset_id = asset ? asset.get("id") : asset_types[0];
let feeID = feeAsset ? feeAsset.get("id") : "1.3.0";
balance = (<span style={{borderBottom: "#A09F9F 1px dotted", cursor: "pointer"}} onClick={this._setTotal.bind(this, current_asset_id, account_balances[current_asset_id], fee, feeID)}><Translate component="span" content="transfer.available"/>: <BalanceComponent balance={account_balances[current_asset_id]}/></span>);
} else {
balance = "No funds";
}
} else {
let core = ChainStore.getObject("1.3.0");
fee_asset_types = ["1.3.0"];
if (core) {
fee = utils.limitByPrecision(utils.get_asset_amount(fee, feeAsset || core), feeAsset ? feeAsset.get("precision") : core.get("precision"));
}
}
let propose_incomplete = propose && ! propose_account;
let submitButtonClass = "button float-right no-margin";
if(!from_account || !to_account || !amount || amount === "0"|| !asset || from_error || propose_incomplete)
submitButtonClass += " disabled";
let accountsList = Immutable.Set();
accountsList = accountsList.add(from_account);
let tabIndex = 1;
return (
<div className="grid-block vertical">
<div className="grid-block shrink vertical medium-horizontal" style={{paddingTop: "2rem"}}>
<form style={{paddingBottom: 20, overflow: "visible"}} className="grid-content small-12 medium-6 large-5 large-offset-1 full-width-content" onSubmit={this.onSubmit.bind(this)} noValidate>
<Translate content="transfer.header" component="h2" />
{/* F R O M */}
<div className="content-block">
<AccountSelector label="transfer.from" ref="from"
accountName={from_name}
onChange={this.fromChanged.bind(this)}
onAccountChanged={this.onFromAccountChanged.bind(this)}
account={from_name}
size={60}
error={from_error}
tabIndex={tabIndex++}
onDropdownSelect={this._onAccountDropdown.bind(this)}
dropDownContent={AccountStore.getMyAccounts()}
/>
</div>
{/* T O */}
<div className="content-block">
<AccountSelector
label="transfer.to"
accountName={to_name}
onChange={this.toChanged.bind(this)}
onAccountChanged={this.onToAccountChanged.bind(this)}
account={to_name}
size={60}
tabIndex={tabIndex++}
/>
</div>
{/* A M O U N T */}
<div className="content-block transfer-input">
<AmountSelector
label="transfer.amount"
amount={amount}
onChange={this.onAmountChanged.bind(this)}
asset={asset_types.length > 0 && asset ? asset.get("id") : ( asset_id ? asset_id : asset_types[0])}
assets={asset_types}
display_balance={balance}
tabIndex={tabIndex++}
/>
</div>
{/* M E M O */}
<div className="content-block transfer-input">
{memo && memo.length ? <label className="right-label">{memo.length}</label> : null}
<Translate className="left-label" component="label" content="transfer.memo"/>
<textarea style={{marginBottom: 0}} rows="1" value={memo} tabIndex={tabIndex++} onChange={this.onMemoChanged.bind(this)} />
{/* warning */}
{ this.state.propose ?
<div className="error-area" style={{position: "absolute"}}>
<Translate content="transfer.warn_name_unable_read_memo" name={this.state.from_name} />
</div>
:null}
</div>
{/* F E E */}
<div className={"content-block transfer-input fee-row" + (this.state.propose ? " proposal" : "")}>
<AmountSelector
refCallback={this.setNestedRef.bind(this)}
label="transfer.fee"
disabled={true}
amount={fee}
onChange={this.onFeeChanged.bind(this)}
asset={fee_asset_types.length && feeAsset ? feeAsset.get("id") : ( fee_asset_types.length === 1 ? fee_asset_types[0] : fee_asset_id ? fee_asset_id : fee_asset_types[0])}
assets={fee_asset_types}
tabIndex={tabIndex++}
/>
{propose ?
<button className={submitButtonClass} type="submit" value="Submit" tabIndex={tabIndex++}>
<Translate component="span" content="propose" />
</button> :
<button className={submitButtonClass} type="submit" value="Submit" tabIndex={tabIndex++}>
<Translate component="span" content="transfer.send" />
</button>
}
</div>
{/* P R O P O S E F R O M
Having some proposed transaction logic here (prior to the transaction confirmation)
allows adjusting of the memo to / from parameters.
*/}
{propose ?
<div className="full-width-content form-group transfer-input">
<label className="left-label"><Translate content="account.propose_from" /></label>
<AccountSelect
account_names={AccountStore.getMyAccounts()}
onChange={this.onProposeAccount.bind(this)}
tabIndex={tabIndex++}
/>
</div>:null}
{/* S E N D B U T T O N */}
{error ? <div className="content-block has-error">{error}</div> : null}
<div>
{propose ?
<span>
<button className=" button" onClick={this.onPropose.bind(this, false)} tabIndex={tabIndex++}>
<Translate component="span" content="cancel" />
</button>
</span> :
null}
</div>
{/* TODO: show remaining balance */}
</form>
<div className="grid-content small-12 medium-6 large-4 large-offset-1 right-column">
<div className="grid-content no-padding">
<RecentTransactions
accountsList={accountsList}
limit={25}
compactView={true}
filter="transfer"
fullHeight={true}
/>
</div>
</div>
<div className="grid-content medium-6 large-4">
</div>
</div>
</div>
);
}
}
export default connect(Transfer, {
listenTo() {
return [AccountStore];
},
getProps() {
return {
currentAccount: AccountStore.getState().currentAccount,
passwordAccount: AccountStore.getState().passwordAccount
};
}
});