Smart Contracts - no need for middlemen

Smart Contracts - no need for middlemen

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 runs on the blockchain. 3) You send the money for the phone to an address, specify in the contract and owned by the smart contract. 3) The seller packs your phone and send it to you with UPS 4) UPS delivers the phone to you and sends an event that indicates a successful delivery to you. 5) The Smart Contract is receiving that event and forwards the payment you made to the seller. 6) The transaction is completed and will be saved on the blockchain because of transparency reasons.

enter image description here

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. [Wikipedia] (en.wikipedia.org/wiki/Solidity)

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

enter image description here 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 enter image description here 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. enter image description here 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

  • Boolean: This data type accepts only two values True or False.
  • Integer: This data type is used to store integer values, int and uint are used to declare signed and unsigned integers respectively.
  • Fixed Point Numbers: These data types are not fully supported in solidity yet, as per the Solidity documentation. They can be declared as fixed and unfixed for signed and unsigned fixed-point numbers of varying sizes respectively.
  • Address: Address hold a 20-byte value which represents the size of an Ethereum address. An address can be used to get a balance or to transfer a balance by balance and transfer method respectively.
  • Bytes and Strings: Bytes are used to store a fixed-sized character set while the string is used to store the character set equal to or more than a byte. The length of bytes is from 1 to 32, while the string has a dynamic length. Byte has the advantage that it uses less gas, so better to use when we know the length of data.
  • Enums: It is used to create user-defined data types, used to assign a name to an integral constant which makes the contract more readable, maintainable, and less prone to errors. Options of enums can be represented by unsigned integer values starting from 0.

from geeksforgeeks.org/solidity-types

Lessons learned

  • Smart Contracts are immutable, transparent, secure, decentral
  • Solidity is a programing language for writing Smart Contracts
  • EVM stands for Etherum Virtual Machine, so we can deploy and test our Smart Contract in a test blockchain
  • Variables can be accessed within the Smart Contract, not from the blockchain
  • Public functions can be called from the blockchain, internal/private function within the Smart Contract
  • View functions are read-only function which not modify the state(s) in a function

Did you find this article valuable?

Support XamHans Blog by becoming a sponsor. Any amount is appreciated!