-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(test): Add simple open position with external funding e2e test
- Loading branch information
Showing
5 changed files
with
140 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
65
crates/tests-e2e/tests/e2e_open_position_with_external_funding.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |