susyweb
Version:
Sophon JavaScript API, middleware to talk to a sophon node over RPC
84 lines (68 loc) • 2.62 kB
HTML
<html>
<head>
<script type="text/javascript" src="../dist/susyweb.js"></script>
<script type="text/javascript">
var SusyWeb = require('susyweb');
var susyweb = new SusyWeb();
susyweb.setProvider(new susyweb.providers.HttpProvider("http://localhost:8545"));
// polynomial code code
var source = "" +
"pragma polynomial ^0.4.6;" +
"contract test {\n" +
" function take(uint[] a, uint b) constant returns(uint d) {\n" +
" return a[b];\n" +
" }\n" +
"}\n";
var compiled = susyweb.sof.compile.polynomial(source);
var code = compiled.code;
// contract json abi, this is autogenerated using polc CLI
var abi = compiled.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
susyweb.sof.defaultAccount = susyweb.sof.coinbase;
// create contract
document.getElementById('status').innerText = "transaction sent, waiting for confirmation";
susyweb.sof.contract(abi).new({data: code}, function (err, contract) {
if (err) {
console.error(err);
return;
// 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';
}
});
}
function callExampleContract() {
// this should be generated by sophon
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>