Web 3.0 development with Substrate. Part 7: APIs.
This post is part of a series on Web 3.0 Development with Substrate.
APIs are light-weight software that run between applications and are designed to help them consume data. APIs play a crucial role in blockchain ecosystems because they allow a multitude of DApps to seamlessly interact with each other via pre-defined endpoints. In this article, we consider how Substrate-based APIs establish a robust programming layer for DApps that persists on top of recurring runtime upgrades.
With Polkadot-JS API, interfaces are generated dynamically by connecting to a node and retrieving the metadata to populate set categories such as runtime constants, chain state, extrinsics and events. It is the go-to for tinkering with real-time DApps, because it allows developers to pick up the state of any Substrate chain and single-handedly consume its data. This popular library also gives plenty of room for flexibility and creativity, since a DApp can subscribe to multiple chain states and submit custom-made transactions.
Substrate RPC API
Substrate nodes expose a set of metadata via HTTP and WS endpoints that developers can access through RPC connections. Substrate RPC API contains the default JSON-RPC methods used to submit queries related to modules such as contracts, eth, off-chain and system. Web developers who are used to a REST-based approach to client-server operations will find the RPC-based approach to DApp-node operations more open-ended. This is because REST primarily deals with long-term database design and CRUD; whereas RPC focuses on making data available so that any method can be called on it at a later time.
Substrate chains that have included the Contracts pallet in their runtime require a specific API to manage SC in addition to the standard API that is used for transaction management. Contracts API interfaces allow developers to use ABIs when encoding or decoding names, arguments, messages and results from Smart Contract-specific languages like Ink! into wasm byte code, and vice-versa. It is recommended to install and upgrade @polkadot/api-contracts alongside the standard Polkadot-JS API package to avoid version mismatch during development.
Although the Contracts API comes with a variety of helpers, they provide the same core interface functionalities: handling and deploying new wasm code, templating contracts from on-chain code hashes, reading values and executing transactions on on-chain contracts. It is important to note that Relay (Layer 0) chains like Polkadot and Kusama do not and will never host SC the way legacy (Layer 1) blockchains like Ethereum or Algorand do. Instead, SC will gradually become available on parachains, well after they have obtained a slot. Ultimately, this means that the existing SC infrastructure will likely evolve and change for the better in upcoming months.
Whether you choose to build on an application-specific blockchain or a Smart Contract, you will find plenty of support in the multi-language client libraries that come with Substrate. Just as developers rely on APIs to manage blockchains data, users also need interfaces to interact with blockchain networks. In Part 8, we will review some UI components and libraries that have become essential parts of the Substrate Framework.
Smashing magazine: Understanding RPC vs REST for HTTP APIs
Shawn Tabrizi: Porting Web3.js to Polkadot.js