以太坊开发 | 实战一个智能合约
场景:
根据具体需求尝试写一个合约过程中用到的语法。
1| function和variables的visibility问题[链接]
规定函数、变量可以被谁调用。点击上面链接查看Solildity官方英文教程Visibility and Getters部分。
以太坊规定了两种调用智能合约的方式:
(1)internal:合约通过message调用合约,不会产生交易;
(2)external:外部账户通过transaction调用合约,会产生一笔交易。
在创建一个function或变量时,必须指定其属性:external/publlic/internal/private
- external:只能被外部账户或其它合约调用,不能被本合约内的其它函数调用。只适用于函数,不适用于变量。
- public:可以被其它合约通过message调用,也可以被合约内的函数调用
- internally:可以被本合约内的函数调用,也可以被继承本合约的子合约调用
- private:只能被本合约内的函数调用,不可以被继承本合约的子合约调用
public和private示例:
pragma solidity >=0.4.0 <0.6.0;
contract C {
uint private data;
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(); // Initialize an object of contract C
/* setData and getData are public in contract C, so they can be called by another contract */
c.setData(3); // Call the function setData in contract C
local = c.getData(); // Call the function getData in contract C
}
}
/* Contract E is derived from contract C */
contract E is C {
function g() public {
C c = new C(); // Initialize an object of contract C
/* compute is private in contract C, so it can be called by the contract E which is derived from contract C */
uint val = compute(3, 5);
}
}
2| function的view/pure[链接]
点击上面链接查看Solildity官方英文教程View Function部分。
2.1 | view function
函数不会写state。
写操作包括:
- Writing to state variables.
- Emitting events.
- Creating other contracts.
- Using
selfdestruct
. - Sending Ether via calls.
- Calling any function not marked
view
orpure
. - Using low-level calls.
- Using inline assembly that contains certain opcodes.
一个view函数的示例如下:
pragma solidity >0.4.99 <0.6.0;
contract C {
function f(uint a, uint b) public view returns (uint) {
return a * (b + 42) + now;
}
}
2.2 | pure function
函数不会读或写state。
其中读操作包括:
(1)读合约中的变量(state variables),
(2)address(this).balance或
.balance,(3)读block,tx,msg的信息(除msg.sig和msg.data之外),
(4)调用任何非pure的函数,
(5)使用包含某个opcodes的内联集合(这个不是很懂。。)
一个pure函数的示例如下:
pragma solidity >0.4.99 <0.6.0;
contract C {
function f(uint a, uint b) public pure returns (uint) {
return a * (b + 42);
}
}
注意returns后面括号里只给出返回值的类型(不用给返回的变量名)。
3| 合约如何读transaction中的字段
msg.data 读取payload => 读data的长度msg.data.length == 0
msg.sig 读取交易签名
msg.value 读取交易金额
msg.sender 读取交易发起者的地址
4| Struct类型
声明和初始化一个struct对象的方法:
struct Funder {
address addr;
uint amount;
}
Funder f = Funder({addr: msg.sender, amount: msg.value});
5| 区块、交易的属性字段
blockhash(uint blockNumber) returns (bytes32)
: hash of the given block - only works for 256 most recent, excluding current, blocksblock.coinbase
(address payable
): current block miner’s addressblock.difficulty
(uint
): current block difficultyblock.gaslimit
(uint
): current block gaslimitblock.number
(uint
): current block numberblock.timestamp
(uint
): current block timestamp as seconds since unix epochgasleft() returns (uint256)
: remaining gasmsg.data
(bytes calldata
): complete calldatamsg.sender
(address payable
): sender of the message (current call)msg.sig
(bytes4
): first four bytes of the calldata (i.e. function identifier)msg.value
(uint
): number of wei sent with the messagenow
(uint
): current block timestamp (alias forblock.timestamp
)tx.gasprice
(uint
): gas price of the transactiontx.origin
(address payable
): sender of the transaction (full call chain)
6| 所有的opcodes[链接]
点击上面链接可查看所有opcodes
还没有评论,来说两句吧...