---
# System prepended metadata

title: Learning-solidity note tz

---

# Learning-solidity note tz

reference:
https://github.com/willitscale/learning-solidity


## Tutorial 1 Basic

```solidity=
function getName() public view returns(string memory) {
  return name;
}
// public 代表此 function 暴露出來可用
// view 代表此 function 不會變更 contract 的 storage state，只會讀取
// 如果 view 改成 pure 會更極端，跟 storage state 不會有任何關係
```

## Tutorial 2 Inheritance
```solidity=
contract Bank {
  uint internal myInternalValue;
}
// internal 可被繼承者引用，但不能被其他外部使用，剩下的 private 代表不可被外部使用

interface Regulator {
  function checkValue(uint amount) external returns (bool);
  function loan() external returns (bool);
}

// interface 定義介面：function name, return value
// external 代表要被外部使用

...
contract Bank is Regulator {

...
// 介面實現
function checkValue(uint amount) public view returns (bool) {
  return value >= amount;
}

function loan() public view returns (bool) {
  return value > 0;
}
    
```


## Tutorial 3 modifiers and error handleing
```solidity=
modifier ownerFunc {
    require(owner == msg.sender); // require, revert, assert
    _; // 接下來要執行的 function
  }

function deposit(uint amount) public ownerFunc {
    saving +=amount;
  }
```
參考資料
https://medium.com/taipei-ethereum-meetup/%E6%AF%94%E8%BC%83-require-assert-%E5%92%8C-revert-%E5%8F%8A%E5%85%B6%E9%81%8B%E4%BD%9C%E6%96%B9%E5%BC%8F-30c24d534ce4


## Tutorial 4 imports and libraries
```solidity=

library IntExtended {
  function increment(uint _self) public pure returns(uint) {
    return _self + 1;
  }
}

contract tutorial_4_imports_and_libraries {
  using IntExtended for uint;

  function testIncrement(uint _base) public pure returns (uint) {
    return _base.increment();
  }
}
```

## Tutorial 5 event logging and transaction info
![](https://hackmd.io/_uploads/B1DrNI4hK.png)
```solidity=
event SenderLogger(address);
  event ValueLogger(uint);
fallback() external payable isOwner validValue {
    emit SenderLogger(msg.sender);
    emit ValueLogger(msg.value);
  }
```

## Tutorial 6 
```
// function fund() public payable {
    //     _accounts[msg.sender].balance += msg.value;
    // }
    
payable(myAddress).transfer(10);
```

