Serum JS Client Library
JavaScript client library for interacting with the Project Serum DEX.
Installation
Using npm:
npm install @solana/web3.js @project-serum/serum
Using yarn:
yarn add @solana/web3.js @project-serum/serum
Usage
import { Account, Connection, PublicKey } from '@solana/web3.js';
import { Market } from '@project-serum/serum';
let connection = new Connection('https://testnet.solana.com');
let marketAddress = new PublicKey('...');
let market = await Market.load(connection, marketAddress);
let bids = await market.loadBids(connection);
let asks = await market.loadAsks(connection);
for (let [price, size] of bids.getL2(20)) {
console.log(price, size);
}
for (let order of asks) {
console.log(
order.orderId,
order.price,
order.size,
order.side,
);
}
let owner = new Account('...');
let payer = new PublicKey('...');
await market.placeOrder(connection, {
owner,
payer,
side: 'buy',
price: 123.45,
size: 17.0,
orderType: 'limit',
});
let myOrders = await market.loadOrdersForOwner(connection, owner.publicKey);
for (let order of myOrders) {
await market.cancelOrder(connection, owner, order);
}
for (let fill of await market.loadFills(connection)) {
console.log(fill.orderId, fill.price, fill.size, fill.side);
}
for (let openOrders of await market.findOpenOrdersAccountsForOwner(
connection,
owner.publicKey,
)) {
if (openOrders.baseTokenFree > 0 || openOrders.quoteTokenFree > 0) {
let baseTokenAccount = new PublicKey('...');
let quoteTokenAccount = new PublicKey('...');
await market.settleFunds(
connection,
owner,
openOrders,
baseTokenAccount,
quoteTokenAccount,
);
}
}