Go to file
armaniferrante 312672d845 readme: Add move notice 2020-11-14 19:29:44 -08:00
src Update TOMO token mint 2020-11-02 09:14:41 +08:00
.editorconfig Initial commit 2020-08-06 10:17:24 -07:00
.eslintignore Initial commit 2020-08-06 10:17:24 -07:00
.eslintrc Update deps 2020-10-28 14:44:44 -07:00
.gitignore Fix cjs build 2020-08-09 20:31:54 -07:00
.travis.yml Initial commit 2020-08-06 10:17:24 -07:00
README.md readme: Add move notice 2020-11-14 19:29:44 -08:00
jest.config.js Switch to tsc for transpilation 2020-08-13 07:29:12 -07:00
package.json v0.13.11 2020-11-02 09:25:09 +08:00
shell Fix getLayoutVersion 2020-09-18 13:04:31 +08:00
tsconfig.json Move tokens and markets to json files (#32) 2020-10-30 12:08:36 +08:00
yarn.lock Update @solana/web3.js 2020-10-28 22:50:32 -07:00

README.md

npm (scoped) Build Status

Moved

This repository has been moved to the serum-ts monorepo.

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);

// Fetching orderbooks
let bids = await market.loadBids(connection);
let asks = await market.loadAsks(connection);
// L2 orderbook data
for (let [price, size] of bids.getL2(20)) {
  console.log(price, size);
}
// Full orderbook data
for (let order of asks) {
  console.log(
    order.orderId,
    order.price,
    order.size,
    order.side, // 'buy' or 'sell'
  );
}

// Placing orders
let owner = new Account('...');
let payer = new PublicKey('...'); // spl-token account
await market.placeOrder(connection, {
  owner,
  payer,
  side: 'buy', // 'buy' or 'sell'
  price: 123.45,
  size: 17.0,
  orderType: 'limit', // 'limit', 'ioc', 'postOnly'
});

// Retrieving open orders by owner
let myOrders = await market.loadOrdersForOwner(connection, owner.publicKey);

// Cancelling orders
for (let order of myOrders) {
  await market.cancelOrder(connection, owner, order);
}

// Retrieving fills
for (let fill of await market.loadFills(connection)) {
  console.log(
    fill.orderId,
    fill.price,
    fill.size,
    fill.side,
  );
}

// Settle funds
for (let openOrders of await market.findOpenOrdersAccountsForOwner(
  connection,
  owner.publicKey,
)) {
  if (openOrders.baseTokenFree > 0 || openOrders.quoteTokenFree > 0) {
    // spl-token accounts to which to send the proceeds from trades
    let baseTokenAccount = new PublicKey('...');
    let quoteTokenAccount = new PublicKey('...');

    await market.settleFunds(
      connection,
      owner,
      openOrders,
      baseTokenAccount,
      quoteTokenAccount,
    );
  }
}