Solidity Basics
Need a refresher on the language? Review Solidity syntax, EVM compatibility, and TRON-specific patterns.
This guide is your launchpad into the TRON ecosystem. By the end of it, you will have a deployed smart contract and a solid understanding of the underlying tooling, resource management, and secure network bridging. We assume basic knowledge of JavaScript and Solidity — let’s get building.
Before writing any code, ensure your local environment has the essentials:
node --version)You should never mix your personal funds with development. Development tools require access to private keys to sign deployments, and by creating a dedicated, isolated “sandbox” wallet, you completely eliminate the risk of accidentally burning real TRX on a botched test script.
Every operation that modifies the blockchain (like deploying a contract) consumes network resources (Energy and Bandwidth). On the Mainnet, this costs real TRX. On a testnet, you use free, valueless “Testnet TRX” to simulate these costs without financial risk.
TRON maintains two primary public testnets:
For general development and following this guide, we’ll use Nile since it’s the standard sandbox for new developers. Let’s fund your wallet:
Compiling Solidity down to bytecode and manually pushing it to the blockchain via raw HTTP requests is tedious and error-prone. TronBox is the standard framework that automates the compilation, deployment, and testing pipeline, keeping your project organized.
# Task: Install the TronBox framework globally so you can use it from any directory.npm install -g tronboxtronbox --versionLet’s scaffold a clean working directory:
# Task: Initialize a new TronBox project to generate the required directory structure.mkdir my-tron-projectcd my-tron-projecttronbox initTronBox creates a standard structure for you:
/contracts/: Where your raw Solidity files live./migrations/: Scripts that dictate exactly how your contracts should be deployed./test/: Where you’ll write tests to ensure your logic is bulletproof.tronbox.js: The master configuration file.TronBox needs to know where to deploy your contracts (the network endpoints) and who is paying for the deployment (your dev wallet’s private key). We use environment variables (.env) to inject the private key dynamically so it never gets hardcoded into your source files.
Replace the contents of tronbox.js with this configuration:
// Task: Tell TronBox how to connect to the Nile testnet securely.module.exports = { networks: { nile: { privateKey: process.env.PRIVATE_KEY_NILE, userFeePercentage: 100, feeLimit: 1000000000, fullHost: 'https://nile.trongrid.io', network_id: '3', }, mainnet: { privateKey: process.env.PRIVATE_KEY_MAINNET, userFeePercentage: 100, feeLimit: 1000000000, fullHost: 'https://api.trongrid.io', network_id: '1', }, }, compilers: { solc: { version: '0.8.18', }, },};Create a .env file in the root of your project (and immediately add it to your .gitignore!):
# Task: Securely store your testnet private key away from source control.PRIVATE_KEY_NILE=your_nile_dev_wallet_private_key_here(To get your private key: Open TronLink → select your dev account → click the three-dot menu → Export Account).
TRON’s Virtual Machine (TVM) is highly compatible with the Ethereum Virtual Machine (EVM). This means we write our contracts in Solidity. When we compile, TronBox translates this human-readable Solidity into machine-readable bytecode and generates an ABI (Application Binary Interface), which serves as a “manual” for how frontends can interact with the contract.
Create a new file at contracts/SimpleStorage.sol:
// Task: Define a minimal contract that stores and retrieves a single number.// SPDX-License-Identifier: MITpragma solidity ^0.8.18;
contract SimpleStorage { uint256 private value;
// Events let frontends listen for changes on the blockchain event ValueSet(uint256 indexed newValue, address indexed setter);
// 'external' functions can be called by users or other contracts function set(uint256 _value) external { value = _value; emit ValueSet(_value, msg.sender); }
// 'view' means this function reads data but doesn't modify the blockchain function get() external view returns (uint256) { return value; }}Now, command TronBox to compile it:
# Task: Translate the Solidity code into TVM-ready bytecode and ABI.tronbox compileLook inside the build/contracts/ folder. TronBox just generated the artifacts needed for deployment.
A blockchain is a state machine. “Migrating” simply means moving the blockchain from its current state to a new state that includes your contract. The migration script tells TronBox exactly which contracts to deploy and in what order.
Create migrations/2_deploy_simple_storage.js:
// Task: Instruct TronBox to deploy the SimpleStorage contract artifact.const SimpleStorage = artifacts.require('SimpleStorage');
module.exports = function (deployer) { deployer.deploy(SimpleStorage);};It’s time to make it real. By running the migrate command, TronBox will construct a deployment transaction, sign it with your .env private key, and broadcast it to the Nile network. The network will deduct Testnet TRX (Energy/Bandwidth) to permanently store your contract’s bytecode.
# Task: Broadcast your contract to the network to make it live.source .env # Load your private key into the terminal sessiontronbox migrate --network nileWatch the terminal. TronBox will output a deployed contract address (starting with T...). Copy this address.
Victory Check: Go to nile.tronscan.org and search for your new contract address. You’ll see the exact Energy consumed and the verified deployment transaction. You are officially live on TRON.
A smart contract isn’t very useful if nobody can talk to it. tronweb is the official JavaScript SDK. We use it to bridge a Node.js script (or a React frontend) directly to the blockchain to execute our set() and get() functions.
First, install the SDK:
# Task: Add the TronWeb SDK to your project to enable blockchain communication.npm install tronwebCreate scripts/interact.js:
// Task: Use TronWeb to read from and write to your live contract.import TronWeb from "tronweb"
// Initialize TronWeb with the Nile endpoint and your dev keyconst tronWeb = new TronWeb({ fullHost: 'https://nile.trongrid.io', privateKey: process.env.PRIVATE_KEY_NILE,});
const CONTRACT_ADDRESS = 'T...'; // Paste your deployed address here!
async function main() { // Load the ABI so TronWeb knows what functions exist const contract = await tronWeb.contract().at(CONTRACT_ADDRESS);
console.log('Executing write operation...'); // .send() creates a transaction, burns Energy, and alters the blockchain state const tx = await contract.set(42).send(); console.log('Transaction hash:', tx);
console.log('Executing read operation...'); // .call() is a free, local query to a node. It doesn't alter state. const result = await contract.get().call(); console.log('Value currently stored on-chain:', result.toString());}
main().catch(console.error);Run your interaction script:
# Task: Execute the script to talk to the Nile testnet.node scripts/interact.jsCongratulations! You’ve successfully completed the full lifecycle of DApp development on TRON. You now have:
tronweb to read from and write to the blockchain.You have the foundation. Now it’s time to build something complex.
Solidity Basics
Need a refresher on the language? Review Solidity syntax, EVM compatibility, and TRON-specific patterns.
Smart Contracts
Ready to write complex logic? Dive deep into TVM-specific behaviors and Energy optimization techniques.
tronweb SDK
Want to build a frontend? Get the full reference for account management, transaction signing, and event listening.
Fee Model & Resources
Wondering why deployments cost Energy? Learn how TRON’s resource model differs from traditional gas.