Déployer et exécuter (partie 2)

Interacting with deployed contracts

After a contract is deployed or after it is accessed with AtAddress, the deployed instance will appear in the Deploy/Unpinned Contracts section at the bottom of the Deploy & Run panel.

The deployed contract’s address is visible as are a few other icons - one of which is the pin icon.

Pinned contracts

When a contract is pinned, it will jump up to the Pinned Contracts section and Remix will save the contract’s address and the ABI (in the .deploys folder of the current Workspace). When Remix is refreshed, the pinned contracts will be loaded into the Pinned Contracts section.

Pinned contracts are chain & Workspace specific

Because a pinned contract’s address and ABI are stored in a File Explorer Workspace, the same Workspace must be active to see its pinned contracts. Similarly, only the pinned contracts of the currently selected chain will show.

Functions

To see a contract’s functions, click the caret on the left side of the panel.

The functions” buttons can have different colors.

  • Blue buttons are for view or pure functions. Clicking a blue button does not create a new transaction - so there will be no gas fees.

  • Orange buttons are for non-payable functions. Non-payable functions change the state of the contract BUT do not accept value (typically ETH) being sent with the transaction. Clicking an orange button will create a transaction and will cost gas.

  • Red buttons are for payable functions. Clicking a red button will create a new transaction and this transaction can accept a value (typically ETH). The amount of value is set in in the Value field which is under the Gas Limit field.

See more information about Solidity modifiers - in the Solidity docs.

Saisie des paramètres

A function has two views - the collapsed and the expanded view, which is visible after clicking the caret on the right side of the panel.

  • The input box shows the expected type of each parameter.

  • Les nombres et les adresses ne doivent pas être placés entre guillemets.

  • Les chaînes de caractères n’ont pas besoin d’être enveloppées.

Saisie de paramètres dans la vue réduite

In the collapsed view:

  • Les paramètres sont séparés par des virgules.

Dans l’exemple ci-dessus, la fonction « delegate » a 3 paramètres.

Saisie de paramètres dans la vue élargie

Clicking the “down” caret brings you to the expanded view - where parameters are input one at a time.

Interactions de bas niveau

Les interactions de bas niveau sont utilisées pour envoyer des fonds ou des données d’appel ou des fonds et des données d’appel à un contrat par l’intermédiaire de la fonction receive() ou fallback(). En règle générale, vous n’avez besoin d’implémenter la fonction fallback que si vous suivez un modèle de mise à niveau ou de procuration.

La section des interactions de bas niveau se trouve sous les fonctions de chaque contrat déployé.

Veuillez noter ce qui suit :

  • Si vous exécutez un simple transfert d’Ether vers un contrat, vous devez avoir la fonction receive() dans votre contrat. Si votre contrat a été déployé et que vous souhaitez lui envoyer des fonds, vous devez saisir le montant d’Ether ou de Wei, etc. (voir A dans le graphique ci-dessous), puis saisir rien dans le champ calldata de Interactions de bas niveau (voir B dans le graphique) et cliquer sur le bouton Transact (voir C dans le graphique ci-dessous).

  • Si vous envoyez des données à votre contrat avec l’Ether, alors vous devez utiliser la fonction fallback() et l’avoir avec l’état de mutabilité de payable.

  • Si vous n’envoyez pas d’éther au contrat mais envoyez des données d’appel, vous devez utiliser la fonction fallback().

  • Si vous enfreignez les règles lors de l’utilisation des interactions de bas niveau, vous recevrez un avertissement.

Veuillez consulter les [solidity docs] (https://solidity.readthedocs.io/en/latest/contracts.html#receive-ether-function) pour plus de détails sur l’utilisation des fonctions fallback et receive.

Inputting a tuple or struct to a function

To pass a tuple, you need to put in an array [].

Similarly, to pass in a struct as a parameter of a function, it needs to be put in as an array [].

NOTE: the file’s pragma must be set to: pragma experimental ABIEncoderV2;

Exemple de passage d’une structure imbriquée à une fonction

Considérez une structure imbriquée définie comme suit :

struct Garden {
    uint slugCount;  
    uint wormCount;
    Flower[] theFlowers;
}
struct Flower {
    uint flowerNum;
    string color;
}

Si une fonction a la signature fertilizer(Garden memory gardenPlot) alors la syntaxe correcte est :

[1,2,[[3,"Petunia"]]]

Pour poursuivre cet exemple, voici un exemple de contrat :

pragma solidity >=0.4.22 <0.7.0;
pragma experimental ABIEncoderV2;

contract Sunshine {
    struct Garden {
      uint slugCount;  
      uint wormCount;
      Flower[] theFlowers;
    }
    struct Flower {
        uint flowerNum;
        string color;
    }
      
    function fertilizer(Garden memory gardenPlot) public {
        uint a = gardenPlot.slugCount;
        uint b = gardenPlot.wormCount;
        Flower[] memory cFlowers = gardenPlot.theFlowers;
        uint d = gardenPlot.theFlowers[0].flowerNum;
        string memory e = gardenPlot.theFlowers[0].color;
    }
}

Après avoir compilé, déployé le contrat et ouvert l’instance déployée, nous pouvons ajouter les paramètres d’entrée suivants à la fonction nommée fertilizer :

[1,2,[[3,"Black-eyed Susan"],[4,"Pansy"]]]

La fonction fertilizer accepte un seul paramètre du type Garden. Le type Jardin est une struct. Les structures sont enveloppées dans des crochets carrés. À l’intérieur de Jardin se trouve un tableau qui est un tableau de structures nommé lesFleurs. Il y a un jeu de crochets pour le tableau et un autre pour la structure. D’où les doubles crochets.