It is now time for
us to begin coding up the back end of our decentralized application.
This involves defining the smart contracts
which will interact with the Ganache network which we had set up previously.
And to do that, we will be making use of the Truffle framework.
Before we proceed though,
just a quick roundup of what a decentralized application is meant to do.
This is meant to allow ventures to crowdsource funds for their projects.
The venture will be able to set up an instance of a smart contract,
which will allow individuals to contribute towards their project.
And once the threshold of contributions has been breached before a specified
deadline date, then the project is considered funded and
the contributed funds will make their way to the venture.
If the project deadline has been crossed,
and the required funds have not been raised by then, then the individuals who
contributed towards it will be able to claim refunds.
So let us now proceed with the coding of the back end,
for which we will need to install the Truffle framework.
This can be downloaded using npm, so if you don't have npm already,
you will need to set it up.
But once you have it,
to install Truffle you can run the command sudo npm install truffle.
And to make Truffle available globally you can specify the -g flag.
Once this is done, you can observe the version of Truffle which
has been installed, in my case this is version 5.0.19.
And just in case you run into compatibility issues,
I will just show you the versions of npm and node which I have installed.
So in my case, the npm version is 5.6.0.
As for the version of node,
this is v10.15.3.
So just note the combination of the Truffle, npm, and
node versions in case you run into issues with compatibility.
So now that the Truffle tool has been installed,
it is now time for us to initialize our Truffle project.
To do that, I'm creating a new project directory within my home directory.
So this is called crowdFunding
and once the directory is created I just cd into it.
And in order to initialize the Truffle project,
we can run the command truffle init.
So this will create a standard directory structure for a Truffle project,
which will include separate directories for the contracts,
the migration scripts, as well as the test scripts,
and also a Truffle config JavaScript file.
So once we run this, our blank Truffle project will have been created.
And we are also notified of the different Truffle commands which we can run
in order to Compile, Migrate, and Test out our contracts.
So we can just confirm that all of the files and
directories are being created by running an ls.
And yes, the three directories and
the config file which I had mentioned have been provisioned right here.
Now in order to begin the development process, I will switch out of the Shell
and then bring up this directory in my text editor.
So I'm making use of the Sublime Text editor, but you can use
any other text editor or IDE which you're comfortable with for this purpose.
We can quickly explore the contents of the directories here,
starting with the contracts directory.
And this includes a Migrations.sol solidity source file,
which will handle the deployments for us.
I won't be getting into the details of this.
But just to summarize, this contract will be able to keep track of the last
successful deployment script which was executed from the Truffle project.
And when we perform a Truffle migrate,
only the subsequent deployment scripts will be run.
Moving ahead to the migrations directory which includes the deployment scripts.
This is where the deployment scripts are specified in a sequence.
And number one in our list and the only one at the moment is the one to
deploy the migrations contract to the blockchain network.
Moving over now to the test directory, this is by default mp,
and we will be setting up a test script later on.
And then finally, when we look at the truffle-config.js file,
this includes a standard structure for the JS file, but
there are no details populated here yet.
This can be the first file which we modify.
So I'm now going to clear out the contents here and
paste in the new configuration for our Truffle project, which includes
the networks to which the contracts in this project will be deployed to.
So while we can specify a list of networks,
there is just one which we wish to connect to, and that we will name Ganache.
We will then populate the details of our Ganache network which
includes the host, which happens to be a localhost,
the port which is 7545, the default gas limit.
And then we also specify a wildcard for the network ID
so that our Truffle project will connect to any network
which is available on port 7545 of the localhost.
And with that done, we can save this file,
but we will also modify its name to just be truffle.js.
Once the renaming is complete,
we will now head over to the contracts directory,
where we will begin coding up the contracts which we require for the project.
The first of these will represent some Utility functions which we will use.
So within this new blank file, I will go ahead and
paste the contents of this Utils contract.
So once I have set it a name, which is Utils.sol,
we can go through the different blocks in this source file.
Starting with the pragma directors so that the Solidity Compiler version is
restricted to some variation of 0.5.
And then following that,
we define this to be a library rather than a smart contract.
So this Utils library includes two different functions.
The first of these is called etherToWei, and just as the name implies,
this will accept as input an ether value and return its Wei equivalent.
While we could use one of the Web3 Util functions for this,
this is just a little easier for us.
So once we define this etherToWei function, we will make use of
another function in this library and this is to convert minutes to seconds.
This is because the crowdsourcing venture which we will set up will have
an expiration time and that time we will like to specify in minutes,
but we will need to express it as seconds in the smart contract.
So with both the functions in our library,
we will be essentially converting from a unit which is easier for
us to express to the unit which is required for the contract.