Skip to content

Commit

Permalink
chore(test): Add simple open position with external funding e2e test
Browse files Browse the repository at this point in the history
  • Loading branch information
holzeis committed May 24, 2024
1 parent 38afdcf commit 7acd209
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,10 @@ jobs:
run: |
just run-maker-detached
echo "Maker successfully started."
- name: Run lnd-mock
run: |
just run-lnd-mock-detached
echo "Lnd mock successfully started."
- name: Run e2e tests
run: RUST_BACKTRACE=1 ${{ matrix.tests }} --nocapture --ignored
- name: Print maker logs on e2e tests error
Expand Down
20 changes: 20 additions & 0 deletions crates/tests-e2e/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,26 @@ pub fn submit_order(order: NewOrder) -> String {
block_in_place(move || api::submit_order(order).unwrap())
}

pub fn submit_unfunded_channel_opening_order(
order: NewOrder,
coordinator_reserve: u64,
trader_reserve: u64,
estimated_margin: u64,
order_matching_fee: u64,
) -> anyhow::Result<()> {
block_in_place(move || {
api::submit_unfunded_channel_opening_order(
order,
coordinator_reserve,
trader_reserve,
estimated_margin,
order_matching_fee,
)
})?;

Ok(())
}

pub fn submit_channel_opening_order(
order: NewOrder,
coordinator_reserve: u64,
Expand Down
1 change: 1 addition & 0 deletions crates/tests-e2e/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub mod app;
pub mod bitcoind;
pub mod coordinator;
pub mod http;
pub mod lnd_mock;
pub mod logger;
pub mod setup;
pub mod test_flow;
Expand Down
50 changes: 50 additions & 0 deletions crates/tests-e2e/src/lnd_mock.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use anyhow::Context;
use anyhow::Result;
use reqwest::Client;
use serde::Serialize;

/// A wrapper over the coordinator HTTP API.
///
/// It does not aim to be complete, functionality will be added as needed.
pub struct LndMock {
client: Client,
host: String,
}

impl LndMock {
pub fn new(client: Client, host: &str) -> Self {
Self {
client,
host: host.to_string(),
}
}

pub fn new_local(client: Client) -> Self {
Self::new(client, "http://localhost:18080")
}

pub async fn pay_invoice(&self) -> Result<reqwest::Response> {
self.post::<()>("/pay_invoice", None).await
}

async fn post<T: Serialize>(&self, path: &str, body: Option<T>) -> Result<reqwest::Response> {
let request = self.client.post(format!("{0}{path}", self.host));

let request = match body {
Some(ref body) => {
let body = serde_json::to_string(body)?;
request
.header("Content-Type", "application/json")
.body(body)
}
None => request,
};

request
.send()
.await
.context("Could not send POST request to lnd mock")?
.error_for_status()
.context("Lnd mock did not return 200 OK")
}
}
65 changes: 65 additions & 0 deletions crates/tests-e2e/tests/e2e_open_position_with_external_funding.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
use bitcoin::Amount;
use native::api;
use native::api::ContractSymbol;
use native::health::Service;
use native::health::ServiceStatus;
use native::trade::order::api::NewOrder;
use native::trade::order::api::OrderType;
use native::trade::position::PositionState;
use std::time::Duration;
use tests_e2e::app::submit_unfunded_channel_opening_order;
use tests_e2e::http::init_reqwest;
use tests_e2e::lnd_mock::LndMock;
use tests_e2e::setup::TestSetup;
use tests_e2e::wait_until;

#[tokio::test(flavor = "multi_thread")]
#[ignore = "need to be run with 'just e2e' command"]
async fn can_open_position_with_external_lightning_funding() {
let test = TestSetup::new().await;
test.fund_coordinator(Amount::ONE_BTC, 2).await;
let app = &test.app;

let order = NewOrder {
leverage: 2.0,
contract_symbol: ContractSymbol::BtcUsd,
direction: api::Direction::Long,
quantity: 1.0,
order_type: Box::new(OrderType::Market),
stable: false,
};

submit_unfunded_channel_opening_order(order.clone(), 10_000, 10_000, 5_000, 1_000).unwrap();

let client = init_reqwest();
let lnd_mock = LndMock::new_local(client.clone());

// wait for the watchers before paying the invoice.
tokio::time::sleep(Duration::from_secs(1)).await;
tracing::info!("Paying invoice");
lnd_mock.pay_invoice().await.unwrap();

assert_eq!(app.rx.status(Service::Orderbook), ServiceStatus::Online);
assert_eq!(app.rx.status(Service::Coordinator), ServiceStatus::Online);

// Assert that the order was posted
wait_until!(app.rx.order().is_some());
assert_eq!(app.rx.order().unwrap().quantity, order.quantity);
assert_eq!(app.rx.order().unwrap().direction, order.direction);
assert_eq!(
app.rx.order().unwrap().contract_symbol,
order.contract_symbol
);
assert_eq!(app.rx.order().unwrap().leverage, order.leverage);

// Assert that the position is opened in the app
wait_until!(app.rx.position().is_some());
assert_eq!(app.rx.position().unwrap().quantity, order.quantity);
assert_eq!(app.rx.position().unwrap().direction, order.direction);
assert_eq!(
app.rx.position().unwrap().contract_symbol,
order.contract_symbol
);
assert_eq!(app.rx.position().unwrap().leverage, order.leverage);
wait_until!(app.rx.position().unwrap().position_state == PositionState::Open);
}

0 comments on commit 7acd209

Please sign in to comment.