TRC-20 Contract Interaction Guide: Methods and Examples

ยท

This comprehensive guide explores how to interact with TRC-20 token contracts using TronWeb and wallet-cli, focusing on the USDT contract on the Shasta testnet as a practical example.

Key Resources

Constant Function Calls

Constant functions allow querying contract data without broadcasting transactions to the blockchain. Ensure your node has supportConstant = true configured.

1. Getting Token Information

Name Function

// TronWeb Example
const result = await contract.name().call();
console.log('Token Name:', result);
// Wallet-cli Example
TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK name() # false

Symbol Function

// TronWeb Example
const symbol = await contract.symbol().call();
console.log('Token Symbol:', symbol);
// Wallet-cli Example
TriggerConstantContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK symbol() # false

Decimals Function

// TronWeb Example
const decimals = await contract.decimals().call();
console.log('Token Decimals:', decimals);

2. Supply and Balance Queries

Total Supply

// TronWeb Example
const totalSupply = await contract.totalSupply().call();
console.log('Total Supply:', totalSupply);

Balance Check

// TronWeb Example
const balance = await contract.balanceOf("TM2TmqauSEiRf...").call();
console.log('Account Balance:', balance);

Transaction Functions

These functions modify blockchain state and require transaction broadcasting.

3. Token Transfers

Transfer Function

// TronWeb Example
await contract.transfer(
  "TVDGp...", // Recipient
  1000000    // Amount
).send({ feeLimit: 1000000 });
// Wallet-cli Example
TriggerContract TQQg4EL8o1BSeKJY4MJ8TB8XK7xufxFBvK transfer(address,uint256) "TBQDy...",100 false 100000000 0 0 #

Approval System

// TronWeb Example (Approve)
await contract.approve(
  "TA1g2...", // Spender
  10000000   // Amount
).send({ feeLimit: 100000000 });
// TronWeb Example (TransferFrom)
await contract.transferFrom(
  "TM2Tm...", // From
  "TVDGp...", // To
  100000     // Amount
).send({ feeLimit: 10000000 });

4. Allowance Check

// TronWeb Example
const allowance = await contract.allowance(
  "TM2Tm...", // Owner
  "TA1g2..."  // Spender
).call();
console.log('Remaining Allowance:', allowance);

FAQ Section

Q1: What's the difference between constant and transaction functions?

A: Constant functions are read-only queries that don't modify blockchain state, while transaction functions change contract data and require broadcasting.

Q2: How do I calculate the correct parameter encoding?

A: Use Tron's parameter encoding guide for proper formatting.

Q3: What fee limit should I set for transactions?

A: Start with 100 TRX (100000000 sun) for most TRC-20 operations and adjust based on network conditions.

๐Ÿ‘‰ For more TRC-20 development resources

๐Ÿ‘‰ Advanced smart contract tutorials