Skip to content

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";

}

Open in Remix

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!

Deploy Smart Contract

Now it's time to deploy the Smart Contract. Head over to the Deploy & Run Transactions Tab!

  1. Select the JavaScript VM (currently the latest one is London) and
  2. 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.
  3. deploy the Contract by clicking on "Deploy".
  4. You will see a contract instance popping up on the bottom
  5. 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.


Last update: August 14, 2022