From 7a394c396e35852b93681d123e614ac56baba297 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Thu, 31 May 2018 16:48:10 -0400 Subject: [PATCH] add token permissions to proposal manager --- .../proposal-manager/proposal-manager.js | 58 ++-------- app/components/standard/TokenPermission.js | 107 ++++++++++++++++++ app/components/standard/utils.js | 9 ++ package.json | 4 +- 4 files changed, 131 insertions(+), 47 deletions(-) create mode 100644 app/components/standard/TokenPermission.js create mode 100644 app/components/standard/utils.js diff --git a/app/components/proposal-manager/proposal-manager.js b/app/components/proposal-manager/proposal-manager.js index a65476d..f578a2c 100644 --- a/app/components/proposal-manager/proposal-manager.js +++ b/app/components/proposal-manager/proposal-manager.js @@ -7,15 +7,9 @@ import { Form, FormGroup, FormControl, HelpBlock, Button, Alert } from 'react-bo import web3 from "Embark/web3"; import { withFormik } from 'formik'; import FieldGroup from '../standard/FieldGroup'; +import TokenPermissions from '../standard/TokenPermission' const { setSubmitPrice } = ProposalCuration.methods; -const setPrice = (address = web3.eth.defaultAccount, allowed = true, stakeValue = 1) => { - setSubmitPrice(address, allowed, stakeValue) - .send() - .then(res => { console.log(res) }) - .catch(err => { console.log(err) }) -} - class InnerForm extends PureComponent { constructor(props) { @@ -51,44 +45,16 @@ class InnerForm extends PureComponent { }); } - async handleClick(){ - let description = { - "url": this.state.url, - "title": this.state.title, - "description": this.state.description - }; - - EmbarkJS.Storage.saveText(JSON.stringify(description)) - .then(async (hash) => { - let hexHash = web3.utils.toHex(hash); - - let receipt = await SNT.methods.approve( - ProposalCuration.options.address, - this.state.submitPrice) - .send({from: web3.eth.defaultAccount, gasLimit: 1000000}); - - console.log(receipt); - - receipt = await ProposalCuration.methods.submitProposal( - "0x00", - "0x0000000000000000000000000000000000000000", - 0, - "0x00", - hexHash - ) - .send({from: web3.eth.defaultAccount, gasLimit: 1000000}); - - console.log(receipt); - }) - .catch((err) => { - if(err){ - // TODO show error - console.log("Storage saveText Error => " + err.message); - } - }); + setPrice = (address = web3.eth.defaultAccount, allowed = true, stakeValue = 1) => { + setSubmitPrice(address, allowed, stakeValue) + .send() + .then(res => { + this.setState({ ...state, canSubmit: true }); + console.log(res); + }) + .catch(err => { console.log(err) }) } - render() { const { values, errors, touched, handleChange, handleBlur, handleSubmit, isSubmitting, setFieldValue } = this.props; const { canSubmit } = this.state; @@ -96,12 +62,12 @@ class InnerForm extends PureComponent { {!canSubmit && - Account not allowed to submit proposals + Account not allowed to submit proposals } + +

Add proposal

-

Execute this on the console if proposal submit is not allowed

- await ProposalCuration.methods.setSubmitPrice(web3.eth.defaultAccount, true, 1).send();

Price: {this.state.submitPrice}

web3.eth.defaultAccount; +const SUPPORTED_TOKENS = ['SNT']; +const commaize = (str) => Number(str).toLocaleString(); + +const balanceTooltip = (balance, approved) => ( + + {`Your balance of ${commaize(balance)} is ${approved ? 'approved' : 'not approved'} for spending by the contract`} + +); + +class TokenHandle extends PureComponent { + constructor(props){ + super(props); + this.state = { balance: 0, approved: 0 }; + } + + componentDidMount() { + setTimeout(() => { + this.getBalance(); + this.getAllowance(); + }, 1000) + } + + getBalance = () => { + this.props.methods.balanceOf(getDefaultAccount()) + .call() + .then(balance => { this.setState({ ...this.state, balance }) }); + } + + getAllowance = () => { + const { methods, spender } = this.props; + methods.allowance(getDefaultAccount(), spender) + .call() + .then(approved => { + this.setState({ ...this.state, approved }) + }) + } + + toggleApproved = () => { + const { approved } = this.state; + const { methods: { approve }, spender } = this.props; + const isApproved = !!Number(approved); + let amountToApprove = isApproved ? 0 : unlimitedAllowance; + console.log("approve(\""+spender+"\",\"" +amountToApprove +"\")") + approve( + spender, + amountToApprove + ) + .send() + .then(approval => { + const { events: { Approval: { returnValues: { _value, _amount } } } } = approval + console.log('value',_value, approval) + this.setState({ ...this.state, approved: _value || _amount }) + }).catch(err => { + console.log("Approve failed: " + err); + }) + } + + render() { + const { symbol } = this.props; + const { balance, approved } = this.state; + const balanceTooltip = ( + + {`Your balance of ${commaize(balance)} is ${approved ? 'approved' : 'not approved'} for spending by the contract`} + + ); + return ( + +
+ + +
+
+ ) + } +} + +const tooltip = ( + + Turn on permissions for a token to enable its use with the ENS subdomain registry. + +); + +const TokenPermissions = (props) => ( + + +

Token Permissions

+
+
+ +
+) + +export default TokenPermissions; diff --git a/app/components/standard/utils.js b/app/components/standard/utils.js new file mode 100644 index 0000000..0541bc4 --- /dev/null +++ b/app/components/standard/utils.js @@ -0,0 +1,9 @@ +import BigNumber from 'bignumber.js'; + +// By default BigNumber's `toString` method converts to exponential notation if the value has +// more then 20 digits. We want to avoid this behavior, so we set EXPONENTIAL_AT to a high number +BigNumber.config({ + EXPONENTIAL_AT: 1000, +}); + +export { BigNumber }; diff --git a/package.json b/package.json index f1cc579..fc5afc3 100644 --- a/package.json +++ b/package.json @@ -19,11 +19,13 @@ "dependencies": { "babel-plugin-transform-object-rest-spread": "^6.26.0", "babel-preset-stage-2": "^6.24.1", + "bignumber.js": "^5.0.0", "formik": "^0.11.11", "jquery": "^3.3.1", "react": "^16.3.2", "react-blockies": "^1.3.0", "react-bootstrap": "^0.32.1", - "react-dom": "^16.3.2" + "react-dom": "^16.3.2", + "react-toggle": "^4.0.2" } }