Running scripts in Remix - Remix Tutorial 13

EatTheBlocksAbout 4 min readMay 27, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

Scripts, Remix IDE, Smart Contract Deployment, JavaScript/TypeScript, Web3/Ethers Libraries, Asynchronous Self-Executing Functions, Deploy Function, Contract Interaction, Terminal Execution, Compile and Run, Repeatable Processes.

Running Scripts in Remix IDE

Main Advantages:

  • Scripts provide a repeatable process for deploying and interacting with smart contracts.
  • This process is safer and faster, especially for complex deployments.

Example Smart Contract:

  • A simple smart contract with an integer variable, a function to update the variable (store), and a function to read the variable (retrieve).

Script Structure:

  • Scripts are written in JavaScript or TypeScript.
  • Files are saved with the .js or .ts extension.
  • Scripts must be wrapped in an asynchronous self-executing function: (async () => { ... })();
  • Scripts can use the Web3 or Ethers libraries.

Example Script Usage:

  • The default workspace includes example scripts.
  • Scripts can use utility functions defined in other files (e.g., a deploy function).
  • Example: Deploying a "Storage" smart contract and logging its address to the console.

Four Ways to Run a Script:

  1. Play Button: Click the play button in the script editor.
  2. Right-Click: Right-click on the script file and select "Run".
  3. Terminal: Execute the script in the terminal using a specific statement (not provided in the transcript, but implied). Ensure the script is the active tab.
  4. Compile and Run: Use the "Compile and Run" button in the compiler menu. This allows setting the contract state right after compiling.

Compile and Run Details:

  • To use "Compile and Run", add a comment in the smart contract specifying which script to run. Example: //@script myScript.js
  • This method streamlines the process of compiling, deploying, and setting the initial state of the contract.

Demo of Script Execution:

  • The video demonstrates running a script in the terminal.
  • The script execution includes console logs of the contract address.
  • After execution, the contract is deployed, and you can interact with it in the "Deploy" tab as if deployed manually.

Creating a Custom Script

Step-by-Step Process:

  1. Create a New File: Create a new JavaScript file in the "scripts" folder.
  2. Asynchronous Function: Create an asynchronous self-invoking function: (async () => { ... })();
  3. Deploy Contract: Use the deploy function (from a Remix-provided library) to deploy the smart contract. Store the contract object in a variable: let contract = await deploy("Storage");
  4. Interact with Contract:
    • Call the store function to update the variable: await contract.store(10);
    • Send a transaction: let transaction = await contract.store(10);
    • Wait for the transaction to be mined: await transaction.wait();
    • Call the retrieve function to get the updated value: let value = await contract.retrieve();
    • Console log the result: console.log("Updated value:", value);
  5. Execute the Script: Run the script in the terminal.

Example:

  • The script updates the integer variable in the smart contract to 10.
  • The script then retrieves the value and logs it to the console.

Advanced Scripting

Adding a New Function to the Smart Contract:

  • Define a new variable (e.g., anotherValue).
  • Create a new setter function (e.g., setAnotherValue).
  • Create a new getter function (e.g., getAnotherValue).

Updating the Script:

  • Add code to set the value of the new variable using the new setter function.
  • Console log the result using the new getter function.

Using Compile and Run:

  • Update the //@script comment in the smart contract to reference the new script.
  • Go to the compile tab and click "Compile and Run".
  • The script execution is displayed in the terminal.

Convenience:

  • This method avoids manually clicking buttons in the Remix IDE to set the contract state.

Customizing Example Scripts:

  • Instead of creating scripts from scratch, you can customize the example scripts provided by Remix.

Synthesis/Conclusion

The video demonstrates how to use scripts in Remix IDE to automate smart contract deployment and interaction. Scripts offer a repeatable, safer, and faster alternative to manual deployment, especially for complex contracts. The video covers creating custom scripts, using utility functions, and leveraging the "Compile and Run" feature for streamlined development. The key takeaway is that scripting significantly enhances the efficiency and reliability of smart contract development workflows within the Remix IDE.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.