Eseguire Script

Remix IDE supports execution of JS & TS scripts.

Scrivi & Esegui uno script

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

  • Click the green play button in the upper left of 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

Why run scripts in Remix?

  • Per imitare il modo in cui il front-end della tua dapp utilizzerà web3.js o ethers.js

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

  • Per eseguire alcuni test su un contratto distribuito in precedenza.

Script per distribuire un contratto

Remix accetta script async/await per eseguire comandi web3.js o ethers.js. Lo script deve essere racchiuso in una funzione autoesecutiva.

Configurazione

  1. Questi script dovranno accedere all’ABI del contratto. L’ABI si trova nel file dei metadati del contratto. Assicurarsi che questo file di metadati venga creato andando nel modulo Impostazioni e controllando che l’opzione Genera metadati del contratto sia effettivamente selezionata.

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

  3. Nel plugin Distribuisci & Esegui, scegliere l’Ambiente.

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

Script JS negli Esplora File

Nella cartella scripts di un ambiente di lavoro, ci sono 2 file di esempio: uno che usa web3.js e l’altro che usa ethers.js.

Compile a contract and run a script in one click

When drafting a contract, it can be helpful to run a script just after the compilation succeeds.

With this technique, one can write some code then quickly deploy and set the state of the contracts.

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.

Scorciatoia: Ctrl+Shift+S, quando si modifica un file di Solidity, compila il file e poi esegue lo script. Al contrario, Ctrl+S avvia solo la compilazione.

Uno Script di Esempio

L’esempio seguente distribuisce un contratto solidity denominato CustomERC20.sol. Questo esempio utilizza la libreria web3.js. È possibile utilizzare anche Ethers.js.

Per ulteriori informazioni su questo esempio, vedere: esecuzione di script async/await

(async () => {
  try {
    console.log('deploy...')

    // Note that the script needs the ABI which is generated from the compilation artifact.
    const metadata = JSON.parse(await remix.call('fileManager', 'getFile', 'browser/artifacts/CustomERC20.json'))
    const accounts = await web3.eth.getAccounts()

    let contract = new web3.eth.Contract(metadata.abi)

    contract = contract.deploy({
      data: metadata.data.bytecode.object,
      arguments: ["Mask", "N95"]
    })

    newContractInstance = await contract.send({
      from: accounts[0],
      gas: 1500000,
      gasPrice: '30000000000'
    })
    console.log(newContractInstance.options.address)
  } catch (e) {
    console.log(e.message)
  }
})()

Per ulteriori esempi di script, vedere Script Richiesti Frequentemente, in inglese «Frequently Asked Scripts».

require negli script su Remix

require or importstatement is supported in a limited manner for Remix supported modules with Remix Scripts.

For now, modules supported by Remix are:

  • ethers

  • web3

  • swarmgw

  • chai

  • starknet

  • multihashes

  • zokrates-js

  • snarkjs

  • circomlibjs

  • @zk-kit/incremental-merkle-tree

  • @semaphore-protocol/proof

  • @semaphore-protocol/group

  • @semaphore-protocol/identity

  • @semaphore-protocol/data

  • @chainlink/functions-toolkit

  • @personaelabs/spartan-ecdsa

  • @ethereumjs/util

  • ffjavascript

  • sindri

  • remix

  • hardhat (only for hardhat.ethers object)

Per i moduli non supportati, verrà mostrato l’errore <module_name> il modulo richiesto non è supportato da Remix IDE.