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

 

场景:

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

 

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。

写操作包括:

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

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或<address>.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, 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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值