Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ jobs:
DRIFT_GATEWAY_KEY: ${{ secrets.DRIFT_GATEWAY_KEY }}
TEST_DELEGATED_SIGNER: ${{ secrets.TEST_DELEGATED_SIGNER }}
TEST_RPC_ENDPOINT: ${{ secrets.DEVNET_RPC_ENDPOINT }}
TEST_MAINNET_RPC_ENDPOINT: ${{ secrets.MAINNET_RPC_ENDPOINT }}

# --test-threads, limit parallelism to prevent hitting RPC rate-limits
run: |
cargo -V
Expand Down
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
[package]
name = "drift-gateway"
version = "1.5.0"
version = "1.5.1"
edition = "2021"

[dependencies]
actix-web = "*"
argh = "*"
drift-rs = { git = "https://github.com/drift-labs/drift-rs", rev = "4da4966" }
drift-rs = { git = "https://github.com/drift-labs/drift-rs", rev = "975d81f" }
env_logger = "*"
futures-util = "*"
log = "*"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -743,7 +743,7 @@ curl -X POST "http://localhost:8080/v2/swap" \
"outputMarket": 1,
"exactIn": true,
"amount": "500.0",
"slippage": 10
"slippageBps": 10
}'
```

Expand Down
51 changes: 41 additions & 10 deletions src/controller.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use drift_rs::{
priority_fee_subscriber::{PriorityFeeSubscriber, PriorityFeeSubscriberConfig},
types::{
self, accounts::SpotMarket, MarketId, MarketType, ModifyOrderParams, OrderStatus,
RpcSendTransactionConfig, SdkError, SdkResult, VersionedMessage,
ProgramError, RpcSendTransactionConfig, SdkError, SdkResult, VersionedMessage,
},
utils::get_http_url,
DriftClient, Pubkey, TransactionBuilder, Wallet,
Expand Down Expand Up @@ -643,9 +643,10 @@ impl AppState {
)
};

let signer = self.wallet.signer();
let (jupiter_swap_info, account_data) = tokio::try_join!(
self.client.jupiter_swap_query(
self.wallet.authority(),
&signer,
amount,
swap_mode,
req.slippage_bps,
Expand All @@ -669,13 +670,14 @@ impl AppState {
jupiter_swap_info,
in_market,
out_market,
&Wallet::derive_associated_token_address(self.wallet.authority(), in_market),
&Wallet::derive_associated_token_address(self.wallet.authority(), out_market),
&Wallet::derive_associated_token_address(&signer, in_market),
&Wallet::derive_associated_token_address(&signer, out_market),
None,
None,
)
.with_priority_fee(ctx.cu_price.unwrap_or(pf), ctx.cu_limit)
.build();

self.send_tx(tx, "swap", ctx.ttl).await
}

Expand Down Expand Up @@ -739,11 +741,12 @@ impl AppState {
}),
_ => {
let err: SdkError = err.into();
if let Some(code) = err.to_anchor_error_code() {

if let Some(program_error) = err.to_anchor_error_code() {
return Ok(TxEventsResponse::new(
Default::default(),
false,
Some(format!("program error: {code}")),
Some(format!("program error: {program_error}")),
));
}
if err.to_out_of_sol_error().is_some() {
Expand All @@ -753,6 +756,7 @@ impl AppState {
Some("ouf of sol, top-up account".into()),
));
}

Ok(TxEventsResponse::new(
Default::default(),
false,
Expand Down Expand Up @@ -794,6 +798,27 @@ impl AppState {
self.priority_fee_subscriber.priority_fee_nth(0.9)
}

/// Test tx simulate only
#[cfg(test)]
async fn send_tx(
&self,
tx: VersionedMessage,
reason: &'static str,
_ttl: Option<u16>,
) -> GatewayResult<TxResponse> {
match self.client.simulate_tx(tx).await?.err {
Some(err) => {
log::error!("test tx failed: {err:?}");
Err(ControllerError::TxFailed {
reason: reason.into(),
code: 0,
})
}
None => Ok(TxResponse::new("".into())),
}
}

#[cfg(not(test))]
async fn send_tx(
&self,
tx: VersionedMessage,
Expand Down Expand Up @@ -875,10 +900,16 @@ impl AppState {
}

fn handle_tx_err(err: SdkError) -> ControllerError {
if let Some(code) = err.to_anchor_error_code() {
ControllerError::TxFailed {
reason: code.name(),
code: code.into(),
if let Some(program_err) = err.to_anchor_error_code() {
match program_err {
ProgramError::Drift(code) => ControllerError::TxFailed {
reason: code.name(),
code: code.into(),
},
ProgramError::Other { ix_idx, code } => ControllerError::TxFailed {
reason: format!("ix idx: {ix_idx}"),
code,
},
}
} else {
ControllerError::Sdk(err)
Expand Down
84 changes: 80 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -554,15 +554,15 @@ mod tests {
Some(delegated_seed),
None,
Some(
"GiMXQkJXLVjScmQDkoLJShBJpTh9SDPvT2AZQq8NyEBf"
"DxoRJ4f5XRMvXU9SGuM4ZziBFUxbhB3ubur5sVZEvue2"
.try_into()
.unwrap(),
),
);

let rpc_endpoint = std::env::var("TEST_RPC_ENDPOINT")
.unwrap_or_else(|_| "https://api.devnet.solana.com".to_string());
let state = AppState::new(&rpc_endpoint, true, wallet, None, None, false, vec![]).await;
let rpc_endpoint = std::env::var("TEST_MAINNET_RPC_ENDPOINT")
.unwrap_or_else(|_| "https://api.mainnet-beta.solana.com".to_string());
let state = AppState::new(&rpc_endpoint, false, wallet, None, None, false, vec![]).await;

let app = test::init_service(
App::new()
Expand All @@ -582,6 +582,82 @@ mod tests {
assert!(resp.status().is_success());
}

// likely safe to ignore during development, mainly regression test for CI
#[actix_web::test]
async fn delegated_swap_works() {
let _ = env_logger::try_init();
let delegated_seed =
std::env::var("TEST_DELEGATED_SIGNER").expect("delegated signing key set");
let wallet = create_wallet(
Some(delegated_seed),
None,
Some(
"DxoRJ4f5XRMvXU9SGuM4ZziBFUxbhB3ubur5sVZEvue2"
.try_into()
.unwrap(),
),
);

let rpc_endpoint = std::env::var("TEST_MAINNET_RPC_ENDPOINT")
.unwrap_or_else(|_| "https://api.mainnet-beta.solana.com".to_string());
let state = AppState::new(&rpc_endpoint, false, wallet, None, None, false, vec![]).await;

let app =
test::init_service(App::new().app_data(web::Data::new(state)).service(swap)).await;
tokio::time::sleep(Duration::from_secs(1)).await;

let payload = serde_json::json!({
"inputMarket": 0,
"outputMarket": 1,
"exactIn": true,
"amount": "5.0",
"slippageBps": 10
});

let req = test::TestRequest::default()
.set_payload(serde_json::to_vec(&payload).unwrap())
.method(Method::POST)
.uri("/swap")
.to_request();

let resp = test::call_service(&app, req).await;
dbg!(resp.response().body());
assert!(resp.status().is_success());
}

// likely safe to ignore during development, mainly regression test for CI
#[actix_web::test]
async fn swap_works() {
let _ = env_logger::try_init();
let wallet = create_wallet(Some(get_seed()), None, None);

let rpc_endpoint = std::env::var("TEST_MAINNET_RPC_ENDPOINT")
.unwrap_or_else(|_| "https://api.mainnet-beta.solana.com".to_string());
let state = AppState::new(&rpc_endpoint, false, wallet, None, None, false, vec![]).await;

let app =
test::init_service(App::new().app_data(web::Data::new(state)).service(swap)).await;
tokio::time::sleep(Duration::from_secs(1)).await;

let payload = serde_json::json!({
"inputMarket": 0,
"outputMarket": 1,
"exactIn": false,
"amount": "0.1",
"slippageBps": 10
});

let req = test::TestRequest::default()
.set_payload(serde_json::to_vec(&payload).unwrap())
.method(Method::POST)
.uri("/swap")
.to_request();

let resp = test::call_service(&app, req).await;
dbg!(resp.response().body());
assert!(resp.status().is_success());
}

#[actix_web::test]
async fn set_leverage_works() {
let controller = setup_controller(None).await;
Expand Down
Loading