Skip to content

Commit

Permalink
fix: improve design patterns, decouple market logic from openbook cli…
Browse files Browse the repository at this point in the history
…ent and open orders logic, Optimize crate bundle size and drop unnecessary crates (#11)
  • Loading branch information
wiseaidev authored May 6, 2024
1 parent 398cd88 commit b9b077c
Show file tree
Hide file tree
Showing 16 changed files with 2,376 additions and 2,902 deletions.
351 changes: 16 additions & 335 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 1 addition & 8 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,15 @@ exclude = ["tests"]

[dependencies]
openbook_dex = { version = "0.5.6" , features = ["test", "client"]}
solana-rpc-client = "=1.18.6"
anyhow = "1.0.80"
solana-program = "=1.18.6"
solana-sdk = "=1.18.6"
rand = "0.8.5"
solana-rpc-client-api = "=1.18.6"
anchor-spl = "0.29.0"
solana-client = "=1.18.6"
serum_dex = "0.5.4"
pyth-sdk-solana = "0.10.0"
memoffset = "0.9.0"
borsh = "1.3.1"
serde_json = "1.0.114"
tokio = { version = "1.36.0", features = ["full"] }
tokio = "1.36.0"
spl-associated-token-account = "=2.3.0"
solana-account-decoder = "=1.18.6"
clap = { version = "4.5.1", features = ["derive"] , optional = true }
solana-cli-output = { version = "1.18.6" , optional = true }
solana-transaction-status = "1.18.6"
Expand Down
46 changes: 19 additions & 27 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ cargo install --locked openbook --all-features

## Usage

Before using the `openbook` crate or CLI ot TUI, make sure to set the following environment variables:
Before using the `openbook` crate or CLI, make sure to set the following environment variables:

```bash
export RPC_URL=https://api.mainnet-beta.solana.com
Expand Down Expand Up @@ -104,7 +104,7 @@ openbook settle -e
### Cancel Settle Place Order:

```sh
openbook cancel-settle-place -u 10.0 -t 0.5 -p 15.0 -a 1.3
openbook cancel-settle-place -u 5.0 -t 2.5 -p 5.0 -a 5.0
```

### Cancel Settle Place Bid Order:
Expand Down Expand Up @@ -151,30 +151,22 @@ openbook = "0.0.11"
```

```rust
use openbook::{pubkey::Pubkey, rpc_client::RpcClient};
use openbook::market::Market;
use openbook::utils::read_keypair;
use openbook::matching::Side;
use openbook::orders::OrderReturnType;
use openbook::commitment_config::CommitmentConfig;
use openbook::market::OrderReturnType;
use openbook::tokens_and_markets::{Token, DexVersion};
use openbook::ob_client::OBClient;
use openbook::tokens_and_markets::{DexVersion, Token};
use openbook::matching::Side;
use openbook::pubkey::Pubkey;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let rpc_url = std::env::var("RPC_URL").expect("RPC_URL is not set in .env file");
let key_path = std::env::var("KEY_PATH").expect("KEY_PATH is not set in .env file");

let commitment_config = CommitmentConfig::confirmed();
let rpc_client = RpcClient::new_with_commitment(rpc_url, commitment_config);

let keypair = read_keypair(&key_path);

let mut market = Market::new(rpc_client, DexVersion::default(), Token::JLP, Token::USDC, keypair, true).await?;
let commitment = CommitmentConfig::confirmed();

println!("Initialized Market: {:?}", market);
let mut ob_client = OBClient::new(commitment, DexVersion::default(), Token::JLP, Token::USDC, true, 1000).await?;
println!("Initialized OpenBook Client: {:?}", ob_client);

println!("[*] Place Limit Order");
if let Some(ord_ret_type) = market
if let Some(ord_ret_type) = ob_client
.place_limit_order(
0.1,
Side::Bid, // or Side::Ask
Expand All @@ -195,7 +187,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}

println!("[*] Cancel Orders");
if let Some(ord_ret_type) = market
if let Some(ord_ret_type) = ob_client
.cancel_orders(
true
)
Expand All @@ -212,7 +204,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}

println!("[*] Settle Balance");
if let Some(ord_ret_type) = market
if let Some(ord_ret_type) = ob_client
.settle_balance(
true
)
Expand All @@ -229,7 +221,7 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
}

println!("[*] Cancel Settle Place Order");
let result = market
let result = ob_client
.cancel_settle_place(
10.0,
0.5,
Expand All @@ -240,27 +232,27 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("[*] Transaction successful, signature: {:?}", result);

println!("[*] Cancel Settle Place Bid Order");
let result = market
let result = ob_client
.cancel_settle_place_bid(0.5, 15.0)
.await?;
println!("[*] Transaction successful, signature: {:?}", result);

println!("[*] Cancel Settle Ask Order");
let result = market
let result = ob_client
.cancel_settle_place_ask(0.5, 15.0)
.await?;
println!("[*] Transaction successful, signature: {:?}", result);

let m = market.make_match_orders_transaction(1).await?;
let m = ob_client.make_match_orders_transaction(1).await?;
println!("Match Order Result: {:?}", m);

let open_orders_accounts = vec![Pubkey::new_from_array([0; 32])];
let limit = 10;

let e = market.make_consume_events_instruction(open_orders_accounts.clone(), limit).await?;
let e = ob_client.make_consume_events_instruction(open_orders_accounts.clone(), limit).await?;
println!("Consume Events Result: {:?}", e);

let p = market.make_consume_events_permissioned_instruction(open_orders_accounts.clone(), limit).await?;
let p = ob_client.make_consume_events_permissioned_instruction(open_orders_accounts.clone(), limit).await?;
println!("Consume Events Permissioned Result: {:?}", p);

Ok(())
Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
pub mod cli;
pub mod fees;
pub mod market;
pub mod ob_client;
pub mod orders;
pub mod rpc;
pub mod tokens_and_markets;
pub mod traits;
#[cfg(feature = "cli")]
pub mod tui;
pub mod utils;

// Re-export common func
pub use openbook_dex::matching;
pub use openbook_dex::state;
pub use solana_client::nonblocking::rpc_client;
pub use solana_client::rpc_config;
pub use solana_client::rpc_filter;
pub use solana_program::pubkey;
pub use solana_rpc_client::nonblocking::rpc_client;
pub use solana_sdk::account;
pub use solana_sdk::bs58;
pub use solana_sdk::commitment_config;
pub use solana_sdk::pubkey;
pub use solana_sdk::signature;
pub use solana_sdk::signer::keypair;
Loading

0 comments on commit b9b077c

Please sign in to comment.