Angenommen, ich habe einen Token-Vertrag wie folgt. Wie kann ich die neue Datei in Trüffel schreiben, migration.js
um 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);
};
Nach meinem Wissen gibt es zwei Arten von Bibliotheken:
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
}
var BuyList = artifacts.require("BuyList");
module.exports = function(deployer) {
deployer.deploy(BuyList);
};
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
}
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);
};
Stapelanzeige
Adler
SafeMath
mit verknüpfen, da Sie den KonstruktorTestCoin
nicht verwendenSafeMath
Stapelanzeige
Stapelanzeige
Adler
npm
Paket (openzeppelin-solidity
), wenn Sie die Bibliothek verwenden, müssen Sie nicht verknüpfen.