OSDN Git Service

update BigNumber.js error correction.
[bytom/Bytom-Dapp-Demo.git] / src / components / layout / profit / index.jsx
1 import React from 'react'
2 import { FixedLimitProfit } from './action'
3 import GetContractArgs from "../../constants";
4 import BigNumber from 'bignumber.js'
5
6 class Profit extends React.Component {
7
8   constructor(props) {
9     super(props);
10     this.state = {
11       amount: '',
12       address: '',
13       msg:'',
14       error:''
15     };
16
17     this.handleInputChange = this.handleInputChange.bind(this);
18     this.handleSubmit = this.handleSubmit.bind(this);
19   }
20
21   componentDidMount() {
22     if (
23       window.bytom
24       && window.bytom.defaultAccount
25     ) {
26       this.setState({ account: window.bytom.defaultAccount })
27     }
28   }
29
30   handleInputChange(event) {
31     const target = event.target;
32     const value = target.value;
33     const name = target.name;
34
35     this.setState({
36       [name]: value
37     });
38   }
39
40   handleSubmit(event) {
41     event.preventDefault();
42
43     const amount = Number(event.target.amount.value)
44     const account = this.state.account
45     const address = event.target.address.value
46
47     FixedLimitProfit(account, amount, address)
48       .then(()=> {
49         this.setState({
50           error:'',
51           msg:`Submit success!!! you spent ${amount} bill asset, and gain ${BigNumber(amount).multipliedBy(GetContractArgs().radio).toNumber()} deposit asset.`
52         })
53       }).catch(err => {
54       this.setState({
55         error: err,
56         msg:''
57       })
58     })
59   }
60
61   render() {
62     return (
63       <div>
64         <h2>Profit</h2>
65         <div className="mt-3 mb-4">
66           <p className='lead'>Profit should get above the block height {GetContractArgs().dueBlockHeight}.</p>
67           <p className='lead'>Send {this.state.amount} Bill Asset from your chrome extension account <b className="font-weight-bolder text-uppercase">{this.state.account && this.state.account.alias}</b>, and the address {this.state.address} will gain {BigNumber(this.state.amount).multipliedBy(GetContractArgs().radio).toNumber() || ''} Deposit Asset.</p>
68         </div>
69         <form onSubmit={this.handleSubmit}>
70           <div className="form-group">
71             <label>Bill Asset Amount</label>
72             <input
73               type="amount"
74               className="form-control"
75               placeholder="Amount Profit"
76               name="amount"
77               value={this.state.amount}
78               onChange={this.handleInputChange} />
79           </div>
80           <div className="form-group">
81             <label >Address</label>
82             <input
83               type="address"
84               className="form-control"
85               placeholder="Address"
86               name="address"
87               value={this.state.address}
88               onChange={this.handleInputChange} />
89           </div>
90           <p>Fee:  {GetContractArgs().gas} BTM</p>
91           <button type="submit" className="btn btn-primary">Profit to address</button>
92           {this.state.msg && <div className="alert alert-success mt-4" role="alert">
93             {this.state.msg}
94           </div>}
95           {this.state.error && <div className="alert alert-danger mt-4" role="alert">
96             {this.state.error}
97           </div>}
98         </form>
99       </div>
100     )
101   }
102 }
103
104 export default Profit