2018-05-29 20:51:19 +00:00
|
|
|
import React, { Fragment, PureComponent } from 'react';
|
2018-08-15 20:59:36 +00:00
|
|
|
import { connect } from 'react-redux';
|
2018-05-29 20:51:19 +00:00
|
|
|
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-08-15 20:19:13 +00:00
|
|
|
import FormLabel from '@material-ui/core/FormLabel';
|
|
|
|
import FormControl from '@material-ui/core/FormControl';
|
|
|
|
import FormGroup from '@material-ui/core/FormGroup';
|
|
|
|
import FormControlLabel from '@material-ui/core/FormControlLabel';
|
|
|
|
import FormHelperText from '@material-ui/core/FormHelperText';
|
|
|
|
import Switch from '@material-ui/core/Switch';
|
2018-08-15 20:59:36 +00:00
|
|
|
import { actions, getSNTAllowance } from '../../reducers/accounts';
|
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-08-15 20:59:36 +00:00
|
|
|
this.state = {};
|
2018-05-29 20:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
toggleApproved = () => {
|
|
|
|
const { approved } = this.state;
|
2018-08-15 20:59:36 +00:00
|
|
|
const { methods: { approve }, spender, receiveSntAllowance } = 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
|
|
|
|
)
|
2018-08-15 20:19:13 +00:00
|
|
|
.send()
|
|
|
|
.then(approval => {
|
|
|
|
const { events: { Approval: { returnValues: { _value } } } } = approval
|
2018-08-15 20:59:36 +00:00
|
|
|
receiveSntAllowance(_value)
|
|
|
|
this.setState({ ...this.state, updating: false })
|
2018-08-15 20:19:13 +00:00
|
|
|
}).catch(err => {
|
|
|
|
console.log("Approve failed: " + err);
|
|
|
|
this.setState({ updating: false });
|
|
|
|
})
|
2018-05-29 20:51:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-08-15 20:59:36 +00:00
|
|
|
const { symbol, account, isLoading, mobile, SNTAllowance } = this.props;
|
|
|
|
const { updating } = this.state;
|
2018-05-29 20:51:19 +00:00
|
|
|
return (
|
2018-06-13 16:58:19 +00:00
|
|
|
<Fragment>
|
2018-08-15 20:19:13 +00:00
|
|
|
{!updating && !isLoading && !!account && <div>
|
|
|
|
<FormControl component="fieldset">
|
|
|
|
<FormLabel component="legend" style={{ textAlign: 'center' }}>Token Permissions</FormLabel>
|
|
|
|
<FormGroup style={{ alignItems: 'center' }}>
|
|
|
|
<FormControlLabel
|
|
|
|
control={
|
|
|
|
<Switch
|
2018-08-15 20:59:36 +00:00
|
|
|
checked={!!Number(SNTAllowance)}
|
2018-08-15 20:19:13 +00:00
|
|
|
onChange={this.toggleApproved}
|
|
|
|
value={symbol}
|
|
|
|
/>
|
|
|
|
}
|
|
|
|
label={`${Number(fromWei(account[BALANCE_KEYS[symbol]])).toLocaleString()} ${symbol}`}
|
|
|
|
/>
|
|
|
|
</FormGroup>
|
|
|
|
<FormHelperText style={{ textAlign: 'center' }}>Registry needs permission to transfer SNT from your account prior to registration</FormHelperText>
|
|
|
|
</FormControl>
|
|
|
|
</div>}
|
|
|
|
{isLoading || updating && <CircularProgress style={{ marginLeft: mobile ? '45%' : null }} />}
|
2018-06-13 16:58:19 +00:00
|
|
|
</Fragment>
|
2018-05-29 20:51:19 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const TokenPermissions = (props) => (
|
|
|
|
<Fragment>
|
2018-08-15 20:19:13 +00:00
|
|
|
{!props.mobile && <Fragment>
|
|
|
|
<Tooltip title="Turn on permissions for a token to enable its use with the ENS subdomain registry">
|
|
|
|
<h3>Token Permissions</h3>
|
|
|
|
</Tooltip>
|
|
|
|
<hr/>
|
|
|
|
</Fragment>}
|
2018-05-29 20:51:19 +00:00
|
|
|
<TokenHandle {...props} />
|
|
|
|
</Fragment>
|
|
|
|
)
|
|
|
|
|
2018-08-15 20:59:36 +00:00
|
|
|
const mapDispatchToProps = dispatch => ({
|
|
|
|
receiveSntAllowance(amount) {
|
|
|
|
dispatch(actions.receiveSntAllowance(amount));
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
|
SNTAllowance: getSNTAllowance(state),
|
|
|
|
});
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(TokenPermissions);
|