5. Using Truffle
Use Truffle to quickly build a dApp on ChainIDE
Consensys Announces the Sunset of Truffle and Ganache and New HardHat Partnership, more details.
1. Open Sandbox
Note: Sandbox functionality is only available after logging in to ChainIDE via GitHub.
Open Sandbox
2. Create a project
You need to run most Truffle commands against an existing Truffle project. So the first step is to create a Truffle project.
You can create a bare project without smart contracts using truffle init
, but for those getting started, you can use Truffle Boxes, which are example applications and project templates. We'll use the MetaCoin box, which creates a token that can be transferred between accounts. Note that this is not ERC-20 compatible.
Download ("unbox") the MetaCoin box:
Once this operation is completed, you'll now have a project structure with the following items:
contracts/
: Directory for Solidity contractsmigrations/
: Directory for scriptable deployment filestest/
: Directory for test files for testing your application and contractstruffle.js
: Truffle configuration file
3. Explore the project
Open the
contracts/MetaCoin.sol
file in a text editor. This is a smart contract (written in Solidity) that creates a MetaCoin token. Note that this also references another Solidity filecontracts/ConvertLib.sol
in the same directory.Open the
migrations/1_deploy_contracts.js
file. This file is the migration (deployment) script.Open the
test/TestMetaCoin.sol
file. This is a test file written in Solidity which ensures that your contract is working as expected.Open the
test/metacoin.js
file. This is a test file written in JavaScript which performs a similar function to the Solidity test above. The box does not include one, but Truffle tests can also be written in typescript.Open the
truffle-config.js
file. This is the Truffle configuration file, for setting network information and other project-related settings. The file is blank, but this is okay, as we'll be using a Truffle command that has some defaults built-in.
4. Test
To run all tests, you can simply run truffle test
. Because development
is commented out in truffle-config.js
, truffle test
will spin up and tear down a local test instance (ganache
). If you want to use more of ganache's features, you can spin up a separate instance and specify the port number in the truffle-config
.
You can also run each test individually by calling truffle test ./test/TestMetaCoin.sol
and truffle test ./test/metacoin.js
.
5. Compile
If you want to only compile, you can simply run truffle compile
.
You will see the following output:
6. Migrate with Truffle Develop
To deploy our smart contracts, we're going to need to connect to a blockchain. Truffle has a built-in personal blockchain that can be used for testing. This blockchain is local to your system and does not interact with the main Ethereum network.
Run Truffle Develop:
You will see the following information:
This shows ten accounts (and their private keys) that can be used when interacting with the blockchain.
On the Truffle Develop prompt, Truffle commands can be run by omitting the
truffle
prefix. For example, to runtruffle compile
on the prompt, typecompile
. The command to deploy your compiled contracts to the blockchain istruffle migrate
. By default,truffle migrate
will also runtruffle compile
, so you can just do the following:
You will see the following output:
This shows the transaction IDs and addresses of your deployed contracts. It also includes a cost summary and real-time status updates.
Note: Your transaction hashes, contract addresses, and accounts will be different from the above.
Note: To see how to interact with the contract, please skip to the next section.
7. Migrate with Truffle Console
While Truffle Develop is an all-in-one personal blockchain and console, it spins up a very basic instance of ganache. You can also use a desktop application, to launch your personal blockchain, which is an easier to understand tool for those new to Ethereum and the blockchain, as it displays much more information up-front. Alternatively, if you want to customize your ganache instance using all the options available to you through ganache v7.
The only extra step, aside from running Ganache, is that it requires editing the Truffle configuration file to point to the Ganache instance.
Open
truffle-config.js
in a text editor. Replace the content with the following, ensuring your port number is correct:
This will allow a connection using Ganache's default connection parameters.
Save and close that file.
On the terminal, migrate the contract to the blockchain created by Ganache:
You will see the following output:
This shows the transaction IDs and addresses of your deployed contracts. It also includes a cost summary and real-time status updates.
Note: Your transaction IDs and contract addresses may be different from the above.
To interact with the contract, you can use the Truffle console. The Truffle console is similar to Truffle Develop, except it connects to an existing blockchain (in this case, the one generated by Ganache).
You will see the following prompt:
8. Interact with the contract
Interact with the contract using the console in the following ways:
We're using web3.eth.getAccounts()
in these examples, which returns a promise which resolves to an array of all the accounts generated by the mnemonic. So, given the addresses generated by our mnemonic above, specifying (await web3.eth.getAccounts())[0]
is equivalent to the address 0x627306090abab3a6e1400e9345bc60c78a8bef57
.
As of Truffle v5, the console supports async/await functions, enabling much simpler interactions with the contract.
Begin by establishing both the deployed MetaCoin contract instance and the accounts created by either Truffle's built-in blockchain or Ganache:
Check the metacoin balance of the account that deployed the contract:
See how much ether that balance is worth (and note that the contract defines a metacoin to be worth 2 ether):
Transfer some metacoin from one account to another:
Check the balance of the account that received the metacoin:
Check the balance of the account that sent the metacoin:
9. Deploy to Mainnet, Testnet, and Beyond
If you want to deploy to alternative networks, consider using Truffle Dashboard. Just call truffle dashboard
and deploy, test, and run the console using --network dashboard
.
10. Continue learning
This quickstart showed you the basics of the Truffle project lifecycle, but there is much more to learn. Please continue on with the rest of truffle documentation, BNB Chain: Using Truffle and check out truffle unleashed series for the latest tutorials and interviews with industry experts!
Last updated