Distribuisci & Esegui (Parte 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
orpure
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.
Inserimento dei parametri
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.
I numeri e gli indirizzi non hanno bisogno di essere racchiusi in virgolette doppie.
Le stringhe non hanno bisogno di essere impacchettate.
Inserimento dei parametri nella modalità di vista collassata
In the collapsed view:
I parametri sono separati da virgole.
Nell’esempio precedente, la funzione «delega» ha 3 parametri.
Inserimento dei parametri nella modalità di vista estesa
Clicking the “down” caret brings you to the expanded view - where parameters are input one at a time.
Interazioni di livello basso
Le interazioni di basso livello sono utilizzate per inviare fondi o calldata o fondi & calldata a un contratto tramite la funzione recieve() o fallback(). In genere, è necessario implementare la funzione di fallback solo se si segue uno schema di aggiornamento o proxy.
La sezione delle interazioni di basso livello si trova al di sotto delle funzioni in ciascun contratto distribuito.
Si prega di notare quanto segue:
Se stai eseguendo un semplice trasferimento di Ether a un contratto, devi avere la funzione receive() nel tuo contratto. Se il tuo contratto è stato distribuito e vuoi inviargli dei fondi, devi inserire la quantità di Ether o Wei ecc. (vedi A nel grafico sottostante), e poi non inserire NULLA nel campo calldata delle Interazioni di basso livello (vedi B nel grafico) e fare clic sul pulsante «Transact» (vedi C nel grafico sottostante).
Se si inviano calldata al contratto con Ether, è necessario utilizzare la funzione fallback() e dotarla della mutabilità di stato di pagabile.
Se non stai inviando ether al contratto, ma stai inviando calldata, allora devi utilizzare la funzione fallback().
Se si violano le regole quando si utilizzano le Interazioni di basso livello, si riceve un avvertimento.
Per maggiori informazioni sull’uso delle funzioni fallback e receive, consultare i documenti di Solidity.
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;
Esempio di come passare una struct annidata a una funzione
Considera una struct annidata come definita in questo modo:
struct Garden {
uint slugCount;
uint wormCount;
Flower[] theFlowers;
}
struct Flower {
uint flowerNum;
string color;
}
Se una funzione ha la firma fertilizer(Garden memory gardenPlot)
, la sintassi corretta è:
[1,2,[[3,"Petunia"]]]
Per continuare con questo esempio, ecco un esempio di contratto:
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;
}
}
Dopo aver compilato, distribuito il contratto e aperto l’istanza distribuita, possiamo aggiungere i seguenti parametri di input alla funzione denominata fertilizzante :
[1,2,[[3,"Black-eyed Susan"],[4,"Pansy"]]]
La funzione fertilizzante accetta un singolo parametro del tipo Giardino. Il tipo Giardino è una struct. Le struct sono racchiuse tra parentesi quadre. All’interno di Giardino c’è una matrice che è una matrice di struct denominata iFiori. Si ottiene un insieme di parentesi per la matrice e un altro insieme per la struct. Ecco perché le doppie parentesi quadre.