Skip to main content

Getting Started with multichain

Install

To get started install the package using npm or yarn:

npm i @okcontract/multichain

How to use

Here's an example to quickly set up and use multichain library:

// Import necessary modules
import { Sheet } from "@okcontract/cells";
import {
MultiChainRPC,
LocalRPCSubscriber,
Address,
EVMAddress,
} from "@okcontract/multichain";

// Create a cells proxy
const proxy = new Sheet().newProxy();

// Initialize a multichain instance
const multi = new MultiChainRPC(proxy);
// Initialize a local instance -- doing reference counting
const local = new LocalRPCSubscriber(proxy, multi);

// Create cells for ABI, contract, method, and args
const abi = proxy.new(parseAbi(["..."]));
const contract = proxy.new<EVMAddress>({
addr: new Address("0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"),
chain: "sepolia",
});
const method = proxy.new("fake");
const args = proxy.new([]);

Fetch and react to RPC data

Use local.call to retrieve a reactive cell:

const data = local.call(contract, abi, method, args);
const next = 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.