美文网首页
合约开发(solidity)进阶2

合约开发(solidity)进阶2

作者: 张亚伦 | 来源:发表于2022-02-28 20:32 被阅读0次

底层函数解析

1. abi.encode, abi.encodePacked, abi.encodeWithSelector and abi.encodeWithSignature

Example:
bytes  memory  payload  =  abi.encodeWithSignature("register(string)",  "MyName");
(bool  success,  bytes  memory  returnData)  =  address(nameReg).call(payload);
require(success);

It is possible to adjust the supplied gas with the gas modifier:

address(nameReg).call{gas:  1000000}(abi.encodeWithSignature("register(string)",  "MyName"));

Similarly, the supplied Ether value can be controlled too:

address(nameReg).call{value:  1  ether}(abi.encodeWithSignature("register(string)",  "MyName"));

Lastly, these modifiers can be combined. Their order does not matter:

address(nameReg).call{gas:  1000000,  value:  1  ether}(abi.encodeWithSignature("register(string)",  "MyName"));

2. shr,calldataload,sub,calldatasize

EVM Dialect: https://docs.soliditylang.org/en/v0.8.9/yul.html#evm-dialect

assembly {
                ret := shr(96,calldataload(sub(calldatasize(),20)))
            }
  • shr(x, y) : logical shift right y by x bits
  • calldataload(p) : call data starting from position p (32 bytes)
  • sub(x, y) : x - y
  • calldatasize() : size of call data in bytes

解答如下:

image.png

相关文章

网友评论

      本文标题:合约开发(solidity)进阶2

      本文链接:https://www.haomeiwen.com/subject/gtzxrrtx.html