Skip to main content
The Crypto Shop Backend supports both TRON testnet (Nile) and mainnet. Proper network configuration is critical for development, testing, and production deployments.

Environment Variables

Network configuration is managed through the TRON_NETWORK environment variable in your .env file:
~/workspace/source/.env.example:8-9
# TRON Network (usa Nile para testnet o mainnet para producción)
TRON_NETWORK=https://nile.trongrid.io

Network Endpoints

TRON_NETWORK=https://nile.trongrid.io
NODE_ENV=development
Nile Testnet Features:
  • Free test TRX from faucets
  • Identical to mainnet functionality
  • No real money at risk
  • Perfect for development and testing
  • Block time: ~3 seconds
Get Free Test TRX: Visit the Nile Testnet Faucet to receive free TRX for testing.

Mainnet (Production Only)

TRON_NETWORK=https://api.trongrid.io
NODE_ENV=production
Mainnet Features:
  • Real TRX cryptocurrency
  • Production-ready transactions
  • Requires real funds
  • Same block time and confirmation rules
Production Warning: Mainnet uses real TRX. Ensure thorough testing on Nile testnet before deploying to mainnet. Transactions are irreversible!

Network Comparison

FeatureNile TestnetMainnet
Endpointhttps://nile.trongrid.iohttps://api.trongrid.io
CurrencyTest TRX (no value)Real TRX
Block Time~3 seconds~3 seconds
Confirmations21 blocks21 blocks
FaucetAvailableNot available
Use CaseDevelopment, testingProduction
CostFreeReal money

Configuration by Environment

Use different configurations for each environment:
NODE_ENV=development
TRON_NETWORK=https://nile.trongrid.io
PORT=3000
MONGODB_URI=mongodb://localhost:27017/crypto-shop-dev

Using TronWeb with Network Config

The backend initializes TronWeb using the environment variable:
~/workspace/source/src/services/tron.service.js:1-10
import pkg from "tronweb";
import dotenv from "dotenv";

dotenv.config();

const { TronWeb } = pkg;

const tronWeb = new TronWeb({
  fullHost: process.env.TRON_NETWORK
});
This same pattern is used across all services:
~/workspace/source/src/services/transactionListener.js:4-14
import pkg from 'tronweb';
import dotenv from 'dotenv';

dotenv.config();

const { TronWeb } = pkg;

const tronWeb = new TronWeb({
  fullHost: process.env.TRON_NETWORK
});

Network-Specific Considerations

Testnet (Nile)

Advantages:
  • Safe for experimentation
  • Free test TRX available
  • Identical API to mainnet
  • Full transaction history
Limitations:
  • Test data may be reset periodically
  • Not for production use
  • Test TRX has no real value
Getting Test TRX:
  1. Create a wallet using the backend API
  2. Visit Nile Faucet
  3. Enter your testnet address
  4. Receive 10,000 test TRX

Mainnet (Production)

Advantages:
  • Real cryptocurrency transactions
  • Production-ready
  • Full network security
Requirements:
  • Real TRX for transactions
  • Proper security measures
  • Comprehensive testing completed
  • Error handling and monitoring

Switching Networks

To switch between networks, update your .env file:
1

Update Environment Variable

Edit .env and change TRON_NETWORK:
# Switch to testnet
TRON_NETWORK=https://nile.trongrid.io

# OR switch to mainnet
TRON_NETWORK=https://api.trongrid.io
2

Restart the Server

The server must be restarted to load the new configuration:
npm run dev
3

Verify Connection

Check server logs to confirm the network:
[TRON] Connected to network: https://nile.trongrid.io
Important: Addresses and private keys generated on testnet can also be used on mainnet, but they will have different balances and transaction histories. Keep testnet and mainnet wallets separate!

Address Compatibility

TRON addresses are compatible across networks:
// Same address format on both networks
const address = 'TJMKu9GcRaRaXqMVYhJBVPFVs1g5RgGJax';

// Valid on Nile testnet
const testnetBalance = await tronWeb.trx.getBalance(address);

// Also valid on mainnet (but different balance)
const mainnetBalance = await tronWeb.trx.getBalance(address);

Best Practices

Maintain separate .env files for each environment:
  • .env.development (Nile testnet)
  • .env.staging (Nile testnet)
  • .env.production (Mainnet)
Use tools like dotenv-cli to load the correct file:
dotenv -e .env.production -- node server.js
Add .env to .gitignore:
.gitignore
.env
.env.local
.env.*.local
Commit only .env.example with safe placeholder values.
Add validation to ensure correct network configuration:
const validateNetwork = async () => {
  const isProduction = process.env.NODE_ENV === 'production';
  const isMainnet = process.env.TRON_NETWORK.includes('api.trongrid.io');
  
  if (isProduction && !isMainnet) {
    throw new Error('Production must use mainnet!');
  }
  
  if (!isProduction && isMainnet) {
    console.warn('⚠️  Warning: Using mainnet in non-production environment!');
  }
};
Before mainnet deployment:
  • Test all transaction types
  • Verify refund processing
  • Test error handling
  • Validate transaction listener
  • Check Socket.io notifications

Monitoring Network Health

Check network status programmatically:
const checkNetworkHealth = async () => {
  try {
    const block = await tronWeb.trx.getCurrentBlock();
    const blockNumber = block.block_header.raw_data.number;
    const timestamp = block.block_header.raw_data.timestamp;
    
    console.log('Network Status:');
    console.log('- Endpoint:', process.env.TRON_NETWORK);
    console.log('- Current Block:', blockNumber);
    console.log('- Block Time:', new Date(timestamp));
    console.log('- Status: ✓ Connected');
    
    return true;
  } catch (error) {
    console.error('Network Status: ✗ Disconnected');
    console.error('Error:', error.message);
    return false;
  }
};

Additional Resources

TRON Documentation

Official TRON developer documentation

Nile Testnet Explorer

View testnet transactions and blocks

Mainnet Explorer

View mainnet transactions and blocks

TronGrid API

API documentation and rate limits

Next Steps

TRON Setup

Configure TronWeb and create wallets

Transaction Listener

Monitor blockchain transactions

Build docs developers (and LLMs) love