Skip to content

Commit

Permalink
Optimize scheduler and rearrange the try/catch block
Browse files Browse the repository at this point in the history
The number of threads for the scheduled executor service has been increased to 2 for performance improvement. The try/catch block in the scheduling method has been moved outside, so the trading account is created before scheduling tasks, avoiding unnecessary exception handling inside the schedule method. Additionally, added a new schedule method to handle open book markets.
  • Loading branch information
skynetcap committed Dec 27, 2023
1 parent 6706946 commit 138fbc1
Showing 1 changed file with 35 additions and 9 deletions.
44 changes: 35 additions & 9 deletions src/main/java/markets/arcana/obcranker/ObCrankerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.google.common.io.Resources;
import com.mmorrell.openbook.manager.OpenBookManager;
import com.mmorrell.openbook.model.OpenBookMarket;
import lombok.extern.slf4j.Slf4j;
import org.p2p.solanaj.core.Account;
import org.p2p.solanaj.core.PublicKey;
Expand All @@ -22,7 +23,7 @@
@Slf4j
public class ObCrankerApplication {

private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(2);

public static void main(String[] args) {
SpringApplication.run(ObCrankerApplication.class, args);
Expand All @@ -33,18 +34,20 @@ public void crankEventHeapLoop() throws InterruptedException, IOException {
OpenBookManager manager = new OpenBookManager(new RpcClient("https://mainnet.helius-rpc" +
".com/?api-key=a778b653-bdd6-41bc-8cda-0c7377faf1dd"));

Account tradingAccount = null;
try {
tradingAccount = Account.fromJson(
Resources.toString(Resources.getResource("mikeDBaJgkicqhZcoYDBB4dRwZFFJCThtWCYD7A9FAH.json"), Charset.defaultCharset()));
} catch (IOException e) {
e.printStackTrace();
}
Account finalTradingAccount = tradingAccount;

scheduler.scheduleAtFixedRate(() -> {
// SOL/USDC
PublicKey marketId = PublicKey.valueOf("C3YPL3kYCSYKsmHcHrPWx1632GUXGqi2yMXJbfeCc57q");
Account tradingAccount = null;
try {
tradingAccount = Account.fromJson(
Resources.toString(Resources.getResource("mikeDBaJgkicqhZcoYDBB4dRwZFFJCThtWCYD7A9FAH.json"), Charset.defaultCharset()));
} catch (IOException e) {
e.printStackTrace();
}
Optional<String> transactionId = manager.consumeEvents(
tradingAccount,
finalTradingAccount,
marketId,
8
);
Expand All @@ -56,5 +59,28 @@ public void crankEventHeapLoop() throws InterruptedException, IOException {
}

}, 0, 5, TimeUnit.SECONDS);

scheduler.scheduleAtFixedRate(() -> {
manager.cacheMarkets();
for (OpenBookMarket market : manager.getOpenBookMarkets()) {
Optional<String> transactionId = manager.consumeEvents(
finalTradingAccount,
market.getMarketId(),
8
);

if (transactionId.isPresent()) {
log.info("Cranked events [{}]: {}", market.getName(), transactionId.get());
} else {
log.info("No events found to consume.");
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
log.error("Error consuming: {}", e.getMessage());
}
}

}, 0, 60, TimeUnit.SECONDS);
}
}

0 comments on commit 138fbc1

Please sign in to comment.