Library
SolidityDéfinition
Contrat spécial contenant du code réutilisable, déployé une seule fois. Utilisé avec le mot-clé using pour attacher des fonctions à des types. Les fonctions de bibliothèque sont internal par défaut. Ne peut pas avoir de variables d'état (sauf constants), ni recevoir d'ether. Économise du gas en mutualisant le code.
Version anglaise
Special contract containing reusable code, deployed once. Used with using keyword to attach functions to types. Library functions are internal by default. Cannot have state variables (except constants) or receive ether. Saves gas by sharing code.
Exemple de Code
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "Overflow");
return c;
}
}
contract MyContract {
using SafeMath for uint256; // Attache SafeMath aux uint256
uint256 public value;
function increment(uint256 x) public {
value = value.add(x); // Appel : value.add(x) au lieu de SafeMath.add(value, x)
}
}Pages Liées
Pratique ce concept sur Solingo
Maîtrise Library avec des exercices interactifs et un IDE intégré.
Commencer gratuitement