Skip to content

Commit

Permalink
Add OpenOrdersAccount functionality
Browse files Browse the repository at this point in the history
A new class OpenBookOpenOrdersAccount has been added to retrieve OpenOrdersAccount information. An additional method to OpenBookManager enables fetching this account data. Furthermore, a corresponding unit test has been added to validate this new functionality. Unrelated to these changes, developer organization details have been removed from pom.xml.
  • Loading branch information
skynetcap committed Dec 23, 2023
1 parent e2de730 commit 9d5a95b
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.mmorrell.openbook.model.LeafNode;
import com.mmorrell.openbook.model.OpenBookEventHeap;
import com.mmorrell.openbook.model.OpenBookMarket;
import com.mmorrell.openbook.model.OpenBookOpenOrdersAccount;
import com.mmorrell.openbook.model.OpenBookOrder;
import com.mmorrell.openbook.program.OpenbookProgram;
import lombok.extern.slf4j.Slf4j;
Expand Down Expand Up @@ -124,4 +125,18 @@ public Optional<OpenBookEventHeap> getEventHeap(PublicKey eventHeap) {
}


public Optional<OpenBookOpenOrdersAccount> getOpenOrdersAccount(PublicKey ooa) {
try {
OpenBookOpenOrdersAccount openBookOoa = OpenBookOpenOrdersAccount.readOpenBookOpenOrdersAccount(
client.getApi()
.getAccountInfo(ooa, Map.of("commitment", Commitment.PROCESSED))
.getDecodedData()
);

return Optional.of(openBookOoa);
} catch (RpcException e) {
log.error("Error getting OOA: {}", e.getMessage());
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
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 OpenBookOpenOrdersAccount {

private PublicKey owner;
private PublicKey market;
private String name; // 32 bytes
private PublicKey delegate;
private int accountNum;
private byte bump;
// 3 bytes of padding
private byte[] padding;

// position x 1 DTO TBD
// openOrders x 24 DTO TBD

public static OpenBookOpenOrdersAccount readOpenBookOpenOrdersAccount(byte[] data) {
return OpenBookOpenOrdersAccount.builder()
.owner(PublicKey.readPubkey(data, 8))
.market(PublicKey.readPubkey(data, 40))
.name(new String(ByteUtils.readBytes(data, 72, 32)))
.build();
}

}
18 changes: 17 additions & 1 deletion openbook/src/test/java/OpenBookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
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.model.OpenBookOpenOrdersAccount;
import com.mmorrell.openbook.program.OpenbookProgram;
import lombok.extern.slf4j.Slf4j;
import org.bitcoinj.core.Base58;
Expand All @@ -22,7 +22,9 @@
import java.util.List;
import java.util.Optional;

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

@Slf4j
public class OpenBookTest {
Expand Down Expand Up @@ -113,4 +115,18 @@ public void openBookEventHeapTest(){
log.info("Fill: {}", openBookFillEvent.toString());
});
}

@Test
public void openBookOpenOrdersAccountTest() throws RpcException, IOException {
Optional<OpenBookOpenOrdersAccount> openBookOpenOrdersAccount = openBookManager.getOpenOrdersAccount(
new PublicKey("G1hKFxyM3qNCd1nnjnuvydw6VjCowVp5Jm6w1mwyWH4r")
);
assertTrue(openBookOpenOrdersAccount.isPresent());
OpenBookOpenOrdersAccount account = openBookOpenOrdersAccount.get();
assertEquals(new PublicKey("7uixr2n3aawRYFKu5L6Wjwf37Fe6Twh6Ns3upAPq9H7k"), account.getOwner());

byte[] data = client.getApi().getAccountInfo(new PublicKey("G1hKFxyM3qNCd1nnjnuvydw6VjCowVp5Jm6w1mwyWH4r"))
.getDecodedData();
Files.write(data, new File("ooa.bin"));
}
}
2 changes: 0 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@
<developer>
<name>Michael Morrell</name>
<email>[email protected]</email>
<organization>Openserum</organization>
<organizationUrl>https://openserum.io/</organizationUrl>
</developer>
</developers>

Expand Down

0 comments on commit 9d5a95b

Please sign in to comment.