n
news11/24newsMultiple Tokens, One Contract: TRC-6909 on TRON with OpenZeppelin Tokens can represent many things: reward points, credits, currencies, or digital items. When an application needs several token types, it also needs a way to track who owns each type and who has permission to spend it. TRC-6909 brings multiple token types into one smart contract on TRON. Each type has its own token ID, and each wallet has a separate balance for each ID. A holder can transfer tokens directly or approve another wallet to spend a limited amount of a specific token. This article walks through a small TRC-6909 project built with OpenZeppelin Contracts for TRON. It uses Gold and Sword as two example tokens to show different decimals, transfers, and per-token allowances. You will clone the project, deploy the contract on Nile testnet, and check its behavior with a script that sends real testnet transactions. How the tokens work The contract has two token types: Both use the same contract address. The token ID tells the contract which balance to read or move. Gold uses six decimals, so one gold is represented by 1,000,000 units. A transfer of 25 gold uses the integer 25,000,000. Sword uses the metadata extension’s default of zero decimals, so one unit means one sword. These swords are interchangeable. Holding three swords means your balance for token ID 1 is three; each sword does not have a separate serial number. What OpenZeppelin provides GameItems inherits TRC6909Metadata from OpenZeppelin: import {TRC6909Metadata} from "@openzeppelin/tron-contracts/token/TRC6909/extensions/TRC6909Metadata.sol"; contract GameItems is TRC6909Metadata { // Configure token metadata and mint the initial supplies. } OpenZeppelin handles balances, transfers, allowances, operator permissions, and interface detection. The metadata extension adds a name, symbol, and decimals value for each token ID. The application contract sets those values and mints both supplies to the initial holder during deployment. It has no public functions for creating more tokens, burning tokens, or changing metadata afterward. That keeps this example focused on tokens with fixed supplies. Read the OpenZeppelin TRC-6909 guide for the underlying implementation and extensions. Clone and prepare the project Use Node.js 22 or newer, npm, and two dedicated Nile testnet wallets. The project is configured for TRON Solidity 0.8.31. All blockchain interactions in this walkthrough target Nile. Clone the repository: git clone [email protected]:aziz1975/tron-trc-6909-demo.git cd tron-trc-6909-demo npm ci npm run compile cp .env.sample .env Create two different wallets: A will hold the initial tokens, and B will receive tokens and spend an allowance. Add their private keys to .env: PRIVATE_KEY_NILE=YOUR_HOLDER_PRIVATE_KEY PRIVATE_KEY_NILE_SPENDER=YOUR_SPENDER_PRIVATE_KEY CONTRACT_ADDRESS_NILE= Keys must be 64 hexadecimal characters without the 0x prefix. Use dedicated testnet keys, keep them private, and never commit .env. Fund both wallets with Nile test TRX before continuing. A signs deployment, transfer, and approval transactions. B signs delegated transfers, including the transaction that is expected to fail. You can check public addresses and transactions in the Nile explorer. Deploy first, then test that address Deploy the token contract: npm run deploy:nile Copy the printed GameItems contract address into .env. Use the GameItems address: CONTRACT_ADDRESS_NILE=YOUR_GAMEITEMS_ADDRESS The deployment gives the initial gold and swords to wallet A. Now run: npm run test:nile:deployed This command runs scripts/test-nile-deployed.js against the configured address. It does not deploy another contract. What the script checks The script performs four transactions in order: A sends 25 gold to B. It checks that A loses 25 gold and B gains 25 gold. A approves B to spend two swords. B’s allowance becomes two. B transfers one sword from A to B. The allowance falls to one, and both sword balances are checked. B tries to spend two more swords. The transaction must revert with TRC6909InsufficientAllowance. Sword balances and the remaining allowance must stay unchanged. This shows how a holder can let another wallet spend a limited amount of one token type. An approval gives permission; it does not transfer tokens by itself. Before sending transactions, the script checks that A has at least 25 gold and three swords, both wallets have test TRX, and B is not already A’s operator. The three-sword minimum ensures that the rejected transfer fails because of its allowance, rather than an insufficient sword balance. The contract also inherits setOperator, which can authorize spending across all token IDs. This script does not call it. It only reads isOperator(A, B), because operator access would bypass the allowance being demonstrated. Read the result correctly A transaction ID means a transaction was broadcast. The script waits for mined receipts and checks execution results before printing passing assertions. Its final success message is: Both deployed Nile feature tests passed. B retains an allowance of one sword. A successful run changes the existing token balances: B gains 25 gold and one sword, and can still spend one more sword. If a later step fails, earlier successful transactions remain. Inspect the last printed transaction before retrying, and avoid other transfers involving these wallets during the run. This is a small educational example, not a full contract test suite. It has not been audited. Its purpose is to make TRC-6909 balances and per-token spending permissions easy to follow through real Nile transactions. Useful resources Project source code and README OpenZeppelin Contracts for TRON: TRC-6909 TronBox documentation: compilation and deployment TronWeb documentation: contract interaction Nile explorer: inspect wallets, contracts, and transactions
35/100·CLong+3