Skip to content

Instantly share code, notes, and snippets.

@davidhq
Last active August 29, 2015 14:16
Show Gist options
  • Save davidhq/b051fdd308181a82bc73 to your computer and use it in GitHub Desktop.
Save davidhq/b051fdd308181a82bc73 to your computer and use it in GitHub Desktop.
go-ethereum example
<html>
<head>
<script type="text/javascript" src="dist/bignumber.min.js"></script>
<script type="text/javascript" src="dist/ethereum.js"></script>
<script type="text/javascript">
var web3 = require('web3');
web3.setProvider(new web3.providers.HttpSyncProvider('http://localhost:8090'));
// solidity source code
var source = "" +
"contract test {\n" +
" function multiply(uint a) returns(uint d) {\n" +
" return a * 7;\n" +
" }\n" +
"}\n";
// contract description, this will be autogenerated somehow
var desc = [{"constant":false,"inputs":[{"name":"a","type":"uint256"}],"name":"multiply","outputs":[{"name":"d","type":"uint256"}],"type":"function"}]
var bin = "0x605180600c6000396000f3007c0100000000000000000000000000000000000000000000000000000000600035048063c6888fa114602d57005b60366004356040565b8060005260206000f35b6000600782029050604c565b91905056"
var contract;
function createExampleContract() {
// hide create button
document.getElementById('create').style.visibility = 'hidden';
document.getElementById('source').innerText = source;
var address = web3.eth.sendTransaction({code: bin})
console.log(address)
var MyContract = web3.eth.contract (desc);
contract = new MyContract (address);
document.getElementById('call').style.visibility = 'visible';
}
function callExampleContract() {
// this should be generated by ethereum
var param = parseInt(document.getElementById('value').value);
// call the contract
var res = contract.call().multiply(param);
document.getElementById('result').innerHTML = res.toString(10);
}
</script>
</head>
<body>
<h1>contract</h1>
<div id="source"></div>
<div id='create'>
<button type="button" onClick="createExampleContract();">create example contract</button>
</div>
<div id='call' style='visibility: hidden;'>
<input type="number" id="value" onkeyup='callExampleContract()'></input>
</div>
<div id="result"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment