He creado una biblioteca e importado en mi contrato. No puedo vincular la biblioteca en el archivo de implementación.
solución necesaria. gracias en adv
pragma solidity ^0.4.2;
library StringUtils {
/// @dev Does a byte-by-byte lexicographical comparison of two strings.
/// @return a negative number if `_a` is smaller, zero if they are equal
/// and a positive numbe if `_b` is smaller.
function compare(string _a, string _b) returns (int) {
bytes memory a = bytes(_a);
bytes memory b = bytes(_b);
uint minLength = a.length;
if (b.length < minLength)
minLength = b.length;
//@todo unroll the loop into increments of 32 and do full 32 byte comparisons
for (uint i = 0; i < minLength; i ++) {
if (a[i] < b[i])
return -1;
else if (a[i] > b[i])
return 1;
if (a.length < b.length)
return -1;
else if (a.length > b.length)
return 1;
else
return 0;
}
}
/// @dev Compares two strings and returns true iff they are equal.
function equal(string _a, string _b) returns (bool) {
return compare(_a, _b) == 0;
}
/// @dev Finds the index of the first occurrence of _needle in _haystack
function indexOf(string _haystack, string _needle) returns (int) {
bytes memory h = bytes(_haystack);
bytes memory n = bytes(_needle);
if (h.length < 1 || n.length < 1 || (n.length > h.length))
return -1;
else if (h.length > (2**128 - 1))
return -1;
else {
uint subindex = 0;
for (uint i = 0; i < h.length; i ++) {
if (h[i] == n[0]) {
subindex = 1;
while (subindex < n.length && (i + subindex) < h.length && h[i + subindex] == n[subindex]) {
subindex++;
}
if (subindex == n.length) {
return int(i);
}
}
}
return -1;
}
}
}
mi contrato
import "./StringUtils.sol";
contract SaveData is Whitelist {
function verifyHash(address publicAddress, string hashstring) external returns (string) {
require(StringUtils.equal(data[publicAddress],hashstring));
}
}
en despliegue: -
var WhiteList = artifacts.require("./Whitelist.sol");
var SaveData = artifacts.require("./SaveData.sol");
var StringUtils = artifacts.require("./StringUtils.sol");
module.exports = function(deployer) {
deployer.deploy(WhiteList);
deployer.deploy(SaveData);
deployer.deploy(StringUtils);
};
Lo primero es lo primero, debe implementar su biblioteca en la cadena de bloques. Después de la implementación, tendrá una dirección de contrato inteligente, que debe usar más adelante para acceder a su biblioteca ( cómo hacer esto ).
No puede hacerlo cuando la biblioteca está almacenada localmente en su computadora.
Ha pasado mucho tiempo, pero por si alguien busca lo mismo.
La función de biblioteca se puede llamar interna o externamente. Si todas las funciones de la biblioteca son internal
o private
entonces ese contrato de la biblioteca está vinculado estáticamente a su contrato. Por lo tanto, no tiene que implementar el contrato de la biblioteca por separado y vincularlo. Si la función es external
o public
necesita implementarla por separado y usar la dirección del contrato de la biblioteca en cadena. A continuación se muestra un ejemplo de cómo se hace esto durante la migración de la trufa.
deployer.deploy(StringUtils);
deployer.link(StringUtils, [SaveData]);
deployer.deploy(SaveData);
Román Frólov
Sandeep Gupta