Home | 簡體中文 | 繁體中文 | 雜文 | 知乎專欄 | Github | OSChina 博客 | 雲社區 | 雲棲社區 | Facebook | Linkedin | 視頻教程 | 打賞(Donations) | About
知乎專欄多維度架構 | 微信號 netkiller-ebook | QQ群:128659835 請註明“讀者”

第 14 章 智能合約語言 Solidity v0.5.0

本文作者最近在找工作,有意向致電 13113668890

目錄

14.1. Remix - browser-solidity
14.1.1. 將 Remix(browser-solidity) 安裝到本地
14.1.2. 輸入數組
14.2. solc 命令
14.2.1. 使用 solc 編譯 *.sol 代碼
14.3. 智能合約入門演示
14.4. 數據類型
14.4.1. 數值型
14.4.1.1. 加 +,減 -,乘 *,除 / 運算演示
14.4.1.2. 求余 % 運算演示
14.4.1.3. 冪運算演示
14.4.1.4. 與 &,| 或,非 ~,異或 ^ 演示
14.4.1.5. 位移演示
14.4.2. 字元串
14.4.2.1. 獲取字元串長度
14.4.3. 布爾(Booleans)
14.4.4. 位元組類型
14.4.5. 數組
14.4.5.1. length
14.4.5.2. push() 方法
14.4.6. 枚舉類型
14.4.7. 結構體
14.4.7.1. 函數返回Struct
14.4.8. address
14.4.8.1. payable
14.4.8.2. .value()
14.4.8.3. .gas()
14.4.9. mapping
14.5. 單位
14.5.1. 貨幣單位(Ether Units)
14.5.2. 時間單位(Time Units)
14.6. 變數
14.6.1. 全局變數
14.6.2. storage
14.6.3. memory
14.7. 函數
14.7.1. 構造方法
14.7.2. 定義函數
14.7.3. 函數返回值
14.7.4. 參數傳遞
14.7.5. 函數的例子
14.7.6. Fallback function
14.7.7. modifier
14.8. 事件
14.9. 面向對象編程
14.9.1. 可見性修飾符
14.9.2. 錯誤處理
14.9.3. interface 介面
14.9.4. library 庫
14.9.4.1. 使用庫來擴展數據類型
14.9.5. 繼承
14.10. 合約調用
14.11. 合約接收 ETH
14.11.1. 調用 selfdestruct(msg.sender); 取出合約中的 ETH
14.11.2. 自動退款合約
14.11.3. 收款合約自動轉賬
14.11.4. 指定賬號提取 ETH
14.12. 合約中實例化一個介面
14.13. 合約中實例化另一個合約
14.13.1. msg.sender 與 this 的區別
14.13.2. 地址格式
14.14. Solidity 安全問題
14.14.1. 整型溢出
14.15. solidity example
14.15.1. Voting
14.15.2. MetaCoin
14.15.3. Anonymous voting on Ethereum without a tally authority. Protocol from this paper
14.15.4. Ballot
14.15.5. Conference

Solidity 是什麼?Solidity是以太坊智能合約的編程語言。

14.1. Remix - browser-solidity

在綫使用 browser-solidity

https://ethereum.github.io/browser-solidity/ https://remix.ethereum.org/

國內網絡有時不給力,建議將 Remix 安裝到本地目錄。

14.1.1. 將 Remix(browser-solidity) 安裝到本地

共享合約目錄

			
npm install -g remixd
remixd -S "/home/ethereum/codebase/blocks/contracts"		
			
			

安裝 browser-solidity

				
git clone https://github.com/ethereum/browser-solidity 
cd browser-solidity 
npm install 
npm run prepublish

sudo chown -R $USER:$(id -gn $USER) /home/neo/.config

npm start
				
			

啟動後瀏覽器中輸入 http://localhost:8080 可以看到 Remix 界面

[注意]Web3 Provider

Remix 提供三種運行環境,常用的有 JavaScript VM 和 Web3 Provider (連接到 --rpc --rpcaddr="0.0.0.0" --rpccorsdomain "*" --rpcport 8545)

Web3 Provider 方式需要解鎖賬號和啟動挖礦

					
> personal.unlockAccount(eth.accounts[0],"");
> miner.start(2); admin.sleepBlocks(1); miner.stop();			
					
				

14.1.2. 輸入數組

			
	function mint(address[] _to, uint256 _value) public returns (bool success) {
        for (uint i=0; i<_to.length; i++) {
            balanceOf[_to[i]] = _value;
        }
        return true;
    }
			
			

在Remix中輸入數組的方法

			
["0x6F56648fbD2306f843442f8dC61d5C8861Fac7C9","0x81b7E08F65Bdf5648606c89998A9CC8164397647"]