Example Tutorial Project
The MetaCoin TronBox example tutorial project can be found here.
Smart Contract Development
The ./contracts
directory contains all your contracts. By default, there is a contract file and a library file within the .sol extension. Even though the library file has some unique properties, for simplicity purposes, it will be referred to as a contract.
Tronbox requires the contract name and file name to be defined the same. For example, if the file name is Test.sol
, we can write the contract as follows:
pragma solidity ^0.4.23;
contract Test{
function f() returns (string){
return "method f()";
}
function g() returns (string){
return "method g()";
}
}
This type of contract is case-sensitive. The recommendation is to capitalize each opening letter, as shown in the above code.
Increase Deploy Configuration
Configuring migrations/2_deploy_contracts.js as follows:
var Migrations = artifacts.require("./Migrations.sol");
var Test = artifacts.require("./Test.sol");
module.exports = function(deployer) {
deployer.deploy(Migrations);
deployer.deploy(Test);
};
The above code has two extra lines. One line is var Test = artifacts.require("./Test.sol")
, which declares a new contract instance as Test
; This increases a new line deployer.deploy(Test)
; Use Test
for deployment.
Configuration
The tronbox.js file holds configurations the contract will deploy to. To specify the network, use — network NETWORK_NAME when migrating or testing.
module.exports = {
networks: {
development: {from: 'some address',
privateKey: 'some private key',
consume_user_resource_percent: 30,
fee_limit: 100000000,
fullNode: "https://api.trongrid.io",
solidityNode: "https://api.trongrid.io",
eventServer: "it is optional",
network_id: "*" // Match any network id
},
production: {
from: 'some other address',
privateKey: 'some other private key',
consume_user_resource_percent: 30,
fee_limit: 100000000,
fullNode: "https://api.trongrid.io",
solidityNode: "https://api.trongrid.io",
eventServer: "it is optional",
network_id: "*" // Match any network id
},
..... you can define other network configuration as well
}
};
Compiler Configuration
In tronbox.js
, you can add the option useZeroFourCompiler: true
to force TronBox to compile the contract as if the connected full node hosts a version of JavaTron lower than 3.6.
Contract Compiling
To compile the contract, use:
tronbox compile
By default, tronbox compiler only compiles modified contracts since last compile, to reduce unnecessary compiling. If you wish to compile the entire file, you can use --compile-all
.
tronbox compile --compile-all
The compile output is in ./build/contracts
directory. If the directory doesn't exist, it will be auto-generated.
Contract Deployment
Script migration is with a JavaScript file composed to broadcast onto Tron network. The script migration caches your broadcast responsibility. Its existence is based on your broadcast needs and will adjust accordingly. When your work incurs significant change, the migration script you created will propagate the changes through the blockchain. Previous migration history records will undergo a unique Migrations
contract to be recorded on the blockchain. Below is a detailed instruction.
To initiate the migration, use the following command:
tronbox migrate
This command will initiate all migration scripts within the migration
directory. If your previous migration was successful, tronbox migrate
will initiate a new migration and use the development
network by default.
If there is no new migration script, this command will have no operation. Instead, you can use — reset to re-deploy. You can also use — network NETWORK_NAME to specify a network. Eg. tronbox migrate --reset --network production
PS C:\**\bare-box> tronbox migrate --reset --network production
Using network 'production'.
Running migration: 1_initial_migration.js
Deploying Migrations...
Migrations: 41271233ac2eea178ec52f1aea64627630403c67ce
Deploying Test...
Test: 41477f693ae6f691daf7d399ee61c32832c0314871
Saving successful migration to network...
Saving artifacts...
Trigger Contract
The testing scripts are in the ./tests
directory. TronBox will ignore all other extensions except for .js, .es, .es6, and .jsx
Below is an example testing script for test.js:
var Test = artifacts.require("./Test.sol");
contract('Test', function(accounts) {
it("call method g", function() {
Test.deployed().then(function(instance) {
return instance.call('g');
}).then(function(result) {
assert.equal("method g()", result[0], "is not call method g");
});
});
it("call method f", function() {
Test.deployed().then(function(instance) {
return instance.call('f');
}).then(function(result) {
assert.equal("method f()", result[0], "is not call method f");
});
});
});
Running Testing Script
PS C:\**\bare-box> tronbox test ./test/test.js
Using network 'production'.
Contract: Test
√ call method g
√ call method f
2 passing (23ms)