04 Dapps
04 Dapps
1 / 36
Summary
During this chapter we will see
what is a dApps
work with javascript to access the blockchain
time: 7h
2 / 36
dApps
3 / 36
dApps
In the previous chapter we have seen how to basically create a contract, what
is this language called solidity and how the blockchain ecosystem works
Yet you cannot expect user to use some CLI to use your services, send and
get information and display them. For that you need to create a frontend
interface that you can customize aka HTML, CSS and JavaScript (+any
flavored framework)
4 / 36
dApps
But first before jumping into the code, what is a dApp?
Decentralized Application
operate autonomously thanks to smart contracts
run on a decentralized computing aka blockchain
without human intervention
ownership is assured by tokens
which are distributed by an algorithm
5 / 36
dApps
dApps are divided into numerous categories
finance
game
governance
media
exchange
and a lot more
6 / 36
dApps
7 / 36
dApps
Ok great! But why dApps can be useful ? How can we create and use them ?
They are open-source and operate on their own without anyone entity
controlling them.
Their data and records are public.
They use a cryptographic token to help keep their network secure.
8 / 36
dApps
dApps have several exciting aspects:
11 / 36
dApps
While dApps can be about everything, the three main concepts being used
can be resumed as
12 / 36
dApps
Exercise:
13 / 36
dApps
Now that we know a bit more about the dApps landscape let's see how we
can use the web technologies to interact with the blockchain.
14 / 36
dApps
For a basic setup you don't need to install a lot of tooling. Your favorite IDE
and some knowledge about HTML, CSS and JavaScript are enough. But you
can also use React, Angular or any others frontend framework!
When working in the web3 sphere, there is two major package we can use,
namely web3.js and ether.js. Both provide almost the same
functionalities so you can use which ever you want.
In this first part I will be using web3.js. Later on I will switch with ether.js so
you can see how both are working.
15 / 36
dApps
Interaction with the wallet of your user is quite simple. If you followed the
courses with attention - and saw how Metamask works - you should already
be think of this few steps
16 / 36
dApps
All the code below is used with React
And you are all setup ! With only 3 lines of code you are already working with
some concepts of the blockchain.
Now we want to directly work with the wallet and get all the information we
could need when building a dApp
17 / 36
dApps
Request the list of all accounts
web3.eth.requestAccounts([callback]) // return an
array of accounts
18 / 36
dApps
Get the chainid (useful to check you are on the good network)
19 / 36
dApps
Get the chainid (useful to check you are on the good network)
20 / 36
dApps
A lot of time, you will need to transform some data before querying the wallet
or after to display a better output to your users.
For that web3.js also export a list of functionalities that are really useful.
For example, in the previous example we do have our balance in WEI which
is a really big number. Not really useful to display that to our user. How can
we change that ?
21 / 36
dApps
Coming from the util module in web3.js you can simply use the function
fromWei()
Web3.utils.fromWei()
22 / 36
dApps
Exercise :
Create a front end application (read only for now) which allow you to interact
with you wallet
23 / 36
dApps
Now that you are able to query and request information from your wallet how
can you interact with your smart contract deployed on a blockchain ?
24 / 36
dApps
But first you need to "access" you contract. For that you can use the
contract method from web3.js.
const NameContract =
web3.eth.Contract(contract_abi, contract_address);
25 / 36
dApps
What is the ABI of the contract ?
26 / 36
dApps
Both information are available in Remix under the Deploy contract
tab once your contract is deployed. The address is directly visible but
for the ABI you need to check the options and copy/past
27 / 36
dApps
A
{
constant: true,
inputs: [ { name: '_owner', type: 'address'
} ],
name: 'balanceOf',
outputs: [ { name: 'balance', type:
'uint256' } ],
payable: false,
stateMutability: 'view',
type: 'function'
}
28 / 36
29 / 36
dApps
A basic ABI for some ERC20 token (most of the token based on Ethereum
like FTM) would be somewhat similar to the previous example
This code is what allow your JavaScript code to know what is available on
your smart contract. And with the address the web3.js client - through
Metamask - knows how to reach this contract
30 / 36
dApps
Once initialized, we can call any of the methods of our smart contract using
either the call() or send() methods.
NameContract.methods.getName().call();
NameContract.methods.setName("some data").send();
31 / 36
dApps
The difference between the call() and send() methods has to do with the type
of function they are calling and their effect. Solidity functions can be divided
into two categories:
32 / 36
dApps
The reason we have two separate methods for calling functions that do or do
not alter the state of a smart contract is because the former requires a
transaction on the blockchain and the expense of ether in order to be
effective.
The ether required to execute a smart contract function is called “gas” and it is
a transaction that the user of the application will have to accept. If the user is
using an ethereum wallet such as the Metamask plugin in their browser, they
will have to accept the expense of the transaction via a popup window.
33 / 36
dApps
Example:
34 / 36
dApps
Exercise :
Now you can interact directly with your contract and every contracts you can
find ! Try building an interface for your smart contract and add multiples
functionalities. You want to be able to read and write multiples values Why not
create an army of pokemons for example ?
35 / 36
dApps
Exercise : review all
To practice a bit more, let's review what we saw in this chapter! You can
continue to use cryptozombie to learn more about implementation
36 / 36