# Krypto Camp 第二週課前學習 Solidity ## Solidity > Solidity是什麼? Solidity 是靜態型語言,一種合約式導向的程式語言,用來撰寫智能合約,編譯後可以在 EVM上執行。 > EVM (Ethereum Virtual Machine):中文翻譯為「以太坊虛擬機」,是智能合約的運行環境。 就從每個入門程式Hello World開始。然後使用 Remix 在線上 IDE 進行部屬、測試。 > Hello World的合約 ``` =solidity // 說明這個合約License的類型 // SPDX-License-Identifier: MIT //solidity的版本 pragma solidity ^0.8.11; //contract 是保留字,用法類似於其他程式語言的class。HelloWorld合約名稱 contract HelloWorld { //string 字串型別、public 可視性、helloworld變數名。 string public helloworld = "Hello World!"; //建構式 constructor() {} } ``` >Solidity對函數和狀態變量提供了四種可視性 public (公開) : 任何使用者或者合約都能呼叫和訪問 external (外部) : 這些函式只能在合約之外呼叫 - 它們不能被合約內的其他函式呼叫 internal (內部) : 某個合約繼承自其父合約,這個合約即可以訪問父合約中定義的“內部”函式 private (私有) : 只能在其所在的合約中呼叫和訪問,即使是其子合約也不能訪問 | | public | external | internal | private | |:--------:|:------:|:--------:|:--------:|:-------:| | 內部調用 | 可 | 否 | 可 | 可 | | 繼承調用 | 可 | 否 | 可 | 否 | | 外部調用 | 可 | 可 | 否 | 否 | ## Solidity constructor(建構式) - 用於合約初始化數值的方法,創建合約的時候執行一次 - 放一些變數初始值 - 一個合約只能有一個構造函數 ## Solidity基本型別 #### Booleans 僅能使用true or false表示,不能使用0 or 1 ``` =solidity contract Bool { bool isActive = false; } ``` #### Integer 1. int - 可以有負數 - int8 to int256 (8的倍數) 2. uint - 只能是正數 - uint8 to uint256(8的倍數) ``` =solidity contract Integer { uint16 myNumber = 256; int256 myNumber_int = -270; } ``` #### Address 錢包的地址 每個地址紀錄的都是20 bytes ``` =solidity contract Address { //只能查看地址無法提供轉帳功能 address WETH = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; //如需使用付款轉帳功能必須加上payable, 例如 transfer address payable Vault = payable(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2); } ``` #### Bytes 1 byte = 8 bits 每個bit都是由 0, 1組成 ``` =solidity contract Bytes { bytes1 public b1 = 0x01; bytes2 public b2 = 0x01f2; bytes3 public b3 = 0x01f273; bytes4 public b4 = 0x01f273a0; } ``` #### String 一般用於提示錯誤、給使用者的訊息 儲存語意化的文字, 要用雙引號 " " ``` =solidity contract String { string message = "Hello World"; // 字串要用雙引號 " " string name = "KryptoCamp"; } ``` #### Enums 定義常數時,使用更有共同認知的方法 方便辨識、好閱讀 ``` =solidity contract Enums { // keyword enum enum State { TODO, // 0 COMPLETED, // 1 PAUSED // 2 } uint256 isZero = State.TODO; uint256 isOne = State.COMPLETED; uint256 isTwo = State.PAUSED; } ``` #### Struct 可讀性高且有結構性的物件 ``` =solidity contract Struct { struct Car { uint256 width; string color; string name; } Car myCar; constructor() { myCar = Car(300, "black", "BMW"); } function getCarColor() public view returns (string memory) { return myCar.color; } } ``` #### Array 可以固定大小或動態大小 ``` =solidity contract Arrays { // 動態大小 uint[] public arr; uint[] public arr2 = [1, 2, 3]; string[] public eggBox = ["egg1", "egg2", "egg3"]; // 固定大小 uint[10] public myFixedSizeArr; function dynamicArrays() public pure returns (uint256[] memory) { uint256[] memory luckyNumber = new uint256[](5); // 0,0,0,7,0 luckyNumber[3] = 7; luckyNumber[5] = 1; return luckyNumber; } } ``` #### Mapping mapping(keyType => valueType) ``` =solidity contract Mapping { mapping(address => uint256) public balances; constructor() { balances["msg"] = 1; balances[0x17F6AD8Ef982297579C203069C1DbfFE4348c372] = 15; } } ``` #### Variables 。local : 在function內, 不儲存在區塊鏈上 。state : 在function外, 儲存在區塊鏈上 。global : 提供有關區塊鏈的信息 ``` =solidity contract Variables { // State : 儲存在區塊鏈上 string public text = "Hello"; uint public num = 123; function doSomething() public { // Local : 不儲存在區塊鏈上 uint i = 456; // global uint timestamp = block.timestamp; // 當下區塊鏈的時間戳(timestamp) address sender = msg.sender; // 呼叫的address } } ``` #### Function function function-name(parameter-list) scope returns() { //statements } Example: ``` =solidity function foo() public { // do something.. } function foo2( int a ) public { // do something.. } function callMyMintNFT() public payable returns (string memory) { return _mintNFT(); } function _mintNFT() internal pure returns (string memory) { return "Mint Success."; } function getBalance() public view returns (uint256, string memory, address) { return (address(this).balance, "Hello World", 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4); } ``` #### 延伸讀物 [Solidity Tutorial](https://www.tutorialspoint.com/solidity/) [Solidity by Example](https://solidity-by-example.org/)