Why is Sandbox Indispensable for Building dApps?
Introduction
There is no sugar coating it. Building a dApp is complex, especially for those who never built on a blockchain before. By the end of this article, you will fully understand why Sandbox is indispensable for building dApp on Algorand, how to take advantage of it fully, and ultimately make your dApp development on Algorand a walk in the park.
I will cover the following topics:
- What is Sandbox?
- Why do we need Sandbox?
- How I set up Sandbox when building dApps.
What is Sandbox?
Sandbox is the easiest way to set up an Algorand development environment. You will use Sandbox to connect to various Algorand networks that suit your needs, manage accounts, interact with smart contracts, query the blockchain and debug your code.
All very important when developing dApps, and they all come packaged inside of Sandbox.
Why do we need Sandbox?
To understand why we need Sandbox, we will compare what dApp development process is like with and without Sandbox.
Connecting to an Algorand node
When developing a dApp, you need to be able to interact with the Algorand blockchain to run and test your code and you do that by connecting to an Algorand node. There are two ways to connect to an Algorand node without Sandbox.
1. Run your Algorand node
Running your Algorand node will provide you with the most control but requires learning how to set up your node, wait several hours or days for the node to catch up to the latest block, and make sure your node stays up to date throughout your development period. This method requires too much effort for most smart contract developers.
2. Use third-party node services
Using third-party node services will let you connect to public Algorand networks right away but there are a lot of limitations. Most services have a limited amount of transactions per day, which would limit how much testing you can do. Also, if their node goes down, you cannot continue building until the node is restored or find another working node. In summary, fast setup but has limitations.
3. Use Sandbox
Some will prefer the above methods, but they could be too hard or unsuitable when developing dApps. This is where Sandbox comes in. With Sandbox, you can connect to all available Algorand networks with a single line of terminal command. (Learn how to launch Sandbox here ) You can get straight to building your dApp in a matter of minutes.
Note
Sandbox is generally used for development, not production. Running your node or using third-party node services is ideal for Mainnet production applications.
Interacting with an Algorand node
Sandbox not only lets you connect to an Algorand node but it also lets you interact with it. You simply use goal
commands within Sandbox or Algorand SDKs to send and manage ALGO / ASAs or call applications.
Now let’s see what interacting with Algorand without Sandbox looks like. You still use goal
commands for interaction but first, you have to install and configure your Algorand node and set up the algorand binaries directory to use goal
. Not so ideal, when you can just use goal
or Algorand SDKs straightaway with Sandbox.
Note
To see how to interact with an Algorand node using Algorand SDKs, check out the examples repo.
Managing keys and accounts
When building dApps, you need to manage several accounts to simulate the typical usage of your application. Algorand’s Key Management Daemon (kmd) is a built-in wallet that lets you create and delete accounts(or private keys). Just like goal
, you will need to set up your Algorand node if you want to use kmd without Sandbox.
Connecting different Algorand networks
During development, you will want to connect to a private network and then to public networks based on your development stage. There are three different Algorand public networks:
- Mainnet
- Betanet
- Testnet
Learn about these networks here.
Also, Sandbox provides dev mode, a local private blockchain that lets you build in a safe private environment. This is one of the most important reasons why using Sandbox when developing dApps is essential, as it lets you work out bugs and do local testing before deploying your contracts to public networks.
With Sandbox, you can easily switch between these networks or dev mode whenever you want. However, it is not so easy without Sandbox. You will need to manually configure your Algorand node every time you want to switch networks.
Querying the blockchain history
If your dApp requires querying blockchain records? You will need to use an Indexer on Algorand. Indexer is not a part of the Algorand node, so if you decided to set up your node when building your dApp, you will also need to download and install Indexer separately.
The Sandbox provides a built-in setup process for the indexer, making this requirement trivial to achieve.
Debugging your smart contract
Debugging tooling is a developer’s best friend and it is no different for dApp developers. Debugging smart contracts can be tough especially when you need to interact with the blockchain every time you want to test your code. This is where Sandbox shines as it provides the teal
program debugger tealdbg
allowing you to test in a controlled environment, off the chain, or even on the chain.
Sandbox allows all of the following out of the box:
- Connecting to an Algorand node
- Interacting with Algorand with
goal
- Key management with kmd
- Access to different Algorand networks
- Querying Algorand records with Indexer
- Debug your smart contract code with Tealdbg
There is no going back now. 👑
How I setup Sandbox when building dApp
I remember scrambling around the developer portal trying to figure out how to set up my developer environment so that I can just get started building. Here is how I set up Sandbox just as a quick reference.
Sandbox shortcut
To launch Sandbox, you need to be in your sandbox directory. This can get cumbersome so I added an alias by editing the ~/.zhrc file. You can edit ~/.zhrc file with nano ~/.zhrc
Note
I am using Zsh. If you are using Bash, try nano ~/.bashrc
Point “sb” to the path of the sandbox
shell script anywhere you want inside the ~/.zshrc file:
alias sb="/Users/chris/Documents/sandbox/sandbox"
Note
Make sure to point it to the sandbox
shell script directory and not just your Sandbox folder.
You are all set! Now instead of doing ./sandbox up
only inside of your Sandbox directory, you can do sb up
wherever you are in the terminal.
Development environment
Before I start coding, here is what I set up.
- Launch Sandbox in dev mode by doing
sb up dev
in my terminal.
Note
I always start with the dev mode because you can fire up a private blockchain locally in your machine and do quick checks and tests along your development. Also, the dev mode will only generate a new block when a transaction is submitted and confirmed, which is useful for various types of testing and debugging. Specifying configurations after sb up
lets you connect to different networks by using config files defined here.
- Link Dappflow to my Sandbox by going to settings and then selecting “Sandbox” and “Save” as in Figure 1.0.
Figure 1.0 - Connect Sandbox in Dappflow |
Note
Dappflow is a private blockchain explorer with an easy-to-understand UI. You can see all of the transactions, accounts, assets, and applications you create during your development.
Figure 1.1 - Dappflow homepage |
That’s it! With Sandbox, you can set up your dApp development environment which is probably one of the most confusing and involved parts of building a dApp, within a couple of minutes.
Summary
With Sandbox, you will be able to get building quickly and effortlessly. I hope this article clarifies why Sandbox is indispensable when building dApps on Algorand.
If you have any questions, join the Algorand Developer Discord. If you want to stay up-to-date with Algorand development, follow @algodevs on Twitter.