What is a Smart Contract?

Imagine a normal written contract between you and a phone seller. A part of the imaginary contract could be "Payment for the item will be made, only upon successful delivery". So a Smart Contract is almost the same. We define the requirements of a contract as code, compile it and let it run on the blockchain. The smart contract aka 'mini program' is watching that the contract partners fulfill their job. That's great because we can do business with people we don't know. Bye, bye middlemen 🙂

Let's go this through, step by step.

  1. You and the seller confirmed that the requirements of the (smart) contract are correct. This is important because a smart contract can not change the requirements afterward, like a normal contract.
  2. The Smart Contract is compiled and run on the blockchain.
  3. You send the money for the phone to an address, specify in the contract and owned by the smart contract.
  4. The seller packs your phone and send it to you with UPS
  5. UPS delivers the phone to you and sends an event that indicates a successful delivery to you.
  6. The Smart Contract is receiving that event and forwards the payment you made to the seller.
  7. The transaction is completed and will be saved on the blockchain because of transparency reasons.

Solidity - a language for writing Smart Contracts

Solidity is an object-oriented programming language for writing smart contracts. It is used for implementing smart contracts on various blockchain platforms, most notably, Ethereum.

To run our smart contract we need a blockchain environment. Therefore we use the Etherum Virtual Machine (EVM). We don't need to install or setup this up locally, we simply can use Remix IDE, which does this for us :)

We are going to write our first contract.

Go to the Remix IDE and create a new file called 'SimpleStorage.sol' and paste in this:

pragma solidity  >=0.4.16  <0.9.0;

contract  SimpleStorage  {
  uint  storedData;

  function  set(uint  x)  public  {
      storedData  =  x;
  }

  function  get()  public  view  returns  (uint)  {
      return  storedData;
  }
}

The first line tells you that the source code is licensed under the GPL version 3.0. We need to specify this, otherwise, Remix IDE won't let you compile. The second line specifies that the source code is written for Solidity version 0.4.16, or a newer version of the language up to, but not including version 0.9.0. Next, we declare our smart contract by putting contract before SimpleStorage followed by curly braces, like a class definition.

A contract in the sense of Solidity is a collection of code (its functions) and data (its state) that resides at a specific address on the Ethereum blockchain

uint storedData;

Here we define an unsigned integer (uint) variable with the name storedData. This variable holds only positive numbers (from 0 to 65535) because it's unsigned. The scope of this variable lies within the contract.

Our variable acts like a state, that we read/write with the get() and set() functions

As you may have noticed, functions in Solidity are structured a bit differently than in Typescript. The visibility of the function is specified after the name. This is followed by the return type. Have you noticed the view right before the return type definition? This is not necessary. view ensures that this function is a read-only function, which ensures that state variables cannot be modified after calling them. Getters are normally view functions. A public function can be called outside of the smart contract. So another Smart Contract could call our set() function here. If you want to reduce the scope of the contract, use the internal keyword.

Let's test our first Smart Contract

Head over to the Compiler Tab in Remix IDE. I highlighted here with a red rectangle You don't need to adjust any options here. Just click "Compile BuyContract.sol" and it's getting compiled. After this is finished, head over to the deploy section. Click on the deploy tab on the left side. Under Environment choose the JavaScriptVM (Berlin) or (London). You can leave the rest of the options as they are. Chose your Contract and then press Deploy (marked as 2 in the picture). This will take a while but when it's ready you should see the SimpleStorage Contract beneath the Deployed Contracts. Here we have access to our get and set functions. Use the input fields (3) and play a little bit around.

Congratulation! You have just written your first Smart Contract that is accessible from the blockchain. Before we end, here is a little overview, which data types exist in Solidity. Thank you so much for reading

from https://www.geeksforgeeks.org/solidity-types/

Lessons learned