0

I created a contract that insert one number (uint) in blockchain with set function but how can i retrieve all numbers inserted?

This is my html, i put 1 number in a blockchain and i will see just the last inserted:

<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js"></script>
</head>
<body>
<script>

window.onload = function () {
// check to see if user has metamask addon installed on his browser. check to make sure web3 is defined
if (typeof web3 === 'undefined') {
document.getElementById('metamask').innerHTML = 'You need <a href="https://metamask.io/">MetaMask</a> browser plugin to run this example'
}
// call the getvalue function here
getvalue();
}
//function to retrieve the last inserted value on the blockchain
function getvalue() {
try {
// contract Abi defines all the variables,constants and functions of the smart contract. replace with your own abi
var abi = []
//contract address. please change the address to your own
var contractaddress = '';
//instantiate and connect to contract address via Abi
var myAbi = web3.eth.contract(abi);
var myfunction = myAbi.at(contractaddress);
//call the get function of our SimpleStorage contract
myfunction.get.call(function (err, xname) {
if (err) { console.log(err) }
if (xname) {
//display value on the webpage
document.getElementById("xbalance").innerHTML = "primo: " + xname;

}
});
}
catch (err) {
document.getElementById("xbalance").innerHTML = err;
}
}

function setvalue() {
try {
// contract Abi defines all the variables,constants and functions of the smart contract. replace with your own abi
var abi = []
//contract address. please change the address to your own
var contractaddress = '';
//instantiate and connect to contract address via Abi
var myAbi = web3.eth.contract(abi);
var myFunction= myAbi.at(contractaddress);
//call the set function of our SimpleStorage contract
myFunction.set.sendTransaction(document.getElementById("xvalue").value,  { from: web3.eth.accounts[0], gas: 4000000 }, function (error, result) {
if (!error) {
console.log(result);
} else {
console.log(error);
}
})
} catch (err) {
document.getElementById("xvalue").innerHTML = err;
}
}

</script>
<center>
<div id="metamask" class="container"></div>
<h3>Inserimento dati nella Blockchain Ethereum</h3>
<br />
<table class="table">
                <div id="xbalance" class="container"></div>

                <thead>
                  <tr>
                    <th scope="col">Inserisci token: </th>
                  </tr>
                </thead>
                <tbody>
                  <tr>
                    <th scope="row">1</th>
                    <td><input id="xvalue" type="text" class="form-control" /></td>
                  </tr>

                  <tr>
                  <td><input id="Button1" type="button" class="btn btn-primary" onclick="setvalue()" value="Add to Blockchain"  /></td>
                  </tr>
                </tbody>
              </table>

</center>
</body>
</html>

And my contract is:

pragma solidity ^0.4.24;

contract M {
    uint256 storeddata;

    function set(uint256 x) public {
        storeddata = x;
    }

    function get() public view returns(uint256) {
        return storeddata; 
    }
}
  • Please, edit your question to add the code of the contract so we can suggest an appropiate solution. – Ismael Feb 1 at 18:58
  • @Ismael just done – Simone R.C. Feb 8 at 10:11
1

Rob's answer is a good one. If you don't want to use events for some reason, you could:

  • add your number to the uint array
  • increment a counter

Then if you wanted to retrieve all the numbers, you would use the counter as your index for iterating over the array.

  • I also uploaded my first code, now i'm trying to use Rob's code and connect it to my contract from UI but i have some problems i think. – Simone R.C. Feb 8 at 14:02
0

In the example below, there is a dynamic array of numbers and the appendNumber() function just keeps adding to the list. The contract emits an event for every important state change which I strongly recommend.

A software client can observe and inspect several ways.

  1. Subscribe/watch the event log. This facilitates construction of an offline representation of the state, since nothing can happen without a corresponding event. The client will always know the true state of the array without asking.
  2. Inspect the numbers() function. It accepts one argument for index and returns one number. Iteration over the index is a client-side concern. I often add a helper function to return the array length to assist with this. Omitted for brevity, it would look like function getCount() public view returns(uint count) { return numbers.length; }
  3. One could add a function to return an array of numbers. This is generally not recommended because the gas cost (processing) increases with scale. This would even make it impossible to use beyond a certain array size. Generally, this isn't necessary because the two alternatives above already address the concern.

-

pragma solidity 0.5.1;

contract SetGet {

    uint[] public numbers;

    event LogNewNumber(address setter, uint number);

    function appendNumber(uint value) public {
        numbers.push(value);
        emit LogNewNumber(msg.sender, value);
    }
}

Hope it helps.

  • This example is perfect but i have some problems to connect this contract to User interface created using HTML and javascript, i don't know how to call event i think or i do it in a bad way. – Simone R.C. Feb 8 at 12:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.