update token permissions to use redux for SNT allowance
This commit is contained in:
parent
7f1540d387
commit
ccccd099d0
|
@ -1,4 +1,5 @@
|
||||||
import React, { Fragment, PureComponent } from 'react';
|
import React, { Fragment, PureComponent } from 'react';
|
||||||
|
import { connect } from 'react-redux';
|
||||||
import web3 from "Embark/web3"
|
import web3 from "Embark/web3"
|
||||||
import Toggle from 'react-toggle';
|
import Toggle from 'react-toggle';
|
||||||
import { BigNumber } from './utils'
|
import { BigNumber } from './utils'
|
||||||
|
@ -11,6 +12,7 @@ import FormGroup from '@material-ui/core/FormGroup';
|
||||||
import FormControlLabel from '@material-ui/core/FormControlLabel';
|
import FormControlLabel from '@material-ui/core/FormControlLabel';
|
||||||
import FormHelperText from '@material-ui/core/FormHelperText';
|
import FormHelperText from '@material-ui/core/FormHelperText';
|
||||||
import Switch from '@material-ui/core/Switch';
|
import Switch from '@material-ui/core/Switch';
|
||||||
|
import { actions, getSNTAllowance } from '../../reducers/accounts';
|
||||||
|
|
||||||
// We set an allowance to be "unlimited" by setting it to
|
// We set an allowance to be "unlimited" by setting it to
|
||||||
// it's maximum possible value -- namely, 2^256 - 1.
|
// it's maximum possible value -- namely, 2^256 - 1.
|
||||||
|
@ -23,27 +25,12 @@ const BALANCE_KEYS = { 'SNT': 'SNTBalance', 'STT': 'SNTBalance' };
|
||||||
class TokenHandle extends PureComponent {
|
class TokenHandle extends PureComponent {
|
||||||
constructor(props){
|
constructor(props){
|
||||||
super(props);
|
super(props);
|
||||||
this.state = { approved: 0 };
|
this.state = {};
|
||||||
}
|
|
||||||
|
|
||||||
componentDidMount() {
|
|
||||||
__embarkContext.execWhenReady(() => {
|
|
||||||
this.getAllowance();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
getAllowance = () => {
|
|
||||||
const { methods, spender } = this.props;
|
|
||||||
methods.allowance(getDefaultAccount(), spender)
|
|
||||||
.call()
|
|
||||||
.then(approved => {
|
|
||||||
this.setState({ ...this.state, approved })
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
toggleApproved = () => {
|
toggleApproved = () => {
|
||||||
const { approved } = this.state;
|
const { approved } = this.state;
|
||||||
const { methods: { approve }, spender } = this.props;
|
const { methods: { approve }, spender, receiveSntAllowance } = this.props;
|
||||||
const isApproved = !!Number(approved);
|
const isApproved = !!Number(approved);
|
||||||
let amountToApprove = isApproved ? 0 : unlimitedAllowance;
|
let amountToApprove = isApproved ? 0 : unlimitedAllowance;
|
||||||
console.log("approve(\""+spender+"\",\"" +amountToApprove +"\")")
|
console.log("approve(\""+spender+"\",\"" +amountToApprove +"\")")
|
||||||
|
@ -55,7 +42,8 @@ class TokenHandle extends PureComponent {
|
||||||
.send()
|
.send()
|
||||||
.then(approval => {
|
.then(approval => {
|
||||||
const { events: { Approval: { returnValues: { _value } } } } = approval
|
const { events: { Approval: { returnValues: { _value } } } } = approval
|
||||||
this.setState({ ...this.state, approved: _value, updating: false })
|
receiveSntAllowance(_value)
|
||||||
|
this.setState({ ...this.state, updating: false })
|
||||||
}).catch(err => {
|
}).catch(err => {
|
||||||
console.log("Approve failed: " + err);
|
console.log("Approve failed: " + err);
|
||||||
this.setState({ updating: false });
|
this.setState({ updating: false });
|
||||||
|
@ -63,8 +51,8 @@ class TokenHandle extends PureComponent {
|
||||||
}
|
}
|
||||||
|
|
||||||
render() {
|
render() {
|
||||||
const { symbol, account, isLoading, mobile } = this.props;
|
const { symbol, account, isLoading, mobile, SNTAllowance } = this.props;
|
||||||
const { approved, updating } = this.state;
|
const { updating } = this.state;
|
||||||
return (
|
return (
|
||||||
<Fragment>
|
<Fragment>
|
||||||
{!updating && !isLoading && !!account && <div>
|
{!updating && !isLoading && !!account && <div>
|
||||||
|
@ -74,7 +62,7 @@ class TokenHandle extends PureComponent {
|
||||||
<FormControlLabel
|
<FormControlLabel
|
||||||
control={
|
control={
|
||||||
<Switch
|
<Switch
|
||||||
checked={!!Number(approved)}
|
checked={!!Number(SNTAllowance)}
|
||||||
onChange={this.toggleApproved}
|
onChange={this.toggleApproved}
|
||||||
value={symbol}
|
value={symbol}
|
||||||
/>
|
/>
|
||||||
|
@ -103,4 +91,13 @@ const TokenPermissions = (props) => (
|
||||||
</Fragment>
|
</Fragment>
|
||||||
)
|
)
|
||||||
|
|
||||||
export default TokenPermissions;
|
const mapDispatchToProps = dispatch => ({
|
||||||
|
receiveSntAllowance(amount) {
|
||||||
|
dispatch(actions.receiveSntAllowance(amount));
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const mapStateToProps = state => ({
|
||||||
|
SNTAllowance: getSNTAllowance(state),
|
||||||
|
});
|
||||||
|
export default connect(mapStateToProps, mapDispatchToProps)(TokenPermissions);
|
||||||
|
|
Loading…
Reference in New Issue