Skip to content

Commit

Permalink
Phoenix tests: Add JitoSol test
Browse files Browse the repository at this point in the history
  • Loading branch information
skynetcap committed Nov 19, 2023
1 parent fc2ca06 commit cc56df7
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public class PhoenixMarket {
private List<Pair<PublicKey, PhoenixTraderState>> traders;
private List<Pair<PublicKey, PhoenixTraderState>> tradersSanitized;

private PhoenixMarketHeader phoenixMarketHeader;

public static PhoenixMarket readPhoenixMarket(byte[] data, PhoenixMarketHeader header) {
PhoenixMarket phoenixMarket = PhoenixMarket.builder()
.baseLotsPerBaseUnit(Utils.readInt64(data, START_OFFSET))
Expand All @@ -51,6 +53,7 @@ public static PhoenixMarket readPhoenixMarket(byte[] data, PhoenixMarketHeader h
.askListSanitized(new ArrayList<>())
.traders(new ArrayList<>())
.tradersSanitized(new ArrayList<>())
.phoenixMarketHeader(header)
.build();

long bidsSize =
Expand Down
42 changes: 41 additions & 1 deletion phoenix/src/test/java/PhoenixTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,22 @@
import org.p2p.solanaj.core.PublicKey;
import org.p2p.solanaj.core.Transaction;
import org.p2p.solanaj.programs.ComputeBudgetProgram;
import org.p2p.solanaj.rpc.Cluster;
import org.p2p.solanaj.rpc.RpcClient;
import org.p2p.solanaj.rpc.RpcException;
import org.p2p.solanaj.rpc.types.AccountInfo;
import org.p2p.solanaj.rpc.types.ProgramAccount;
import org.p2p.solanaj.rpc.types.config.Commitment;
import org.p2p.solanaj.ws.SubscriptionWebSocketClient;
import org.p2p.solanaj.ws.listeners.AccountNotificationEventListener;

import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -100,6 +104,42 @@ public void phoenixGetJitoSolMarketTest() throws RpcException {
});
}

@Test
public void phoenixGetMarketDetailStreamingTest() throws RpcException, IOException, InterruptedException {
final SubscriptionWebSocketClient mainnet = SubscriptionWebSocketClient.getInstance(
Cluster.MAINNET.getEndpoint()
);

mainnet.accountSubscribe(
SOL_USDC_MARKET.toBase58(),
data -> {
log.info("Price tick.");
Map<String, Object> map = (Map<String, Object>) data;
String base64 = (String) ((List) map.get("data")).get(0);
byte[] bytes = Base64.getDecoder().decode(base64);
PhoenixMarket phoenixMarket = PhoenixMarket.readPhoenixMarket(
bytes,
PhoenixMarketHeader.readPhoenixMarketHeader(bytes)
);

var bids = phoenixMarket.getBidListSanitized().stream().sorted(
(o1, o2) -> Math.toIntExact(o2.component1().getPriceInTicks() - o1.getFirst().getPriceInTicks())
).toList();
bids = bids.subList(0, 5);
bids.forEach(fifoOrderIdFIFORestingOrderPair -> {
log.info(String.format("Bids: $%.4f, Size: %.2f SOL, Trader: %s",
(double) fifoOrderIdFIFORestingOrderPair.getFirst().getPriceInTicks() / phoenixMarket.getBaseLotsPerBaseUnit(),
(double) fifoOrderIdFIFORestingOrderPair.getSecond().getNumBaseLots() / phoenixMarket.getTickSizeInQuoteLotsPerBaseUnit(),
phoenixMarket.getTradersSanitized().get((int) (fifoOrderIdFIFORestingOrderPair.getSecond().getTraderIndex() - 1)).getFirst().toBase58()));
});


}
);

Thread.sleep(120_000);
}

@Test
public void phoenixGetMarketDetailTest() throws RpcException, IOException {
final AccountInfo marketAccountInfo = client.getApi().getAccountInfo(
Expand Down Expand Up @@ -153,7 +193,7 @@ public void phoenixGetMarketDetailTest() throws RpcException, IOException {
log.info(String.format("Bid: $%.2f, Size: %.2f SOL, Trader: %s",
(double) fifoOrderIdFIFORestingOrderPair.getFirst().getPriceInTicks() / phoenixMarket.getTickSizeInQuoteLotsPerBaseUnit(),
(double) fifoOrderIdFIFORestingOrderPair.getSecond().getNumBaseLots() / phoenixMarket.getBaseLotsPerBaseUnit(),
phoenixMarket.getTradersSanitized().get((int) (fifoOrderIdFIFORestingOrderPair.getSecond().getTraderIndex() - 1)).getFirst().toBase58()));
phoenixMarket.getTradersSanitized().get((int) (fifoOrderIdFIFORestingOrderPair.getSecond().getTraderIndex() - 1)).getFirst().toBase58()));
});

var traders = phoenixMarket.getTradersSanitized();
Expand Down

0 comments on commit cc56df7

Please sign in to comment.