From cc56df7dead830d9a147940e61009128c7e69cb2 Mon Sep 17 00:00:00 2001 From: skynetcap <100323448+skynetcap@users.noreply.github.com> Date: Sun, 19 Nov 2023 00:36:25 -0800 Subject: [PATCH] Phoenix tests: Add JitoSol test --- .../mmorrell/phoenix/model/PhoenixMarket.java | 3 ++ phoenix/src/test/java/PhoenixTest.java | 42 ++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/phoenix/src/main/java/com/mmorrell/phoenix/model/PhoenixMarket.java b/phoenix/src/main/java/com/mmorrell/phoenix/model/PhoenixMarket.java index e816e05..b4103f2 100644 --- a/phoenix/src/main/java/com/mmorrell/phoenix/model/PhoenixMarket.java +++ b/phoenix/src/main/java/com/mmorrell/phoenix/model/PhoenixMarket.java @@ -37,6 +37,8 @@ public class PhoenixMarket { private List> traders; private List> tradersSanitized; + private PhoenixMarketHeader phoenixMarketHeader; + public static PhoenixMarket readPhoenixMarket(byte[] data, PhoenixMarketHeader header) { PhoenixMarket phoenixMarket = PhoenixMarket.builder() .baseLotsPerBaseUnit(Utils.readInt64(data, START_OFFSET)) @@ -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 = diff --git a/phoenix/src/test/java/PhoenixTest.java b/phoenix/src/test/java/PhoenixTest.java index 02a54da..082ce28 100644 --- a/phoenix/src/test/java/PhoenixTest.java +++ b/phoenix/src/test/java/PhoenixTest.java @@ -13,11 +13,14 @@ 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; @@ -25,6 +28,7 @@ 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; @@ -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 map = (Map) 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( @@ -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();