Create and index contracts
Last updated
Was this helpful?
Was this helpful?
pragma solidity >=0.4.0 <0.6.0;
contract SimpleStorage {
uint storedData;
function set(uint x) public {
storedData = x;
}
function get() public view returns (uint) {
return storedData;
}
}<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>Data Stores</title>
</head>
<body>
<div class="container">
<p><br/>The table shows on-chain storage contracts. You can create a new one, or change the number stored in an existing one. All actions are recorded on-chain as immutable history.</p>
<p><button id="create" class="btn btn-primary" onclick="create(this)">Create a new storage contract</button></p>
<table class="table">
<thead>
<tr>
<th scope="col">Created</th>
<th scope="col">Data</th>
<th scope="col"></th>
</tr>
</thead>
<tbody id="tbody">
</tbody>
</table>
</div>
</body>
</html>var contract = window.web3 && web3.ss && web3.ss.contract(abi);
var instance = contract && contract.at(cAddr);
window.addEventListener('web3Ready', function() {
contract = web3.ss.contract(abi);
instance = contract.at(cAddr);
reload();
});
function reload() {
document.querySelector("#create").innerHTML = "Create a new storage contract";
var tbodyInner = "";
esss.shaAbi(JSON.stringify(abi)).then((shaResult) => {
var sha = JSON.parse(shaResult).abiSha3;
esss.searchUsingAbi(sha).then((searchResult) => {
var items = JSON.parse(searchResult);
items.sort(compareItem);
items.forEach(function(item) {
tbodyInner = tbodyInner +
"<tr id='" + item.contractAddress + "'><td>" + item.blockNumber +
"</td><td>" + item.functionData.get +
"</td><td><button class='btn btn-info' onclick='setData(this)'>Set</button></td></tr>";
}); // end of JSON iterator
document.querySelector("#tbody").innerHTML = tbodyInner;
});
}); // end of esss
}
function create(element) {
element.innerHTML = "Wait ...";
var data = '0x' + contract.new.getData({
data: bytecode
});
contract.new({
data: data
}, function(ee, i) {
if (!ee && i.address != null) {
esss.submitAbi(JSON.stringify(abi), i.transactionHash).then((submitResults) => {
setTimeout(function() {
reload();
}, 3 * 1000);
});
}
});
}
function setData(element) {
var tr = element.closest("tr");
instance = contract.at(tr.id);
var n = window.prompt("Input a number:");
n && instance.set(n);
setTimeout(function() {
esss.updateStateOfContractAddress(JSON.stringify(abi), instance.address).then((c2i) => {
setTimeout(function() {
esss.searchUsingAddress(instance.address).then((r) => {
var data = JSON.parse(r);
resultToDisplay = JSON.stringify(data.functionData.get);
element.closest("td").previousSibling.innerHTML = resultToDisplay.replace(/['"]+/g, '');
element.innerHTML = "Set";
});
}, 1 * 1000);
});
}, 1 * 1000);
element.innerHTML = "Wait ...";
}
function compareItem(a, b) {
let comparison = 0;
if (a.blockNumber < b.blockNumber) {
comparison = 1;
} else if (a.blockNumber > b.blockNumber) {
comparison = -1;
}
return comparison;
}esss.shaAbi(JSON.stringify(abi)).then((shaResult) => {
var sha = JSON.parse(shaResult).abiSha3;
esss.searchUsingAbi(sha).then((searchResult) => {
var items = JSON.parse(searchResult);
// Puts the items into the table
});
});function setData (element) {
instance = contract.at(element.id);
var n = window.prompt("Input a number:");
n && instance.set(n);
}function create (element) {
element.innerHTML = "Wait ...";
var data = '0x' + contract.new.getData({data:bytecode});
contract.new({
data: data
}, function (ee, i) {
if (!ee && i.address != null) {
esss.submitAbi(JSON.stringify(abi), i.transactionHash);
setTimeout(function () {
reload ();
}, 5 * 1000);
}
});
}