Hey, TRON Fam! Today I’d like to explore a great topic of TRON development - deploying smart contracts!
Let’s learn how to build on TRON! Shall we?!
We are gonna learn how to deploy a Solidity contract on the TRON chain using GetBlock’s API. Here is our toolkit:
- NodeJS >=8.10.0
- TronBox
- Tron Wallet
- TRON NILE REST API by GetBlock
Before rolling out your dApps and smart contracts on Tron mainnet, it’s important to test their performance on the testnet! There are two main testnets for Tron - Nile and Shasta. We are gonna use Nile in this case as it’s a more flexible and highly used one.
Step 1: Set Up Your Environment
This step requires setting up a TronBox. Here are TronBox Environment Dependencies
- NodeJS >=8
- Windows, Linux or Mac OS X
- Docker Engine >=v17
To get more insights on how to install the node.js and tronbox refer to the official TRON docs here: Install TronBox
Step 2: Grab your API from GetBlock
To interact with the TRON blockchain you need a robust API. You can grab such from GetBlock for free! Here’s how to do so:
Sign up/in to https://getblock.io/ with MetaMask or email
Navigate to ‘My Endpoints’. Select TRON (Testnet), generate a Fullnode REST API
We’ll use this RPC URL for deployment and further interactions with the contract
Step 3: Write a Solidity contract
Go to your IDE, we use Remix IDE in this example. Create a .sol
file and write your code. We’ll use the very basic storage contract for this demo. You can also use your previous smart contracts and deploy them on TRON!
To compile the contract head to the direction where your project is located and past the following into a terminal: tronbox compile
Step 4: Configure Contract Deployment on Nile Testnet
Now we need to configure how and where our contract will be deployed.
- Open
tronbox.js
file in the root directory; - Edit
nile
configuration: - Insert the TRON wallet’s private key,
- adjust the fee limit,
- add the RPC URLs from GetBlock that you took on Step2
Step 5: Modify Migration (Deployment) Scripts
Go to the migrations
folder and edit 2_deploy_contracts.js
file:
- Add the path to your Solidity file: in this example,
./Storage.sol
- Define the deployment function
If your contract has constructor parameters, you can pass them here.
Step 6: Deploy the Contract
Now we’re on the finish line! It’s time to finally Deploy your first TRON smart contract!
Make sure your TRON Wallet is pre-funded with testnet $TRX to cover deployment fees; you can request 2,000 test coins from the Nile Testnet Faucet
Once it’s done - run the Migration command in your terminal:
source .env && tronbox migrate --network nile
Congradulations! Your first Solidity contract is successfully deployed to TRON Nile Testnet!
Additionally, you can interact with your contract to check if everything is working correctly
- Request a TronBox console from the terminal:
tronbox console --network nile
- Now you can interact with the contract calling available functions
Well done!
Now you’re one step closer to becoming a professional TRON builder!