Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Serum documentation with OpenBook #586

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions code/openbook/fill-and-fund/fill-and-fund.en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Account, Connection, PublicKey } from '@solana/web3.js';
import { Market } from '@openbook-dex/openbook';

let connection = new Connection('https://testnet.solana.com');
let marketAddress = new PublicKey('...');
let programAddress = new PublicKey('...');
let market = await Market.load(connection, marketAddress, {}, programAddress);
let owner = new Account('...');

// 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,
);
}
}
24 changes: 24 additions & 0 deletions code/openbook/fill-and-fund/fill-and-fund.preview.en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// 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,
);
}
}
8 changes: 8 additions & 0 deletions code/openbook/initial-setup/initial-setup.en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Connection, PublicKey } from '@solana/web3.js';
import { Market } from '@openbook-dex/openbook';

let connection = new Connection('https://testnet.solana.com');
let marketAddress = new PublicKey('...');
let programAddress = new PublicKey('...');
let market = await Market.load(connection, marketAddress, {}, programAddress);

1 change: 1 addition & 0 deletions code/openbook/initial-setup/initial-setup.preview.en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
let market = await Market.load(connection, marketAddress, {}, programAddress);
27 changes: 27 additions & 0 deletions code/openbook/order-book-interaction/order-book-interaction.en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Connection, PublicKey } from '@solana/web3.js';
import { Market } from '@openbook-dex/openbook';

let connection = new Connection('https://testnet.solana.com');
let marketAddress = new PublicKey('...');
let programAddress = new PublicKey('...');
let market = await Market.load(connection, marketAddress, {}, programAddress);

// 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'
);
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
let bids = await market.loadBids(connection);
let asks = await market.loadAsks(connection);
27 changes: 27 additions & 0 deletions code/openbook/order-management/order-management.en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { Account, Connection, PublicKey } from '@solana/web3.js';
import { Market } from '@openbook-dex/openbook';

let connection = new Connection('https://testnet.solana.com');
let marketAddress = new PublicKey('...');
let programAddress = new PublicKey('...');
let market = await Market.load(connection, marketAddress, {}, programAddress);

// 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);
}
7 changes: 7 additions & 0 deletions code/openbook/order-management/order-management.preview.en.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// 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);
}
24 changes: 0 additions & 24 deletions code/serum/get-books/get-books.en.ts

This file was deleted.

9 changes: 0 additions & 9 deletions code/serum/get-books/get-books.preview.en.ts

This file was deleted.

20 changes: 0 additions & 20 deletions code/serum/get-orders/get-orders.en.ts

This file was deleted.

1 change: 0 additions & 1 deletion code/serum/get-orders/get-orders.preview.en.ts

This file was deleted.

22 changes: 0 additions & 22 deletions code/serum/load-market/load-market.en.ts

This file was deleted.

1 change: 0 additions & 1 deletion code/serum/load-market/load-market.preview.en.ts

This file was deleted.

2 changes: 1 addition & 1 deletion docs/.vuepress/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ export default defineUserConfig<DefaultThemeOptions>({
text: "Integrations",
children: [
"/integrations",
"/integrations/serum.md",
"/integrations/openbook.md",
"/integrations/pyth.md",
"/integrations/switchboard.md",
"/integrations/mango.md",
Expand Down
133 changes: 133 additions & 0 deletions docs/integrations/openbook.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
---
title: Openbook
head:
- - meta
- name: title
content: Solana Cookbook | Building on Openbook
- - meta
- name: og:title
content: Solana Cookbook | Building on Openbook
- - meta
- name: description
content: Openbook is an innovative CLOB on Solana. Learn how to use and build on top of Openbook.
- - meta
- name: og:description
content: Openbook is an innovative CLOB on Solana. Learn how to use and build on top of Openbook.
- - meta
- name: og:image
content: https://solanacookbook.com/cookbook-sharing-card.png
- - meta
- name: og:image:alt
content: Solana splash card
- - meta
- name: twitter:card
content: summary
- - meta
- name: twitter:site
content: "@solanacookbook"
- - meta
- name: twitter:image
content: "https://solanacookbook.com/cookbook-sharing-card.png"
- - meta
- name: robots
content: index,follow,noodp
- - meta
- name: googlebot
content: index,follow
---

# OpenBook

OpenBook is a community-led fork of the Serum V3 program. OpenBook supports most Solana wallets including web based wallets, app wallets and hardware wallets.

## Initial setup

This section establishes the connection to Solana's testnet, loads the market smart contract, and defines relevant addresses.

<SolanaCodeGroup>
<SolanaCodeGroupItem title="TS" active>

<template v-slot:default>

@[code](@/code/openbook/initial-setup/initial-setup.en.ts)

</template>

<template v-slot:preview>

@[code](@/code/openbook/initial-setup/initial-setup.preview.en.ts)

</template>

</SolanaCodeGroupItem>

</SolanaCodeGroup>

## Order Management

This section illustrates how to retrieve, place, and cancel orders within the market, as well as retrieve open orders by owner.

<SolanaCodeGroup>
<SolanaCodeGroupItem title="TS" active>

<template v-slot:default>

@[code](@/code/openbook/order-management/order-management.en.ts)

</template>

<template v-slot:preview>

@[code](@/code/openbook/order-management/order-management.preview.en.ts)

</template>

</SolanaCodeGroupItem>

</SolanaCodeGroup>

## Order Book Interaction

This part of the code demonstrates interaction with the order book, including fetching L2 order book data and displaying detailed information about the full order book.

<SolanaCodeGroup>
<SolanaCodeGroupItem title="TS" active>

<template v-slot:default>

@[code](@/code/openbook/order-book-interaction/order-book-interaction.en.ts)

</template>

<template v-slot:preview>

@[code](@/code/openbook/order-book-interaction/order-book-interaction.preview.en.ts)

</template>

</SolanaCodeGroupItem>

</SolanaCodeGroup>

## Fill Retrieval and Fund Settlement

This section covers retrieving fills and settling funds after trading operations, transferring funds to specified token accounts.

<SolanaCodeGroup>
<SolanaCodeGroupItem title="TS" active>

<template v-slot:default>

@[code](@/code/openbook/fill-and-fund/fill-and-fund.en.ts)

</template>

<template v-slot:preview>

@[code](@/code/openbook/fill-and-fund/fill-and-fund.preview.en.ts)

</template>

</SolanaCodeGroupItem>

</SolanaCodeGroup>
Loading