0
solidity | type | 犀牛的博客
source link: https://benpaodewoniu.github.io/2023/02/20/solidity62/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
type 的用法。
type(C).name (string)
: 合约的名称type(C).creationCode (bytes memory)
: 合约的创建字节码type(C).runtimeCode (bytes memory)
: 合约的运行时字节码type(I).interfaceId (bytes4)
: 包含给定接口的EIP-165
接口标识符type(T).min (T)
: 所在整型 T 的最小值type(T).max (T)
: 所在整型 T 的最大值
interfaceId
这个是计算接口合约的方法。
看下面的合约
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
interface Test {
function hello() external pure;
function world(int) external pure;
}
interface IERC165 {
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
contract A{
function IERC20_interfaceId() public pure returns(bytes4){
return type(IERC20).interfaceId;
}
function IERC165_interfaceId() public pure returns(bytes4){
return type(IERC165).interfaceId;
}
function Test_interfaceId() public pure returns(bytes4){
return type(Test).interfaceId;
}
function calculate_Test_interfaceId() public pure returns (bytes4) {
Test i;
return i.hello.selector ^ i.world.selector;
}
}
这 4
个方法分别返回
calculate_Test_interfaceId
0xc6be8b58
Test_interfaceId
0xc6be8b58
IERC20_interfaceId
0x36372b07
IERC165_interfaceId
0x01ffc9a7
bytes4(keccak256('supportsInterface(bytes4)')) = 0x01ffc9a7
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK