時間:2023-06-17|瀏覽:233
由于智能合約是放在區(qū)塊鏈上的代碼,因此調(diào)試時存在很多困難。但我們可以使用在線編譯器https://remix.ethereum.org。
下面是Hello World的代碼:
pragma solidity ^0.4.4;
contract Counter { uint count = 0; address owner; constructor public() { owner = msg.sender; } function increment public() { uint step = 10; if(owner == msg.sender){ count = count + step; } } function getCount constant public returns(uint) { return count; } function kill public() { if(owner == msg.sender) { selfdestruct(owner); } } }
第1行代表Solidity的版本,^代表向上兼容版本5.0。
第2行表示Counter為一個智能合約類對象。
第3、4行定義了屬性:
uint count = 0; address owner;
下面是三個函數(shù),其中構(gòu)造函數(shù)為:
constructor public() { owner = msg.sender; }
這段代碼里的實現(xiàn)方式比較簡單,它實現(xiàn)了一個簡單的計數(shù)器功能。但這樣的代碼是很重要的,因為它讓我們更好地理解智能合約的機制。