Solidity

Truffle 使用庫、介面和合約遷移程式碼

  • February 27, 2022

假設我有一個如下的代幣合約。我如何migration.js在松露中編寫新文件來部署契約?

測試幣.sol

import "openzeppelin-solidity/contracts/math/SafeMath.sol";
import "openzeppelin-solidity/contracts/token/ERC20/IERC20.sol";

contract ERC20 is IERC20 {

}

contract TestCoin is ERC20 {

}

1_initial_migration.js

var Migrations = artifacts.require("./Migrations.sol");

module.exports = function(deployer) {
 deployer.deploy(Migrations);
};

我正在考慮這樣的事情,但不確定它是否正確

var SafeMath = artifacts.require("./SafeMath.sol");
var TestCoin = artifacts.require("./TestCoin.sol");

module.exports = function(deployer) {
 deployer.deploy(SafeMath);
 deployer.link(SafeMath, TestCoin);
 deployer.deploy(TestCoin);
};

據我所知,有兩種類型的庫:

  1. 全部採用內部方法
  • 這些類型的庫沒有明確部署,它們包含在契約本身中。IE。來自 openzeppelin 的 SafeMath。
  • 內部庫不需要與主契約連結。
  • 它們包含在合約的字節碼中。
  • 考慮以下庫:
pragma solidity ^0.8.0;
// library for uint array 
library UintArrayLib {
    using UintArrayLib for uints;

    struct uints {
        uint[] array;
    }

    function add(uints storage self, uint _uint)
        internal
    {
        if(! exists(self, _uint)){
            self.array.push(_uint);
        }
    }

    function remove(
        uints storage self,
        uint _uint
    ) internal {
    for (uint256 i = 0; i < self.array.length; i++) {
            if (
                self.array[i] == _uint 
            ) {
                delete self.array[i];
            }
        }
    }


    function exists(
        uints storage self,
        uint _uint
    ) internal view returns (bool) {
        for (uint256 i = 0; i < self.array.length; i++) {
            if (
                self.array[i] == _uint 
            ) {
                return true;
            }
        }
        return false;
    }
}

contract BuyList is Ownable, ReentrancyGuard {
    using UintArrayLib for UintArrayLib.uints;
    // Rest of the code goes here
}
  • 為了部署 BuyList 合約,您將編寫以下遷移:
var BuyList = artifacts.require("BuyList");

module.exports = function(deployer) {
   deployer.deploy(BuyList);
};
  1. 至少一種公共方法
  • 這些合約的公共方法不直接包含在合約中,整個庫被顯式部署為新地址。
  • 這些庫需要連結到主合約。
  • 這些庫不包含在合約的字節碼中,主要在合約達到最大字節碼大小限制時使用。
  • 考慮以下庫範例。

pragma solidity ^0.8.0;
// library for uint array 
library UintArrayLib {
   using UintArrayLib for uints;

   struct uints {
       uint[] array;
   }

   function add(uints storage self, uint _uint)
       public
   {
       if(! exists(self, _uint)){
           self.array.push(_uint);
       }
   }

   function remove(
       uints storage self,
       uint _uint
   ) public {
   for (uint256 i = 0; i < self.array.length; i++) {
           if (
               self.array[i] == _uint 
           ) {
               delete self.array[i];
           }
       }
   }


   function exists(
       uints storage self,
       uint _uint
   ) internal view returns (bool) {
       for (uint256 i = 0; i < self.array.length; i++) {
           if (
               self.array[i] == _uint 
           ) {
               return true;
           }
       }
       return false;
   }
}

contract BuyList is Ownable, ReentrancyGuard {
   using UintArrayLib for UintArrayLib.uints;
   // Rest of the code goes here
}
  • 為了部署 BuyList 合約,您將編寫以下遷移:
var UintArrayLib = artifacts.require("UintArrayLib");
var BuyList = artifacts.require("BuyList");

module.exports = function(deployer) {
   deployer.deploy(UintArrayLib); // Explicity deployment of lib
   deployer.link(UintArrayLib, BuyList); // Linking the lib
   deployer.deploy(BuyList); // Deploying main contract
};

是的,你是對的,但是當你明確使用導入的庫時,你需要為松露使用 linkind,同時你可以使用:

契約:

pragma solidity ^0.4.23;

import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";

contract TestCoin is ERC20 {
}

部署腳本:

var TestCoin = artifacts.require("./TestCoin.sol");

module.exports = function(deployer) {
   deployer.deploy(TestCoin);
};

引用自:https://ethereum.stackexchange.com/questions/62049