NRE and scripting

Nile provides an API to build scripts leveraging the Nile Runtime Environment (NRE), an object exposing Nile’s functionality from compilation to account and deployment management. Nile scripts implement an asynchronous def run(nre) function that receives the NileRuntimeEnvironment object.

Running a script

  1. Create a file named test_script.py inside the scripts/ directory at the root of your project with the following contents. Keep in mind that the scripts/ directory is a suggested convention but not a requirement.

    async def run(nre):
        accounts = await nre.get_accounts(predeployed=True)
        print("First Account:", accounts[0])
    The predeployed parameter leverages the accounts that come already deployed with the local node. Read get_accounts’s API Reference to learn more.
  2. Run a devnet node with nile node

    nile node
  3. Run the script by using nile run

    nile run scripts/test_script.py

You can change the script’s target network with the --network option.

For a full reference of NRE exposed members check the NRE Reference section.

Useful Script Examples

In this section you can find some potentially helpful script examples.

Declare Account

Useful if you need to deploy an account in devnet, for previous declaration:

# script.py
async def run(nre):
  accounts = await nre.get_accounts(predeployed=True)
  declarer_account = accounts[0]

  # nile_account flag tells Nile to use its pre-packed artifact
  output = await declarer_account.declare("Account", nile_account=True)
  print(output)

Transfer funds from predeployed devnet account

Useful for funding addresses in devnet:

from nile.common import ETH_TOKEN_ADDRESS

async def run(nre):
    accounts = await nre.get_accounts(predeployed=True)
    account = accounts[0]

    # define the recipient address
    recipient = "0x053edde5384e39bad137d3c4130c708fb96ee28a4c80bf28049c486d3f369"

    # define the amount to transfer (uint256 format)
    amount = [2 * 10 ** 18, 0]

    print(f"Transferring {amount} to {recipient} from {hex(account.address)}")

    # send the transfer transaction
    output = await account.send(ETH_TOKEN_ADDRESS, "transfer", [recipient, *amount])
    print(output)