Importer et charger des fichiers sources dans Solidity

Il y a deux raisons principales de charger des fichiers externes dans Remix :

  • pour importer une bibliothèque ou une dépendance (pour les fichiers que vous ne modifierez PAS)

  • to load some files for manipulation and editing (for files you might want to edit)

Importation d’une bibliothèque ou d’une dépendance

When importing from npm, or a URL (like GitHub, an IPFS gateway, or a Swarm gateway) you do not need to do anything more than use the import statement in your contract. The dependencies do not need to be « preloaded » into the File Explorer’s current Workspace before the contract is compiled.

Files loaded from the import statement are placed in the File Explorer’s current Workspace’s .deps folder.

Under the hood, Remix checks to see if the files are already loaded in the .deps directory. If not, it gets them via unpkg if it is an npm lib.

Voici quelques exemples de déclarations d’importation :

Import from npm

Use an npm package name directly in the import path. Remix resolves the package automatically via unpkg.

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts@4.2.0/token/ERC20/ERC20.sol";

Note

In the scoped-package example above, @openzeppelin is the name of the npm library. In the following example the library’s name does not begin with an @ - but Remix will go and check npm for a library of that name.

import "solidity-linked-list/contracts/StructuredLinkedList.sol";

Import from a GitHub URL

Paste a full GitHub URL to import a file directly from a public repository.

import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.0/contracts/math/SafeMath.sol";

You should specify the release tag (where available), otherwise you will get the latest code in the main branch. For OpenZeppelin Contracts you should only use code published in an official release, the example above imports from OpenZeppelin Contracts v2.5.0.

Importer depuis Swarm

Use a bzz-raw:// URI to import a file stored on the Swarm decentralized storage network.

import 'bzz-raw://5766400e5d6d822f2029b827331b354c41e0b61f73440851dd0d06f603dd91e5';

Importation à partir d’IPFS

Use an ipfs:// URI to import a file stored on the IPFS network.

import 'ipfs://Qmdyq9ZmWcaryd1mgGZ4PttRNctLGUSAMpPqufsk6uRMKh';

Importer un fichier local qui n’est pas dans .deps

Pour importer un fichier qui ne se trouve PAS dans le dossier .deps, utilisez un chemin relatif (./). Par exemple, vous pouvez utiliser un chemin relatif (./) :

import "./myLovelyLovelyLib.sol";

Avertissement

Imports cannot reference files in a different Workspace. Both the contract and its dependencies must be in the same Workspace.

Importation d’un fichier à partir du système de fichiers de votre ordinateur

This method uses Remix Desktop, which has direct access to your computer’s filesystem. Open the folder you want to work with from within the Remix Desktop app and its files will appear in the File Explorer.

En savoir plus sur le mot-clé import

Pour une explication détaillée du mot-clé import, voir la [documentation Solidity] (https://docs.soliditylang.org/en/latest/layout-of-source-files.html?highlight=import#importing-other-source-files).

Importing files for manipulation

When importing from the home tab widgets or with a remix command in the console, the files are placed in the root of the current Workspace inside a folder that shows their source - eg GitHub or gists.

Boutons d’importation dans l’onglet d’accueil de Remix

The Gist, GitHub, Swarm, IPFS, & HTTPS buttons are to assist in getting files into Remix so you can explore.

En cliquant sur l’un des boutons d’importation, vous ferez apparaître une fenêtre modale comme celle-ci :

No need to wrap the URL or ID in quotes.

Chargement avec une commande remix dans la console

Les 2 commandes remix pour le chargement sont :

  • remix.loadurl(url)

  • remix.loadgist(id)

remix.loadurl('https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.0/contracts/math/SafeMath.sol')
remix.loadgist('5362adf2000afa4cbdd2c6d239e06bf5')

Accès aux fichiers chargés à partir de l’onglet Accueil ou d’une commande de remixage

When you load from GitHub, a folder named github is created in the root of your current workspace. To import a file from the github folder, you would use a command like this:

import "github/OpenZeppelin/openzeppelin-contracts/contracts/math/SafeMath.sol";

Notice that this import statement doesn’t include the version information that was in the remix.load(url) command. So it is recommended that you use the methods described at the top of this page for importing dependencies that you are not intending to edit.

Assume the .sol file that contained the import statement above is in the contracts folder. Notice that this import statement didn’t need to traverse back to the github folder with a relative path like: ../github.