In this tutorial, we are going to create and deploy a simple smart contract Truffle
. We will also learn how to use it with the Truffle-CLI
tool.
What is Truffle?
Truffle is a development environment, testing framework and asset pipeline for Ethereum, aiming to make life as an Ethereum developer easier. With Truffle, you get:
- Built-in smart contract compilation, linking, deployment and binary management.
- Automated contract testing with Mocha and Chai.
- Configurable build pipeline with support for custom build processes.
- Scriptable deployment & migrations framework.
- Network management for deploying to many public & private networks.
- Interactive console for direct contract communication.
- Instant rebuilding of assets during development.
- External script runner that executes scripts within a Truffle environment.
Install Truffle
In order to install Truffle, you must have Node.JS
and NPM
installed in your machine.
Truffle requirements
- NodeJS v8.9.4 or later
- Windows, Linux or Mac OS X
Installation
|
|
Solidity - Ethereum Solidity Language for Visual Studio Code
Solidity Extension for Visual Studio Code
Create a Solidity Smart Contract Project
Let’s create our project folder, which we will call HelloTruffle
. You can do this using the same terminal/cmd, and executing these commands:
|
|
|
|
With Truffle, you can create a bare project template, or use Truffle Boxes, which are example applications and project templates. For this tutorial, we will be starting from scratch, so we execute this command:
|
|
This command creates a bare Truffle project without anything else included.
|
|
Once this command runs successfully, you can check the default project structure using ls
command.
|
|
It will create the following files and folders in your project folder.
- contracts/: Directory for Solidity contracts
- migrations/: Directory for scriptable deployment
- test/: Directory for test files for testing your application and contracts
- truffle-config.js: Truffle configuration file
Running truffle develop command
Open a console with a development blockchain. Spawns a local development blockchain, and allows you to interact with contracts via the command line.
|
|
This command will show you the url of local development blockchain with 10 default accounts and private keys.
Open a new tab in your terminal window and run a log command.
|
|
--log
: Start/Connect to a Truffle develop session and log all RPC activity.
Creating a Solidity Smart Contract
Open this project in your Visual Studio Code editor.
Let’s create a solidity smart contract named as Hello.sol
in your /contracts
folder. Here, I am assuming that you are aware of creating solidity smart contracts. This tutorial doesn’t focus on creating a smart contract. For more details about Solidity, click the below link.
|
|
Compiling Smart Contract
Once you have created your first smart contract Hello.sol
. It’s time to compile it using truffle-cli
which is now showing in your terminal window. Run below command to compile your smart contract class file.
|
|
If there is no error, it will successfully compile your smart contract and show the above message.
Migrating Smart Contract
Once you compiled your smart contract class, it’s time to create a migration file. Create a migration file 2_deploy_contracts.js
in /migrations
folder.
|
|
Once you created the contract migration file, it is time to migrate it using truffle development cli. Let’s get back to the terminal window again and type this command there.
|
|
It will start from migrating 1_initial_migration.js
file and it will show various migration details like transaction hash, contract address, and total cost, etc.
Then it will migrate our contract file 2_deploy_contract.js
and all migration details. Now, you can get your contract’s address by the following command.
|
|
Reading Smart Contract
Once our smart contract deployed to a blockchain, now it’s time to interact with it. We are going to use our terminal to interact with it.
Let’s first create an instance of our smart contract class using the below code.
|
|
Once you have created an app
instance of our smart contract class, you can get complete details of Hello
smart contract.
|
|
Now, let’s access our smart contract function getGreeting()
and get the message in the terminal.
|
|
It will display hello
in the terminal.
Writing to Smart Contract
Before going to write to our smart contract. Let me tell you briefly about web3.eth
package. Click the below link to know more about web3.eth
The web3-eth
package allows you to interact with an Ethereum blockchain and Ethereum smart contracts.
There is a function called setGreeting()
which is used to write data in smart contract block. In order to call this function through the terminal, we have to pass the sender account’s address.
To get all available accounts, use this command.
|
|
Let’s assign all these available accounts to a variable.
|
|
Now, you can check this accounts
array variable. Just type accounts
in your terminal window.
Now, write our next command to call the setGreeting()
function in the terminal.
|
|
Once this command executes successfully, it will show the transaction details.
To check whether our message is updated in our smart contract or not, call getGreeting()
function again. It will show you the latest message now.
|
|
Exiting the truffle cli
Just type .exit
to exit from truffle develop window.
|
|
Further Reading
How to build lists and navigation in SwiftUI