You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
92 lines
2.9 KiB
92 lines
2.9 KiB
<!doctype>
|
|
<html>
|
|
|
|
<head>
|
|
<script type="text/javascript" src="../dist/web3.js"></script>
|
|
<script type="text/javascript">
|
|
|
|
var web3 = require('web3');
|
|
web3.setProvider(new web3.providers.HttpProvider("http://localhost:8545"));
|
|
|
|
// solidity code code
|
|
var source = "" +
|
|
"contract test {\n" +
|
|
" function multiply(uint a) constant returns(uint d) {\n" +
|
|
" return a * 7;\n" +
|
|
" }\n" +
|
|
"}\n";
|
|
|
|
var code = web3.eth.compile.solidity(source).code;
|
|
/*var code = "605280600c6000396000f3006000357c010000000000000000000000000000000000000000000000000000000090048063c6888fa114602e57005b60376004356041565b8060005260206000f35b6000600782029050604d565b91905056";*/
|
|
|
|
// contract description, this is autogenerated using solc CLI
|
|
var desc = [{
|
|
"constant" : true,
|
|
"inputs" : [{
|
|
"name" : "a",
|
|
"type" : "uint256"
|
|
}],
|
|
"name" : "multiply",
|
|
"outputs" : [{
|
|
"name" : "d",
|
|
"type" : "uint256"
|
|
}],
|
|
"type" : "function"
|
|
}];
|
|
|
|
var myContract;
|
|
|
|
function createExampleContract() {
|
|
// hide create button
|
|
document.getElementById('create').style.visibility = 'hidden';
|
|
document.getElementById('code').innerText = code;
|
|
|
|
// let's assume that coinbase is our account
|
|
web3.eth.defaultAccount = web3.eth.coinbase;
|
|
|
|
var watch = web3.eth.filter('latest');
|
|
|
|
// create contract
|
|
myContract = web3.eth.contract(desc).new({data: code});
|
|
console.log('address: ' + myContract.address);
|
|
document.getElementById('status').innerText = "transaction sent, waiting for confirmation";
|
|
watch.watch(function (err, hash) {
|
|
var block = web3.eth.getBlock(hash, true);
|
|
var contractMined = block.transactions.reduce(function (mined, th) {
|
|
// TODO: compiled code do not have 0x prefix
|
|
return mined || (th.from === web3.eth.defaultAccount && th.input.indexOf(code) !== -1);
|
|
}, false);
|
|
|
|
if (contractMined) {
|
|
document.getElementById('status').innerText = 'Mined!';
|
|
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 = myContract.multiply(param);
|
|
document.getElementById('result').innerText = res.toString(10);
|
|
}
|
|
|
|
</script>
|
|
</head>
|
|
<body>
|
|
<h1>contract</h1>
|
|
<div id="code"></div>
|
|
<div id="status"></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>
|
|
|
|
|