Skip to content

Instantly share code, notes, and snippets.

@iisaint
Last active August 7, 2017 14:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iisaint/a94de687af1a0ed9632400ce13c84255 to your computer and use it in GitHub Desktop.
Save iisaint/a94de687af1a0ed9632400ce13c84255 to your computer and use it in GitHub Desktop.
import PushButton from '../build/contracts/PushButton.json'
import getWeb3 from './utils/getWeb3'
//其餘省略
componentWillMount() {
getWeb3.then(results => {
this.setState({
web3: results.web3
})
// Instantiate contract once web3 provided.
this.instantiateContract()
}).catch(() => {
console.log('Error finding web3.')
})
}
instantiateContract() {
const contract = require('truffle-contract')
const pushButton = contract(PushButton)
pushButton.setProvider(this.state.web3.currentProvider)
var pushButtonInstance
this.state.web3.eth.getAccounts((error, accounts) => {
pushButton.at(contractAddress).then((instance) => {
pushButtonInstance = instance
this.setState({pushButtonInstance});
return pushButtonInstance.totalPush.call();
}).then((result) => {
this.setState({totalPush: this.state.web3.toDecimal(result)});
return pushButtonInstance.nextTimeoutBlock.call();
}).then((result) => {
this.setState({nextTimeoutBlock: this.state.web3.toDecimal(result)});
return pushButtonInstance.title.call();
}).then((result) => {
this.setState({title: result});
return true;
})
})
}
push() {
this.state.web3.eth.getAccounts((error, accounts) => {
this.state.pushButtonInstance.push({from: accounts[0]}).then((result) => {
console.log(result.tx);
}).catch((error) => {
console.log(error);
})
})
}
// 其餘省略
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment