Skip to content

Commit

Permalink
OBv2: Normalize prices/sizes into doubles, instead of longs
Browse files Browse the repository at this point in the history
  • Loading branch information
skynetcap committed Dec 20, 2023
1 parent 923c066 commit 62de5fd
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
16 changes: 16 additions & 0 deletions openbook/src/main/java/com/mmorrell/openbook/OpenBookUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,20 @@ public static int readInt32(byte[] data, int offset) {
return bb.getInt(0);
}

public static double priceLotsToNumber(long price, byte baseDecimals, byte quoteDecimals, long baseLotSize,
long quoteLotSize) {
double top = (price * quoteLotSize * getBaseSplTokenMultiplier(baseDecimals));
double bottom = (baseLotSize * getQuoteSplTokenMultiplier(quoteDecimals));

return (top / bottom);
}

public static double getBaseSplTokenMultiplier(byte baseDecimals) {
return Math.pow(10, baseDecimals);
}

public static double getQuoteSplTokenMultiplier(byte quoteDecimals) {
return Math.pow(10, quoteDecimals);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -80,28 +80,23 @@ public Optional<OpenBookMarket> getMarket(PublicKey marketId, boolean useCache,
.getAccountInfo(openBookMarket.getBids(), Map.of("commitment", Commitment.PROCESSED))
.getDecodedData()
);
bids.setBaseDecimals(openBookMarket.getBaseDecimals());
bids.setQuoteDecimals(openBookMarket.getQuoteDecimals());
bids.setBaseLotSize(openBookMarket.getBaseLotSize());
bids.setQuoteLotSize(openBookMarket.getQuoteLotSize());

BookSide asks = BookSide.readBookSide(
client.getApi()
.getAccountInfo(openBookMarket.getAsks(), Map.of("commitment", Commitment.PROCESSED))
.getDecodedData()
);
asks.setBaseDecimals(openBookMarket.getBaseDecimals());
asks.setQuoteDecimals(openBookMarket.getQuoteDecimals());
asks.setBaseLotSize(openBookMarket.getBaseLotSize());
asks.setQuoteLotSize(openBookMarket.getQuoteLotSize());

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())
);
openBookMarket.setBidOrders(bids.getOrders());
openBookMarket.setAskOrders(asks.getOrders());
}

return Optional.of(openBookMarket);
Expand Down
18 changes: 18 additions & 0 deletions openbook/src/main/java/com/mmorrell/openbook/model/BookSide.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.mmorrell.openbook.model;

import com.mmorrell.openbook.OpenBookUtil;
import lombok.Builder;
import lombok.Data;

Expand All @@ -26,6 +27,12 @@ public class BookSide {
private List<OrderTreeRoot> reservedRoots;
private OrderTreeNodes orderTreeNodes;

// From the parent Market
private byte baseDecimals;
private byte quoteDecimals;
private long baseLotSize;
private long quoteLotSize;

public static BookSide readBookSide(byte[] data) {
return BookSide.builder()
.roots(
Expand Down Expand Up @@ -62,4 +69,15 @@ public List<LeafNode> getLeafNodes() {
.map(LeafNode::readLeafNode)
.collect(Collectors.toList());
}

public List<OpenBookOrder> getOrders() {
return getLeafNodes().stream()
.map(leafNode -> OpenBookOrder.builder()
.price(OpenBookUtil.priceLotsToNumber(leafNode.getPrice(), baseDecimals, quoteDecimals,
baseLotSize, quoteLotSize))
.size((leafNode.getQuantity() * baseLotSize) / OpenBookUtil.getBaseSplTokenMultiplier(baseDecimals))
.trader(leafNode.getOwner())
.build())
.toList();
}
}
11 changes: 9 additions & 2 deletions openbook/src/test/java/OpenBookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
import java.util.Comparator;
import java.util.List;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

@Slf4j
public class OpenBookTest {

Expand Down Expand Up @@ -77,10 +80,14 @@ public void orderBookTest() throws RpcException, IOException {
@Test
public void getOBv2MarketWithBooksTest() {
// SOL/USDC
log.info("Manager: {}", openBookManager.getMarket(
OpenBookMarket solUsdc = openBookManager.getMarket(
PublicKey.valueOf("C3YPL3kYCSYKsmHcHrPWx1632GUXGqi2yMXJbfeCc57q"),
false,
true
));
).get();

log.info("Manager: {}", solUsdc);

assertFalse(solUsdc.getBidOrders().isEmpty());
}
}

0 comments on commit 62de5fd

Please sign in to comment.