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 138fbc1 commit 75b4c21
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@

## Building

`docker build .`
- Place private key in `main/resources`
- Set `application.endpoint` (RPC) & `application.privateKey` (file name) in application.properties

- `docker build .`

## Running
`docker run CONTAINER_NAME`
16 changes: 11 additions & 5 deletions src/main/java/markets/arcana/obcranker/ObCrankerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.p2p.solanaj.core.Account;
import org.p2p.solanaj.core.PublicKey;
import org.p2p.solanaj.rpc.RpcClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
Expand All @@ -23,23 +24,28 @@
@Slf4j
public class ObCrankerApplication {

@Value("${application.endpoint}")
private String endpoint;

@Value("${application.privateKey}")
private String privateKeyFileName;

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

public static void main(String[] args) {
SpringApplication.run(ObCrankerApplication.class, args);
}

@EventListener(ApplicationReadyEvent.class)
public void crankEventHeapLoop() throws InterruptedException, IOException {
OpenBookManager manager = new OpenBookManager(new RpcClient("https://mainnet.helius-rpc" +
".com/?api-key=a778b653-bdd6-41bc-8cda-0c7377faf1dd"));
public void crankEventHeapLoop() {
OpenBookManager manager = new OpenBookManager(new RpcClient(endpoint));

Account tradingAccount = null;
try {
tradingAccount = Account.fromJson(
Resources.toString(Resources.getResource("mikeDBaJgkicqhZcoYDBB4dRwZFFJCThtWCYD7A9FAH.json"), Charset.defaultCharset()));
Resources.toString(Resources.getResource(privateKeyFileName), Charset.defaultCharset()));
} catch (IOException e) {
e.printStackTrace();
log.error("Error reading private key: {}", e.getMessage());
}
Account finalTradingAccount = tradingAccount;

Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@

application.endpoint=https://mainnet.helius-rpc.com/?api-key=a778b653-bdd6-41bc-8cda-0c7377faf1dd
application.privateKey=mikeDBaJgkicqhZcoYDBB4dRwZFFJCThtWCYD7A9FAH.json

0 comments on commit 75b4c21

Please sign in to comment.