Development Getting Started Guide

Quick guide on how to do interact with the TRON blockchain when developing DApps. I will regularly update this guide with more examples and libraries

Javascript / Node.js

Libraries

Install TronWeb using NPM

npm install tronweb

This code assumes you imported the TronWeb object using const tronWeb = require("tronweb");

Generating a new wallet

const account = await TronWeb.createAccount()

result

{
    "address": {
      "base58": "TPbBpRXnt6ztse8XkCLiJstZyqQZvxW2sx",
      "hex": "4195679F3AAF5211991781D49B30525DDDFE9A18DE"
    },
    "privateKey": "08089C24EC3BAEB34254DDF5297CF8FBB8E031496FF67B4EFACA738FF9EBD455",
    "publicKey":  "04EE63599802B5D31A29C95CC7DF04F427E8F0A124BED9333F3A80404ACFC3127659C540D0162DEDB81AC5F74B2DEB4962656EFE112B252E54AC3BA1207CD1FB10"
}

Sending TRC10 tokens

// Use an existing private key. Can be generated with tronWeb.createAccount()
const privateKey = "..."

// First contstruct a tronWeb object with a private key
const tronWeb = new TronWeb({
  privateKey: privateKey,
});

tronWeb.trx.sendToken(
  "TVDGpn4hCSzJ5nkHPLetk8KQBtwaTppnkr", // TO Address
  1000, // Amount you want to send
  '100010' // TRC10 Token ID
);

Reading TRC10 Balance


const trc10TokenId = "1000001"

const tokenDetails = await tronWeb.trx.getTokenFromID(trc10TokenId)

const account = await tronWeb.trx.getAccount('TTSFjEG3Lu9WkHdp4JrWYhbGP6K1REqnGQ')
const trc10TokenBalance = account.assetV2.find(asset => asset.key === trc10TokenId).value;

// to then use the precision to convert
const trc10Balance = trc10TokenBalance / Math.pow(10, tokenDetails.precision || 0)

Sending TRC20 tokens

TRC20 tokens are smart contracts. Transfers and checking balances are done by calling
smart contract functions.

This example assumes the token contract has correctly implemented the TRC20 standard

// Use an existing private key. Can be generated with tronWeb.createAccount()
const privateKey = "..."

// This example is the TRC20-USDT token, change this with the token you want
const trc20ContractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";

// First contstruct a tronWeb object with a private key
const tronWeb = new TronWeb({
  privateKey: privateKey,
});

const trc20Contract = await tronWeb.contract().at(trc20ContractAddress);
await trc20Contract
  .transfer(
    "TR7N....", // Address to which to send the tokens
    1000000, // Amount of tokens you want to send
  )
  .send({
    feeLimit: 1000000 // Make sure to set a reasonable feelimit
  })

Reading TRC20 Balances

const privateKey = "..."
const trc20ContractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";
const walletAddress = "...";

// First contstruct a tronWeb object with a private key
const tronWeb = new TronWeb({
  privateKey: privateKey,
});

const trc20Contract = await tronWeb.contract().at(trc20ContractAddress);
const trc20Decimals = await trc20Contract.decimals().call();
const trc20AccountBalance = await trc20Contract.balanceOf(walletAddress).call();

// trc20 balance
const trc20Balance = trc20AccountBalance
                       .dividedBy(Math.pow(10, trc20Decimals.toNumber()))
                       .dp(trc20Decimals.toNumber(), BigNumber.ROUND_DOWN);

More examples comings soon…

PHP

Libraries

Coming soon…

Python

Libraries

Coming soon…

Java

Coming soon…

Rust

Libraries

>> This guide will be kept updated with frequently asked questions. Don’t hesitate to post any questions here if you feel the guide is missing something <<

3 Likes

+Rust

https://github.com/andelf/rust-tron

1 Like

Thanks, added! Completely missed that one, i might contribute to the code later because i have been planning to learn Rust for a while but haven’t found a good project to start with.

1 Like

Great and thank you for creating this dev guide.
shared your posting and tronics community homepage launching news in twitter

2 Likes

Thanks for creating the dev guide. The code works perfectly, and it’s cleaner that using triggerSmartContract.

The next steps for me are:

  • checking status of the transaction
  • and checking balances

I added this to the code which seems to simplify the transfer amount and fixes some weird outputs

var amount = 1.000001 // as an example
amount = Number(amount) * Math.pow(10, 6) // outputs 1000000.9999999999
amount = amount.toFixed(0) // but this fixes it: 1000001

Note: I didn’t find this conversion in the docs. I put through a trans for 1 USDT-TRON and it transferred .000001. So, I added the conversion. SUN is a TRX conversion, so I’m not sure why USDT-TRON requires this conversion. Does anyone know why?

To check balances:

TRC10


const trc10TokenId = "1000001"

const tokenDetails = await tronWeb.trx.getTokenFromID(trc10TokenId)

const account = await tronWeb.trx.getAccount('TTSFjEG3Lu9WkHdp4JrWYhbGP6K1REqnGQ')
const trc10TokenBalance = account.assetV2.find(asset => asset.key === trc10TokenId).value;

// to then use the precision to convert
const trc10Balance = trc10TokenBalance / Math.pow(10, tokenDetails.precision || 0)

TR20

const privateKey = "..."
const trc20ContractAddress = "TR7NHqjeKQxGTCi8q8ZY4pL8otSzgjLj6t";
const walletAddress = "...";

// First contstruct a tronWeb object with a private key
const tronWeb = new TronWeb({
  privateKey: privateKey,
});

const trc20Contract = await tronWeb.contract().at(trc20ContractAddress);
const trc20Decimals = await trc20Contract.decimals().call();
const trc20AccountBalance = await trc20Contract.balanceOf(walletAddress).call();

// trc20 balance
const trc20Balance = trc20AccountBalance
                       .dividedBy(Math.pow(10, trc20Decimals.toNumber()))
                       .dp(trc20Decimals.toNumber(), BigNumber.ROUND_DOWN);

Thanks Rovak, Code works perfectly.

I’m trying to get the number of blocks that have confirmed a TRC20 transaction. Any ideas?

I can get the result using: tronWeb.trx.getTransactionInfo(txn), but I don’t see a method for returning the number of blocks confirmed.