Application Development Tutorial)
Learn how to create your first decentralized application on the Ethereum network with
Gregory from DAP University. We will create an election where network accounts can
vote and track the vote count in real-time on a local Ethereum blockchain.
A blockchain is both a database and a network, where all nodes communicate with each
other. Unlike traditional web models, a blockchain is a network and database in one,
helping to ensure that all data recorded on the public ledger remains secure and
unchanged.
In a voting application, this is especially important as it ensures that the rules of the
election cannot be changed. Our decentralized application, Adapt, is truly decentralized,
as the network is peer-to-peer, and data is decentralized. We will write our smart
contract code for the decentralized application to allow accounts on the network to use
the app and vote in the election.
The Truffle framework provides packages of boilerplate code called boxes that can be
easily added to our project for faster setup. To interact with our smart contract, we can
connect to our local Ethereum network using personal accounts and MetaMask.
Additionally, we must install Google Chrome to utilize the Ethereum blockchain. Our
first step is to create a smoke test to verify proper setup, ensuring that our contract is set
up correctly, can be deployed, and responds as expected. To achieve this, we need a
constructor to be executed during deployment.
Truffle will use an initial migration file as a reference point to create our migration file,
which deploys our smart contract to the blockchain.
Truffle allows for easy interaction with our contract. This can be done through the
console during testing or through a frontend application. In the example below, we use
the directive to deploy our contract.
The migrations contract is referenced in three places. It is read from the project
directory, assigned to a variable called "migrations", and then referenced later.
In this example, Truffle is using Solidity version 0.4.11. Let's change this to 0.4.2,
which is the version that comes with this version of Truffle.
It's important to note that while Truffle can read from the blockchain, writing to the
blockchain incurs a cost in gas. We will discuss the concept of gas more in-depth later.
To model a candidate in our smart contract, we need a way to create a new type. In
Solidity, we can use the "struct" keyword followed by the name of our new type, which
in this case is "candidate".
Once we have defined our new type, we need a place to store instances of it. This can be
done by instantiating the structure type.