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;
}
}
Variable Naming
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...