合约开发(solidity)进阶2

2022-02-28  本文已影响0人  95加不满

底层函数解析

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)))
            }

解答如下:

image.png
上一篇 下一篇

猜你喜欢

热点阅读