Supongamos que tengo un contrato simbólico de la siguiente manera. ¿ Cómo puedo escribir el nuevo migration.js
archivo en truffle para implementar el contrato?
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_migración_inicial.js
var Migrations = artifacts.require("./Migrations.sol");
module.exports = function(deployer) {
deployer.deploy(Migrations);
};
Estoy pensando en algo como esto, pero no estoy seguro si es correcto
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);
};
Según mi conocimiento, hay dos tipos de bibliotecas:
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
};
Sí, tienes razón, pero necesitas linkind para truffle cuando usas bibliotecas importadas explícitamente, mientras tanto puedes usar:
Contrato:
pragma solidity ^0.4.23;
import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
contract TestCoin is ERC20 {
}
Guión de implementación:
var TestCoin = artifacts.require("./TestCoin.sol");
module.exports = function(deployer) {
deployer.deploy(TestCoin);
};
apilar
Águila
SafeMath
,TestCoin
porque no usaSafeMath
en el constructorapilar
apilar
Águila
npm
paquete (openzeppelin-solidity
), cuando usa la biblioteca, no necesita vincularla.