Truffle has a sort of pre-packaged template directory for projects called Truffle boxes. According to their websites,
Truffle Boxes are helpful boilerplates that allow you to focus on what makes your dapp unique. In addition to Truffle, Truffle Boxes can contain other helpful modules, Solidity contracts & libraries, front-end views and more; all the way up to complete example dapps.
To use a Truffle box:
truffle unbox <NAME>
Or to start your own:
truffle init
Directory of a Truffle Project
contracts/: Solidity source files for smart contracts. Here there is also an important contract called Migrations.sol
migrations/: Truffle uses a migration system to handle smart contract deployment; A migration is a special smart contract that keeps track of changes.
test/ Tests written both in JavaScript and Solidity.
truffle/ The Truffle configuration files.
Truffle Develop
Truffle Develop: Truffle's built in developer console with a few useful functionalities
a) generates a development blockchain that can be used to test deploy contracts
b) has the ability to run Truffle commands directly from the console
Ethereum Smart Contracts must be compiled. In the Truffle Develop console, run:
compile
Migrations
A migration is a deployment script meant to alter the state of your application's contracts, moving it from one state to the next. For the first migration, you might just be deploying new code, but over time, other migrations might move data around or replace a contract with a new one.
To migrate the contract, go to the Truffle Develop console and run:
migrate
Testing
1) Import the necessary files:
a) Assert.sol -- provides testing assertions
b) DeployedAddresses.sol -- gets the address of the deployed contract we want to test
c) The contract we want to test
To run the tests, go to the Truffle Develop console and run:
test
A Front-End UI
Instantiate Web3:
if (typeof web3 !== 'undefined') {
App.web3Provider = web3.currentProvider;
} else {
// If no injected web3 instance is detected, fallback to the TestRPC
App.web3Provider = new Web3.providers.HttpProvider('http://localhost:8545');
}
web3 = new Web3(App.web3Provider);
Instantiate the contract:
$.getJSON('Adoption.json', function(data) {
// Get the necessary contract artifact file and instantiate it with truffle-contract
var AdoptionArtifact = data;
App.contracts.Adoption = TruffleContract(AdoptionArtifact);
// Set the provider for our contract
App.contracts.Adoption.setProvider(App.web3Provider);
// Use our contract to retrieve and mark the adopted pets
return App.markAdopted();
});
1) Get the ABI
2) Pass the ABI toTruffleContract()
3) Set its web3 provider
4) Run functions on the contract
Truffle → Framework for Dapp Development
Install the NPM package.
npm install - truffle
Truffle has a sort of pre-packaged template directory for projects called Truffle boxes. According to their websites,
To use a Truffle box:
truffle unbox <NAME>
Or to start your own:
truffle init
Directory of a Truffle Project
contracts/: Solidity source files for smart contracts. Here there is also an important contract called Migrations.sol
migrations/: Truffle uses a migration system to handle smart contract deployment; A migration is a special smart contract that keeps track of changes.
test/ Tests written both in JavaScript and Solidity.
truffle/ The Truffle configuration files.
Truffle Develop
Truffle Develop: Truffle's built in developer console with a few useful functionalities a) generates a development blockchain that can be used to test deploy contracts b) has the ability to run Truffle commands directly from the console
Ethereum Smart Contracts must be compiled. In the Truffle Develop console, run:
compile
Migrations
To migrate the contract, go to the Truffle Develop console and run:
migrate
Testing
1) Import the necessary files: a) Assert.sol -- provides testing assertions b) DeployedAddresses.sol -- gets the address of the deployed contract we want to test c) The contract we want to test
To run the tests, go to the Truffle Develop console and run:
test
A Front-End UI
Instantiate Web3:
Instantiate the contract:
1) Get the ABI 2) Pass the ABI toTruffleContract() 3) Set its web3 provider 4) Run functions on the contract
Using the Dapp in a Browser
1) Setup MetaMask 2) Connect to custom RPC: http://localhost:9545