Mushroomy's 5th Blockcahin Story

blockchain

* geth REPL (Read Eval Print Loop)

REPL will be appeared to you if you start startgethconsole.sh. Practice these geth commands.

miner.start()
eth.pendingTransactions
eth.getTransaction("{transaction-hash-id}")
eth.blockNumber
miner.stop()

personal.newAccount()
miner.setEtherbase(eth.accounts[1])


- web3

Ethereum javascript API, so, can manage ethereum blockchain transactions in web browser.

Proxy {_requestManager: a, currentProvider: Proxy, eth: n, db: e.exports, shh: s, …}


* Solidity (language)

A computer language for smart contract of blockchain.
참고: remix.ethereum.org


pragma solidity ^0.4.25;

contract MyName {
    string myName = "Roomy";

    function getMyName() view public returns(string) {
        return myName;
    }

    function setMyName(string name) public {
        myName = name;
    }
}


1. Access Modifier (접근제어자)


2. Function Type (함수타입)


이 타입을 쓰면 msg라는 객체를 받을 수 있다. 해당 트랜잭션을 발생시킨 사람이 msg가 된다.

pragma solidity ^0.4.25;

contract account {
    uint private amount = 100;

    function plus() payable public {
        amount += msg.value;
    }
}


3. Variable Type


4. Simple example: Smart contract

pragma solidity ^0.4.24;

contract Bank {
    uint private balance = 100;
    address public myAddress;

    constructor() public {
        myAddress = msg.sender;
    }

    function deposit() payable public {
        balance += msg.value;
    }

    function withdraw(uint n) public {
        if ((balance >= n) && (msg.sender == myAddress)) {
            balance -= n;
            msg.sender.transfer(n);
        }
    }

    function getBalance() view public returns(uint) {
        return balance;
    }
}


* Creating token in your network

pragma solidity ^0.4.24;

contract MyToken {
    string public name;
    string public symbol;
    uint8 public decimals;

    mapping (address => uint256) public balanceOf;
    event Transfer(address _from, address _to, uint _value);

    constructor(string tokenName,string tokenSymbol,uint8 decimalUnits,uint256 initialSupply) public {
        name = tokenName;
        symbol = tokenSymbol;
        decimals = decimalUnits;
        balanceOf[msg.sender] = initialSupply;
    }

    function transfer(address _to, uint256 _value) public {
        balanceOf[msg.sender] -= _value;
        balanceOf[_to] += _value;
        emit Transfer(msg.sender,_to,_value);
    }
}


- Good Ref for ERC20Token

참고: Open Zeppelin