Skip to content

Repository files navigation

multichain: Reactive Blockchain RPC Calls

CI

size

multichain is a low-level library that facilitates reactive blockchain RPC calls, leveraging the cells library - a simplified reactive functional library inspired by spreadsheet functionalities.

Problem Statement

Decentralized applications (dApps) often face challenges when connecting to blockchain RPCs due to asynchronous operations that result in significant delays. Common issues include:

  1. Difficulty in implementing multicall aggregation to optimize RPC calls.
  2. Lack of default mechanisms for retrying or dispatching calls to multiple RPC nodes.
  3. Inefficient caching of immutable data, leading to unnecessary re-queries.
  4. The complexity of integrating reactive, functional programming with existing imperative libraries.

Solution

multichain addresses these challenges by providing a multi-chain cache for RPC queries and their responses, built around reactive functional programming principles. Each response is treated as a cell, allowing predefined computational flows that execute automatically when the data becomes available.

Quick Start

Here's how you can set up and use multichain:

Setup

// Import necessary modulesimport{Sheet}from"@okcontract/cells";import{MultiChainRPC,LocalRPCSubscriber,Address,EVMAddress,}from"@okcontract/multichain";// Create a cells proxyconstproxy=newSheet().newProxy();// Initialize a multichain instanceconstmulti=newMultiChainRPC(proxy);// Initialize a local instance -- doing reference countingconstlocal=newLocalRPCSubscriber(proxy,multi);// Create cells for ABI, contract, method, and argsconstabi=proxy.new(parseAbi(["..."]));constcontract=proxy.new<EVMAddress>({addr: newAddress("0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"),chain: "sepolia",});constmethod=proxy.new("fake");constargs=proxy.new([]);

Fetch and react to RPC data

Use local.call to retrieve a reactive cell:

constdata=local.call(contract,abi,method,args);constnext=data.map(v=> ...)args.set(["new..."]);// Update arguments and recompute automatically data, next, etc.

There are options to set the data validity, e.g. query every 30 sec, or define a specific number of retries.

Additional Query Support

multichain extends its functionality beyond function calls to:

  • getTransactionReceipt: Retrieves the receipt of a transaction, providing critical details like status, gas used, and logs.
  • estimateGas: Estimates the gas needed for a transaction before it's executed on the blockchain.
  • getBalance: Fetches the balance of an account at a given block number.
  • blockNumber: Returns the number of the most recent block in the chain.

Our support for RPC methods will keep expanding: Keep an eye on our release notes and community channels for the latest updates and added query support.

Extended Support: EVM and Starknet

In addition to Ethereum and EVM-compatible networks, multichain natively supports Starknet calls, enabling seamless queries across different blockchain networks.

There is no difference in the call, just make sure that both contract and abi match a Starknet contract:

// Contract is a Starknet Addressconstcontract=proxy.new({chain: "starknet",addr: newAddress("0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",StarkNet)});// Starknet ABIconstabi=proxy.new(...)
...
constvalue=local.call(contract,abi,method,args);

Design & Philosophy

multichain is crafted to offer fine-grained control for developers and dApp implementors, with plans to release a higher-level consumer interface to simplify rapid dApp development.

We aim for ease of use and correction, so chasing down any bug is our top priority.

Contributing

Contributions are welcome! For minor fixes, please feel free to submit a pull request. For significant changes, kindly discuss with us via Discord or Twitter before proceeding.

License & Support

multichain is developed by OKcontract and is released under the Apache license. The project is supported by a strategic partnership with Starknet.

About

Low-level library for functional reactive blockchain RPC calls

Resources

Stars

1 star

Watchers

3 watching

Forks

Releases

Packages

Used by

Contributors

Languages