Create TRC10 Token on TRON network with nodejs

About TRC10: https://developers.tron.network/docs/trc10-token

Create TRON wallet with browser extension
Tronlink: https://chrome.google.com/webstore/detail/tronlink /ibnejdfjmmkpcnlpebklmnkoeoihofec
Go to setting, update the environment to shasta
Get some TRX for development: https://www.trongrid.io/faucet

Install tronweb: https://developers.tron.network/docs/tron-web-intro

npm i tronweb --save

TRONTokenClient.ts

import TronWeb from 'tronweb'

export default class TRONTokenClient {
  async start(): Promise<any> {
    const mainNetProvider = 'https://api.trongrid.io';
    const testNetProvider = 'https://api.shasta.trongrid.io';
    const netProvider = testNetProvider;
    const HttpProvider = TronWeb.providers.HttpProvider; // Optional provider, can just use a url for the nodes instead
    const fullNode = new HttpProvider(`${netProvider}`); // Full node http endpoint
    const solidityNode = new HttpProvider(`${netProvider}`); // Solidity node http endpoint
    const eventServer = `${netProvider}`; // Contract events http endpoint
    const privateKey = 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx';
    const tronWeb = new TronWeb(
      fullNode,
      solidityNode,
      eventServer,
      privateKey
    );
    const defaultTestAddress = tronWeb.address.fromPrivateKey(privateKey);
    await this.createTestTRC10Token(tronWeb, defaultTestAddress, privateKey);
    // Verify at: https://shasta.tronscan.org/#/
  }

  async signAndSubmitTx(tronWeb, rawTxObject, privateKey) {
    console.log(rawTxObject);
    const sign = await tronWeb.trx.sign(rawTxObject, privateKey);
    console.log({sign});
    const tx = await tronWeb.trx.sendRawTransaction(sign);
    // console.log(tx);
    return tx;
  }


  async createTestTRC10Token(tronWeb, issuerAddress, privateKey) {
    try {
      let options = {
        name: 'EricCaoToken',
        abbreviation: 'nhancv',
        description: 'Nhan Cao Token for Test',
        url: 'nhancv.com',
        totalSupply: 1000000000,
        trxRatio: 1, // How much TRX will tokenRatio cost?
        tokenRatio: 1, // How many tokens will trxRatio afford?
        saleStart: Date.now() + 10000, // sale start mus > now
        saleEnd: Date.now() + 31536000000, // 365 days
        freeBandwidth: 0, // The creator's "donated" bandwidth for use by token holders
        freeBandwidthLimit: 0, // Out of totalFreeBandwidth, the amount each token holder get
        frozenAmount: 0,
        frozenDuration: 0
      };
      const issuerHex = tronWeb.address.toHex(issuerAddress);
      const rawTxObj = await tronWeb.transactionBuilder.createToken(options, issuerHex);
      // https://shasta.tronscan.org/#/
      console.log({rawTxObj});
      const res = await this.signAndSubmitTx(tronWeb, rawTxObj, privateKey);
      console.log({res});
    } catch (e) {
      console.error(e);
    }
  }
}

Note: An account can only issue one asset

https://medium.com/@nhancv/create-trc10-token-on-tron-network-with-nodejs-9970116d5284

Leave a Reply

Your email address will not be published.Required fields are marked *