Work with NPM
TronBox comes standard with npm integration, and is aware of the node_modules directory in your project if it exists. This means you can use and distribute contracts, DApps, and libraries in Solidity via npm, making your code available to others and others' code available to you.
Package layout
Projects created with TronBox have a specific layout by default which enables them to be used as packages. This layout isn't required, but if used as a common convention–or "de-facto standard"–then distributing contracts and DApps through NPM will become much easier.
The most important directories in a TronBox package are the following:
/contracts/build(which includes/build/contractscreated by TronBox)
The first directory is your contracts directory, which includes your raw Solidity contracts. Including raw contracts in your package will allow others to import those contracts within their own solidity code. The second directory is the build directory, and more specifically /build/contracts, which holds build artifacts in the form of .json files. Including your .json build artifacts in your package will allow others to seamlessly interact with your contracts from JavaScript, which can be used in DApps, scripts and migrations.
Using a package
When using a package within your own project, it is important to note that there are two places where you might be interested in using other's contract code: within your contracts and within your JavaScript code (migrations and tests). The following provides an example of each case, and discusses techniques for making the most of other's contracts and build artifacts.
Installing
This example uses OpenZeppelin Contracts to create a token. Install the package in your TronBox project:
cd my_project && npm install @openzeppelin/[email protected]Installing a package places it in the my_project/node_modules directory, which is important for the examples below. See the npm documentation for help using npm to install packages.
Use a Solidity compiler version of 0.8.20 or later for this example.
Within your contracts
To use a package's contracts within your contracts, this can be as simple as Solidity's import statement. When your import path doesn't start with ./ or ../, this signifies to TronBox that you're looking for a file from a specifically named package. For example:
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";Since the path didn't start with ./, TronBox knows to look in your project's node_modules directory for the @openzeppelin/contracts folder. From there, it resolves the path to provide you with the contract you requested.
Create a token by extending the imported ERC20 contract:
Filename: ./contracts/MyToken.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20 {
constructor() ERC20("MyToken", "MTK") {
_mint(msg.sender, 10000 * 10 ** decimals());
}
}Within your migrations
TronBox compiles the imported sources along with your contract. Load MyToken with artifacts.require and deploy it as usual:
Filename: ./migrations/3_deploy_token.js
const MyToken = artifacts.require('MyToken');
module.exports = async function (deployer) {
await deployer.deploy(MyToken);
};With TRE running, compile and deploy:
tronbox compile
tronbox migrateThe deploying account receives 10,000 tokens, with 18 decimal places.

