JavaScript API
Introduction
Javascript client for reading feeding price data of Banksea Oracle
Banksea NFT Price Oracle aims at delivering price prediction and risk assessment to Banksea Protocol as well as the larger NFT ecosystem. Price oracle use cases:
NFT Sale: Buyers can use NFT price oracle to inform their NFT purchase
NFT Collateral: Protocols or lenders can use the price prediction and risk assessment to determine risk of the loan.
NFT Trading: Users can monitor the price prediction and risk assessment fluctuations to make trading decisions.
Installation
Example usage
1. Build a client
The library mainly exported a class named BankseaOracleClient
, which providers a serials of methods for users to interact with our Oracle
program deployed on Solana network.
So the first step of all, you need to build a BankseaOracleClient
instance:
NOTE:
The code above contains the calling of
fs
, which is the Node.js only module, so you can NOT use this in browser environment. If you are developing in browser environment, consider using adapter provided by external wallet(usually a browser extension such as Phantom, Solflare, etc.).The
endpoint
field in the second argumentopts
is not necessary. If not provided, it would be set toclusterApiUrl(cluster)
, that is the public RPC URL provided by Solana. But you need to note that the performance of public RPC can sometimes be a bit bad.In the second argument of the constructor,
opts
, there is another field:oracle
. The field is in type ofPublicKey
(from@solana/web3.js
), and means the address of oracle. In normal, you don't need to specify this argument, because the library has configured the default oracle address for each Solana cluster(if the Oracle program has been deployed on it by Banksea). But we also allow advanced users to create a new custom Oracle on Solana network, in which case you must specify theoracle
argument to find the one you want to interact with.
2. List all subscribable collections
Before subscribing for feeding, you need to know "what can you subscribe and how can you subscribe".
In our design, the unit and minimum unit to subscribe is the collection
. And all subscribable collections are configured as whitelist
.
whitelist
is an account in Banksea Oracle Program. Each whitelist
mainly contains:
oracle: the oracle address which the whitelist belongs to;
code: the unique identifier of a whitelist, which is usually the name of collection;
unit: the unit of feed price data, maybe 'SOL' or 'ETH';
decimals: the decimals of feed price data. For example: if the
decimals
is 9 and the feed price data you read is375000000
and the unit isSOL
, it means that the real(human-readable) price is375000000 * 10^-9 = 0.375 SOL
You can use the listWhitelists
method to read all whitelist, that is, all collections that can be subscribed to:
Typescript version
Javascript version
3. Subscribe to the collection
Before subscribing, you need to confirm the arguments following:
feedCount: the total number of times to feed. Will reduce 1 ever when feeding, and will stop feeding after reducing to 0;
feedInterval: the interval of the price feeding cycle, that is, how often the price feeding will be carried out. (In unit of second)
feedNodeCount: Required minimum feeding node count. If the actual number of nodes that complete the feeding in one cycle is less that the
feedNodeCount
, the aggregating in that cycle will fail, which means there will be no price data in that cycle. NOTE: this value need to be less than or equal tonodeCount
of collection configcollectionTaskKeypair: The keypair of the
collectionTask
to create(A subscribing is essentially creating aCollectionTask
account). You can manually specify a keypair when subscribing. If not, a random one will be automatically generated internally.
Then you can call the subscribe
method:
4. Update config of subscription task
You can also use the updateSubscription
method to update the subscription config.
You can just set the
feedCount
to0
to cancel the subscribing, and set it to a positive number to restart.When updating, you don't need to specify the argument
collectionTaskKeypair
&whitelistPubkey
anymore, but you must specify thecollectionTaskPubkey
argument additionally to tell the library which one task to update.When updating, you just need to pass the arguments which you want to update. The missing arguments will be treated as no need to update
Here is the example of updating:
In the example, only the feedInterval
will be updated, and any other configs will not be changed.
5. Query all tasks you subscribed to
You can use the listTasksBySubscriber
method to query all tasks subscribed to by a particular subscriber:
Last updated