Serum Pools JS Library
JavaScript client library for interacting with Project Serum Pools.
Installation
Using npm:
npm install @solana/web3.js @project-serum/pool
Using yarn:
yarn add @solana/web3.js @project-serum/pool
Usage
Load pool info
Fetch and decode pool state:
import { Connection, PublicKey } from '@solana/web3.js';
import { loadPoolInfo, PoolTransactions } from '@project-serum/pool';
let connection = new Connection('...');
let poolAddress = new PublicKey('...');
let poolInfo = await loadPoolInfo(connection, poolAddress);
console.log(poolInfo.state);
See loadPoolInfo()
and PoolState for details.
If you already have the pool state data and just need to decode it, you can
call isPoolState()
and decodePoolState()
directly.
import { decodePoolState } from '@project-serum/pool';
let data = new Buffer('...');
let poolState = decodePoolState(data);
console.log(poolState);
See PoolState
for details on what the pool state contains.
Get pool basket
Use getPoolBasket()
to fetch the current pool basket (the quantity of each token needed to create N pool tokens
or the quantity of each token received for redeeming N pool tokens).
import { Connection, PublicKey } from '@solana/web3.js';
import { loadPoolInfo, getPoolBasket } from '@project-serum/pool';
import BN from 'bn.js';
let connection = new Connection('...');
let poolAddress = new PublicKey('...');
let poolInfo = await loadPoolInfo(connection, poolAddress);
let basket = await getPoolBasket(
connection,
poolInfo,
{ create: new BN(100) },
new PublicKey('...'),
);
console.log(basket);
Create pool tokens
Send a transaction to create pool tokens:
import { Account, Connection, PublicKey } from '@solana/web3.js';
import { loadPoolInfo, PoolTransactions } from '@project-serum/pool';
import BN from 'bn.js';
let connection = new Connection('...');
let poolAddress = new PublicKey('...');
let payer = new Account('...');
let poolInfo = await loadPoolInfo(connection, poolAddress);
let { transaction, signers } = PoolTransactions.execute(
poolInfo,
{
create: new BN(100),
},
{
poolTokenAccount: new PublicKey('...'),
assetAccounts: [new PublicKey('...'), new Public('...')],
owner: payer.publicKey,
},
[new BN(10), new BN(10)],
);
await connection.sendTransaction(transaction, [payer, ...signers]);
See PoolTransactions.execute
for details.
Redeem pool tokens
Send a transaction to redeem pool tokens:
import { Account, Connection, PublicKey } from '@solana/web3.js';
import { loadPoolInfo, PoolTransactions } from '@project-serum/pool';
import BN from 'bn.js';
let connection = new Connection('...');
let poolAddress = new PublicKey('...');
let payer = new Account('...');
let poolInfo = await loadPoolInfo(connection, poolAddress);
let { transaction, signers } = PoolTransactions.execute(
poolInfo,
{
redeem: new BN(100),
},
{
poolTokenAccount: new PublicKey('...'),
assetAccounts: [new PublicKey('...'), new Public('...')],
owner: payer.publicKey,
},
[new BN(10), new BN(10)],
);
await connection.sendTransaction(transaction, [payer, ...signers]);
See PoolTransactions.execute
for details.
API Reference
API Reference