Eseguire Script

Remix IDE supports execution of JS & TS scripts. For some script examples using Ethers.js, go to the scripts folder in the Default Workspace or the Basic Workspace.

Why run scripts in Remix?

  • To mimic how the front-end of your dapp will use ethers.js

  • Per distribuire e interagire rapidamente con un gruppo di istanze di un contratto senza passare per la GUI di Remix.

  • To run tests on a previous deployed contract.

Writing and running scripts

Create a file with .js or .ts extension and put your logic inside it. To run a script:

  • Click the play button at the upper left of the Main Panel when the script is the active file in the Editor.

  • Right click on the script name in the File Explorers and click on the Run option.

  • Ctrl+Shift+S when the script is displayed in the editor.

  • Rendi lo script il file attivo nell’editor ed esegui remix.exeCurrent() dal terminale di Remix

Here is a sample .js script:

function test() {
  var num=12;
  if(num<10)
    console.log(num + " is less than 10");
  else
    console.log(num + " is not less than 10");
}

test();

Eseguirlo utilizzando una delle opzioni sopra menzionate mostrerà il risultato nel terminale di Remix

Script per distribuire un contratto

Remix accepts async/await scripts to run ethers.js commands. The script needs to be wrapped in a self executing function.

Configurazione

  1. Scripts will need to access the contract’s ABI. The ABI is located in the contract’s metadata file. Make sure that this metadata file will be created by going to the Settings module and checking that the Generate contract metadata option is indeed checked. (It is enabled by default)

  2. Compila un file Solidity - per generare i metadati del contratto.

  3. In the Deploy & Run plugin, choose the Environment for your script.

    • Gli script Async/await funzionano in tutti gli ambienti: Macchina Virtuale di Remix, Injected Provider (di solito MetaMask) e Provider HTTP Esterno.

Compile and Run script

In the Solidity Compiler, there is a button called «Compile and Run script».

When drafting a contract, it can be helpful to run a script just after the compilation succeeds that will deploy and set the state.

The script can contains Mocha tests to be run.

In order to connect a contract with a script, add the NatSpec tag @custom:dev-run-script to the contract followed by the absolute file path, like:

  /**
   * @title ContractName
   * @dev ContractDescription
   * @custom:dev-run-script file_path
   */
  contract ContractName {}

When you are ready to deploy the code for real, remove the NatSpec comment @custom:dev-run-script.

ShortCut: Ctrl+Shift+S , when editing a solidity file, will compile that file and then will run the script. In contrast, ctrl+S will only start the compiling.

Adding external dependencies

Remix has organized JS/TS dependencies into sets called «Configurations».
These dependencies are available for use in your scripts.

The following list contains the Default dependencies:

Loading other script dependencies

If you don’t see what you need in the Default configuration of scripts, you can load other configurations of scripts using the Script Configuration panel. Tthis panel is accessed from the dropdown on the «Run Script» button.

Nota

If the active file in the Editor is not a script, then the «Run Script» button turns into the «Compile» button and the Open script configuration option is not present.

Script config

It is not necessary to install the scripts to use; just use the import or require keyword. For example:

//using require
const { expect } = require("chai");

//using import
import { UltraHonkBackend } from "@aztec/bb.js";

If you try to import an unsupported module, you will get the error below:

<module_name> module require is not supported by Remix IDE