Introduction to Smart Contracts

edward_naidoo

Edward Naidoo

Posted on March 19, 2022

Introduction to Smart Contracts

This is Part 2 of my journey understanding Solidity and answering whether this language can change how we develop software forever. I have been reading the official documentation and watching videos on the language and how it is done in a visual and audible environment. I have noticed a newer version as I was reading it and I have been reading it on that version. Version 0.8.12. Now there is 0.8.13.

What I have been reading could be old news depending when you see this so I deeply suggest looking at the latest versions if you want to stay up to date. These changes are more frequent than Flutter updates so keep that in mind. so I have started out by reading examples of smart contracts using this language and I will sum up all of the things I have read so far as the docs are really long.


Intro to Smart Contracts

So some of us might know, but just in case, a smart contract is the behaviour of the transaction between to people on Web3 or with NFTs and Crypto. It's files like a normal project with code written in Solidity with the .sol file type and can be compiled on the internet rather on your machine which is cool. It is what allows you to pay for that Ape or resale it for more currency but smart contracts are more versatile. I'll start off with this example, the Storage example.

// SPDX-License-Identifier: GPL-3.0
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;
    }
}
Enter fullscreen mode Exit fullscreen mode
  • The first line indicates the license,
  • The second line defines the Solidity Version with pragma being an instruction for compilers about how the code should be treated,
  • There are two functions, set() and get()

uint storedData; //unit is unsigned int with 256 bits
Enter fullscreen mode Exit fullscreen mode

To access it normally won’t need the use of this.storedData as you just need to use the name. When you put this contract onto the blockchain, nobody will forcefully stop you doing so but people can overwrite x to be a different int. Your original number and the changed number will stay on the blockchain history.

Here is another example: Subcurrency

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.4;

contract Coin {
    // The keyword "public" makes variables
    // accessible from other contracts
    address public minter;
    mapping (address => uint) public balances;

    // Events allow clients to react to specific
    // contract changes you declare
    event Sent(address from, address to, uint amount);

    // Constructor code is only run when the contract
    // is created
    constructor() {
        minter = msg.sender;
    }

    // Sends an amount of newly created coins to an address
    // Can only be called by the contract creator
    function mint(address receiver, uint amount) public {
        require(msg.sender == minter);
        balances[receiver] += amount;
    }

    // Errors allow you to provide information about
    // why an operation failed. They are returned
    // to the caller of the function.
    error InsufficientBalance(uint requested, uint available);

    // Sends an amount of existing coins
    // from any caller to an address
    function send(address receiver, uint amount) public {
        if (amount > balances[msg.sender])
            revert InsufficientBalance({
                requested: amount,
                available: balances[msg.sender]
            });

        balances[msg.sender] -= amount;
        balances[receiver] += amount;
        emit Sent(msg.sender, receiver, amount);
    }
}
Enter fullscreen mode Exit fullscreen mode

NOW NOW, don't panic! I don't understand everything here, so we are most likely in the same boat. Only some things from the general programming syntax but they can still mean different things and still have the same name which makes things confusing.

What I have shown you is what a smart contract can look like, and in the second part I'll go down into detail what each word may mean and the context behind any terms or phrases in the documentation. I'll also continue reading the documentation.


Thank you very much for unpacking my short findings. If you would like to keep track of my posts and get news of certain posts, follow me on Twitter! If you would like to see my projects, you can go to my Github or go to this account to see the main projects I worked or am still working on.


Cited Works

💖 💪 🙅 🚩
edward_naidoo
Edward Naidoo

Posted on March 19, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

Introduction to Smart Contracts
solidity Introduction to Smart Contracts

March 19, 2022