Skip to content

Commit 0c397dd

Browse files
committed
Implement trigger for special orders
1 parent de4340a commit 0c397dd

File tree

11 files changed

+355
-3
lines changed

11 files changed

+355
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "zo-keeper"
33
version = "0.1.0"
44
edition = "2021"
5-
rust-version = "1.58"
5+
rust-version = "1.63"
66

77
[features]
88
devnet = ["zo-abi/devnet"]
@@ -37,3 +37,4 @@ num-traits = "0.2"
3737
fixed = "1"
3838
serum_dex = "0.5"
3939
spl-token = "3.2"
40+
parking_lot = "0.12"

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
FROM rust:latest
22
RUN apt-get update \
33
&& apt-get install -y libudev-dev libclang-dev lld cmake \
4-
&& rustup component add rustfmt
4+
&& rustup component add rustfmt \
5+
&& rustup default nightly
56
WORKDIR /srv
67
COPY . .
78
RUN cargo build --release \

Procfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
crank: ./target/release/zo-keeper crank
22
consumer: ./target/release/zo-keeper consumer
33
recorder: ./target/release/zo-keeper recorder
4+
trigger: ./target/release/zo-keeper trigger

abi

scripts/heroku-docker-cmd.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ case "$DYNO" in
99
consumer.*) run consumer ;;
1010
liquidator.*) run liquidator ;;
1111
recorder.*) run recorder ;;
12+
trigger.*) run trigger ;;
1213
esac

src/custom.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
use anchor_client::{
2+
solana_client::{
3+
nonblocking::pubsub_client::PubsubClient,
4+
rpc_config::{RpcTransactionLogsConfig, RpcTransactionLogsFilter},
5+
rpc_response::RpcLogsResponse,
6+
},
7+
solana_sdk::commitment_config::CommitmentConfig,
8+
};
9+
use futures::StreamExt as _;
10+
use zo_abi as zo;
11+
12+
pub async fn run(st: &'static crate::AppState) -> Result<(), crate::Error> {
13+
let cli = PubsubClient::new(st.cluster.ws_url()).await.unwrap();
14+
let (mut rx, _unsub) = cli
15+
.logs_subscribe(
16+
RpcTransactionLogsFilter::Mentions(vec![zo::ID.to_string()]),
17+
RpcTransactionLogsConfig {
18+
commitment: Some(CommitmentConfig::finalized()),
19+
},
20+
)
21+
.await
22+
.unwrap();
23+
24+
while let Some(x) = rx.next().await {
25+
tokio::task::spawn_blocking(move || print_log(&x.value));
26+
}
27+
28+
Ok(())
29+
}
30+
31+
fn print_log(r: &RpcLogsResponse) {
32+
let status = if r.err.is_none() { "ok " } else { "err" };
33+
34+
let mut i = 0;
35+
for l in r.logs.iter() {
36+
if let Some(ix) = l.strip_prefix("Program log: Instruction: ") {
37+
tracing::info!("ix: {} {} [{}] {}", status, r.signature, i, ix);
38+
i += 1;
39+
}
40+
}
41+
}

src/error.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ pub enum Error {
1111
#[error("{0}: {0:?}")]
1212
SolanaClient(#[from] solana_client::client_error::ClientError),
1313
#[error("{0}: {0:?}")]
14+
SolanaPubsubClient(#[from] solana_client::pubsub_client::PubsubClientError),
15+
#[error("{0}: {0:?}")]
1416
JsonRpc(#[from] jsonrpc_core_client::RpcError),
1517
#[error("{0}: {0:?}")]
1618
TransactionError(

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ pub mod consumer;
22
pub mod crank;
33
pub mod liquidator;
44
pub mod recorder;
5+
pub mod trigger;
56

67
mod db;
78
mod error;

src/main.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,9 @@ enum Command {
7474

7575
/// Listen and store events into a database
7676
Recorder,
77+
78+
/// Trigger special orders.
79+
Trigger,
7780
}
7881

7982
fn main() -> Result<(), lib::Error> {
@@ -90,6 +93,16 @@ fn main() -> Result<(), lib::Error> {
9093
.init();
9194
}
9295

96+
{
97+
// Ensure that a panic in a spawned thread exits the main process.
98+
// Unfortunately, other threads' resources are not necessarily freed.
99+
let hook = std::panic::take_hook();
100+
std::panic::set_hook(Box::new(move |x| {
101+
hook(x);
102+
std::process::exit(255);
103+
}));
104+
}
105+
93106
let Cli {
94107
rpc_url,
95108
ws_url,
@@ -160,6 +173,7 @@ fn main() -> Result<(), lib::Error> {
160173
},
161174
))?,
162175
Command::Recorder => rt.block_on(lib::recorder::run(app_state))?,
176+
Command::Trigger => lib::trigger::run(app_state)?,
163177
};
164178

165179
Ok(())

0 commit comments

Comments
 (0)