Skip to content

Commit 90118f9

Browse files
committed
Initate spark payments from background
Previously payments could block and we would end up waiting for the payment to complete before ever returning. This could cause our payment metadata to get screwed up as we weren't able to mark it as a trusted/rebalance payment immedaitely. Now we generate the id ourselves and init the payment in the background so we do not block on this and an safely set our payment metadata.
1 parent 262ecae commit 90118f9

File tree

1 file changed

+23
-11
lines changed
  • orange-sdk/src/trusted_wallet/spark

1 file changed

+23
-11
lines changed

orange-sdk/src/trusted_wallet/spark/mod.rs

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -218,17 +218,29 @@ impl TrustedWalletInterface for Spark {
218218
};
219219
let prepare = self.spark_wallet.prepare_send_payment(params).await?;
220220

221-
let res = self
222-
.spark_wallet
223-
.send_payment(SendPaymentRequest {
224-
prepare_response: prepare,
225-
options: None,
226-
idempotency_key: None,
227-
})
228-
.await?;
229-
230-
let id = parse_payment_id(&res.payment.id)?;
231-
Ok(id)
221+
let uuid = Uuid::now_v7();
222+
// spawn payment send in background since it can take a while and we don't want to block the caller
223+
let w = Arc::clone(&self.spark_wallet);
224+
let logger = Arc::clone(&self.logger);
225+
tokio::spawn(async move {
226+
match w
227+
.send_payment(SendPaymentRequest {
228+
prepare_response: prepare,
229+
options: None,
230+
idempotency_key: Some(uuid.to_string()),
231+
})
232+
.await
233+
{
234+
Ok(res) => {
235+
log_info!(logger, "Payment sent successfully: {res:?}");
236+
},
237+
Err(e) => {
238+
log_error!(logger, "Failed to send payment: {e:?}");
239+
},
240+
}
241+
});
242+
243+
Ok(parse_payment_id(&uuid.to_string())?)
232244
} else {
233245
Err(TrustedError::UnsupportedOperation(
234246
"Only BOLT 11 is currently supported".to_owned(),

0 commit comments

Comments
 (0)