Web3 Stack

Polkadot


Polkadot - Layer 0

Relay chain - nominated proof of stake chain
limited functionality
no support for smart contract
collect states from parachains
Parachains - layer 1
offers dapp, smart contract, ... and connect to relay chain
parachains block was sent to relay chain for validity
layer 2 can be built upon layer 1 parachain
can also act as bridges to connect with other blockchain ecosystem (Bitcoin, Ethereum)
Parathreads
function the same as parachains but was charged based on each block instead of signing a two year lease with Polkadot
Kusama - standalone independent blockchain
“test protocol”
Substrate - sdk to build blockchain

RPC (Remote procedure calls)
a way for an external program—for example, a browser or front-end application—to communicate with a Substrate node. They are used for checking storage values, submitting transactions, and querying the current consensus authorities. Substrate comes with several . In many cases, it is useful to add custom RPCs to your node.


API Design Paradigms
image.png
event driven API
is created to solve the above problem where the client needs to send multiple requests before the web API returns a complete status
WebHooks
Client - register with interested events and callback URL
API Provider sends event data whenever updated to the callback URL
Drawbacks
Failures - ensure delivery through retries
Firewalls - apps running behind firewalls can send but receiving can be tricky
Noise - typically, each webhook represents a single even. Many events in a short time can be noisy
Best for trigger the server to send events to predefined client endpoints
WebSockets
Client - Server
Client sends Handshake (HTTP) & Server upgrade to WebSocket which is a bidirectional communication channel
Benefits
bidirectional low latency communication
reduced overhead of http requests
Drawbacks
Clients are responsible for connections
scalability challenges
Best for Bi-directional communication between clients and servers
HTTP Streaming
client sent a request and server response with a new event that runs indefinitly
Benefits
can stream over simple http
native browser support
Drawbacks
bidirectional communication is challenging
buffering in client side can be limiting
Best for One-way communication over HTTP
request-response API
representational state transfer (REST)
center around resources
collection (https://foobar.com/api/v1/users)
entity (https://foobar.com/api/v1/users/user-1)
Benefits
standard method names, arguments, and status codes
utilizes http features
easy to main
Drawbacks
big payloads
multiple http roundtrips (getting an entity and it sub resources took two api calls)
best for Create Read Update Delete (CRUD) Operations
remote procedural call (RPC)
center around actions
endpoint for each action (https://slack.com/api/chat.postMessage)
Benefits
easy to understand
lightweight payloads
high performance
Drawbacks
discovery is difficult
limited standardization (heavy reference to documentation)
leads to function explosion
best for exposing several actions
GraphQL
get, post for most programs
user defined a structure that the response should be and the api returns the payload in that structure
Benefits
saves multiple round trips
avoids versioning
smaller payload size
Drawbacks
added complexity for server
optimizing performance is difficult
too complicated for a simple API
best when need querying flexibility

Polkadot.js

install by
yarn add @polkadot/api

API Interface was dynamically generated base on the connected node
Metadata
api.<type>.<module>.<section>
const - runtime constants
query - chain state
tx - all extrinsics

API Provider & API Instance
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.