• Solidity智能合约开发 — 3.5-库合约


    1. 库合约介绍

    库函数是一种特殊的合约,主要是为了提升solidity代码的复用性和减少gas fee而存在。库合约一般都是一些好用的函数合集(库函数),由大神或者项目方创作

    智能合约相比,目前库在使用存在一些限制:

    • 没有状态变量
    • 不能够继承或被继承
    • 不能接受以太币
    • 不能别销毁

    常用的库:

    • String:将uint256转换为String
    • Address:判断某个地址是否为合约地址
    • Create2:更安全的使用Create2 EVM opcode
    • Arrays:跟数组相关的库函数
    1. library Strings {
    2. bytes16 private constant _HEX_SYMBOLS = "0123456789abcdef";
    3. /**
    4. * @dev Converts a `uint256` to its ASCII `string` decimal representation.
    5. */
    6. function toString(uint256 value) public pure returns (string memory) {
    7. // Inspired by OraclizeAPI's implementation - MIT licence
    8. // https://github.com/oraclize/ethereum-api/blob/b42146b063c7d6ee1358846c198246239e9360e8/oraclizeAPI_0.4.25.sol
    9. if (value == 0) {
    10. return "0";
    11. }
    12. uint256 temp = value;
    13. uint256 digits;
    14. while (temp != 0) {
    15. digits++;
    16. temp /= 10;
    17. }
    18. bytes memory buffer = new bytes(digits);
    19. while (value != 0) {
    20. digits -= 1;
    21. buffer[digits] = bytes1(uint8(48 + uint256(value % 10)));
    22. value /= 10;
    23. }
    24. return string(buffer);
    25. }
    26. /**
    27. * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation.
    28. */
    29. function toHexString(uint256 value) public pure returns (string memory) {
    30. if (value == 0) {
    31. return "0x00";
    32. }
    33. uint256 temp = value;
    34. uint256 length = 0;
    35. while (temp != 0) {
    36. length++;
    37. temp >>= 8;
    38. }
    39. return toHexString(value, length);
    40. }
    41. /**
    42. * @dev Converts a `uint256` to its ASCII `string` hexadecimal representation with fixed length.
    43. */
    44. function toHexString(uint256 value, uint256 length) public pure returns (string memory) {
    45. bytes memory buffer = new bytes(2 * length + 2);
    46. buffer[0] = "0";
    47. buffer[1] = "x";
    48. for (uint256 i = 2 * length + 1; i > 1; --i) {
    49. buffer[i] = _HEX_SYMBOLS[value & 0xf];
    50. value >>= 4;
    51. }
    52. require(value == 0, "Strings: hex length insufficient");
    53. return string(buffer);
    54. }
    55. }

    2. 使用库合约

    我们用String库函数的toHexString()来演示两种使用库合约中函数的办法。

    1. 利用using for指令

    指令 using A for B; 可用于附加库函数(从库 A)到任何类型(B)。添加完指令后,库A中的函数会自动添加为B类型变量的成员,可以直接调用。注意:在调用的时候,这个变量会被当作第一个参数传递给函数:

    1. // 利用using for指令
    2. using Strings for uint256;
    3. function getString1(uint256 _number) public pure returns(string memory){
    4. // 库函数会自动添加为uint256型变量的成员
    5. return _number.toHexString();
    6. }

    2. 通过库合约名称调用库函数

    1. // 直接通过库合约名调用
    2. function getString2(uint256 _number) public pure returns(string memory){
    3. return Strings.toHexString(_number);
    4. }
  • 相关阅读:
    3D感知技术(2)3D数据获取技术概述
    LCR 072. x 的平方根
    用字符串表达式执行引擎消除掉if else if
    正则表达式
    Python中的闭包
    LeGo-LOAM框架后端优化总结
    .Net Core IIS 程序报错 Access to the path c:\\windows\\TEMP\\poifiles is denied
    OptiCoupe 6:光学切割面板和型材切割优化[OptiCut]
    大三Web课程设计——悬崖上的波妞(4页) HTML+CSS(可以很好的应付老师的作业)
    虚拟现实VR技术在医疗行业的应用介绍
  • 原文地址:https://blog.csdn.net/qincheng168/article/details/128212005