{Smart contacts}

Introduction into coding smart contracts on top of blockchains

me@jaro.lt

2022 01 03

# BASICS

Main parts of Bitcoin

Mining, consensus, transaction structure, nodes, wallets...

# BASICS

Full node

Mining

Wallet

Bitcoin core

# BASICS

Full nodes

Full nodes download every block and transaction and check them against Bitcoin's consensus rules.

# BASICS

Mining

It is a transaction record process with bitcoins to blockchain 

# BASICS

Wallet

Software or hardware which helps you to visualise balances, store private keys and sign blockchain transactions

# BASICS

UTXO

Unspent transaction output - a scheme how balances are stored in bitcoin blockchain 

{Ethereum}

Most popular blockchain that runs smart contracts

# Ethereum
# Ethereum

Blockchain knows only what it was told about

Online
Service

User

Service

# Ethereum

Oracles everywhere

# Ethereum

Ethereum Virtual Machine (EVM)

# Ethereum

Transaction fees and gas management

Gas refers to the fee, or pricing value, required to successfully conduct a transaction or execute a contract on the Ethereum blockchain

Opcode Gas
ADD 3
MUL 5
SUB 3
DIV 5
JUMP 8
AND | OR  3
SSTORE 20 000
SLOAD 2 100
BALANCE 2 600

Example smart contract

// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0 <0.9.0;

contract Token {
    string constant public name = "My Coin";
    string constant public symbol = "COINX";
    uint8 constant public decimals = 18;

    mapping(address => uint256) private _balances;
  
    constructor(uint256 supply) {
        _balances[msg.sender] = supply;
    } 
  
    function balanceOf(address tokenHolder) public view returns (uint256) {
        return _balances[tokenHolder];
    }

    function transfer(address recipient, uint256 amount) public {
        require(_balances[msg.sender] >= amount, "User is over spending");
        _balances[msg.sender] = _balances[msg.sender] - amount;
        _balances[recipient] = _balances[recipient] + amount;
    }
}
# SMART CONTRACTS

ERC20 token standard

interface IERC20 {

    function totalSupply() external view returns (uint256);
    function balanceOf(address account) external view returns (uint256);
    function transfer(address recipient, uint256 amount) external returns (bool);
    
    function allowance(address owner, address spender) external view returns (uint256);
    function approve(address spender, uint256 amount) external returns (bool);
    function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
    function upgrade(uint256 value) external;

    event Transfer(address indexed from, address indexed to, uint256 value);
    event Approval(address indexed owner, address indexed spender, uint256 value);
  
}
# SMART CONTRACTS

Uniswap DEX

function swap(uint amount0Out, uint amount1Out, address to, bytes calldata data) external lock {
        require(amount0Out > 0 || amount1Out > 0, 'UniswapV2: INSUFFICIENT_OUTPUT_AMOUNT');
        (uint112 _reserve0, uint112 _reserve1,) = getReserves(); // gas savings
        require(amount0Out < _reserve0 && amount1Out < _reserve1, 'UniswapV2: INSUFFICIENT_LIQUIDITY');

        uint balance0;
        uint balance1;
        { // scope for _token{0,1}, avoids stack too deep errors
        address _token0 = token0;
        address _token1 = token1;
        require(to != _token0 && to != _token1, 'UniswapV2: INVALID_TO');
        if (amount0Out > 0) _safeTransfer(_token0, to, amount0Out); // optimistically transfer tokens
        if (amount1Out > 0) _safeTransfer(_token1, to, amount1Out); // optimistically transfer tokens
        if (data.length > 0) IUniswapV2Callee(to).uniswapV2Call(msg.sender, amount0Out, amount1Out, data);
        balance0 = IERC20(_token0).balanceOf(address(this));
        balance1 = IERC20(_token1).balanceOf(address(this));
        }
        uint amount0In = balance0 > _reserve0 - amount0Out ? balance0 - (_reserve0 - amount0Out) : 0;
        uint amount1In = balance1 > _reserve1 - amount1Out ? balance1 - (_reserve1 - amount1Out) : 0;
        require(amount0In > 0 || amount1In > 0, 'UniswapV2: INSUFFICIENT_INPUT_AMOUNT');
        { // scope for reserve{0,1}Adjusted, avoids stack too deep errors
        uint balance0Adjusted = balance0.mul(1000).sub(amount0In.mul(3));
        uint balance1Adjusted = balance1.mul(1000).sub(amount1In.mul(3));
        require(balance0Adjusted.mul(balance1Adjusted) >= uint(_reserve0).mul(_reserve1).mul(1000**2), 'UniswapV2: K');
        }

        _update(balance0, balance1, _reserve0, _reserve1);
        emit Swap(msg.sender, amount0In, amount1In, amount0Out, amount1Out, to);
    }
# SMART CONTRACTS

{Practical exercises}

Tooling

Development environment

1.

2.

Crypto wallets

3.

# PRACTICAL EXERCISE

Generate own private key

from cryptos import sha256
from eth_keys import keys
from eth_utils import decode_hex

# Generate Private Key
priv_key_bytes = decode_hex(sha256('Super Cool Seed'))  # You should use random string here
priv_key = keys.PrivateKey(priv_key_bytes)

# Generate Public Key
pub_key = priv_key.public_key
print(pub_key.to_hex()) 

# Generate wallet address
address = pub_key.to_checksum_address()
assert address == '0x1244c0c7DE707afDD19AE6309E37A07169bC85ae'
# Ethereum
# PRACTICAL EXERCISE

Own Token

Task: create own token and deploy it into Ethereum Goerli testnet

Get in touch

@chompomonim

linkedin.com/in/jarolt

me@jaro.lt

Introduction into smart contracts developlent

By Jaro

Introduction into smart contracts developlent

  • 329