From 1a42592051d0d288f255ad4f0ccc5d923a88aea2 Mon Sep 17 00:00:00 2001 From: b-yap <2826165+b-yap@users.noreply.github.com> Date: Thu, 24 Aug 2023 23:05:54 +0800 Subject: [PATCH] https://github.com/pendulum-chain/spacewalk/pull/385#discussion_r1303161473 --- clients/vault/src/requests/structs.rs | 4 +- clients/vault/tests/helper/helper.rs | 1 + .../vault/tests/vault_integration_tests.rs | 12 +++- clients/wallet/src/stellar_wallet.rs | 70 +++++++++---------- 4 files changed, 48 insertions(+), 39 deletions(-) diff --git a/clients/vault/src/requests/structs.rs b/clients/vault/src/requests/structs.rs index 44e6cbba6..801b6bfce 100644 --- a/clients/vault/src/requests/structs.rs +++ b/clients/vault/src/requests/structs.rs @@ -271,12 +271,13 @@ impl Request { let response = match self.request_type { RequestType::Redeem => wallet - .send_payment_to_address_for_redeem_request( + .send_payment_to_address( destination_public_key.clone(), self.asset.clone(), stroop_amount, request_id, DEFAULT_STROOP_FEE_PER_OPERATION, + true, ) .await, RequestType::Replace => @@ -287,6 +288,7 @@ impl Request { stroop_amount, request_id, DEFAULT_STROOP_FEE_PER_OPERATION, + false, ) .await, } diff --git a/clients/vault/tests/helper/helper.rs b/clients/vault/tests/helper/helper.rs index 1478bf267..d8264127b 100644 --- a/clients/vault/tests/helper/helper.rs +++ b/clients/vault/tests/helper/helper.rs @@ -137,6 +137,7 @@ pub async fn assert_issue( stroop_amount, issue.issue_id.0, 300, + false, ) .await .expect("Failed to send payment"); diff --git a/clients/vault/tests/vault_integration_tests.rs b/clients/vault/tests/vault_integration_tests.rs index f77c9e81f..11cd0031c 100644 --- a/clients/vault/tests/vault_integration_tests.rs +++ b/clients/vault/tests/vault_integration_tests.rs @@ -576,6 +576,7 @@ async fn test_issue_overpayment_succeeds() { stroop_amount.try_into().unwrap(), issue.issue_id.0, 300, + false, ) .await .expect("Sending payment failed"); @@ -664,6 +665,7 @@ async fn test_automatic_issue_execution_succeeds_hoho() { stroop_amount, issue.issue_id.0, 300, + false, ) .await .expect("should return a result"); @@ -804,6 +806,7 @@ async fn test_automatic_issue_execution_succeeds_for_other_vault() { stroop_amount, issue.issue_id.0, 300, + false, ) .await; assert!(result.is_ok()); @@ -952,7 +955,14 @@ async fn test_execute_open_requests_succeeds() { let mut wallet_write = vault_wallet.write().await; assert_ok!( wallet_write - .send_payment_to_address(address, asset, stroop_amount, redeem_ids[0].0, 300) + .send_payment_to_address( + address, + asset, + stroop_amount, + redeem_ids[0].0, + 300, + false + ) .await ); drop(wallet_write); diff --git a/clients/wallet/src/stellar_wallet.rs b/clients/wallet/src/stellar_wallet.rs index 8137b53b8..33da6e20d 100644 --- a/clients/wallet/src/stellar_wallet.rs +++ b/clients/wallet/src/stellar_wallet.rs @@ -293,11 +293,7 @@ impl StellarWallet { Ok(envelope) } - /// Sends a 'Payment' transaction specifically for redeem requests. - /// Possible operations are the ff: - /// * `Payment` operation - /// * `CreateClaimableBalance` operation - /// * `CreateAccount` operation + /// Sends a 'Payment' transaction. /// /// # Arguments /// * `destination_address` - receiver of the payment @@ -305,30 +301,7 @@ impl StellarWallet { /// * `stroop_amount` - Amount of the payment /// * `request_id` - information to be added in the tx's memo /// * `stroop_fee_per_operation` - base fee to pay for the payment operation - pub async fn send_payment_to_address_for_redeem_request( - &mut self, - destination_address: PublicKey, - asset: StellarAsset, - stroop_amount: StellarStroops, - request_id: [u8; 32], - stroop_fee_per_operation: u32, - ) -> Result { - // payment can be - let payment_like_op = self - .client - .create_payment_op_for_redeem_request( - self.get_public_key(), - destination_address, - self.is_public_network, - asset, - stroop_amount, - ) - .await?; - - self.send_to_address(request_id, stroop_fee_per_operation, vec![payment_like_op]) - .await - } - + /// * `is_payment_for_redeem_request` - true if the operation is for redeem request pub async fn send_payment_to_address( &mut self, destination_address: PublicKey, @@ -336,6 +309,7 @@ impl StellarWallet { stroop_amount: StellarStroops, request_id: [u8; 32], stroop_fee_per_operation: u32, + is_payment_for_redeem_request: bool, ) -> Result { // user must not send to self if self.secret_key.get_public() == &destination_address { @@ -343,12 +317,24 @@ impl StellarWallet { } // create payment operation - let payment_op = create_payment_operation( - destination_address, - asset, - stroop_amount, - self.get_public_key(), - )?; + let payment_op = if is_payment_for_redeem_request { + self.client + .create_payment_op_for_redeem_request( + self.get_public_key(), + destination_address, + self.is_public_network, + asset, + stroop_amount, + ) + .await? + } else { + create_payment_operation( + destination_address, + asset, + stroop_amount, + self.get_public_key(), + )? + }; self.send_to_address(request_id, stroop_fee_per_operation, vec![payment_op]) .await @@ -576,6 +562,7 @@ mod test { amount, request_id, DEFAULT_STROOP_FEE_PER_OPERATION, + false, ) .await .expect("it should return a success"); @@ -599,6 +586,7 @@ mod test { amount, request_id, DEFAULT_STROOP_FEE_PER_OPERATION, + false, ) .await; @@ -626,12 +614,13 @@ mod test { let request_id = [1u8; 32]; let response = wallet - .send_payment_to_address_for_redeem_request( + .send_payment_to_address( default_destination(), default_usdc_asset(), amount, request_id, DEFAULT_STROOP_FEE_PER_OPERATION, + true, ) .await .expect("payment should work"); @@ -692,12 +681,13 @@ mod test { let request_id = [1u8; 32]; let response = wallet - .send_payment_to_address_for_redeem_request( + .send_payment_to_address( destination_secret_key.get_public().clone(), StellarAsset::AssetTypeNative, amount, request_id, DEFAULT_STROOP_FEE_PER_OPERATION, + true, ) .await .expect("should return a transaction response"); @@ -761,6 +751,7 @@ mod test { amount, request_id, DEFAULT_STROOP_FEE_PER_OPERATION, + false, ) .await; @@ -790,6 +781,7 @@ mod test { 10, [0u8; 32], DEFAULT_STROOP_FEE_PER_OPERATION, + false, ) .await { @@ -826,6 +818,7 @@ mod test { amount, request_id, correct_amount_that_should_not_fail, + false, ) .await; @@ -838,6 +831,7 @@ mod test { amount, request_id, incorrect_amount_that_should_fail, + false, ) .await; @@ -856,6 +850,7 @@ mod test { amount, request_id, correct_amount_that_should_not_fail, + false, ) .await; @@ -883,6 +878,7 @@ mod test { amount, request_id, DEFAULT_STROOP_FEE_PER_OPERATION, + false, ) .await .expect("should be ok");