Skip to main content
The Crypto Shop Backend uses TronWeb to interact with the TRON blockchain. This guide covers how to set up and configure the TRON network connection.

Installation

The project uses the tronweb package to communicate with TRON nodes:
npm install tronweb dotenv

Environment Configuration

Configure your TRON network endpoint in the .env file. The backend supports both testnet and mainnet:
TRON_NETWORK=https://nile.trongrid.io
Always use Nile testnet (https://nile.trongrid.io) for development and testing. Only switch to mainnet for production deployments with real TRX.

TronWeb Initialization

The backend initializes TronWeb in service files. Here’s how it’s configured:
~/workspace/source/src/services/tron.service.js
import pkg from "tronweb";
import dotenv from "dotenv";

dotenv.config();

const { TronWeb } = pkg;

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

Core TRON Service Functions

Create Wallet

Generate a new TRON wallet with address, private key, and public key:
export const createWallet = async () => {
  return await tronWeb.createAccount();
};
Response format:
{
  "address": {
    "base58": "TJMKu9GcRaRaXqMVYhJBVPFVs1g5RgGJax",
    "hex": "41505dd6c2eb5a7e2e3e7e8c4f5a5f5a5f5a5f5a"
  },
  "privateKey": "d3c4f5a6b7c8d9e0f1a2b3c4d5e6f7a8b9c0d1e2f3a4b5c6d7e8f9a0b1c2d3e4",
  "publicKey": "04a1b2c3d4e5f6..."
}

Get Balance

Retrieve TRX balance for any address:
export const getBalance = async (address) => {
  try {
    if (!tronWeb.isAddress(address)) {
      throw new Error("Invalid TRON address");
    }
    const balance = await tronWeb.trx.getBalance(address);
    return tronWeb.fromSun(balance); // Convert from SUN to TRX
  } catch (error) {
    throw error;
  }
};
TRON uses SUN as its smallest unit. 1 TRX = 1,000,000 SUN. Use tronWeb.toSun() and tronWeb.fromSun() for conversions.

Send TRX

Send TRX from one address to another:
export const sendTRX = async (privateKey, toAddress, amount) => {
  try {
    const tronWebWithPK = new TronWeb({
      fullHost: process.env.TRON_NETWORK,
      privateKey
    });

    const transaction = await tronWebWithPK.transactionBuilder.sendTrx(
      toAddress,
      tronWebWithPK.toSun(amount) // Convert TRX to SUN
    );

    const signedTx = await tronWebWithPK.trx.sign(transaction);
    return await tronWebWithPK.trx.sendRawTransaction(signedTx);
  } catch (error) {
    throw error;
  }
};
Usage example:
const result = await sendTRX(
  'your_private_key_here',
  'TJMKu9GcRaRaXqMVYhJBVPFVs1g5RgGJax',
  10 // Amount in TRX
);

Security Best Practices

Never commit private keys to version control! Always store them securely:
  • Use environment variables for configuration
  • Store merchant private keys in secure key management systems
  • Never log private keys in application logs
  • Rotate keys regularly in production

Network Endpoints

NetworkEndpointUse Case
Nile Testnethttps://nile.trongrid.ioDevelopment, testing, staging
Mainnethttps://api.trongrid.ioProduction with real TRX

Testing Connection

Verify your TRON network connection:
const testConnection = async () => {
  try {
    const currentBlock = await tronWeb.trx.getCurrentBlock();
    console.log('Connected to TRON network');
    console.log('Current block:', currentBlock.block_header.raw_data.number);
    return true;
  } catch (error) {
    console.error('Failed to connect:', error.message);
    return false;
  }
};

Next Steps

Transaction Listener

Learn how to monitor blockchain transactions

Network Configuration

Configure testnet vs mainnet settings

Build docs developers (and LLMs) love