Truffle QuickStart¶
This page will take you through the basics of creating a Truffle project and deploying a smart contract to a blockchain.
Note
Before you begin, make sure that you read our Ethereum Overview page.
Table of Contents¶
- Creating a project
- [Exploring the project]
- [Testing]
- [Compiling]
- [Migrating with Truffle Develop]
- [Alternative: Migrating with Ganache]
- [Interacting with the contract]
Creating a project¶
To use most Truffle commands, you need to run them against an existing Truffle project. So the first step is to create a Truffle project.
You can create a bare project template, but for those just 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:
Create a new directory for your Truffle project:
mkdir MetaCoin cd MetaCoin
Download (“unbox”) the MetaCoin box:
truffle unbox metacoin
Note
You can use the truffle unbox <box-name> command to download any of the other Truffle Boxes.
Note
To create a bare Truffle project with no smart contracts included, use truffle init.
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 files目录test/
: Directory for test files for testing your application and contractstruffle.js
: Truffle configuration file
Exploring the project¶
Note
This page is just a quickstart, so we’re not going to go into much detail here. Please see the rest of the Truffle documentation to learn more.
- 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
contracts/Migrations.sol
file. This is a separate Solidity file that manages and updates the status of your deployed smart contract. This file comes with every Truffle project, and is usually not edited. - Open the
migrations/1_initial_migration.js
file. This file is the migration (deployment) script for theMigrations
contract found in theMigrations.sol
file. - Open the
migrations/2_deploy_contracts.js
file. This file is the migration script for theMetaCoin
contract. (Migration scripts are run in order, so the file beginning with2
will be run after the file beginning with1
.) - 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. - 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.
Testing¶
On a terminal, run the Solidity test:
truffle test ./test/TestMetacoin.sol
You will see the following output
TestMetacoin √ testInitialBalanceUsingDeployedContract (71ms) √ testInitialBalanceWithNewMetaCoin (59ms) 2 passing (794ms)
Note
If you’re on Windows and encountering problems running this command, please see the documentation on resolving naming conflicts on Windows. 。
These tree tests were run against the contract, with descriptions displayed on what the tests are supposed to do.
Run the JavaScript test:
truffle test ./test/metacoin.js
You will see the following output
Contract: MetaCoin √ should put 10000 MetaCoin in the first account √ should call a function that depends on a linked library (40ms) √ should send coin correctly (129ms) 3 passing (255ms)
Compiling¶
Compile the smart contracts:
truffle compile
You will see the following output:
Compiling .\contracts\ConvertLib.sol... Compiling .\contracts\MetaCoin.sol... Compiling .\contracts\Migrations.sol... Writing artifacts to .\build\contracts
Migrating¶
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.
You can create this blockchain and interact with it using scripts/node/start.sh shell scripts.
Run Truffle migrate:
truffle migrate
我们可以看到下面的信息:
Starting migrations...
======================
> Network name: 'develop'
> Network id: 4447
> Block gas limit: 6721975
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0x3fd222279dad48583a3320decd0a2d12e82e728ba9a0f19bdaaff98c72a030a2
> Blocks: 0 Seconds: 0
> contract address: 0xa0AdaB6E829C818d50c75F17CFCc2e15bfd55a63
> account: 0x627306090abab3a6e1400e9345bc60c78a8bef57
> balance: 99.99445076
> gas used: 277462
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00554924 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00554924 ETH
2_deploy_contracts.js
=====================
Deploying 'ConvertLib'
----------------------
> transaction hash: 0x97e8168f1c05fc40dd8ffc529b9a2bf45cc7c55b07b6b9a5a22173235ee247b6
> Blocks: 0 Seconds: 0
> contract address: 0xfb39FeaeF3ac3fd46e2123768e559BCe6bD638d6
> account: 0x627306090abab3a6e1400e9345bc60c78a8bef57
> balance: 99.9914458
> gas used: 108240
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.0021648 ETH
Linking
-------
* Contract: MetaCoin <--> Library: ConvertLib (at address: 0xfb39FeaeF3ac3fd46e2123768e559BCe6bD638d6)
Deploying 'MetaCoin'
--------------------
> transaction hash: 0xee4994097c10e7314cc83adf899d67f51f22e08b920e95b6d3f75c5eb498bde4
> Blocks: 0 Seconds: 0
> contract address: 0x6891Ac4E2EF3dA9bc88C96fEDbC9eA4d6D88F768
> account: 0x627306090abab3a6e1400e9345bc60c78a8bef57
> balance: 99.98449716
> gas used: 347432
> gas price: 20 gwei
> value sent: 0 ETH
> total cost: 0.00694864 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.00911344 ETH
Summary
=======
> Total deployments: 3
> Final cost: 0.01466268 ETH
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.
Interacting with the contract¶
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.
truffle console
You will see the following prompt:
truffle(development)>
Note
控制台提示符:truffle(development)> 括号里,指当前连接的网络。
Interact with the contract using the console in the following ways:
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:
truffle(development)> let instance = await MetaCoin.deployed() truffle(development)> let accounts = await web3.eth.getAccounts()
Check the metacoin balance of the account that deployed the contract:
truffle(development)> let balance = await instance.getBalance(accounts[0]) truffle(development)> balance.toNumber()
See how much ether that balance is worth (and note that the contract defines a metacoin to be worth 2 ether):
truffle(development)> let ether = await instance.getBalanceInEth(accounts[0]) truffle(development)> ether.toNumber()
Transfer some metacoin from one account to another:
truffle(development)> instance.sendCoin(accounts[1], 500)
Check the balance of the account that received the metacoin:
truffle(development)> let received = await instance.getBalance(accounts[1]) truffle(development)> received.toNumber()
Check the balance of the account that sent the metacoin:
truffle(development)> let newBalance = await instance.getBalance(accounts[0]) truffle(development)> newBalance.toNumber()
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 our documentation and especially our tutorials to learn more.