Serum

Serum is a protocol for decentralized exchanges built on Solana. You can use Serum to create new markets, get order books, trade, and more.

How to get a Serum market

A market on Serum contains all the orders and capabilities to make orders on Serum. For everything you do on Serum you need to know the market you are working with.

Press </> button to view full source
import { Connection, PublicKey } from "@solana/web3.js";
import { Market } from "@project-serum/serum";

(async () => {
  const marketAddress = new PublicKey(
    "9wFFyRfZBsuAha4YcuxcXLKwMxJR43S7fPfQLusDBzvT"
  );
  const programAddress = new PublicKey(
    "9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin"
  );
  const connection = new Connection(
    "https://ssc-dao.genesysgo.net",
    "confirmed"
  );

  const market = await Market.load(
    connection,
    marketAddress,
    {},
    programAddress
  );
})();

How to get Serum order books

Serum markets consist of orderbooks which have bids and asks. You can query this information so you can see what is going on on the market and act accordingly.

Press </> button to view full source
import { Connection, PublicKey } from "@solana/web3.js";
import { Market } from "@project-serum/serum";

(async () => {
  let marketAddress = new PublicKey(
    "9wFFyRfZBsuAha4YcuxcXLKwMxJR43S7fPfQLusDBzvT"
  );
  let programAddress = new PublicKey(
    "9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin"
  );
  let connection = new Connection("https://ssc-dao.genesysgo.net", "confirmed");

  let market = await Market.load(connection, marketAddress, {}, programAddress);

  let bids = await market.loadBids(connection);
  for (let [price, size] of bids.getL2(20)) {
    console.log(price, size);
  }

  let asks = await market.loadAsks(connection);
  for (let [price, size] of asks.getL2(20)) {
    console.log(price, size);
  }
})();

How to get current open orders

As a trader, you will want to know what current open orders you have on a market. You can query your or anyone else's open orders on a market with Serum.

Press </> button to view full source
import { Connection, PublicKey } from "@solana/web3.js";
import { Market } from "@project-serum/serum";

(async () => {
  let trader = new PublicKey("CuieVDEDtLo7FypA9SbLM9saXFdb1dsshEkyErMqkRQq");
  let marketAddress = new PublicKey(
    "9wFFyRfZBsuAha4YcuxcXLKwMxJR43S7fPfQLusDBzvT"
  );
  let programAddress = new PublicKey(
    "9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin"
  );
  let connection = new Connection("https://ssc-dao.genesysgo.net", "confirmed");

  let market = await Market.load(connection, marketAddress, {}, programAddress);

  const orders = await market.loadOrdersForOwner(connection, trader);
  for (let order of orders) {
    console.log(order);
  }
})();
Last Updated:
Contributors: tuphan-dn