Run Your First Project

All done with installing the Scrypto toolchain? Great, let’s walk through the steps of running and interacting with some Scrypto code in your simulator environment, starting with a new project.

The basic process involves creating a blueprint package with some blueprint Scrypto code, deploying it to your local simulator (as if deploying it to the Radix network’s blueprint catalog), instantiating a component from the blueprint, and interacting with the component via a simulated transaction.

Creating a New Blueprint Package

First, we’ll create a new blueprint package.

scrypto new-package tutorial

This will scaffold a simple package which contains a single Hello blueprint. Open the new package directory in an IDE with Rust support.

The Scrypto development team mostly uses VS Code, which has a free Rust extension (rust-lang) that works well.

Open up src/ to see the source that we’ll be compiling and deploying to the simulator. Hello provides a function which instantiates a new component with a new supply of tokens, and a method which allows the caller to get one of those tokens from the instantiated component.

Deploying Package to Local Simulator

Before we can deploy, we first need to create an account on the simulator. On Radix, accounts are themselves components, but resim provides commands to make it convenient to create and use accounts in the simulator environment. Most of commands offered by the resim tool act within the context of an account.

resim new-account

You should get a success status, and at the bottom of the output you should see something like this:

A new account has been created!
Public key: 04005feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9
Account address: 02526629b90e1142492e934fbe807b446935407064db3ea2fcf856
As this is the first account, it has been set as your default account.

If you do not see the line about seting your default account set, then you already created an account previously. Either reset your entire simulator with the resim reset command and try again, or set this new account as your default account with the resim set-default-account command.

Save the address of your new account component to an environment variable to make your life easier later. For example:

export account=02526629b90e1142492e934fbe807b446935407064db3ea2fcf856
export pubkey=04005feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9

In Windows, do

# Using command shell
SET account=02526629b90e1142492e934fbe807b446935407064db3ea2fcf856
SET pubkey=04005feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9

# Using power shell

You can look at your new account by running:

resim show $account

This will show that your account comes pre-supplied with 1 million XRD.

Next, we’ll publish our package. Note that it is not necessary to perform a scrypto build operation before doing so; publishing will take care of that automatically if a build is needed. Switch to your tutorial directory and run:

resim publish .

At the bottom of the output, you should see └─ Package: <package address>. Save that address in the package environment variable.

Instantiating a Component and Making a Call

Now that our package is deployed "on ledger" in the simulator, we’ll need to instantiate a Hello component by calling the new function on the Hello blueprint.

resim call-function $package Hello new

This will create two new entities with two new addresses: a resource definition for your new HelloToken (save this to the token environment variable), and your fresh Hello component (save this to the component environment variable). On Radix, tokens and other assets are created as system-level resources, not blueprints, and the resource definition contains the parameters for the supply of resources you have requested from the system.

You can use resim show to investigate these new addresses, if you wish.

Next, we’ll call the free_token method on our new component. Note that this requires a different resim command than the one we just used since we are now calling a method on an instantiated component instead of the function of a blueprint.

resim call-method $component free_token

And now you have a shiny new HelloToken in your account, and your Hello component has one less. You can verify this with some investigation using resim show on each component.

You can create more accounts, and use resim set-default-account to change which one you’re acting as.

Final Notes

If you make changes to the structs within your code, then unfortunately you will have to run through the entire publish-instantiate-call flow from scratch, saving the new addresses as they appear. If you only make implementation changes then it is possible to update your package with resim publish . --address $package.

At any point you can use resim reset to instantly get a clean slate in the simulator. You almost certainly need to do this if you switch to working on a different project.

If you ever pull the latest from the Scrypto GitHub repo and see that the core Scrypto implementation has changed (i.e., stuff above the examples directory), you will usually need to re-run cargo install --path ./simulator from the radixdlt-scrypto root directory in order to pick up the changes. And sometimes you will also need to run resim reset to clear the simulator state.

That concludes this most basic tutorial on using resim. More content to come!