diff --git a/code/openbook/fill-and-fund/fill-and-fund.en.ts b/code/openbook/fill-and-fund/fill-and-fund.en.ts new file mode 100644 index 000000000..2c861123a --- /dev/null +++ b/code/openbook/fill-and-fund/fill-and-fund.en.ts @@ -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, + ); + } + } \ No newline at end of file diff --git a/code/openbook/fill-and-fund/fill-and-fund.preview.en.ts b/code/openbook/fill-and-fund/fill-and-fund.preview.en.ts new file mode 100644 index 000000000..e474c1a50 --- /dev/null +++ b/code/openbook/fill-and-fund/fill-and-fund.preview.en.ts @@ -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, + ); + } + } \ No newline at end of file diff --git a/code/openbook/initial-setup/initial-setup.en.ts b/code/openbook/initial-setup/initial-setup.en.ts new file mode 100644 index 000000000..da30c745d --- /dev/null +++ b/code/openbook/initial-setup/initial-setup.en.ts @@ -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); + diff --git a/code/openbook/initial-setup/initial-setup.preview.en.ts b/code/openbook/initial-setup/initial-setup.preview.en.ts new file mode 100644 index 000000000..da847553b --- /dev/null +++ b/code/openbook/initial-setup/initial-setup.preview.en.ts @@ -0,0 +1 @@ +let market = await Market.load(connection, marketAddress, {}, programAddress); diff --git a/code/openbook/order-book-interaction/order-book-interaction.en.ts b/code/openbook/order-book-interaction/order-book-interaction.en.ts new file mode 100644 index 000000000..b48f6f150 --- /dev/null +++ b/code/openbook/order-book-interaction/order-book-interaction.en.ts @@ -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' + ); +} + diff --git a/code/openbook/order-book-interaction/order-book-interaction.preview.en.ts b/code/openbook/order-book-interaction/order-book-interaction.preview.en.ts new file mode 100644 index 000000000..8a9b09e5c --- /dev/null +++ b/code/openbook/order-book-interaction/order-book-interaction.preview.en.ts @@ -0,0 +1,2 @@ +let bids = await market.loadBids(connection); +let asks = await market.loadAsks(connection); \ No newline at end of file diff --git a/code/openbook/order-management/order-management.en.ts b/code/openbook/order-management/order-management.en.ts new file mode 100644 index 000000000..24fdefd46 --- /dev/null +++ b/code/openbook/order-management/order-management.en.ts @@ -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); +} \ No newline at end of file diff --git a/code/openbook/order-management/order-management.preview.en.ts b/code/openbook/order-management/order-management.preview.en.ts new file mode 100644 index 000000000..f97019940 --- /dev/null +++ b/code/openbook/order-management/order-management.preview.en.ts @@ -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); +} \ No newline at end of file diff --git a/code/serum/get-books/get-books.en.ts b/code/serum/get-books/get-books.en.ts deleted file mode 100644 index 95b5bd278..000000000 --- a/code/serum/get-books/get-books.en.ts +++ /dev/null @@ -1,24 +0,0 @@ -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); - } -})(); diff --git a/code/serum/get-books/get-books.preview.en.ts b/code/serum/get-books/get-books.preview.en.ts deleted file mode 100644 index 78b47baed..000000000 --- a/code/serum/get-books/get-books.preview.en.ts +++ /dev/null @@ -1,9 +0,0 @@ -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); -} diff --git a/code/serum/get-orders/get-orders.en.ts b/code/serum/get-orders/get-orders.en.ts deleted file mode 100644 index 1011b31e6..000000000 --- a/code/serum/get-orders/get-orders.en.ts +++ /dev/null @@ -1,20 +0,0 @@ -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); - } -})(); diff --git a/code/serum/get-orders/get-orders.preview.en.ts b/code/serum/get-orders/get-orders.preview.en.ts deleted file mode 100644 index 415fc5127..000000000 --- a/code/serum/get-orders/get-orders.preview.en.ts +++ /dev/null @@ -1 +0,0 @@ -const orders = await market.loadOrdersForOwner(connection, traderPublicKey); diff --git a/code/serum/load-market/load-market.en.ts b/code/serum/load-market/load-market.en.ts deleted file mode 100644 index 1f9241e86..000000000 --- a/code/serum/load-market/load-market.en.ts +++ /dev/null @@ -1,22 +0,0 @@ -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 - ); -})(); diff --git a/code/serum/load-market/load-market.preview.en.ts b/code/serum/load-market/load-market.preview.en.ts deleted file mode 100644 index cd9b1a723..000000000 --- a/code/serum/load-market/load-market.preview.en.ts +++ /dev/null @@ -1 +0,0 @@ -const market = await Market.load(connection, marketAddress, {}, programAddress); diff --git a/docs/.vuepress/config.ts b/docs/.vuepress/config.ts index ee2172ce4..dd1b69e3a 100644 --- a/docs/.vuepress/config.ts +++ b/docs/.vuepress/config.ts @@ -109,7 +109,7 @@ export default defineUserConfig({ text: "Integrations", children: [ "/integrations", - "/integrations/serum.md", + "/integrations/openbook.md", "/integrations/pyth.md", "/integrations/switchboard.md", "/integrations/mango.md", diff --git a/docs/integrations/openbook.md b/docs/integrations/openbook.md new file mode 100644 index 000000000..44425fccd --- /dev/null +++ b/docs/integrations/openbook.md @@ -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. + + + + + + + + + + + + +## Order Management + +This section illustrates how to retrieve, place, and cancel orders within the market, as well as retrieve open orders by owner. + + + + + + + + + + + + +## 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. + + + + + + + + + + + + +## Fill Retrieval and Fund Settlement + +This section covers retrieving fills and settling funds after trading operations, transferring funds to specified token accounts. + + + + + + + + + + + diff --git a/docs/integrations/serum.md b/docs/integrations/serum.md deleted file mode 100644 index 52f811d8a..000000000 --- a/docs/integrations/serum.md +++ /dev/null @@ -1,117 +0,0 @@ ---- -title: Serum -head: - - - meta - - name: title - content: Solana Cookbook | Building on Serum - - - meta - - name: og:title - content: Solana Cookbook | Building on Serum - - - meta - - name: description - content: Serum is an innovative CLOB on Solana. Learn how to use and build on top of Serum. - - - meta - - name: og:description - content: Serum is an innovative CLOB on Solana. Learn how to use and build on top of Serum. - - - 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 ---- - -# 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. - - - - - - - - - - - - -## 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. - - - - - - - - - - - - -## 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. - - - - - - - - - - - \ No newline at end of file