Truffle-Migrationscode mit Bibliothek, Schnittstelle und Vertrag

Angenommen, ich habe einen Token-Vertrag wie folgt. Wie kann ich die neue Datei in Trüffel schreiben, migration.jsum den Vertrag bereitzustellen?

TestCoin.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);
};

Ich denke an so etwas, bin mir aber nicht sicher, ob es richtig ist

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

Antworten (2)

Nach meinem Wissen gibt es zwei Arten von Bibliotheken:

  1. Alle mit interner Methode
    • Diese Art von Bibliotheken werden nicht ausdrücklich bereitgestellt, sie sind im Vertrag selbst enthalten. dh. SafeMath von openzeppelin.
    • Interne Bibliotheken müssen nicht mit dem Hauptvertrag verknüpft werden.
    • Sie sind im Bytecode des Vertrags selbst enthalten.
    • Betrachten Sie folgende Bibliothek:
     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
     }
    
    • Für die Bereitstellung des BuyList-Vertrags schreiben Sie die folgende Migration:
    var BuyList = artifacts.require("BuyList");
    
    module.exports = function(deployer) {
        deployer.deploy(BuyList);
    };
    
  2. Mindestens eine öffentliche Methode
    • Die öffentliche Methode dieses Vertrags ist nicht direkt im Vertrag enthalten und die gesamte Bibliothek wird ausdrücklich als neue Adresse bereitgestellt.
    • Diese Bibliotheken müssen mit dem Hauptvertrag verknüpft werden.
    • Diese Bibliotheken sind nicht im Bytecode des Vertrags enthalten und werden meistens verwendet, wenn der Vertrag die maximale Größe des Bytecodes erreicht.
    • Betrachten Sie das folgende Bibliotheksbeispiel.
    
    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
    }
    
    • Für die Bereitstellung des BuyList-Vertrags schreiben Sie die folgende Migration:
    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
    };
    

Ja, Sie haben Recht, aber Sie benötigen linkind für Truffle, wenn Sie explizit importierte Bibliotheken verwenden. Inzwischen können Sie Folgendes verwenden:

Vertrag:

pragma solidity ^0.4.23;

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

contract TestCoin is ERC20 {
}

Bereitstellungsskript:

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

module.exports = function(deployer) {
    deployer.deploy(TestCoin);
};
Was meinst du? Sie erwähnen, dass eine Verknüpfung für Trüffel erforderlich ist, wenn Sie explizit importierte Bibliotheken verwenden, aber im Beispiel des Bereitstellungsskripts u show keinen Link zur Bibliothek verwendet haben
1. Truffle fragt nicht nach der Verknüpfung einer externen Bibliothek. 2. Wenn Sie die Migration zu Testzwecken verwenden, müssen Sie nicht SafeMathmit verknüpfen, da Sie den Konstruktor TestCoinnicht verwendenSafeMath
Ich lasse die Verwendung von SafeMath im Code für Beispielcode weg. .. im eigentlichen Code wird Safemath verwendet. muss ich also SafeMath verknüpfen?
und was bedeutet es, dass Trüffel nicht auffordern, eine externe Bibliothek zu verknüpfen
externe Bibliothek ist Bibliothek aus npmPaket ( openzeppelin-solidity), wenn Sie die Bibliothek verwenden, müssen Sie nicht verknüpfen.