2018-05-29 20:51:19 +00:00
|
|
|
import React, { Fragment, PureComponent } from 'react';
|
|
|
|
import web3 from "Embark/web3"
|
|
|
|
import Toggle from 'react-toggle';
|
|
|
|
import { BigNumber } from './utils'
|
|
|
|
import "react-toggle/style.css";
|
2018-06-15 15:23:54 +00:00
|
|
|
import CircularProgress from '@material-ui/core/CircularProgress';
|
2018-07-11 17:31:33 +00:00
|
|
|
import Tooltip from '@material-ui/core/Tooltip';
|
2018-05-29 20:51:19 +00:00
|
|
|
|
|
|
|
// We set an allowance to be "unlimited" by setting it to
|
|
|
|
// it's maximum possible value -- namely, 2^256 - 1.
|
2018-06-20 15:13:33 +00:00
|
|
|
const { fromWei } = web3.utils;
|
2018-05-29 20:51:19 +00:00
|
|
|
const unlimitedAllowance = new BigNumber(2).pow(256).sub(1);
|
|
|
|
const getDefaultAccount = () => web3.eth.defaultAccount;
|
2018-06-20 15:13:33 +00:00
|
|
|
const SUPPORTED_TOKENS = ['SNT', 'STT'];
|
|
|
|
const BALANCE_KEYS = { 'SNT': 'SNTBalance', 'STT': 'SNTBalance' };
|
2018-05-29 20:51:19 +00:00
|
|
|
|
|
|
|
class TokenHandle extends PureComponent {
|
|
|
|
constructor(props){
|
|
|
|
super(props);
|
2018-06-13 16:58:19 +00:00
|
|
|
this.state = { approved: 0 };
|
2018-05-29 20:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
2018-06-13 16:58:19 +00:00
|
|
|
__embarkContext.execWhenReady(() => {
|
2018-05-29 20:51:19 +00:00
|
|
|
this.getAllowance();
|
2018-06-13 16:58:19 +00:00
|
|
|
});
|
2018-05-29 20:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getAllowance = () => {
|
|
|
|
const { methods, spender } = this.props;
|
|
|
|
methods.allowance(getDefaultAccount(), spender)
|
2018-05-30 19:00:54 +00:00
|
|
|
.call()
|
|
|
|
.then(approved => {
|
|
|
|
this.setState({ ...this.state, approved })
|
|
|
|
})
|
2018-05-29 20:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleApproved = () => {
|
|
|
|
const { approved } = this.state;
|
|
|
|
const { methods: { approve }, spender } = this.props;
|
2018-05-30 19:00:54 +00:00
|
|
|
const isApproved = !!Number(approved);
|
2018-05-31 04:59:31 +00:00
|
|
|
let amountToApprove = isApproved ? 0 : unlimitedAllowance;
|
|
|
|
console.log("approve(\""+spender+"\",\"" +amountToApprove +"\")")
|
2018-06-15 15:23:54 +00:00
|
|
|
this.setState({ updating: true });
|
2018-06-13 16:58:19 +00:00
|
|
|
approve(
|
|
|
|
spender,
|
|
|
|
amountToApprove
|
|
|
|
)
|
|
|
|
.send()
|
|
|
|
.then(approval => {
|
|
|
|
const { events: { Approval: { returnValues: { _value } } } } = approval
|
2018-06-15 15:23:54 +00:00
|
|
|
this.setState({ ...this.state, approved: _value, updating: false })
|
2018-06-13 16:58:19 +00:00
|
|
|
}).catch(err => {
|
|
|
|
console.log("Approve failed: " + err);
|
2018-06-15 15:23:54 +00:00
|
|
|
this.setState({ updating: false });
|
2018-06-13 16:58:19 +00:00
|
|
|
})
|
2018-05-29 20:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-06-13 16:58:19 +00:00
|
|
|
const { symbol, account, isLoading } = this.props;
|
2018-06-15 15:23:54 +00:00
|
|
|
const { approved, updating } = this.state;
|
2018-05-29 20:51:19 +00:00
|
|
|
return (
|
2018-06-13 16:58:19 +00:00
|
|
|
<Fragment>
|
2018-06-15 15:23:54 +00:00
|
|
|
{!updating && !isLoading && !!account && <div style={{ display: 'flex', justifyContent: 'center', margin: '10px', paddingLeft: '60px' }}>
|
2018-05-29 20:51:19 +00:00
|
|
|
<Toggle
|
2018-05-30 19:00:54 +00:00
|
|
|
checked={!!Number(approved)}
|
2018-05-29 20:51:19 +00:00
|
|
|
name={symbol}
|
|
|
|
onChange={this.toggleApproved} />
|
2018-06-20 15:13:33 +00:00
|
|
|
<label style={{ margin: '2px 0px 0px 10px', fontWeight: 400 }}>{`${Number(fromWei(account[BALANCE_KEYS[symbol]])).toLocaleString()} ${symbol.toUpperCase()}`}</label>
|
2018-06-13 16:58:19 +00:00
|
|
|
</div>}
|
2018-06-15 15:23:54 +00:00
|
|
|
{isLoading || updating && <CircularProgress />}
|
2018-06-13 16:58:19 +00:00
|
|
|
</Fragment>
|
2018-05-29 20:51:19 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const TokenPermissions = (props) => (
|
|
|
|
<Fragment>
|
2018-07-11 17:31:33 +00:00
|
|
|
<Tooltip title="Turn on permissions for a token to enable its use with the ENS subdomain registry">
|
2018-05-29 20:51:19 +00:00
|
|
|
<h3>Token Permissions</h3>
|
2018-07-11 17:31:33 +00:00
|
|
|
</Tooltip>
|
2018-05-29 20:51:19 +00:00
|
|
|
<hr/>
|
|
|
|
<TokenHandle {...props} />
|
|
|
|
</Fragment>
|
|
|
|
)
|
|
|
|
|
|
|
|
export default TokenPermissions;
|