Skip to content

Commit

Permalink
OBv2: Implement Event Heap
Browse files Browse the repository at this point in the history
  • Loading branch information
skynetcap committed Dec 22, 2023
1 parent 62eea8d commit 36d7dca
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.mmorrell.openbook.model;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;

import java.util.Arrays;

@Data
@Builder
public class OpenBookAnyEvent {

public static final int SIZE = 144;
Expand All @@ -16,9 +16,9 @@ public class OpenBookAnyEvent {
private byte[] padding;

public static OpenBookAnyEvent readOpenBookAnyEvent(byte[] data) {
return OpenBookAnyEvent.builder()
.eventType(data[0])
.padding(Arrays.copyOfRange(data, 1, SIZE))
.build();
OpenBookAnyEvent openBookAnyEvent = new OpenBookAnyEvent();
openBookAnyEvent.setEventType(data[0]);
openBookAnyEvent.setPadding(Arrays.copyOfRange(data, 1, SIZE));
return openBookAnyEvent;
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package com.mmorrell.openbook.model;

import com.mmorrell.openbook.OpenBookUtil;
import lombok.Builder;
import lombok.Data;
import org.bitcoinj.core.Utils;
import org.p2p.solanaj.core.PublicKey;
import org.p2p.solanaj.utils.ByteUtils;

import java.util.Arrays;
Expand All @@ -30,6 +30,7 @@ public class OpenBookEventHeap {
// + EventNode x 600 (starts at offset 24)
private static final int NUM_EVENT_NODES = 600;
private List<OpenBookEventNode> eventNodes;
private List<OpenBookFillEvent> fillEvents;

// + 64 bytes reserved

Expand All @@ -51,4 +52,20 @@ public static OpenBookEventHeap readOpenBookEventHeap(byte[] data) {
)
.build();
}

public List<OpenBookFillEvent> getFillEvents() {
byte[] eventType = {0x00};
return eventNodes.stream()
.filter(openBookEventNode -> openBookEventNode.getEvent().getEventType() == (byte) 0)
.map(openBookEventNode -> {
byte[] combined = new byte[eventType.length + openBookEventNode.getEvent().getPadding().length];
System.arraycopy(eventType, 0, combined, 0, eventType.length);
System.arraycopy(openBookEventNode.getEvent().getPadding(), 0, combined, eventType.length, openBookEventNode.getEvent().getPadding().length);
return OpenBookFillEvent.readOpenBookFillEvent(combined);
})
.filter(openBookFillEvent -> !openBookFillEvent.getMaker().equals(new PublicKey(
"11111111111111111111111111111111")))
.toList();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import lombok.Builder;
import lombok.Data;
import org.bitcoinj.core.Utils;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -31,18 +31,18 @@ public static List<OpenBookEventNode> readEventNodes(byte[] bytes) {
int numNodes = bytes.length / SIZE;
for (int i = 0; i < numNodes; i++) {
byte[] nodeBytes = Arrays.copyOfRange(bytes, i * SIZE, (i + 1) * SIZE);
OpenBookEventNode node = OpenBookEventNode.builder()
.next(ByteBuffer.wrap(nodeBytes, NEXT_OFFSET, 2).getShort())
.prev(ByteBuffer.wrap(nodeBytes, PREV_OFFSET, 2).getShort())
.event(
OpenBookAnyEvent.readOpenBookAnyEvent(
Arrays.copyOfRange(
nodeBytes,
EVENT_OFFSET,
nodeBytes.length
)
)
OpenBookAnyEvent anyEvent = OpenBookAnyEvent.readOpenBookAnyEvent(
Arrays.copyOfRange(
nodeBytes,
EVENT_OFFSET,
nodeBytes.length
)
);

OpenBookEventNode node = OpenBookEventNode.builder()
.next((short) Utils.readUint16(nodeBytes, NEXT_OFFSET))
.prev((short) Utils.readUint16(nodeBytes, PREV_OFFSET))
.event(anyEvent)
.build();
results.add(node);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package com.mmorrell.openbook.model;

import lombok.Builder;
import lombok.Data;
import org.p2p.solanaj.core.PublicKey;
import org.p2p.solanaj.utils.ByteUtils;

@Data
@Builder
public class OpenBookFillEvent {

private byte eventType;
private byte takerSide;
private byte makerOut;
private byte makerSlot;

// 4 bytes padding
private long timeStamp;
private long seqNum;
private PublicKey maker;
private long makerTimeStamp;
private PublicKey taker;
private long takerClientOrderId;
private long price;
private long pegLimit;
private long quantity;
private long makerClientOrderId;
// 8 bytes reserved padding

public static OpenBookFillEvent readOpenBookFillEvent(byte[] data) {
return OpenBookFillEvent.builder()
.eventType((byte) 0)
.takerSide(data[1])
.makerOut(data[2])
.makerSlot(data[3])
.timeStamp(ByteUtils.readUint64(data, 8).longValue())
.seqNum(ByteUtils.readUint64(data, 16).longValue())
.maker(PublicKey.readPubkey(data, 24))
.makerTimeStamp(ByteUtils.readUint64(data, 56).longValue())
.taker(PublicKey.readPubkey(data, 64))
.takerClientOrderId(ByteUtils.readUint64(data, 96).longValue())
.price(ByteUtils.readUint64(data, 104).longValue())
.pegLimit(ByteUtils.readUint64(data, 112).longValue())
.quantity(ByteUtils.readUint64(data, 120).longValue())
.makerClientOrderId(ByteUtils.readUint64(data, 128).longValue())
.build();
}

}
6 changes: 5 additions & 1 deletion openbook/src/test/java/OpenBookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.mmorrell.openbook.model.LeafNode;
import com.mmorrell.openbook.model.NodeTag;
import com.mmorrell.openbook.model.OpenBookEventHeap;
import com.mmorrell.openbook.model.OpenBookFillEvent;
import com.mmorrell.openbook.model.OpenBookMarket;
import com.mmorrell.openbook.program.OpenbookProgram;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -107,6 +108,9 @@ public void eventHeapTest() throws RpcException, IOException {
public void openBookEventHeapTest(){
// 2pMETA
Optional<OpenBookEventHeap> eventHeap = openBookManager.getEventHeap(PublicKey.valueOf("5DviyqH9is6EwSjUETEh5XUe6xP9cpJu17cwiCuiGYQq"));
eventHeap.ifPresent(heap -> log.info("Event Heap: {}", heap));
//eventHeap.ifPresent(heap -> log.info("Event Heap: {}", heap));
eventHeap.get().getFillEvents().forEach(openBookFillEvent -> {
log.info("Fill: {}", openBookFillEvent.toString());
});
}
}

0 comments on commit 36d7dca

Please sign in to comment.