From 923c0662eb88001ceda0138ed69adc03ef35c89f Mon Sep 17 00:00:00 2001 From: skynetcap <100323448+skynetcap@users.noreply.github.com> Date: Wed, 20 Dec 2023 14:23:54 -0800 Subject: [PATCH] OBv2: Add orders to each market --- .../openbook/manager/OpenBookManager.java | 37 ++++++++++++++++++- .../openbook/model/OpenBookMarket.java | 5 +++ .../openbook/model/OpenBookOrder.java | 19 ++++++++++ openbook/src/test/java/OpenBookTest.java | 14 ++++++- 4 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 openbook/src/main/java/com/mmorrell/openbook/model/OpenBookOrder.java diff --git a/openbook/src/main/java/com/mmorrell/openbook/manager/OpenBookManager.java b/openbook/src/main/java/com/mmorrell/openbook/manager/OpenBookManager.java index 5a363f3..fd6826f 100644 --- a/openbook/src/main/java/com/mmorrell/openbook/manager/OpenBookManager.java +++ b/openbook/src/main/java/com/mmorrell/openbook/manager/OpenBookManager.java @@ -1,7 +1,10 @@ package com.mmorrell.openbook.manager; import com.mmorrell.openbook.OpenBookUtil; +import com.mmorrell.openbook.model.BookSide; +import com.mmorrell.openbook.model.LeafNode; import com.mmorrell.openbook.model.OpenBookMarket; +import com.mmorrell.openbook.model.OpenBookOrder; import com.mmorrell.openbook.program.OpenbookProgram; import lombok.extern.slf4j.Slf4j; import org.bitcoinj.core.Base58; @@ -11,10 +14,12 @@ import org.p2p.solanaj.rpc.types.ProgramAccount; import org.p2p.solanaj.rpc.types.config.Commitment; +import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; @Slf4j public class OpenBookManager { @@ -53,7 +58,7 @@ public List getOpenBookMarkets() { return marketCache.values().stream().toList(); } - public Optional getMarket(PublicKey marketId, boolean useCache) { + public Optional getMarket(PublicKey marketId, boolean useCache, boolean retrieveOrderBooks) { if (useCache) { if (marketCache.containsKey(marketId)) { return Optional.of(marketCache.get(marketId)); @@ -69,6 +74,36 @@ public Optional getMarket(PublicKey marketId, boolean useCache) marketId ); + if (retrieveOrderBooks) { + BookSide bids = BookSide.readBookSide( + client.getApi() + .getAccountInfo(openBookMarket.getBids(), Map.of("commitment", Commitment.PROCESSED)) + .getDecodedData() + ); + + BookSide asks = BookSide.readBookSide( + client.getApi() + .getAccountInfo(openBookMarket.getAsks(), Map.of("commitment", Commitment.PROCESSED)) + .getDecodedData() + ); + + openBookMarket.setBidOrders( + bids.getLeafNodes().stream() + .sorted(Comparator.comparingDouble(LeafNode::getPrice).reversed()) + .map(leafNode -> new OpenBookOrder(leafNode.getPrice(), leafNode.getQuantity(), + leafNode.getOwner())) + .collect(Collectors.toList()) + ); + + openBookMarket.setAskOrders( + asks.getLeafNodes().stream() + .sorted(Comparator.comparingDouble(LeafNode::getPrice).reversed()) + .map(leafNode -> new OpenBookOrder(leafNode.getPrice(), leafNode.getQuantity(), + leafNode.getOwner())) + .collect(Collectors.toList()) + ); + } + return Optional.of(openBookMarket); } catch (Exception e) { log.error("Unable to retrieve OpenBook v2 market {}", marketId); diff --git a/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookMarket.java b/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookMarket.java index 1590be5..d6a41e2 100644 --- a/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookMarket.java +++ b/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookMarket.java @@ -10,6 +10,7 @@ import java.nio.ByteOrder; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.List; @Data @Builder @@ -51,6 +52,10 @@ public class OpenBookMarket { private PublicKey marketQuoteVault; private long quoteDepositTotal; + // Added after deserialization + private List bidOrders; + private List askOrders; + public static OpenBookMarket readOpenBookMarket(byte[] data, PublicKey marketId) { ByteBuffer buffer = ByteBuffer.wrap(data); buffer.order(ByteOrder.LITTLE_ENDIAN); diff --git a/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookOrder.java b/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookOrder.java new file mode 100644 index 0000000..138b6a8 --- /dev/null +++ b/openbook/src/main/java/com/mmorrell/openbook/model/OpenBookOrder.java @@ -0,0 +1,19 @@ +package com.mmorrell.openbook.model; + +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.extern.slf4j.Slf4j; +import org.p2p.solanaj.core.PublicKey; + +@Data +@Slf4j +@AllArgsConstructor +@Builder +public class OpenBookOrder { + + private double price; + private double size; + private PublicKey trader; + +} diff --git a/openbook/src/test/java/OpenBookTest.java b/openbook/src/test/java/OpenBookTest.java index fbadc43..9adf521 100644 --- a/openbook/src/test/java/OpenBookTest.java +++ b/openbook/src/test/java/OpenBookTest.java @@ -53,11 +53,11 @@ public void openBookGetMarketsTest() { @Test public void orderBookTest() throws RpcException, IOException { - byte[] data = client.getApi().getAccountInfo(new PublicKey("7fPxRDvtPWaDMLy8R8Jhr3mLuc32JbxVf98YZ1ogk5cH")) + byte[] data = client.getApi().getAccountInfo(new PublicKey("DJY185dSMyF6TJZ61Gz1XhoQPWCEkpGAG5dmJmgUjhnQ")) .getDecodedData(); BookSide bookSide = BookSide.readBookSide(data); - log.info("fMETA Bids: {}", bookSide); + log.info("SOL/USDC Bids: {}", bookSide); // Files.write(data, new File("fMeta.bin")); bookSide.getOrderTreeNodes().getNodes().stream() @@ -73,4 +73,14 @@ public void orderBookTest() throws RpcException, IOException { leafNode.getOwner()); }); } + + @Test + public void getOBv2MarketWithBooksTest() { + // SOL/USDC + log.info("Manager: {}", openBookManager.getMarket( + PublicKey.valueOf("C3YPL3kYCSYKsmHcHrPWx1632GUXGqi2yMXJbfeCc57q"), + false, + true + )); + } }