Skip to content

Latest commit

 

History

History
57 lines (51 loc) · 1.42 KB

File metadata and controls

57 lines (51 loc) · 1.42 KB

库合约

库合约和普通合约的区别

  • 不能存在状态变量
  • 不能够继承和被继承
  • 不能接受以太币
  • 不可以被销毁
  • 库合约的可见效为public或者external时,为delegatecall调用

如何使用库合约

  1. 使用using for 指令
    // 利用using for指令
    using Strings for uint256;
    function getString1(uint256 _number) public pure returns(string memory){
        // 库合约中的函数会自动添加为uint256型变量的成员
        return _number.toHexString();
    }
    
  2. 通过库合约名称调用函数
    // 直接通过库合约名调用
    function getString2(uint256 _number) public pure returns(string memory){
    return Strings.toHexString(_number);
    }
    

常用的库合约有

  • Strings:将uint256转换为String
  • Address:判断某个地址是否为合约地址
  • Create2:更安全的使用Create2 EVM opcode
  • Arrays:跟数组相关的库合约

Import

通过源文件相对位置导入

文件结构
├── Import.sol
└── Yeye.sol

// 通过文件相对位置import
import './Yeye.sol';

通过网址引入

// 通过网址引用
import 'https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/utils/Address.sol';

通过npm目录导入

import '@openzeppelin/contracts/access/Ownable.sol';

指定全局符号

import {Yeye} from './Yeye.sol';