Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
pragma solidity 0.4.24;
contract EthereumSession {
uint myInt;
function setTheInt(uint _myInt) public {
myInt = _myInt;
}
function getTheInt() public view returns(uint) {
return myInt;
}
}
<!doctype html>
<html>
<head>
<script src="node_modules/web3/dist/web3.js" type="text/javascript">
</script>
<script src="node_modules/truffle-contract/dist/truffle-contract.js" type="text/javascript">
</script>
</head>
<body>
<script>
var web3;
if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
}
var EthereumSession = web3.eth.contract([
{
"constant": false,
"inputs": [
{
"name": "_myInt",
"type": "uint256"
}
],
"name": "setTheInt",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getTheInt",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]);
var ethereumSessionInstance = EthereumSession.at("0xa6ceaac576ff14931c0f6d2f5fd8f546a4c0d128");
web3.eth.defaultAccount = web3.eth.accounts[0];
ethereumSessionInstance.setTheInt(4546);
//wait until the transaction is mined.
console.log(ethereumSessionInstance.getTheInt());
//Truffle-Contract
var provider = new Web3.providers.HttpProvider("http://localhost:7545");
var EthereumSessionTruffle = TruffleContract({abi: [
{
"constant": false,
"inputs": [
{
"name": "_myInt",
"type": "uint256"
}
],
"name": "setTheInt",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "getTheInt",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]});
EthereumSessionTruffle.setProvider(provider);
var esTruffleInstance = null;
EthereumSessionTruffle.at("0xa6ceaac576ff14931c0f6d2f5fd8f546a4c0d128").then(instance => {
esTruffleInstance = instance;
console.log(instance);
return instance.setTheInt(4545);
}).then(function() {
return esTruffleInstance.getTheInt();
}).then(result => { console.log(result);});
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment