In this blog, we will create to-do list app using ethereum blockchain smart contracts. It will be 3 part series, we will cover below details.
Part-1 Create a smart contract with the Solidity programming language.
Part-2 Write tests against the smart contract, and deploy it to a blockchain.
Part-3 Create a client side application to for the to-do list.
Install Prerequisite :
- Ganache (https://www.trufflesuite.com/ganache)
- Nodejs (https://nodejs.org/en/)
- Truffle (https://www.trufflesuite.com/)- npm install -g truffle or sudo npm install -g truffle
- Metamask plugin and configure to browser(https://chrome.google.com/webstore/detail/metamask/nkbihfbeogaeaoehlefnkodbefgpgknn?hl=en)
Step-1 Create the project.
I’ll first create a project directory and go into directory.
$ mkdir todo-list
$ cd todo-list
Now we initialise a new truffle project to develop our project like this:
$ truffle init
Step-2 Package.json file
Create a package.json file to install some development dependencies that will need for the project. You can do that from the command line like this:
$ touch package.json
Install all dependencies for your project by simply copy-and-pasting the code below into your package.json file:
“description”: “Todo List”,
“test”: “echo \”Error: no test specified\” && exit 1″
Step-3 Install the dependencies
$ npm install
After installing all dependencies ,below will be project structure.
- contracts directory: this is where all smart contacts live. We already have a Migration contract that handles our migrations to the blockchain.
- migrations directory: this is where all of the migration files live. These migrations are similar to other web development frameworks that require migrations to change the state of a database. Whenever we deploy smart contracts to the blockchain, we are updating the blockchain’s state, and therefore need a migration.
- node_modules directory: this is the home of all of our Node dependencies we just installed.
- test directory: this is where we’ll write our tests for our smart contract.
- truffle-config.js file: this is the main configuration file for our Truffle project, where we’ll handle things like network configuration.
Step-4 Develop the smart contract
This will manage our to-do list. We can do this by creating a new file in the contracts directory like this:
$ touch ./contracts/TodoList.sol
We’ll write code to just keep track of the number of tasks inside the to-do list. Here task Count is a special kind of variable called a “state variable”. Any data that we store inside this state variable is written to storage on the blockchain. It changes the smart contract’s state, and has scope within the entire smart contract, as opposed to local variables which only have scope inside of functions. We can set a default value of 0 for this state variable like this:
Step-5 Compile the smart contract
$ truffle compile
To talk to the smart contract on the personal blockchain network inside the Truffle console, we must do a few things:
- Update our project’s configuration file to specify the personal blockchain network we want to connect to (Ganache).
- Create a migration script that tells Truffle how to deploy the smart contract to the personal blockchain network.
Run the newly created migration script, deploying the smart contract to the personal blockchain network.
Step-6 Project configuration file
First, we’ll update the project configuration file to specify the personal blockchain network we want set up in the first section. Find the file truffle-config.js and update the following code:
Note: these should match the default settings provided by the Ganache personal blockchain network. If you changed any settings inside the Ganache settings page, like the port, those should be reflected here.
Step-7 Create a migration script
Next, we’ll create a migration script inside the migrations directory to deploy the smart contract to the personal blockchain network. From your project root, create a new file from the command line like this:
$ touch migrations/2_deploy_contracts.js
Any time we create a new smart contract, we are updating the state of the blockchain. As earlier i mentioned ,blockchain fundamentally is a database. Hence, whenever we permanently change it, we must migrate it from one state to another. This is very similar to a database migration that you might have performed in other web application development frameworks.
Notice that we number all of our files inside the migrations directory with numbers so that Truffle knows which order to execute them in. Inside this newly created migration file, you can use this code to deploy the smart contract:
Step-8 Assign Variable
First, we require the contract we’ve created, and assign it to a variable called “TodoList”. Next, we add it to the manifest of deployed contracts to ensure that it gets deployed when we run the migrations. Now let’s run this migration script from the command line like this:
$ truffle migrate
Step-9 Console to interact with the smart contract
Now that we have successfully migrated the smart contract to the personal blockchain network, let’s open the console to interact with the smart contract. You can open the truffle console from the command line like this:
$ truffle console
Step-10 Read the task Count from the contract
Now that we’re inside the console, let’s get an instance of our deployed smart contract and see if we can read the task Count from the contract. From the console, run this code:
todoList = await TodoList.deployed()
Here To-do List is the name of the variable that we created in the migration file. We retrieved a deployed instance of the contract with the deployed() function, and assigned it to an to-do List. Also, note the use of the await keyword. We must interact with the blockchain in an asynchronous fashion.
Step-11 Value of task Count
First, we can get the address of the smart contract that was deployed to the blockchain like this and then we can read the value of task Count from the storage :
We have done all of the following:
- Set up your machine for blockchain development
- Created a new truffle project
- Created your first smart contract
- Interacted with your newly created smart contract on a live blockchain.