合约(contract)由变量(variable)、函数(functions)、函数修饰符(function modifiers)、事件(events)、结构体类型(struct type)、枚举类型(enum type)组成。
pragma solidity ^0.4.0;
contract SimpleStorage {
uint storedData; // 变量
// ...
}
pragma solidity ^0.4.0;
contract SimpleAuction {
function bid() public payable { // 函数
// ...
}
pragma solidity ^0.4.11;
contract Purchase {
address public seller;
modifier onlySeller() { // 函数修改符
require(msg.sender == seller);
_;
}
function abort() public onlySeller { // 使用修饰符
// ...
}
}
pragma solidity ^0.4.11;
contract Ballot {
struct Voter { // 结构体
uint weight;
bool voted;
address delegate;
uint vote;
}
}
pragma solidity ^0.4.11;
contract Purchase {
enum State { Created, Locked, Inactive } // 枚举
}