Lectures Publications Theses Events

Solidity

Solidity is a programming language for writing smart contracts. Solidity is the de facto standard language for Ethereum. Solidity is a statically typed language that is compiled to EVM opcodes that can be run on the Ethereum Virtual Machine (EVM).

In this exercise session, we will deploy a smart contract on the Rinkeby testnet. For that you will need testnet Ethers.


Exercise 1:

Install Metamask and send me your address, so that I can send you 1 ETH.


A convenient way to develop and implement Solidity is with the Remix IDE. Alternatively, a plugin for Intellij could be used, but this plugin lacks proper client integration.


Exercise 2: Open Remix IDE. Connect via Metamask to your account.


You are ready to go and you can create your first contract.


Exercise 3: Create your first contract and deploy it on rinkeby:

pragma solidity ^0.5.8;
//minimal contract
contract Example1 {
    uint256 counter;
}

Now you have deployed your first contract. As a next step, add a getter and setter to that counter.


Exercise 4: Create a getter and setter for counter


Since you want to restrict the setter function call to the one who create the smart contract, you need to check this condition. But first you need to store who the owner is.


Exercise 5: Add a constructor and store the one who created the contract (msg.owner) in a new address variable

contract Example1 {
    uint256 counter;
    address owner;
    constructor() public {
        //...
    }
}

Now, add require() to your setter and try access it from an other account (create a second account and transfer some coins to that account)


Since you want to notify external applications that you have set a value, you want to emit an event.


Exercise 6: After setting the counter and checking the permission, emit the event Message():

contract Example1 {
    uint256 counter;
    address owner;
    event Message(string msg);
    constructor() public {
        //...
    }
    function set(uint256 nr) public {
        //emit Message(...);
    }
}

In the next step we want to store in a key-value map, the accounts of a person including the name and the balance.


Exercise 7: Adjust your getter and setter to the new struct and map.

contract Example1 {
    uint256 counter;
    address owner;
    event Message(string msg);
    struct Account {
        string name;
        uint256 balance;
    }
    mapping(address => Account) accounts;
    
    constructor() public {
        //...
    }
    function set(uint256 nr) public {
        //emit Message(...);
    }
}