Skip to content

Commit

Permalink
PHX: New PhoenixManager access patterns
Browse files Browse the repository at this point in the history
OBv2: New OpenBookManager access patterns
  • Loading branch information
skynetcap committed Dec 9, 2023
1 parent f5e4ce2 commit d7598c2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,23 @@
import com.mmorrell.openbook.OpenBookUtil;
import com.mmorrell.openbook.model.OpenBookMarket;
import com.mmorrell.openbook.program.OpenbookProgram;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.bitcoinj.core.Base58;
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.rpc.types.config.Commitment;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Slf4j
public class OpenBookManager {

private RpcClient client;

@Getter
private final RpcClient client;
private final Map<PublicKey, OpenBookMarket> marketCache = new HashMap<>();

public OpenBookManager(RpcClient client) {
Expand Down Expand Up @@ -50,5 +49,33 @@ public void cacheMarkets() {
});
}

public List<OpenBookMarket> getOpenBookMarkets() {
return marketCache.values().stream().toList();
}

public Optional<OpenBookMarket> getMarket(PublicKey marketId, boolean useCache) {
if (useCache) {
if (marketCache.containsKey(marketId)) {
return Optional.of(marketCache.get(marketId));
} else {
return Optional.empty();
}
} else {
try {
OpenBookMarket openBookMarket = OpenBookMarket.readOpenBookMarket(
client.getApi()
.getAccountInfo(marketId, Map.of("commitment", Commitment.PROCESSED))
.getDecodedData(),
marketId
);

return Optional.of(openBookMarket);
} catch (Exception e) {
log.error("Unable to retrieve OpenBook v2 market {}", marketId);
return Optional.empty();
}
}
}


}
2 changes: 1 addition & 1 deletion openbook/src/test/java/OpenBookTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void openBookV2Test() throws RpcException {

@Test
public void openBookGetMarketsTest() {
log.info("Market cache: {}", openBookManager.getMarketCache());
log.info("Market cache: {}", openBookManager.getOpenBookMarkets());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,17 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

@Data
@Slf4j
public class PhoenixManager {

private RpcClient rpcClient;
private Set<PhoenixMarket> phoenixMarkets = new HashSet<>();
private final RpcClient rpcClient;
private final Map<PublicKey, PhoenixMarket> marketCache = new HashMap<>();

public PhoenixManager(RpcClient rpcClient) {
this.rpcClient = rpcClient;
Expand Down Expand Up @@ -52,19 +51,21 @@ public void cacheMarkets() {
)
);
phoenixMarket.setMarketId(new PublicKey(programAccount.getPubkey()));
phoenixMarkets.add(phoenixMarket);
marketCache.put(phoenixMarket.getMarketId(), phoenixMarket);
});
}

public List<PhoenixMarket> getPhoenixMarkets() {
return phoenixMarkets.stream().toList();
return marketCache.values().stream().toList();
}

public Optional<PhoenixMarket> getMarket(PublicKey marketId, boolean useCache) {
if (useCache) {
return phoenixMarkets.stream()
.filter(market -> market.getMarketId().equals(marketId))
.findFirst();
if (marketCache.containsKey(marketId)) {
return Optional.of(marketCache.get(marketId));
} else {
return Optional.empty();
}
} else {
try {
PhoenixMarket phoenixMarket = PhoenixMarket.readPhoenixMarket(
Expand Down

0 comments on commit d7598c2

Please sign in to comment.