2015-07-01 12:17:50 +00:00
|
|
|
<!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 take(uint[] a, uint b) constant returns(uint d) {\n" +
|
|
|
|
" return a[b];\n" +
|
|
|
|
" }\n" +
|
|
|
|
"}\n";
|
|
|
|
|
|
|
|
var compiled = web3.eth.compile.solidity(source);
|
|
|
|
var code = compiled.test.code;
|
|
|
|
// contract json abi, this is autogenerated using solc CLI
|
|
|
|
var abi = compiled.test.info.abiDefinition;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
// create contract
|
2015-07-07 09:56:50 +00:00
|
|
|
document.getElementById('status').innerText = "transaction sent, waiting for confirmation";
|
2015-07-07 09:51:13 +00:00
|
|
|
web3.eth.contract(abi).new({data: code}, function (err, contract) {
|
|
|
|
if (err) {
|
2015-07-07 09:58:42 +00:00
|
|
|
console.error(err);
|
2015-07-07 09:51:13 +00:00
|
|
|
return;
|
2015-07-01 12:17:50 +00:00
|
|
|
|
2015-07-20 14:44:25 +00:00
|
|
|
// callback fires twice, we only want the second call when the contract is deployed
|
|
|
|
} else if(contract.address){
|
|
|
|
|
|
|
|
myContract = contract;
|
|
|
|
console.log('address: ' + myContract.address);
|
|
|
|
|
|
|
|
document.getElementById('status').innerText = 'Mined!';
|
|
|
|
document.getElementById('call').style.visibility = 'visible';
|
|
|
|
}
|
2015-07-07 09:51:13 +00:00
|
|
|
});
|
2015-07-01 12:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function callExampleContract() {
|
|
|
|
// this should be generated by ethereum
|
|
|
|
var param = parseInt(document.getElementById('value').value);
|
|
|
|
|
|
|
|
// call the contract
|
|
|
|
var res = myContract.take([0,6,5,2,1,5,6], 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;'>
|
|
|
|
<div>var array = [0,6,5,2,1,5,6];</div>
|
|
|
|
<div>var x = array[
|
|
|
|
<input type="number" id="value" onkeyup='callExampleContract()'></input>
|
|
|
|
];
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<div id="result"></div>
|
|
|
|
</body>
|
|
|
|
</html>
|
|
|
|
|