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 21, 2023
1 parent ae59b2c commit 2d11f42
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ public class OpenBookUtil {
(byte) 0xC6, (byte) 0x9A
};

public final static byte[] EVENT_HEAP_DISCRIMINATOR = {
(byte)0x77, (byte)0x3B, (byte)0x3D, (byte)0x13, (byte)0xA5, (byte)0x54,
(byte)0x39, (byte)0xAF
};


/**
* Encodes the "global::initialize" sighash
* @return byte array containing sighash for "global::initialize"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.mmorrell.openbook.OpenBookUtil;
import com.mmorrell.openbook.model.BookSide;
import com.mmorrell.openbook.model.LeafNode;
import com.mmorrell.openbook.model.OpenBookEventHeap;
import com.mmorrell.openbook.model.OpenBookMarket;
import com.mmorrell.openbook.model.OpenBookOrder;
import com.mmorrell.openbook.program.OpenbookProgram;
Expand Down Expand Up @@ -107,5 +108,20 @@ public Optional<OpenBookMarket> getMarket(PublicKey marketId, boolean useCache,
}
}

public Optional<OpenBookEventHeap> getEventHeap(PublicKey eventHeap) {
try {
OpenBookEventHeap openBookEventHeap = OpenBookEventHeap.readOpenBookEventHeap(
client.getApi()
.getAccountInfo(eventHeap, Map.of("commitment", Commitment.PROCESSED))
.getDecodedData()
);

return Optional.of(openBookEventHeap);
} catch (RpcException e) {
log.error(e.getMessage());
return Optional.empty();
}
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
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.utils.ByteUtils;

@Data
@Builder
public class OpenBookEventHeap {

// EventHeapHeader x 1
private short freeHead;
private short usedHead;
private short count;
private short padding;
private long seqNum;

// + EventNode x 600 (starts at offset 24)

// + 64 bytes reserved

public static OpenBookEventHeap readOpenBookEventHeap(byte[] data) {
return OpenBookEventHeap.builder()
.freeHead((short) Utils.readUint16(data, 8))
.usedHead((short) Utils.readUint16(data, 10))
.count((short) Utils.readUint16(data, 12))
.padding((short) Utils.readUint16(data, 14))
.seqNum(ByteUtils.readUint64(data, 16).longValue())
.build();
}
}
13 changes: 11 additions & 2 deletions openbook/src/test/java/OpenBookTest.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import com.google.common.io.Files;
import com.mmorrell.openbook.manager.OpenBookManager;
import com.mmorrell.openbook.model.BookSide;
import com.mmorrell.openbook.model.LeafNode;
Expand All @@ -6,19 +7,19 @@
import com.mmorrell.openbook.program.OpenbookProgram;
import lombok.extern.slf4j.Slf4j;
import org.bitcoinj.core.Base58;
import org.junit.Ignore;
import org.junit.Test;
import org.p2p.solanaj.core.PublicKey;
import org.p2p.solanaj.rpc.RpcClient;
import org.p2p.solanaj.rpc.RpcException;
import org.p2p.solanaj.rpc.types.ProgramAccount;
import org.p2p.solanaj.utils.ByteUtils;

import java.io.File;
import java.io.IOException;
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 @@ -91,4 +92,12 @@ public void getOBv2MarketWithBooksTest() {

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

@Test
@Ignore
public void eventHeapTest() throws RpcException, IOException {
byte[] data = client.getApi().getAccountInfo(new PublicKey("GY5HKym4yKNUpdHpBBiqLB3DHbrNKhLHDFTSLPK8AbFX"))
.getDecodedData();
Files.write(data, new File("eventHeap.bin"));
}
}

0 comments on commit 2d11f42

Please sign in to comment.