Blockchain Designed Front-End
Blockchains is that thing, that is driving people crazy in 2022
I gonna prepare you, MY DEAR AUDIENCE, for 2023
Right?
What does your client want?
They may approach you like this:
web3 now
you tell me!
♥️
feature?
button?
♥️
Hey you! Yes you! I want web3 stuff in my website. NOW
So… what features you want?
You tell me! You're an expert!
… Maybe let's make a button?
Amazing! How did you get that?!
Let's look at this example app
Which button makes this website a web3 one?
...
It's this button
The most wanted feature is this button
Connect wallet button
Without that button,
in my opinion?
I don't find your app a web3 one
...
This button is crucial
You may wonder why?
To get acces to someting with Web 1 approach
you used to have login and password
With web2 we got posibility to post data
like in social media
We all do that every day
but...
All those data you send is actually out of your control.
...
Web 3 brings the ownership to the user back.
My name is Kuba
I am regular front-end guy with nothing out of ordinary in my stack:
Just simple React, and sometimes advanced TypeScript.
So what do I do?
I make front-end for IPOR Labs
We bring something from traditional finance, you may know,
to decentralized world
Also I cunduct YouTube channel called JS Dżem
How did I get into web3?
*stuck in garage gif*
This talk is about that
I started exactly one year ago
Just like this
dApps
I gonna talk about front-end
for decentralised applications called D-Apps
D-App is an application which uses blockchain
So, let's focus on the front-end part!
Disclaimer
My stack = React.js
EVM compatible chains
*Before we get off the ground - one DISCLAIMER
My stack ≠ is not the only right stack
We operate only on EVM compatible chains
EVM means Ethereum Virtual Machine
You don't have to know what EVM is
...
And hold on for a while
Before we start to build the button
Let's answer a one crucial question
Why blockchain?
ready to use platform
ownership out of the box
open source
It's Ready to use platform
We can treat blockchain as a deterministic platform
where everyone can participate permissionless
*Provides ownership out of the box*
User can prove they own something
because they hold a token in their wallet
The magic happens thanks to public-key cryptography
Most the code is going open source
web3 ➡️ blockchain
web2 ➡️ backend
Web3 frontend talks to blockchain
like every average web2 apps used to talk to backend
But your browser cannot call blockchain on its own
a node
❌ node.js
You need to have a NODE, which talks to blockchain
What is A NODE? And how to get it?
Don't confuse it with NODE.JS
Node providers
You can have Infura or Alchemy account
These are node providers, Sass services
with really straight forward user experience
Just create an account, copy-paste the link and boom
Done!
✅ read
✅ write 👛
❌ put
❌ delete
So far, you're able to read from blockchain
Nothing hard
But
When you want to write to blockchain
you want USERS to use theirs wallet
We already got read and write.
As the blockchain is designed as immutable,
we cannot change anything in our database.
Blockchain is considered as database though
So
There is no longer PUT or DELETE method in our API
What is the difference between web2 and web3 front-end?
*web2 gets smile*
Almost everithing remains the same
You talk to a node instead of HTTP server
*web2 gets smile*
Although, we don't exclude web2 infrastructure
from existing in parallel
You can mix web3 with everything web2 offers to you
0x5981ac4718e35aE03F5533290eD204a767148450
This is an address
a reference to your smart contract or wallet
To get a smart contract address
Chase your smart contract developer
Or find it in resources or protocol documentation
A
P
B
I
application
programming
binary
schema
interface
schema
You may already know what API is
It stands for Application Programming Interface
In web3 we have something similar
an ABI
ABI is an Application Binary Interface
ABI is a schema of you smart contract
You defenitelly want to have a web3 library
You don't want to reinvent the wheel
A library hepls you to encrypt and decrypt data
It's also useful for computations on frontend
as pure JS is weak at Math, sorry
and it helps you to deal with a wallet
The are two most common libraries
web3.js and ethers.js
Personally, I like to use ethers.js
const provider = new ethers.providers.Web3Provider(
window.ethereum
);
await provider.send("eth_requestAccounts", []);
const contract = new ethers.Contract(
address,
abi,
provider,
);
await contract.mintMyNFT()
Let's figure out this snippet
1. Initialize a provider
It could be a wallet or node
This code fires when you click the connect wallet button
3. Check what accounts you have access to
5. Then can create a smart contract instance
In order to do that you need adress, abi and provider.
then just call the mintMyNFT method.
There are two types of methods..
Reading
free 🆓
no wallet required
() ⇒ Promise<Result>
no event emitted
Reading is free
You don't need a wallet, a node is enough
If you call read method it gives you JS Promise
When you await that Promise
You get the result
While reading there is no event emitted
Writing
In order to write you have to pay for gas
It's a transaction fee
Wallet is required, someone has to pay
It's kinda double promise
When user call an action to write something into blockchain
Metamask wallet prompts them to:
accept the transaction and fee amount
That's been the first Promise
When you get the receipt from the second Promise
it means your transaction is already mined
Unfortunetely you cannot tip developers
It can emmit events
The first wallet you probably meet is MetaMask
It's a browser plugin
I am connected to Polygon blockchain
where MATIC is the native cryptocurrency
Down below...
I hold over $300
Pretty nice...
A got tip for you:
It's about security - you cannot refresh credentials
If you lose your private keys, you cannot even revoke them
Be familiar with the best practices
wallet
identity
account
token owner
value
However, what is a wallet in general?
It's your web3 ID
Its your account, in terms of web2
That also means you can own tokens
Tokens, to be worth your attention,
need to refer to some VALUE
Challenges for front-end devs
What challenges you meet building interface
for web3 front-end?
Fist one is aritmetic
*some weird calculations in JavaScript 😅*
We love numbers in JavaScript ♥️
Because… It's just… unpredictable
But…
BigNumber
*18 decimals precisions
*only integers
That library solves all your problems
* You need 18 decimals precision and even more
* And it supports only integers ❤️
Solidity performs likewise
const myBalance = BigNumber.from('8920584203949257369');
It's looks like this
I pass a numeric string to the constructor
Then a can perform math operations
const myBalance = BigNumber.from('8920584203949257369')
.div('24342667722')
.mul('58407229393064');
It's chainable and immputable
for your convenience of course
I got a quiz for you…
**Where is a bug in this code?
Multiply first, then divide 😆
Otherwise you'll lose precision
** Why numeric string?
Because in case of native JS number type
There is a limit
However sting is just string, there is no limit
You can put as long number as you want
Primitive
BigNumber
Obsession
const collateral = BigNumber.from('5000000000000000000000');
const interestRate = BigNumber.from('8920584203949257369');
const total = BigNumber.from('5180000000000000000000');
You can get obsessed with the Bitcoin price
Oh no, sorry!
I mean primitive obsession
Or actually BigNumber obsession
Everything is a BigNumber type
function openSwap(total: BigNumber) {
...
}
function openSwap(total: Total) {
...
}
type Total = BigNumber & {
__brand: 'TOTAL';
};
Sometimes you want to tell a function
to be not allowed to receive WHICHEVER BigNuber
* It should expect Total to be passed
* You can achieve that with the branding technique
BigNumber.from('5000000000000000000000') as Collateral;
BigNumber.from('8920584203949257369') as InterestRate;
BigNumber.from('5180000000000000000000') as Total;
All those BigNumbers are not a BigNumber type any more
Each of them has its own type
Real time UI
How to get real time UI
Everything is about:
When do I have to update data
to keep my UI responsive?
The old way
refresh data in relevant interval
update when transaction affects your data
filter out redundant calls
manage priority
The old, imperative approach is to
* refresh data in relevant interval,
* update when transaction affects your data,
* filter out redundant calls,
* and manage priority
In web2 data changes over time
In web3, data changes over blocks
Nothing is happening between two consecutive blocks
I guess you'd like to use a framework
* Let's have React
you know, React is mostly for UI
I want to bring some cleaver approach for data menagment
* I got redux
* I also was thinking about RxJS
In case be more reactive
but…
* I discovered useDapp
And I fell in love with it - You know why?
They said:
We tried Redux - not succeeded
❌ Redux
we tried RxJS - And?
❌ RxJS
NOT succeeded 😂
Why is this game changer?
Multicall
+ block change detecting
*It performs multicall
* Multicall squashes all the read calls into one
like GraphQl does
* Also it detects incoming blocks
like useQuery can run at intervals
You don't need to care about nothing I mentioned in before
Run in real browser
with real wallet
what parts involved?
local blockchain
facade
smart contracts
❌
components
Cypress + MetaMask = Synpress
@testing-library
@testing-library
Do you know this pyramid?
This scheme?
It's the test level scheme.
From the bottom: unit, integration and E2E tests
Smart contracts are covered on Solidity level
I want to test components and functions
no difference, like any other app
For integration suite
I have to answer a question
What parts I want involved in pipeline?
either local fake blockchain instance
or facade for all blockchain related communication
Finally you run tests in real browser
with real wallet
I use testing library for integration and unit testing
To E2E community brings a plugin for Cypress called Synpress
That plagin provides support for Metamask wallet
So you no longer need to test it manualy
Error logging
Why you really need Error loging?
Since your customer connected their wallet
You have no idea what happens in the browser
No server logs
*So use event loggers like sentry.io or Log Rocket
REMEMBER! DO NOT SEND ANY SENSITIVE DATA!
How mouch cost developing?
You know gas, paid in ETH
Of courese, You have to pay for deploying to production
however
We got testnets!
with free token streams - faucets
There are plenty of Sass services with a free tier
I don't remember I paid for anything up front.
Really did you thing
we charge devs for writing code?!
grab the slides
Who wants web3 developing?
You?! Don't you?
Don't YOU?
Don't YOU?
Really, donna want?
come on!
🆓