Ethereum Tutorials
  • 🤩All About Ethereum
  • Foundation
    • 💰Install Metamask
    • ⛳Setup Remix
    • 👉Introduction to smart contracts
    • 👷Working with Smart Contracts
    • 🏅Deploy Your First Smart Contract
  • Play with NFTs
    • 👨‍💻What is NFT?
  • 📔How to Create an NFT
  • 🏗️Build a Full-Stack NFT dApp
Powered by GitBook
On this page
  • Starting, Stopping and Interacting with Smart Contracts
  • The Contract
  • Deploy Smart Contract
  • Interact with the Smart Contract
  • Update the Smart Contract
  • How to stop Smart Contracts¶
  • Read and Write to Smart Contract
  • Contract¶
  • Interaction
  • Next Step

Was this helpful?

  1. Foundation

Working with Smart Contracts

PreviousIntroduction to smart contractsNextDeploy Your First Smart Contract

Last updated 2 years ago

Was this helpful?

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.

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!

Wondering what that all means?

This is a very basic version of a Smart Contract. Let's go through it line by line:

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!

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.

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!

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

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

  1. "Hello Earth" in the input field and

  2. send off the transaction, then

  3. a new Transaction will appear in the log windows and

  4. 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...

// SPDX-License-Identifier: GPL-3.0: The 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 , 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 . 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.

How to stop Smart Contracts

Contract

According to the 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

👷
The Software Package Data Exchange® (SPDX®)
SPDX identifier is optional
SemVer versioning standard
¶
¶
Open in Remix
Naming Conventions
Open in Remix