CentrifugeJS provides a JavaScript client to interact with the Centrifuge/Altair chains. 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.
npm install --save @centrifuge/centrifuge-js
Create an instance and pass optional configuration
import Centrifuge from '@centrifuge/centrifuge-js'
const centrifuge = new Centrifuge({
centrifugeWsUrl: 'wss://fullnode.development.cntrfg.com',
})
The following config options can be passed on initilization of CentrifugeJS:
Default value: centrifuge
Network the instance should run on, either altair
or centrifuge
.
Default value: wss://fullnode.centrifuge.io
Collator websocket URL.
Default value: https://api.subquery.network/sq/centrifuge/pools
Altair collator websocket URL.
Default value: https://centrifuge.mypinata.cloud
IPFS gateway url for retrieving metadata.
Default value: https://api.subquery.network/sq/centrifuge/pools
Indexed subquery for retrieving historical chain data.
Can either be passed in the config on initialization or can be set programmatically by calling centrifuge.connect(<signing-address>, <signer>)
Can either be passed in the config on initialization or can be set programmatically by calling centrifuge.connect(<signing-address>, <signer>)
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
- and a few more..
Methods are accessed like this:
// pools
const data = centrifuge.pools.createPool([...])
// nfts
const data = centrifuge.nfts.mintNft([...])
// metadata
const 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.
centrifuge.pools.getPools().subscribe({
next: (value) => {
console.log('next', value) // Pool[]
},
complete: () => {
console.log('complete')
},
error: () => {
console.log('error')
},
})
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
yarn add --save rxjs
Then the query could look like this
import { firstValueFrom } from 'rxjs'
// ...
const 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 mutliple wallets.
// wallet setup imported from @subwallet/wallet-connect/dotsama/wallets
const wallet = getWalletBySource("polkadot-js");
await wallet?.enable();
const accounts = await wallet?.getAccounts();
const signingAddress = accounts?.[0].address as string;
// connect centrifuge to wallet to enable signatures
const connectedCent = centrifuge.connect(signingAddress, wallet?.signer);
// subscription based
connectedCent.pools.closeEpoch(["<your-pool-id>"]).subscribe({
complete: () => {
console.log("Tx complete");
}
});
// or promise based (using firstValueFrom imported from rxjs)
await firstValueFrom(connectedCent.pools.closeEpoch(["<your-pool-id>"]))
Creating a pool requires a series of extrinsics to be executed sequentially. Using the cent.pools.createPool()
abstraction provides three ways of creating pools:
- No restrictions, pools is ready and available after tx is confirmed. Usually used in dev environments.
- Pool requires democracy but can be fast-tracked (noting the preimage). Usually used in staging environments, e.g Altair.
- Pool requires democracy and must go through the regular voting process (pool proposal). Offical POP (pool onboarding process).
The method also uploads metadata using the pinFile()
method should be defined when creating the centrifuge
instance.
The wallet address creating the pool.
A new unique pool ID. An available ID can be queried using cent.pools.getAvailablePoolId()
.
A new unique NFT collection ID (for assets). An available ID can be queried using centrifuge.nfts.getAvailableCollectionId()
An array of tranches to be associated with the pool. The only data the pool is concerned about is interestRatePerSec
(Rate) and minRiskBuffer
(Perquintill) for all tranches that are not residual (the most junior tranche). For the most junior tranche an empty object is expected.
Example:
const tranches = [
{}, // most junior tranche (residual tranche)
{
interestRatePerSec: Rate.fromAprPercent('2'),
minRiskBuffer: Perquintill.fromPercent('20'),
},
// ...
]
The pools initial maximum reserve (can be changed later).
An object containing all of the required keys from the PoolMetadataInput
type. Note that any images associated with the pool metadata must be uploaded prior to calling the createPoolMethod
Along with the regular tx options the createPool()
supports an additional option: createType
. This refers to the three different ways to create pools.
createType | Description |
---|---|
immediate |
Sign the tx, no further actions required |
notePreimage |
Signing the tx will create a fast tracked democracy proposal. Voting will be required. Pool must be initiliazed after voting period. |
propose |
Signing the tx will create a regular democracy proposal. Voting will be required. Pool must be initiliazed after voting period. |
Like creating pools, minting assets also requires a series of transactions to be executed sequentially.
The following steps must be executed in order to mint an asset on-chain:
-
cent.nfts.mintNft
: Mint the collateral NFT and will add it to the supplied collection. -
cent.pools.createLoan
Create the loan (asset) from the collateral NFT on-chain.
collectionId nftId owner metadata
The id used for the collateral collection.
The id to give the NFT.
The owner of the NFT. This will usually be the Asset Originator account
type NFTMetadataInput = {
name: string
description?: string
image?: string
properties?: Record<string, string | number>
}
The public metadata of the NFT.
The poolId to which the assets belongs.
The id used for the collateral collection.
The id of the previously minted NFT.
Install dependencies with yarn
or npm
.
Start dev server
yarn 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.