Read and Write to Smart Contract
So far, we were only reading information from Smart Contracts, but not writing. Itās time we actually write something!
Video
š”Watch the video first, then try it yourself!
Contract
Weāre going to expand our Smart Contract from before and add a function to write!
//SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract MyContract {
string public myString = "Hello world";
function updateOurString(string memory _myString) public {
myString = _myString;
}
}
According to the Naming Conventionsā a variable should be mixedCase, without a leading underscore. Turns out I was wrong teaching you a leading underscore (_variableName) - I still use it in my contracts as I like the distinction between JavaScript, Web3js and Solidity. But I need to mention it here: If you want to write Smart Contracts according to the official suggested naming convention, write variables only as mixedCase
Interaction
If you deploy the Smart Contract to the JavaScript VM, youāll see that a new button appeared in a different color:
You can also uncollapse the input field:
When you enter
- āHello Earthā in the input field and
- send off the transaction, then
- a new Transaction will appear in the log windows and
- The string āmyStringā is updated to āHello Earthā
Next Step
Now that you know how to interact with a Smart Contract, its time to do something more meaningful with our newly gained knowledge. But before that, let me summarize what we learned in the next lecture real quickā¦