Déployer et exécuter (partie 2)
This is part 2 of the documentation covering the Deploy & Run plugin. Part 1 covers deploying and accessing a contract.
Interacting with deployed contracts
After you deploy a contract or load it into Remix using Add Contract, the deployed instance will appear in the Deployed Contracts section on Deploy & Run.
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, its address and ABI are stored in the .deploys folder of the current Workspace. Upon reloading Remix, pinned contracts will automatically appear in the Deployed Contracts section, provided that both the Workspace and the connected blockchain network remain the same as when the contract was originally pinned.
Functions
To see a contract’s functions, click on the deployed contract.
The functions” buttons can have different colors.
Blue buttons are for
vieworpurefunctions. Clicking a blue button does not create a new transaction - so there will be no gas fees.Orange buttons are for
non-payablefunctions. 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
payablefunctions. 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 the Value field which is under the Gas Limit field.
Saisie des paramètres
A function has two views - the collapsed and the expanded view. Clicking the function brings you to the expanded view, where you can input the parameters required for the function.
The input box shows the expected type of each parameter.
Les nombres et les adresses ne doivent pas être placés entre guillemets.
Strings must be wrapped in double quotes.
Interactions de bas niveau
Low level interactions are used to send funds or calldata or funds & calldata to a contract through the receive() or fallback() function. Typically, you should only need to implement the fallback function if you are following an upgrade or proxy pattern.
To find the low level interactions section, you have to click the contract from the Deployed Contracts section, then click the plus icon with the label « Low level interaction ».
Note
If you are executing a plain Ether transfer to a contract, you need to have the receive() function in your contract. If your contract has been deployed and you want to send it funds, you would input the amount of Ether or Wei etc. (see A in graphic below), and then input NOTHING in the calldata field of Low level interactions (see B in graphic) and click the Transact button (see C in graphic below).
When using low level interactions, keep the following rules in mind:
If you are sending calldata to your contract with Ether, then you need to use the
fallback()function and have it with the state mutability of payable.If you are not sending ether to the contract but are sending call data then you need to use the
fallback()function.If you break the rules when using the Low level interactions you will see a warning.
Please see the Solidity docs for more specifics about using the fallback and receive functions.
Scientific Notation in Function Inputs
You can use scientific notation to pass numbers as function argument. For example, instead of inputting 12000000000000000000, you can input 12e18. This works for both arrays and single inputs.
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
For Solidity versions before 0.8.0, add pragma abicoder v2; or pragma experimental ABIEncoderV2; to your file. This is enabled by default in 0.8.0 and later.
Exemple de passage d’une structure imbriquée à une fonction
Considérez une structure imbriquée définie comme suit :
struct Flower {
uint flowerNum;
string color;
}
struct Garden {
uint slugCount;
uint wormCount;
Flower[] theFlowers;
}
If a function has the signature fertilizer(Garden memory gardenPlot) then the correct syntax is:
[1,2,[[3,"Petunia"]]]
Pour poursuivre cet exemple, voici un exemple de contrat :
pragma solidity ^0.8.0;
contract Sunshine {
struct Flower {
uint flowerNum;
string color;
}
struct Garden {
uint slugCount;
uint wormCount;
Flower[] theFlowers;
}
function fertilizer(Garden memory gardenPlot) public pure returns (uint, string memory) {
require(gardenPlot.theFlowers.length > 0, "No flowers");
return (gardenPlot.theFlowers[0].flowerNum, gardenPlot.theFlowers[0].color);
}
}
After compiling, deploying the contract and opening up the deployed instance, we can then add the following input parameters to the function named fertilizer:
[1,2,[[3,"Black-eyed Susan"],[4,"Pansy"]]]
The function fertilizer accepts a single parameter of the type Garden. The type Garden is a struct. Structs are wrapped in square brackets. Inside Garden is an array that is an array of structs named theFlowers. It gets a set of brackets for the array and another set for the struct. Thus the double square brackets.