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.
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
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″
$ npm install
After installing all dependencies ,below will be project structure.
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:
$ truffle compile
To talk to the smart contract on the personal blockchain network inside the Truffle console, we must do a few things:
Run the newly created migration script, deploying the smart contract to the personal blockchain network.
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.
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:
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
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
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.
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:
Please share and subscribe if you liked this and stay tuned with us for second and third part of this series.
Please refer code github . Click here to read my articles on Blockchain.