Changing Data: Transactional Functions

Beginners Guide to Solidity Development - Episode 3



Changing Data: Transactional Functions

In this episode, we will be talking about how you can change what data is currently stored inside the data field of a smart contract.

You can change what data is stored inside by writing a function, a so-called transactional function, which you can pass a value and then this value becomes the new value that is stored inside the data field that you have specified.

Writing a Transactional Function

pragma solidity ^0.4.24;

contract FirstContract {
    string public message = "I like cookies";
}

This is what we ended up with after the end of the last episode. We have a very simple smart contract which contains a hard-coded message and we can look at the current value of this message but we cannot currently alter what value it is. So, if we want to alter this value we need to write a transactional function, and the transactional function is a function that assigns a new value to one or multiple data fields of the smart contract.

It looks like this: function, and then we have the name of the function like changeMessage and then we have some arguments in here and then we have a block which is limited by using curly braces.

Inside the function, we need to assign a new value to message. Of course, where does this value come from? Well, it comes from the argument that we pass in. So we need an argument over here, a parameter, such as something called new_message. So, whenever we specify the new message and call this function with its new message then the current message becomes the new message.

contract FirstContract {
    string public message = "I like cookies";
    function changeMessage(string new_message) {
        message = new_message;
    }
}

Now, if we try to compile this as is we will get a warning. It tells us "No visibility specified. Defaulting to public". So, actually, that is right, public over here. What the visibility of a function means is from where you can execute the function. A public function can be executed from outside of the block chain so this is what we want in this case. We will talk more about visibility in a later episode. Now, after clicking compile again, everything is fine.

contract FirstContract {
    string public message = "I like cookies";
    function changeMessage(string new_message) public {
        message = new_message;
    }
}

Deploying a new version of our Smart Contract

Now, if we want to deploy this new version, and if you, run this tutorial just after you finished the previous one, then you will still see the old contract that we deployed over here. If I click deploy again I will see the new one over here below the old one. So, let us click on the cross over here so we don't get confused by the old version of the contract.

Again, the reason that the new version doesn't just replace the old version is because a once published smart contract is on the block chain forever. Of course, in this case, our block chain is limited to our current browser window, but in the real block chain it would be inside this public, shared block chain for the rest of time.

Interacting with the Function

Alright, that's enough of the side tour, let's talk about the code we just published, so the current version of the contract. As you can see there are now two fields over here. We still have the message just as in the last episode and when we click on it we see what string is currently stored inside. But we also have the changeMessage function and if we click on here it will execute this function with whatever we currently enter in here. Well, right now I didn't actually entered anything before clicking it so when I want to read any value by clicking on the blue message again I now get an empty string as a result.

So, let's actually enter some more interesting information in here. "This is a test". Do note you have to enclose this value in quotation marks because that's the way the Remix is currently set up. Now, if we click on the red button again we see some information again down in the log information and then if we check again by clicking on the blue button, you see the new value is over here.