Centrifuge SDK provides a JavaScript client to interact with Centrifuge Chain. It provides comprehensive modules to easily create and manage pools, nfts, loans and metadata. CentrifugeJS is built on top of @polkadot/api and uses the RxJS API to query chaindata and submit extrinsics.
1npm install --save @centrifuge/centrifuge-js
Create an instance and pass optional configuration
1import Centrifuge from "@centrifuge/centrifuge-js";23const centrifuge = new Centrifuge({4 centrifugeWsUrl: "wss://fullnode.development.cntrfg.com",5});
The following config options can be passed on initilization of CentrifugeJS:
network
Default value: centrifuge
Network the instance should run on, either altair
or centrifuge
.
centrifugeWsUrl
Default value: wss://fullnode.centrifuge.io
Collator websocket URL.
altairWsUrl
Default value: https://api.subquery.network/sq/centrifuge/pools
Altair collator websocket URL.
metadataHost
Default value: https://centrifuge.mypinata.cloud
IPFS gateway url for retrieving metadata.
centrifugeSubqueryUrl
Default value: https://api.subquery.network/sq/centrifuge/pools
Indexed subquery for retrieving historical chain data.
signer
Can either be passed in the config on initialization or can be set programmatically by calling centrifuge.connect(<signing-address>, <signer>)
signingAddress
Can either be passed in the config on initialization or can be set programmatically by calling centrifuge.connect(<signing-address>, <signer>)
pinFile
A function that returns an object { uri: string }
containing the URI of the pinned file. This is used to upload and reference metadata in pools, collections and nfts. If not set, pools.createPool
, nfts.mintNft
etc will not work.
Creating a centrifuge
instance will give you access to the entire polkadot API and subset of modules to make easier to query and write data. We recommend using Typescript for autocompletion.
The modules include:
pools
nfts
metadata
Methods are accessed like this:
1// pools2const data = centrifuge.pools.createPool([...])34// nfts5const data = centrifuge.nfts.mintNft([...])67// metadata8const data = centrifuge.metadata.getMetadata("uri")
All of the CentrifugeJS modules have queries prefixed with get
that return Observables.
Here's a full sample how to query all of the pools and subscribe to the state. Behind the scenes the pool data is aggregated from multiple sources and formatted into an object. By subscribing to the observable you're also subscribing to events on-chain that will cause the subscription to update when necessary.
1centrifuge.pools.getPools().subscribe({2 next: (value) => {3 console.log("next", value); // Pool[]4 },5 complete: () => {6 console.log("complete");7 },8 error: () => {9 console.log("error");10 },11});
Some cases don't require a subscription. We find it easist to use a helper from rxjs
to convert the observable into a promise. You'll have to install rxjs
1yarn add --save rxjs
Then the query could look like this
1import { firstValueFrom } from "rxjs";23// ...45const pools = await firstValueFrom(cenrtifuge.pools.getPools()); // Pool[]
Transactions/extrinsics require a little more configuration because they need to be signed. Please note that this does not cover how to sign transactions with a proxy.
By connecting the centrifuge
instance with a signer
, the sourced wallet extension will be triggered to ask for a signature. The signer
can be any signer that's compatible with the polkadot API. We use @subwallet/wallet-connect to source multiple wallets.
1// wallet setup imported from @subwallet/wallet-connect/dotsama/wallets2const wallet = getWalletBySource("polkadot-js");3await wallet?.enable();4const accounts = await wallet?.getAccounts();56const signingAddress = accounts?.[0].address as string;78// connect centrifuge to wallet to enable signatures9const connectedCent = centrifuge.connect(signingAddress, wallet?.signer);1011// subscription based12connectedCent.pools.closeEpoch(["<your-pool-id>"]).subscribe({13 complete: () => {14 console.log("Tx complete");15 }16});1718// or promise based (using firstValueFrom imported from rxjs)19await firstValueFrom(connectedCent.pools.closeEpoch(["<your-pool-id>"]))
Install dependencies with yarn
or npm
.
Start dev server
1yarn start
Run test with yarn test
Create a bundle in the ./dist
folder with yarn build
.
Make sure the version in package.json
has been increased since the last publish, then push a tag starting with centrifuge-js/v*
to kick of the publish Github action.