Skip to content

Commit

Permalink
feat(api): execute settlement if negative settle_to is provided durin…
Browse files Browse the repository at this point in the history
…g account creation
  • Loading branch information
gakonst committed Feb 5, 2020
1 parent 965cbc5 commit 138ecc2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
40 changes: 37 additions & 3 deletions crates/interledger-api/src/routes/accounts.rs
Expand Up @@ -173,7 +173,14 @@ where
async move {
let account = store.insert_account(account_details.clone()).await?;

connect_to_external_services(handler, account.clone(), store_clone, btp).await?;
connect_to_external_services(
handler,
account.clone(),
store_clone,
btp,
account_details.settle_to,
)
.await?;
Ok::<Json, Rejection>(warp::reply::json(&account))
}
})
Expand Down Expand Up @@ -212,8 +219,16 @@ where
btp.close_connection(&id);
}
async move {
let settle_to = account_details.settle_to;
let account = store.update_account(id, account_details).await?;
connect_to_external_services(outgoing_handler, account.clone(), store, btp).await?;
connect_to_external_services(
outgoing_handler,
account.clone(),
store,
btp,
settle_to,
)
.await?;

Ok::<Json, Rejection>(warp::reply::json(&account))
}
Expand Down Expand Up @@ -308,6 +323,7 @@ where
modified_account.clone(),
store,
btp,
None,
)
.await?;
Ok::<Json, Rejection>(warp::reply::json(&modified_account))
Expand Down Expand Up @@ -554,6 +570,7 @@ async fn connect_to_external_services<O, A, S, B>(
account: A,
store: S,
btp: BtpOutgoingService<B, A>,
settle_to: Option<i64>,
) -> Result<A, warp::reject::Rejection>
where
O: OutgoingService<A> + Clone + Send + Sync + 'static,
Expand Down Expand Up @@ -596,14 +613,31 @@ where
);

let response = http_client
.create_engine_account(id, se_url)
.create_engine_account(id, se_url.clone())
.map_err(|err| {
Rejection::from(ApiError::internal_server_error().detail(err.to_string()))
})
.await?;

if response.status().is_success() {
trace!("Account {} created on the SE", id);

// On successful account creation, if the settle_to was negative, we should send a
// settlement request to them
if let Some(settle_to) = settle_to {
// prefund the absolute value
if settle_to < 0 {
let amount = settle_to.abs() as u64;
http_client
.send_settlement(id, se_url, amount, account.asset_scale())
.map_err(|err| {
Rejection::from(
ApiError::internal_server_error().detail(err.to_string()),
)
})
.await?;
}
}
} else {
error!(
"Error creating account. Settlement engine responded with HTTP code: {}",
Expand Down
Expand Up @@ -53,7 +53,7 @@ impl SettlementClient {
// The `Prepare` packet's data was sent by the peer's settlement
// engine so we assume it is in a format that our settlement engine
// will understand
// format. `to_vec()` needed to work around lifetime error
// format.
let mut settlement_engine_url = engine_url;
settlement_engine_url
.path_segments_mut()
Expand Down

0 comments on commit 138ecc2

Please sign in to comment.