0

I hope this is not a stupid question, but I am currently quite new to ethereum and haven`t found any other resource that explains this issue.

I am having a really simple contract:

pragma solidity ^0.4.18;

contract TextSave {
    uint public mostSent = 0;
    string public currentText = "Put your own text here!";
    address public owner = msg.sender;
    uint private maxLength = 50;

    function setText(string newText) public payable returns (bool) {
        if (msg.value > mostSent && bytes(newText).length < maxLength) {
            currentText = newText;
            mostSent = msg.value;
            return true;
        } else {
            msg.sender.transfer(msg.value);
            return false;
        }
    }

    function withdrawEther() external {
        require(msg.sender == owner);
        owner.transfer(this.balance);
    }

    function () public payable{
        setText("Default text!");
    }
 }

Now I would like to display all saved texts that are on the blockchain?

How can I do that? Do I need to change the contract or can I just use web3 for that?

I highly appreciate your replies!

2

How can I do that?

First, you will need some storage to save your history texts.

string[] public saved_texts;

Second, change your funciton.

function setText(string newText) public payable returns (bool) {
        if (msg.value > mostSent && bytes(newText).length < maxLength) {
            saved_texts.push(currentText);
            currentText = newText;
            mostSent = msg.value;
            return true;
        } else {
            msg.sender.transfer(msg.value);
            return false;
        }
}

Third, you can execute call like TextSaveInstance.saved_texts.call(_index) in the loop from your js code to retrieve all texts from the storage.

  • Thx for your reply! Can you show the part with the look as a javascript example. – Soli Feb 14 '18 at 19:25
  • 1
    @Kare you accepted this answer and then removed acceptance to ask another question?! Sorry, but this is not how a site works. I already provided you with a function to retrieve data from js code. If you cannot create loop or connect js code to blockchain, then this is another question. You can find answers for these questions using search, because they are pretty basic and for sure were already answered before. – Roman Frolov Feb 14 '18 at 19:35
  • Frovol Sorry my fault. You are right. Just accepted your very helpful answer! – Soli Feb 14 '18 at 19:49

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.