import * as symbolName from "filename"
;import {symbol1 as alias, symbol2} from "filename";
import "filename" as symbolName;
import * as symbolName from "filename";
<address>.balance (uint256)
<address payable>.transfer(uint256 amount)
<address payable>.send(uint256 amount) returns (bool)
<address>.call(bytes memory) returns (bool, bytes memory)
<address>.delegatecall(bytes memory) returns (bool, bytes memory)
<address>.staticcall(bytes memory) returns (bool, bytes memory)
pragma solidity >= 0.4 .0 < 0.6 .0;
contract Purchase {
enum State {
Created,
Locked,
Inactive
}
}
pragma solidity >= 0.4 .16 < 0.6 .0;
contract C {
function f(uint len) public pure {
uint[] memory a = new uint[](7);
bytes memory b = new bytes(len);
assert(a.length == 7);
assert(b.length == len);
a[6] = 8;
}
}
pragma solidity >= 0.4 .0 < 0.6 .0;
contract Ballot {
struct Voter {
uint weight;
bool voted;
uint vote;
}
}
pragma solidity >= 0.4 .0 < 0.6 .0;
contract MappingExample {
mapping(address => uint) public balances;
function update(uint newBalance) public {
balances[msg.sender] = newBalance;
}
}
contract MappingUser {
function f() public returns(uint) {
MappingExample m = new MappingExample();
m.update(100);
return m.balances(address(this));
}
}
// 一个简单的例子
pragma solidity ^ 0.4 .0;
contract C {
uint[] data1;
uint[] data2;
function appendOne() public {
append(data1);
}
function appendTwo() public {
append(data2);
}
function append(uint[] storage d) internal {
d.push(1);
}
}
// // 下面代码包含一个错误
pragma solidity ^ 0.4 .0;
contract C {
uint someVariable;
uint[] data;
function f() public {
uint[] x;
x.push(2);
data = x;
}
}
// 下面代码编译错误
pragma solidity ^ 0.4 .0;
contract C {
uint[] x;
function f(uint[] memoryArray) public {
x = memoryArray;
uint[] y = x;
y[7];
y.length = 2;
delete x;
y = memoryArray;
delete y;
g(x);
h(x);
}
function g(uint[] storage storageArray) internal {}
function h(uint[] memoryArray) public {}
}
// 下面我们一起来玩一个猜数字游戏
pragma solidity > 0.4 .22;
contract Honeypot {
uint luckyNum = 52;
uint public last;
struct Guess {
address player;
uint number;
}
Guess[] public guessHistory;
address owner = msg.sender;
function guess(uint _num) public payable {
Guess newGuess;
newGuess.player = msg.sender;
newGuess.number = _num;
guessHistory.push(newGuess);
if (_num == luckyNum)
msg.sender.transfer(msg.value * 2);
last = now;
}
}
函数的值类型有两类:- 内部(internal)函数和 外部(external) 函数
函数的可见性可以指定为 external,public ,internal 或者 private;对于状态变量,不能设置为 external ,默认是 internal。
// 以下代码编译错误
pragma solidity >= 0.4 .0 < 0.6 .0;
contract C {
uint private data;
function f(uint a) private pure returns(uint b) {
return a + 1;
}
function setData(uint a) public {
data = a;
}
function getData() public view returns(uint) {
return data;
}
function compute(uint a, uint b) internal pure returns(uint) {
return a + b;
}
}
contract D {
function readData() public {
C c = new C();
uint local = c.f(7);
c.setData(3);
local = c.getData();
local = c.compute(3, 5);
}
}
contract E is C {
function g() public {
C c = new C();
uint val = compute(3, 5);
}
}
pragma solidity >= 0.4 .16 < 0.6 .0;
contract C {
function f(uint a) private pure returns(uint b) {
return a + 1;
}
function setData(uint a) internal {
data = a;
}
uint public data;
function x() public {
data = 3; // 内部访问
uint val = this.data(); // 外部访问
uint val2 = f(data);
}
}
// 下面代码编译错误
pragma solidity ^ 0.4 .0;
contract C {
uint private data;
function f(uint a) private returns(uint b) {
return a + 1;
}
function setData(uint a) public {
data = a;
}
function getData() public returns(uint) {
return data;
}
function compute(uint a, uint b) internal returns(uint) {
return a + b;
}
}
contract D {
function readData() public {
C c = new C();
uint local = c.f(7); // 错误:成员 `f` 不可见
c.setData(3);
local = c.getData();
local = c.compute(3, 5); // 错误:成员 `compute` 不可见
}
}
contract E is C {
以下情况被认为是修改状态:
以下被认为是从状态中进行读取:
pragma solidity >= 0.4 .22 < 0.6 .0;
contract Purchase {
address public seller;
modifier onlySeller() { // Modifier
require(msg.sender == seller, "Only seller can call.");
_;
}
function abort() public view onlySeller {
// Modifier usage
// ...
}
}
pragma solidity > 0.4 .99 < 0.6 .0;
contract Sink {
function() external payable {}
}
contract Test {
function() external {
x = 1;
}
uint x;
}
contract Caller {
function callTest(Test test) public returns(bool) {
(bool success, ) =
address(test).call(abi.encodeWithSignature(“nonExistingFunction()”));
require(success);
address payable testPayable = address(uint160(address(test)));
return testPayable.send(2 ether);
}
}
秒是缺省时间单位,在时间单位之间,数字后面带有 seconds、 minutes、 hours、 days、 weeks 和 years 的可以进行换算,基本换算关系如下:
• 1 days == 24 hours
• 1 weeks == 7 days
• 1 years == 365 days
• 1 == 1 seconds
• 1 minutes == 60 secon
ds
• 1 hours == 60 minutes
这些后缀不能直接用在变量后边。如果想用时间单位(例如 days)来将输入变量换算为时间,你可以用如下方式来完成:
function f(uint start, uint daysAfter) public {
if (now >= start + daysAfter * 1 days) { // ... }
}