以太坊开发 | 实战一个智能合约

Myth丶恋晨 2022-03-31 13:16 568阅读 0赞

场景:

根据具体需求尝试写一个合约过程中用到的语法。

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示例:

  1. pragma solidity >=0.4.0 <0.6.0;
  2. contract C {
  3. uint private data;
  4. function setData(uint a) public { data = a; }
  5. function getData() public view returns(uint) { return data; }
  6. function compute(uint a, uint b) internal pure returns (uint) { return a + b; }
  7. }
  8. contract D {
  9. function readData() public {
  10. C c = new C(); // Initialize an object of contract C
  11. /* setData and getData are public in contract C, so they can be called by another contract */
  12. c.setData(3); // Call the function setData in contract C
  13. local = c.getData(); // Call the function getData in contract C
  14. }
  15. }
  16. /* Contract E is derived from contract C */
  17. contract E is C {
  18. function g() public {
  19. C c = new C(); // Initialize an object of contract C
  20. /* compute is private in contract C, so it can be called by the contract E which is derived from contract C */
  21. uint val = compute(3, 5);
  22. }
  23. }

2| function的view/pure[链接]

点击上面链接查看Solildity官方英文教程View Function部分。

2.1 | view function

函数不会写state。

写操作包括:

  1. Writing to state variables.
  2. Emitting events.
  3. Creating other contracts.
  4. Using selfdestruct.
  5. Sending Ether via calls.
  6. Calling any function not marked view or pure.
  7. Using low-level calls.
  8. Using inline assembly that contains certain opcodes.

一个view函数的示例如下:

  1. pragma solidity >0.4.99 <0.6.0;
  2. contract C {
  3. function f(uint a, uint b) public view returns (uint) {
  4. return a * (b + 42) + now;
  5. }
  6. }

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函数的示例如下:

  1. pragma solidity >0.4.99 <0.6.0;
  2. contract C {
  3. function f(uint a, uint b) public pure returns (uint) {
  4. return a * (b + 42);
  5. }
  6. }

注意returns后面括号里只给出返回值的类型(不用给返回的变量名)。

3| 合约如何读transaction中的字段

msg.data 读取payload => 读data的长度msg.data.length == 0

msg.sig 读取交易签名

msg.value 读取交易金额

msg.sender 读取交易发起者的地址

4| Struct类型

声明和初始化一个struct对象的方法:

  1. struct Funder {
  2. address addr;
  3. uint amount;
  4. }
  5. 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, blocks
  • block.coinbase (address payable): current block miner’s address
  • block.difficulty (uint): current block difficulty
  • block.gaslimit (uint): current block gaslimit
  • block.number (uint): current block number
  • block.timestamp (uint): current block timestamp as seconds since unix epoch
  • gasleft() returns (uint256): remaining gas
  • msg.data (bytes calldata): complete calldata
  • msg.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 message
  • now (uint): current block timestamp (alias for block.timestamp)
  • tx.gasprice (uint): gas price of the transaction
  • tx.origin (address payable): sender of the transaction (full call chain)

6| 所有的opcodes[链接]

点击上面链接可查看所有opcodes

发表评论

表情:
评论列表 (有 0 条评论,568人围观)

还没有评论,来说两句吧...

相关阅读

    相关 智能合约 简介

    以太坊是一个分布式的计算平台。它会生成一个名为Ether的加密货币。程序员可以在以太坊区块链上写下“智能合约”,这些以太坊智能合约会根据代码自动执行。 以太坊是什么?

    相关 智能合约是什么?

    以太坊是最早提出做智能合约的平台。由于以太坊区块链被普遍接受,因此多数区块链的智能合约采取与以太坊相似的设计。本文将详细介绍以太坊的智能合约:它是什么?它有什么用? 以太...