Article Image

A Comprehensive Guide to Capi

25th June 2023

Capi is a groundbreaking framework designed to streamline interactions with Substrate chains. This article will delve into the features and capabilities of Capi, providing insights into its setup process, basic usage, and advanced features. We'll also address some common queries about Capi in a Q&A section sprinkled throughout the guide.

An Introduction to Capi

Capi is a novel solution that addresses the challenges developers face when interacting with blockchain networks. It encompasses a development server and a fluent API, facilitating multichain interactions without sacrificing either performance or ease of use

Background

Blockchain resources are intentionally constrained by economic incentives. This limitation informs the design of on-chain programs, which in turn restricts an app developer's ability to implement access patterns that would otherwise enhance the end-user experience. Capi presents a solution to this conundrum by defining interactions as "Runes"—declarative, portable, and strongly-typed building blocks for describing client-chain interactions. Runes allow complex interactions to be streamlined into their most minimal and parallelized form.

Setting up Capi

Setting up Capi is straightforward. After installing Capi via NPM, developers can add Capi to their scripts/tasks for convenience. The setup process also includes creating a nets.ts file and syncing the configurations using the Capi CLI's sync command.

Here is an example nets.ts file:

1import { net } from "capi/nets"
2
3export const polkadot = net.ws({ url: "wss://rpc.polkadot.io" })
Example: Creating and executing a Rune using Capi

To eliminate the necessity of switching import statements between development and production, targets can be defined within the nets.ts file like so:

1import { bins, nets } from "capi/nets"
2
3const bin = bins({ polkadot: ["polkadot", "v0.9.43"] })
4
5export const polkadotDev = net.dev({
6 bin: bin.polkadot,
7 chain: "polkadot-dev",
8})
9
10export const polkadot = net.ws({
11 url: "wss://polkadot-rpc.dwellir.com/",
12 targets: { dev: polkadotDev },
13})
Example: Creating and executing a Rune using Capi

Once you've set up Capi, you can start working with your desired blockchain network. After syncing, import the generated API via its @capi/-prefixed kebab-cased name. You can access a wide range of APIs, such as that of storage, extrinsic building, block and event retrieval, and more

The Basics of Capi and Runes

Capi-generated modules expose a fluent API root or ChainRune subclass, each containing a broad range of members. These members allow access to other important APIs, such as storage, extrinsic building, block and event retrieval, and more. The generated chain rune also exposes pallet-specific containers of functionality

Here is a simple example of creating and executing a Rune:

1import { polkadot } from "@capi/polkadot"
2
3const firstTenEntries = polkadot.System.Account.entries({ limit: 10 })
4
5firstTenEntries.run() // Promise<AccountInfo>
Example: Creating and executing a Rune using Capi

In this example, we first create a Rune named firstTenEntries. This is a description of what we want, not the actual result. Network interaction has yet to occur. We then execute the firstTenEntries Rune, which returns a promise resolving to AccountInfo.

Exploring the World of Runes

The intent behind Runes is to enable the composition of complex interactions, potentiallyTitle: Unleashing the Power of Capi: An In-depth Guide to Streamlining Substrate Chain Interactions.

Capi is a groundbreaking framework designed to facilitate intricate interactions with Substrate chains. With a development server and a fluent API, Capi bridges the gap between multichain interactions, performance, and usability, enabling developers to harness the full potential of on-chain applications without compromising on efficiency.

The limitations of chain resources are intentional, guided by economic incentives. As a result, app developers often encounter constraints in implementing access patterns that would enhance end-user experiences. Capi presents an innovative solution to this conundrum, offering an environment that enables data aggregation and preprocessing without straining already limited resources.

Future Considerations

Capi's design is meant to accommodate new technical possibilities as they arise, without requiring significant effort from developers. Future enhancements might include the ability to serialize and share Runes with other environments, statically analyze Runes to generate minimally-scoped policies that govern session keys, and generate clients to be consumed in other programming languages

Frequently Asked Questions

What is Capi and why is it important?

Capi is a framework designed to facilitate complex interactions with Substrate chains. It offers a solution to the limitations imposed by chain resources, enabling efficient data aggregation and preprocessing to enhance end-user experiences.

What are Runes in the context of Capi?

Runes are declarative, portable, and strongly-typed building blocks designed for client-chain interactions. They allow complex operations to be simplified into their most essential and parallelized form.

How does Capi improve the developer experience?

Capi improves the developer experience by enabling type-safe error handling, automatic parallelism, and a declarative and resource-considerate approach. With Capi, developers can focus on crafting sophisticated client-chain interactions without concerning themselves with redundancy and timing issues.

How can Capi be integrated with build tools like Vite and Webpack?

Capi can be integrated with build tools by ensuring that necessary environment variables from Capi's server process are piped into your application. Both Vite and Webpack can be configured to instantiate the Capi server and pass in their respective development commands.

What future possibilities does Capi's design accommodate?

Capi's design accommodates the serialization and sharing of Runes with other environments, the generation of minimally scoped policies, and the creation of clients in other programming languages, among other possibilities.

Is there more documentation?

You can find the full documentation at https://docs.capi.dev/

Get your free API key today

Seamless API key access, coming soon. Get beta access now.