Skip to content

Commit

Permalink
https://github.com/pendulum-chain/spacewalk/pull/385#discussion_r1303…
Browse files Browse the repository at this point in the history
…161473
  • Loading branch information
b-yap committed Aug 25, 2023
1 parent 8e3118e commit 1a42592
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 39 deletions.
4 changes: 3 additions & 1 deletion clients/vault/src/requests/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 =>
Expand All @@ -287,6 +288,7 @@ impl Request {
stroop_amount,
request_id,
DEFAULT_STROOP_FEE_PER_OPERATION,
false,
)
.await,
}
Expand Down
1 change: 1 addition & 0 deletions clients/vault/tests/helper/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ pub async fn assert_issue(
stroop_amount,
issue.issue_id.0,
300,
false,
)
.await
.expect("Failed to send payment");
Expand Down
12 changes: 11 additions & 1 deletion clients/vault/tests/vault_integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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);
Expand Down
70 changes: 33 additions & 37 deletions clients/wallet/src/stellar_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,62 +293,48 @@ 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
/// * `asset` - Stellar Asset type of the payment
/// * `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<TransactionResponse, Error> {
// 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,
asset: StellarAsset,
stroop_amount: StellarStroops,
request_id: [u8; 32],
stroop_fee_per_operation: u32,
is_payment_for_redeem_request: bool,
) -> Result<TransactionResponse, Error> {
// user must not send to self
if self.secret_key.get_public() == &destination_address {
return Err(Error::SelfPaymentError)
}

// 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
Expand Down Expand Up @@ -576,6 +562,7 @@ mod test {
amount,
request_id,
DEFAULT_STROOP_FEE_PER_OPERATION,
false,
)
.await
.expect("it should return a success");
Expand All @@ -599,6 +586,7 @@ mod test {
amount,
request_id,
DEFAULT_STROOP_FEE_PER_OPERATION,
false,
)
.await;

Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -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");
Expand Down Expand Up @@ -761,6 +751,7 @@ mod test {
amount,
request_id,
DEFAULT_STROOP_FEE_PER_OPERATION,
false,
)
.await;

Expand Down Expand Up @@ -790,6 +781,7 @@ mod test {
10,
[0u8; 32],
DEFAULT_STROOP_FEE_PER_OPERATION,
false,
)
.await
{
Expand Down Expand Up @@ -826,6 +818,7 @@ mod test {
amount,
request_id,
correct_amount_that_should_not_fail,
false,
)
.await;

Expand All @@ -838,6 +831,7 @@ mod test {
amount,
request_id,
incorrect_amount_that_should_fail,
false,
)
.await;

Expand All @@ -856,6 +850,7 @@ mod test {
amount,
request_id,
correct_amount_that_should_not_fail,
false,
)
.await;

Expand Down Expand Up @@ -883,6 +878,7 @@ mod test {
amount,
request_id,
DEFAULT_STROOP_FEE_PER_OPERATION,
false,
)
.await
.expect("should be ok");
Expand Down

0 comments on commit 1a42592

Please sign in to comment.