Use Terrain with LocalTerra
LocalTerra is a complete Terra testnet and ecosystem containerized with Docker. Use LocalTerra to simulate transactions in a test environment.
Prerequisites
- Docker
docker-compose
- At least 16 GB of RAM
- Node.js version 16
- Station Chrome extension
Use LTS Node.js 16 if you encounter the following error code.
error:0308010C:digital envelope routines::unsupported
Click here for details.
Some M1 macs may need to use the latest LTS version of Node to complete this tutorial. Consider using a node version manager such as nvm. After installing nvm, run the following commands to install and then switch to the latest LTS version of node.
_2nvm install --lts_2nvm use --lts
The nvm use --lts
command will need to be run every time you open a new terminal to use the LTS version of node.
To default to the LTS version of node when restarting your terminal, run the following:
_1nvm alias default <desired-node-version>
Install and run LocalTerra
- Download LocalTerra.
- Start LocalTerra.
- You will start seeing LocalTerra block activity in your terminal. Keep LocalTerra running while you perform the next steps in a new terminal window.
- Download LocalTerra.
- Start LocalTerra.
- You will start seeing LocalTerra block activity in your terminal. Keep LocalTerra running while you perform the next steps in a new terminal window.
To view LocalTerra wallet information, visit the LocalTerra accounts page. For more configuration options, visit the LocalTerra configuration page.
Counter tutorial
After installing LocalTerra, you are ready to use Terrain. This short tutorial walks you through setting up your project and creating a simple counter that increments upon request.
1. Scaffold your dApp
With Terrain installed, you can now scaffold a template application in a new terminal window.
- Create a new dApp project.
_1terrain new my_terra_dapp
- Change directory into your newly scaffolded dApp.
_1cd my_terra_dapp
Project structure
Your scaffolded project will have the following structure.
_10._10├── contracts # The contracts' source code._10│ ├── my_terra_dapp_10│ └── ... # Add more contracts here._10├── frontend # The front-end application._10├── lib # Predefined functions for task and console._10├── tasks # Predefined tasks._10├── keys.terrain.js # Keys for signing transactions._10├── config.terrain.json # Config for connections and contract deployments._10└── refs.terrain.json # Deployed code and contract references.
2. Deploy
To deploy the application, run the following command in your terminal.
_1terrain deploy my_terra_dapp
The deploy command performs the following steps automatically.
- Builds the smart contract.
- Optimizes the smart contract.
- Uploads the smart contract to LocalTerra.
- Instantiates the deployed smart contract.
If you are running LocalTerra and the previous deploy
command is not working, try increasing Docker's memory allowance. You can do this by clicking on the Docker icon, clicking on Preferences and then navigating to Resources. Increase the memory to at least 4 GB and then click Apply & Restart. You can then try running the deploy command again. If you are still having trouble with deploying your dApp, you can try increasing the memory to 6 GB.
3. Generate TypeScript client
Terrain 0.5.x and above includes the ability to automatically generate a TypeScript client based on your smart contract schema.
Generating a client is easy, just run the following command in your terminal.
_1terrain contract:generateClient my_terra_dapp
The client will be generated in ./lib/clients
and copied into the frontend directory.
4. Interact with the deployed contract
The template dApp comes with several predefined helpers in lib/index.js
. You may use these to start interacting with your smart contract.
- Start the Terrain console.
_1terrain console
- In the Terrain console, you can increment the counter by running the following command.
_1await lib.increment();
- You can also get the current count.
_1await lib.getCountQuery();
- After incrementing once,
await lib.getCountQuery()
should return a count of 1.
_1{ "count": 1 }
Before proceeding to the next section, exit the terrain console by using "Ctrl + C".
5. Front end scaffolding
When you scaffold a template app with Terrain, it will contain a simple front end.
-
Open the Station Chrome extension, click the gear icon in the upper right-hand corner, and switch the network to LocalTerra.
-
To use the front end, run the following commands.
_2cd frontend_2npm start
- Open the Station extension and click Add a wallet. Click Recover wallet and input the following seed phrase to access the sole validator on the LocalTerra network and gain funds to get started with smart contracts:
_1satisfy adjust timber high purchase tuition stool faith fine install that you unaware feed domain license impose boss human eager hat rent enjoy dawn
- With LocalTerra selected in Station and the local seed phrase imported, you can now increment and reset the counter from the front end.
Demo
Advanced usage
For more advanced use cases, like deploying to the testnet or mainnet, see Terrain's readme.