Starting, Stopping and Interacting with Smart Contracts
In this Hands-On Lab weāre going to start a smart contract, simply interact with it and then stop it again in Remix.
Video
The Contract
Expand the Content of the previous file to:
//SPDX-License-Identifier: MIT
pragma solidity 0.8.14;
contract MyContract {
string public ourString = "Hello World";
}
And make sure the file is compiled without any errors or warnings!
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!
Deploy Smart Contract
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
Interact with the Smart Contract
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ā.
Update the Smart Contract
One thing you probably saw is: You canāt update an already deployed smart contract. You need to deploy a new instance!
How to stop Smart Contracts
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.
Summary
Now you know how to start and stop a Smart Contract. But how to really interact with it? Thatās what we do in the next lecture.