Working with Smart Contracts
Last updated
Last updated
In this Hands-On Lab we're going to start a smart contract, simply interact with it and then stop it again in Remix.
Expand the Content of the previous file to:
And make sure the file is compiled without any errors or warnings!
Wondering what that all means?
This is a very basic version of a Smart Contract. Let's go through it line by line:
// SPDX-License-Identifier: GPL-3.0: The The Software Package Data Exchange® (SPDX®) identifier is there to clearly communicate the license under which the Solidity file will be made available. Well, if you make it available. But you should. Smart Contracts transparency and trust greatly benefit from the source being published and sometimes it's not 100% clear under which license the source is out in the wild. The SPDX identifier is optional, but recommended.
pragma solidity 0.8.14: The pragma keyword is for the compiler to enable certain features or check certain things. The version pragma is a safety measure, to let the compiler know for which compiler version the Solidity file was written for. It follows the SemVer versioning standard. 0.8.14 only version 0.8.14, but if we'd write it as pragma solidity ^0.8.0 it would mean >=0.8.0 and <0.9.0.
contract MyContract: That's the actual beginning of the Smart Contract. Like a Class in almost any other programming language.
string public myString = 'hello world': That is a storage variable. It's public and Solidity will automatically generate a getter function for it - you'll see that in a minute!
Now it's time to deploy the Smart Contract. Head over to the Deploy & Run Transactions Tab!
Select the JavaScript VM (currently the latest one is London) and
Make sure the right contract is selected from the Dropdown here. If nothing is selected, make sure the "Auto Compile" checkbox in the Compiler-Plugin is enabled.
deploy the Contract by clicking on "Deploy".
You will see a contract instance popping up on the bottom
And you will also witness a new transaction being logged in the logging area
When you uncollapse the contract instance, then you can interact with it. Hit the little "ourString" button and it will show the "Hello World" from the string in our contract. It will also log anohter transaction in the logging area, which is now marked as "call".
One thing you probably saw is: You can't update an already deployed smart contract. You need to deploy a new instance!
On the blockchain, you can't really remove contracts in the sense that also their historical data is wiped. You can "selfedestruct" them (which is a functionality that might get deprecated and removed), then you can't interact with them anymore, but their historical data is always baked into historical blocks.
In Remix, however, you just have to reload the page and the contract instances are gone. This is, because the JavaScript VM is only an in-memory blockchain simulation. If you just want to delete 1. all the contract instances, you can also click the trash-bin icon, or if you 2. just want to delete a single instance, you can click the little x-icon. Mind: The layout might shift around from Remix version to Remix version.
So far, we were only reading information from Smart Contracts, but not writing. It's time we actually write something!
We're going to expand our Smart Contract from before and add a function to write!
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
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"
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...