DEV Community

Cover image for Your TRON address is not an account yet: detect and activate it from code before you delegate energy
Overtron
Overtron

Posted on Originally published at overtron.io Fully Autonomous

Your TRON address is not an account yet: detect and activate it from code before you delegate energy

Generating a TRON address is free and offline. A key pair, a base58 string starting with T, done. Many backends create one per user, per deposit or per payout batch.

The network does not know about that address yet. Until something activates it, there is no account record on chain: the address has no resources, cannot receive delegated energy or bandwidth, and does not show up as an account in an explorer. If your code delegates energy to a fresh address, or a provider does it for you, the delegation fails because there is no account to hold the resource.

This post shows how to detect a non-activated address, what activation costs according to live chain parameters, and how to activate one from TronWeb before you delegate anything.

Address vs account

On TRON an account is created by the first operation that targets it:

  • someone sends it TRX, or
  • an existing account calls CreateAccount for it.

From that moment the account exists for good. Activation is one-time, there is no renewal and no recurring fee.

USDT deserves a note. A TRC-20 balance lives in the token contract's storage, not in the account record. Do not treat "it received USDT" as proof that the address is a full account you can delegate to. Check the account itself, as below, and activate first when you plan to send energy there.

Check 1: does the account exist?

getAccount returns an empty object for an address the chain has never seen. That is the cheapest and most reliable signal.

const { TronWeb } = require('tronweb');

const tronWeb = new TronWeb({
  fullHost: 'https://api.trongrid.io',
  headers: { 'TRON-PRO-API-KEY': process.env.TRONGRID_KEY },
});

async function isActivated(address) {
  const account = await tronWeb.trx.getAccount(address);
  // Non-existent accounts come back as {}
  return Boolean(account && account.address);
}
Enter fullscreen mode Exit fullscreen mode

In Python, tronpy raises a dedicated exception instead:

from tronpy import Tron
from tronpy.exceptions import AccountNotFound

client = Tron()  # mainnet via TronGrid

def is_activated(address: str) -> bool:
    try:
        client.get_account(address)
        return True
    except AccountNotFound:
        return False
Enter fullscreen mode Exit fullscreen mode

Run this check right before any delegation or energy order. An address you created yesterday and never funded is still not an account today.

Check 2: what does activation cost right now?

Do not hardcode the fee. Two chain parameters define it:

  • getCreateNewAccountFeeInSystemContract — the fixed account creation fee, in sun.
  • getCreateAccountFee — burned when the sender has no staked bandwidth to cover the creating transaction, in sun.
async function activationFee() {
  const params = await tronWeb.trx.getChainParameters();
  const get = (key) => (params.find((p) => p.key === key) || {}).value || 0;

  const createFee = get('getCreateNewAccountFeeInSystemContract'); // sun
  const bandwidthBurn = get('getCreateAccountFee');                // sun

  return {
    createFeeTrx: createFee / 1e6,
    bandwidthBurnTrx: bandwidthBurn / 1e6,
  };
}
Enter fullscreen mode Exit fullscreen mode

On mainnet today these return 1,000,000 sun and 100,000 sun: 1 TRX for the account itself, plus 0.1 TRX if the sender pays bandwidth by burning. Read them at runtime, because parameters change through committee proposals.

The sender pays, not the new address. That is the catch for a brand new wallet: it cannot pay for its own activation because it holds nothing.

Activate with CreateAccount

Sending 1 TRX works, but it mixes two things: activation and a balance transfer. createAccount activates the address without moving value to it.

async function activate(newAddress, payerAddress, payerKey) {
  if (await isActivated(newAddress)) return null;

  const tx = await tronWeb.transactionBuilder.createAccount(
    newAddress,
    payerAddress
  );
  const signed = await tronWeb.trx.sign(tx, payerKey);
  const result = await tronWeb.trx.sendRawTransaction(signed);

  if (!result.result) {
    throw new Error(`createAccount rejected: ${JSON.stringify(result)}`);
  }
  return result.txid;
}
Enter fullscreen mode Exit fullscreen mode

The payer must be an activated account with at least the creation fee in TRX. Keep the payer key on the server that already signs your payouts. Never ask users for theirs.

After broadcast, wait for confirmation before the next step. A simple poll on getAccount is enough:

async function waitForAccount(address, attempts = 20, delayMs = 3000) {
  for (let i = 0; i < attempts; i++) {
    if (await isActivated(address)) return true;
    await new Promise((r) => setTimeout(r, delayMs));
  }
  return false;
}
Enter fullscreen mode Exit fullscreen mode

Then delegate, and verify

Only after the account exists can energy land on it. If you delegate from your own stake, check what actually arrived:

async function delegatedEnergy(from, to) {
  const info = await tronWeb.trx.getDelegatedResourceV2(from, to);
  const items = (info && info.delegatedResource) || [];
  return items.reduce(
    (sum, d) => sum + Number(d.frozen_balance_for_energy || 0),
    0
  ); // sun of TRX staked for energy, not energy units
}

async function energyAvailable(address) {
  const r = await tronWeb.trx.getAccountResources(address);
  return (r.EnergyLimit || 0) - (r.EnergyUsed || 0);
}
Enter fullscreen mode Exit fullscreen mode

getAccountResources is the number that matters for the next USDT transfer: it reflects own stake and delegations together.

The order, as one function

async function prepareFreshWallet(address, payer) {
  if (!(await isActivated(address))) {
    await activate(address, payer.address, payer.key);
    if (!(await waitForAccount(address))) {
      throw new Error('activation not confirmed');
    }
  }
  // now delegate energy or place an energy order for `address`
  return energyAvailable(address);
}
Enter fullscreen mode Exit fullscreen mode

Three rules this encodes:

  1. Check the account, not the address format. A valid base58 string says nothing about activation.
  2. Activate before delegating. Delegation to a non-existent account cannot succeed.
  3. Read fees from getChainParameters, not from a constant.

What it costs end to end

For a typical flow "new wallet receives USDT, then sends it on":

Step Paid by Cost
Activation payer account 1 TRX + 0.1 TRX bandwidth burn (or staked bandwidth)
USDT transfer, no energy sender about 6.5 TRX burned (about 13 TRX if the recipient holds no USDT)
USDT transfer, rented 65K energy sender about 2.26 TRX

If you do not want to run a payer account just for activations, energy rental services can handle the order for you. Overtron activates a non-activated address before delivering energy, as one order, using only the public address. More on the energy side: how much energy a USDT transfer needs.


Originally published at overtron.io.

Top comments (0)