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.
- Question: What are those seed words?
- Question: What is the difference between Metamask and running your own light client. What are the advantages and disadvantages?
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.
- Question: Can you see your account balance?
- Question: What do you see if you change the environment from Injected Web (Metamask) to JavaScript VM.
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;
}
- Question: can you see your transaction on Etherscan Rinkeby network?
- Question: How much Ethers did you pay for your transaction?
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
- Question: how can you invoke the getter and setters?
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 {
//...
}
}
- Question: how expensive is this contract compare to the contract in exercise 3?
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(...);
}
}
- Question: Where can you see the emitted event?
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(...);
}
}