Create, compile, deploy and test a smart contracts on Ethereum + Angular/react
In this Tutorial, we learn how to create a simple decentlized application by writing a simple smart contract and the proper UI for it, then we use web3 to allow our application to talk to the blockchain, at the end we take a look at two most used javaScript Frontend frameworks "Angular and react" and see which could be the best possible option while creating a DApp.
before we start we need to know what is a blockchain and why and most importantly when should it be used, therefore we look at the pro and cons of using the blockchain to realize blockchain is not always the solution and we need to be sure before approaching it.
before we start with the development, we need to undrestand what blockchain is and how it works.
by interacting with a usual web application, user connects to a central server over a network by using a web browser.
the entire data and the code of this web application is on the central server that has a central database, therefore each transaction between user and the application, must communicate with this central server on the web.
Hoewver in certain situation there could be problems such as the data on the database could be changed, most importantly if an event has to happens( typically in voting system or money transaction) and by having a
central
database, every information could be hacked or changed or removed and therefore cause various problems in higher scales.
not only data are not immune, the source code and logic of the program could also be changed and cause major problems in server and somethimes even cause in economical issue or leak the personal information. these are our
motivation to
build the application on blockchain, where there is no middle man and we don't have to simply trust a thid partie.
By builing our application on blockchain we let everybody connected to the network to use the application without the data being japordized or the avoiding any changes in the source code. Instead of having a network, a central
server,
and a database, the blockchain is a network and a database all in one. A blockchain is a peer-to-peer network of computers, called nodes, that share all the data and the code in the network.
So, any device that is connected to the blockchain, in in fact a node in the network, and can talk to all the other computer nodes in the network. each node has a copy of all the data and the code on the blockchain. There are
no more
central servers. Just a bunch of computers that talk to one another on the same network. Instead of a centralized database, all the transaction data that is shared across the nodes in the blockchain is contained in bundles of
records called
blocks, which are chained together to create the public ledger. This public ledger represents all the data in the blockchain. All the data in the public ledger is secured by cryptographic hashing, and validated by a consensus
algorithm.
Nodes on the network participate to ensure that all copies of the data distributed across the network are the same.
That’s why many applications are build on the blockchain, by doing so they can ensure that each action done by user was counted, and that it did not change(specially in voting applications).
each user needs an account with a wallet address that is charged with some Ethers, Ethereum's cryptocurrency.
Once they connect to the network, they pay a small transaction fee to write this transaction to the blockchain,
each transaction could be an action or request from the user like voting or registering or sending information...
This transaction fee is called “gas”. Whenever an action is done, some of the nodes on the network, called miners,
compete to complete this transaction. The miner who completes this transaction is awarded the Ether that the user has paid for the transaction.
also reading data from the blockchain is free but writing to it is not and the user pays with the ether for it.
As we mentioned above, before we save an information on the blockchain, we need to be sure about it, because once an information is saved, all other nodes are getting the information, thus, deleting the information is nearly impossible, also writing on the blockchain costs time and money therefore, only the most important information should be written. we can use a databank on the backed-side to save the the rest of the information.
Bitcoin blockchain is the base of all blockchains. It main goal was peer to peer transfer of value, later on a framework for code execution was introduced by Ethereum Founders. The difference between bitcoin and the Ethereum is the wallet for initiating transactions. Ethereum supports smart contracts. Smart contracts are used in decentralized application that aim for more than just the transfer of value. A smart contract is a piece of code deployed in the blockchain node, it is where the logic of the blockchain is written and will be executed from. Execution of a smart contract is initiated by a message embedded in the transaction. Bitcoin and other crypto currencies are requesting simple addition and subtraction. However, Ethereum enables transaction that can achieve more complicated operations. We can have conditions or authentication and evaluation of specific accounts. Structurally speaking, a smart contract acts same as a class definition in an object-oriented design. A smart contract like any other data structure has data, functions or methods, which could be private or public, we can set variable sate or change it and then ask for the new changed state of a variable. Solidity is the programming language provided by Ethereum, it’s the language of smart contract.
In this section we will be covering how to make a smart contract?
Solidity like any other language has it’s own syntax and there is a documentation page , which will be update once a new version of library is released. we always start with the "pragma + version" , since it indicates the version of the solidity language. We usually try to keep the contract short, so we don’t have to re-deploy our contract every time something changes, because re-deploying means changing the address and, in some cases, address of smart contract needs to be stable. Other than that, everything that we write in smart contract will be staying on the blockchain, so we try to keep the private/not important information out of smart contract, basically the smart conract contains the logic behind our Application.
Easiest and the fastest way to test, if our contract is functioning and the syntax of our code is correct, is by testing it in remix , remix is “a web browser-based IDE that allows you to write Solidity smart contracts, then deploy and run the smart contract .” [Remix - The Ethereum Wiki] usually when a contract runs on the remix, it means that we can deploy it and know that everything is alright. Remix has 3 type of environments: Javascript VM, Injected provider, or Web3 provider. Both Web3 provider (Ethereum node) and Injected provider (Mist or Metamask) require the use of an external tool. The JavaScript VM mode is convenient because each execution runs in your browser. Thus reloading the page will restart Remix with an empty state.
In order to make sure that our smart contract does exactly as it is sopposed to and checking if the right event is emited, we write a test-based application to test our smart contract and we use truffle init project for it.
Before: install Npm / truffle/testRPC or ganache
With truffle init, we get an empty deployable project from truffle and then we have to add our own smart contract in truffle contract folder.
Then we need a migration file for the new contract and in config file we have to establish our information.
Interact with the smart contract: How to create a static web page and run it on local host: in this section we see how to make transactions on the Ethereum blockchain without using any wallet or extension like Mist or Metamask, this could be very helpful, because then our Dapp can be accesed from anybody and doesen't require metamask.
when we write the smart contract and deploy it on the remix we c an copy the ABI from the remix and save it in a file called contract.jason, we also copy the address of the deployed contact in our jason file, incase we need to use it later, since we deployed on the ropsten testnet, we can find all transaction done by this contract on https://ropsten.etherscan.io/address/ + smartContractAddress. then we need another file called networkinfo.json ,which we'll be using later, what we save in this file is our infura account information public key adress and the private key address.
in the frontend, we keep it very simple :
>node.js >python (v2.7.15)
>npm install
first call >npm start in consule or cmd .
This part may be complicated because the lack of Documentation on web3.js. first, we create a server.js file and Connect to Infura’s peers and add Required node-modules such as:
then we add the path of those 2 jason file that contains our ABI and our networkInfo.
then we save our needed information from theses two files in seprate variable such as const abi or const infura-url, by simply calling :"const abi= contractinfo.abi".
in the next step we create new instance of web3 and set the provider:
what we need now is the get function to make the transaction once the button is clicked, in this get function we call the function sendTx(callback) {} in order to make a transaction and register a car in our case.
now that everything is set, we can write our send transaction function, which create a transaction once the button is clicked
In this section, we see how to deploy the DApp on the test-net: however there are many possibilities such as deploying directly from truffle or from remix or by using web3.js
before we start we need to install meta-mask
MetaMask is a browser extension that can be installed from https://metamask.io/.
It lets you run your dapp without being part of the Ethereum network as a Ethereum Node.
Instead of connecting directly to the Ethereum network, you can connect to another Ethereum Node which is called INFURA and therefore run smart contracts on Infura Node.
it also manages the Ethereum wallet, which contains the Ethers that user is willing to use, by using the metamask, user can send and receive Ethers through a dApp.
there are other tools similar to meta-mask, but because it’s simplicity meta mask is widely used.
in this step we choose our Testnet which is Ropsten, we can ask for test ether: https://faucet.ropsten.be/, https://faucet.metamask.io/
by going back to the Remix IDE,where we wrote our code, once the metamask is connected we can deploy the smart contract by making the transaction through the ropsten testnet.
in this section we will be comparing two most used JavaScript Frontend frameworks
Using a JavaScript Frontend framework makes coding easier and faster, instead of writing code from the scratch we can use currently existing libraries, we can also avoid writing the same part over and over. It also helps us to achieve simpler and shorter code, which is helpful while reviewing and debugging, therefore it is more efficient.
A comparison between angular and React:
Angular is one of the most used javascript Frontend frameworks and it is maintained by Google which is one of the reason that it became very popular.
There is no specific reason why to use and not to use Angular as each developer has it's own reason, however if you decide to learn the Angular framework, it's better to plan the time for learning and be patient with it's new Structure.
React is not a framework but a JavaScript library created by Facebook, it is a User Interface (UI) library and a tool for building UI components.One of the reason that React became popular is that the clinet and server side can be rendered together, therefore they can work together.
same as for the Angular, using react is only the developer's choice, if the time is limited then reacts is the better option since it is easier to learn.
Ember and Vue are other options, they might not be as popular as React and Angular, but they each has their own Advantages and through the years Vue has gained a lot of attention.
yes, it is possible, however in most cases the entire Application has to be written again in order to be compatible with new Framework, therefore it is better to choose the right Framework from the beginning.
In this section, we will be publishing the static website on github so everybody can access it. These steps will guide you through publishing your Project into a repository.