JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 6
  • Score
    100M100P100Q58478F
  • License MIT

A modern development framework for Tron blockchain smart contracts

Package Exports

  • tronhat
  • tronhat/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (tronhat) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

TronHat ๐ŸŽฉ

A modern, professional development framework for Tron blockchain smart contract development. TronHat provides a complete toolkit for compiling, deploying, testing, and debugging Tron smart contracts with an intuitive, developer-friendly experience.

Features

  • ๐Ÿš€ CLI-based toolkit - Complete command-line interface for all development tasks
  • ๐Ÿ”ง Solidity compilation - Integrated Solidity compiler with version management
  • ๐ŸŒ Network management - Support for mainnet, Nile testnet, and custom networks
  • ๐Ÿงช Testing framework - Built-in Mocha/Chai testing with TronWeb integration
  • ๐Ÿ“ฆ Project scaffolding - Quick project initialization with best practices
  • ๐Ÿ”Œ Plugin system - Extensible architecture with plugin support
  • ๐Ÿ’ป Interactive console - REPL environment with preloaded TronWeb instance
  • ๐Ÿ“Š Deployment tracking - Automatic deployment logging and verification

Installation

npx tronhat init my-project

Global Installation

npm install -g tronhat

Local Installation

npm install --save-dev tronhat

Or use with npx:

npx tronhat --version

Quick Start

1. Initialize a new project

mkdir my-tron-project
cd my-tron-project
npx tronhat init

This creates:

my-tron-project/
โ”œโ”€โ”€ contracts/          # Solidity contracts
โ”œโ”€โ”€ scripts/            # Deployment scripts
โ”œโ”€โ”€ test/              # Test files
โ”œโ”€โ”€ tronhat.config.js  # Configuration file
โ””โ”€โ”€ package.json

2. Install dependencies

npm install

3. Compile contracts

npx tronhat compile

4. Run tests

npx tronhat test

5. Deploy to testnet

# Set your private key in .env file
echo "PRIVATE_KEY=your_private_key_here" > .env

# Deploy to Nile testnet
npx tronhat deploy --network nile

Configuration

TronHat uses a tronhat.config.js file for configuration:

module.exports = {
  networks: {
    development: {
      fullHost: 'http://127.0.0.1:9090',
      privateKey: 'your_development_private_key'
    },
    nile: {
      fullHost: 'https://nile.trongrid.io',
      privateKey: process.env.PRIVATE_KEY
    },
    mainnet: {
      fullHost: 'https://api.trongrid.io',
      privateKey: process.env.PRIVATE_KEY
    }
  },
  solidity: {
    version: '0.8.20',
    settings: {
      optimizer: {
        enabled: true,
        runs: 200
      }
    }
  },
  paths: {
    sources: './contracts',
    artifacts: './artifacts',
    cache: './cache',
    tests: './test'
  }
};

Commands

tronhat init [path]

Initialize a new TronHat project with sample contracts and configuration.

tronhat init my-project

tronhat compile

Compile all Solidity contracts in the contracts directory.

tronhat compile
tronhat compile --force  # Force recompilation

tronhat deploy

Deploy contracts using deployment scripts.

tronhat deploy                    # Deploy to development network
tronhat deploy --network nile     # Deploy to Nile testnet
tronhat deploy --network mainnet  # Deploy to mainnet
tronhat deploy --script custom-deploy.js  # Use custom script

tronhat test

Run the test suite with Mocha and Chai.

tronhat test                      # Run all tests
tronhat test --grep "MyToken"     # Run specific tests
tronhat test --network nile       # Run tests on specific network
tronhat test --bail               # Stop on first failure

tronhat console

Open an interactive console with TronWeb preloaded.

tronhat console                   # Connect to development network
tronhat console --network nile    # Connect to Nile testnet

In the console:

// TronWeb instance is available
await tronWeb.trx.getBalance('TRX_ADDRESS')

// Deploy contracts interactively
const contract = await deployContract('MyToken')

// Access artifacts
const artifact = await artifacts.readArtifact('MyToken')

Writing Contracts

Place your Solidity contracts in the contracts/ directory:

// contracts/MyToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract MyToken {
    string public name = "MyToken";
    string public symbol = "MTK";
    uint8 public decimals = 6;
    uint256 public totalSupply = 1000000 * 10**decimals;
    
    mapping(address => uint256) public balanceOf;
    
    constructor() {
        balanceOf[msg.sender] = totalSupply;
    }
    
    function transfer(address to, uint256 value) public returns (bool) {
        require(balanceOf[msg.sender] >= value, "Insufficient balance");
        balanceOf[msg.sender] -= value;
        balanceOf[to] += value;
        return true;
    }
}

Writing Tests

Create test files in the test/ directory:

// test/MyToken.test.js
const { expect } = require('chai');

describe('MyToken', function() {
  let myToken;
  let accounts;

  before(async function() {
    accounts = global.accounts;
  });

  beforeEach(async function() {
    const MyToken = await tronhat.artifacts.readArtifact('MyToken');
    const contract = await tronWeb.contract().new({
      abi: MyToken.abi,
      bytecode: MyToken.bytecode
    });
    
    myToken = await tronWeb.contract(MyToken.abi, contract.address);
  });

  it('Should have correct initial supply', async function() {
    const totalSupply = await myToken.totalSupply().call();
    expect(totalSupply.toString()).to.equal('1000000000000');
  });

  it('Should transfer tokens', async function() {
    await myToken.transfer(accounts[1], 1000).send();
    const balance = await myToken.balanceOf(accounts[1]).call();
    expect(balance.toString()).to.equal('1000');
  });
});

Deployment Scripts

Create deployment scripts in the scripts/ directory:

// scripts/deploy.js
async function main() {
  console.log('Deploying MyToken...');
  
  const MyToken = await tronhat.artifacts.readArtifact('MyToken');
  
  const myToken = await tronhat.tronWeb.contract().new({
    abi: MyToken.abi,
    bytecode: MyToken.bytecode
  });

  console.log('MyToken deployed to:', myToken.address);
  
  // Verify deployment
  const contract = await tronhat.tronWeb.contract(MyToken.abi, myToken.address);
  const name = await contract.name().call();
  console.log('Token name:', name);
}

main()
  .then(() => process.exit(0))
  .catch((error) => {
    console.error(error);
    process.exit(1);
  });

Plugin System

TronHat supports plugins for extending functionality:

// tronhat.config.js
module.exports = {
  // ... other config
  plugins: [
    'tronhat-trc20',
    'tronhat-verify'
  ]
};

Built-in Plugins

  • tronhat-trc20 - Quick TRC20 token deployment
  • tronhat-verify - Contract verification on TronScan

Environment Variables

Create a .env file for sensitive configuration:

# .env
PRIVATE_KEY=your_private_key_without_0x_prefix
NILE_URL=https://nile.trongrid.io
MAINNET_URL=https://api.trongrid.io

Network Configuration

Development Network

For local development, you can use TronBox's development network or docker containers.

Nile Testnet

Free testnet for development and testing. Get test TRX from the faucet.

Mainnet

Production Tron network. Use with caution and proper security measures.

Best Practices

  1. Security

    • Never commit private keys to version control
    • Use environment variables for sensitive data
    • Test thoroughly on testnet before mainnet deployment
  2. Testing

    • Write comprehensive tests for all contract functions
    • Test edge cases and error conditions
    • Use meaningful test descriptions
  3. Deployment

    • Use deployment scripts for reproducible deployments
    • Verify contracts after deployment
    • Keep track of deployed contract addresses

Troubleshooting

Common Issues

  1. Compilation Errors

    # Check Solidity version compatibility
    tronhat compile --force
  2. Network Connection Issues

    # Test network connectivity
    tronhat console --network nile
    > await tronWeb.trx.getCurrentBlock()
  3. Private Key Issues

    # Verify private key format (no 0x prefix)
    # Check .env file configuration

Contributing

We welcome contributions! Please see our Contributing Guide for details.

License

MIT License - see LICENSE file for details.

Support


Built with โค๏ธ for the Tron developer community