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
Using NPX (Recommended)
npx tronhat init my-projectGlobal Installation
npm install -g tronhatLocal Installation
npm install --save-dev tronhatOr use with npx:
npx tronhat --versionQuick Start
1. Initialize a new project
mkdir my-tron-project
cd my-tron-project
npx tronhat initThis creates:
my-tron-project/
โโโ contracts/ # Solidity contracts
โโโ scripts/ # Deployment scripts
โโโ test/ # Test files
โโโ tronhat.config.js # Configuration file
โโโ package.json2. Install dependencies
npm install3. Compile contracts
npx tronhat compile4. Run tests
npx tronhat test5. 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 nileConfiguration
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-projecttronhat compile
Compile all Solidity contracts in the contracts directory.
tronhat compile
tronhat compile --force # Force recompilationtronhat 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 scripttronhat 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 failuretronhat console
Open an interactive console with TronWeb preloaded.
tronhat console # Connect to development network
tronhat console --network nile # Connect to Nile testnetIn 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.ioNetwork 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
Security
- Never commit private keys to version control
- Use environment variables for sensitive data
- Test thoroughly on testnet before mainnet deployment
Testing
- Write comprehensive tests for all contract functions
- Test edge cases and error conditions
- Use meaningful test descriptions
Deployment
- Use deployment scripts for reproducible deployments
- Verify contracts after deployment
- Keep track of deployed contract addresses
Troubleshooting
Common Issues
Compilation Errors
# Check Solidity version compatibility tronhat compile --force
Network Connection Issues
# Test network connectivity tronhat console --network nile > await tronWeb.trx.getCurrentBlock()
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
- ๐ Documentation
- ๐ฌ Discord Community
- ๐ Issue Tracker
- ๐ง Email Support
Built with โค๏ธ for the Tron developer community