Migrating from Truffle to TronBox
This guide walks you through the process of migrating an existing Truffle project to TronBox.
Migration Workflow
Step 1: Initialize TronBox project
Start by creating a fresh TronBox workspace. This scaffolds the standard folders (contracts, migrations, test) and a baseline project structure.
mkdir my-tron-project
cd my-tron-project
tronbox initStep 2: Copy your existing files
Reuse your Truffle sources directly. Most Solidity contracts migrate without changes.
# Copy contracts (no changes needed for most Solidity code)
cp -r ../my-truffle-project/contracts/* ./contracts/
# Copy migrations (minimal changes needed)
cp -r ../my-truffle-project/migrations/* ./migrations/
# Copy tests (will need TronWeb adaptations)
cp -r ../my-truffle-project/test/* ./test/Step 3: Configure TronBox
Create tronbox-config.js based on your truffle-config.js, then set networks and compiler as follows:
// tronbox-config.js
module.exports = {
networks: {
// Local development (TronBox TRE / local node)
development: {
privateKey: 'your-dev-private-key',
feeLimit: 1e8,
fullHost: 'http://127.0.0.1:9090',
network_id: '9'
},
// Nile Testnet (recommended for testing)
nile: {
privateKey: process.env.PRIVATE_KEY,
feeLimit: 1e8,
fullHost: 'https://nile.trongrid.io',
network_id: '3'
},
// Mainnet (production)
mainnet: {
privateKey: process.env.PRIVATE_KEY,
feeLimit: 1e8,
fullHost: 'https://api.trongrid.io',
network_id: '1'
}
},
compilers: {
// TVM Solidity versions
solc: {
version: '0.8.6',
settings: {
optimizer: {
enabled: true,
runs: 200
}
}
}
}
};Changes from Truffle:
- Endpoint: use one
fullHostURL (replaceshost+port). - Accounts: set
privateKeyormnemonicper network. - Fees: set
feeLimit(replacesgas+gasPrice). - Compiler: pick a TVM Solidity version.
Step 4: Update migration scripts
Migration files follow the same pattern as Truffle.
// migrations/2_deploy_contracts.js (Truffle)
const MyContract = artifacts.require('MyContract');
module.exports = function (deployer) {
deployer.deploy(MyContract /* constructor arguments */);
};// migrations/2_deploy_contracts.js (TronBox)
const MyContract = artifacts.require('MyContract');
module.exports = function (deployer) {
deployer.deploy(MyContract /* constructor arguments */);
};Step 5: Update tests
Tests keep the same structure; replace web3 with tronWeb:
// test/my_contract.test.js (Truffle)
const MyContract = artifacts.require('MyContract');
contract('MyContract', accounts => {
it('should work', async () => {
const instance = await MyContract.deployed();
// Log account balance (wei)
const balance = await web3.eth.getBalance(accounts[0]);
console.log('ETH balance (wei):', balance);
// Set value via transaction
await instance.setValue(42);
// Verify state via call
const value = await instance.getValue();
assert.equal(value.toString(), '42', 'getValue should return 42 after setValue');
});
});// test/my_contract.test.js (TronBox)
const MyContract = artifacts.require('MyContract');
contract('MyContract', accounts => {
it('should work', async () => {
const instance = await MyContract.deployed();
// Log account balance (sun)
const balance = await tronWeb.trx.getBalance(accounts[0]);
console.log('TRX balance (sun):', balance);
// Set value via transaction
await instance.setValue(42);
// Verify state via call
const value = await instance.getValue();
assert.equal(value.toString(), '42', 'getValue should return 42 after setValue');
});
});Step 6: Compile contracts
Compile to generate TVM bytecode and ABI artifacts. Outputs are written to ./build/contracts for use in migrations and tests.
tronbox compileExpected output:
➜ my-tron-project tronbox compile
Compiling your contracts...
===========================
Compiling ./contracts/MyContract.sol...
Compiling ./contracts/Migrations.sol...
Writing artifacts to ./build/contracts
> Compiled successfully using:
- solc: 0.8.6Step 7: Deploy contracts
Deploy your contracts to the desired network. For local testing, you can use the TronBox development environment (TRE).
First, start the local development node:
docker run -it -p 9090:9090 --rm --name tron tronbox/treThen, run the migrate command to deploy the contracts to the development network:
tronbox migrateExpected output:
➜ my-tron-project tronbox migrate
Using network 'development'.
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Running migration: 1_initial_migration.js
Deploying Migrations...
Migrations:
(base58) TJh3ZF79JTbvDn6KZuCpZSAy1MAfvxPGK4
(hex) 415faa97a14951ab963c9b21398396344c899a84d4
Saving successful migration to network...
Saving artifacts...
Running migration: 2_deploy_contracts.js
Deploying MyContract...
MyContract:
(base58) TQTKy6ri3if3QJJ6EYpBqW5rokaj1RWgcg
(hex) 419ee3391925a4d5b9c7299faf509478ad30e0ddd9
Saving successful migration to network...
Saving artifacts...To deploy to a different network like Nile, use the --network flag:
tronbox migrate --network nileStep 8: Test contracts
Run your tests against the desired network. By default, tests run against the development network.
tronbox testExpected output:
➜ my-tron-project tronbox test
Using network 'development'.
Compiling your contracts...
===========================
> Everything is up to date, there is nothing to compile.
Deploying contracts to development network...
Preparing JavaScript tests (if any)...
Contract: MyContract
TRX balance (sun): 8062066112
✔ should work
1 passing (35ms)To run tests against a different network like Nile, use the --network flag:
tronbox test --network nileConclusion
Congratulations! You have successfully migrated your project from Truffle to TronBox.
The key takeaways from this guide are:
- Project Structure: TronBox shares a similar directory structure with Truffle, making file migration straightforward.
- Configuration: The main changes are in
tronbox-config.js, where you need to adapt network endpoints, account keys, and compiler settings for the TRON network. - API Differences: When updating tests and application code, the primary task is to replace
web3calls with theirtronWebequivalents.
Your project is now ready to leverage the full power of the TRON ecosystem. Happy coding!

