-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathsend_mana.rs
95 lines (83 loc) · 3.09 KB
/
send_mana.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
// Copyright 2024 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0
use serde::{Deserialize, Serialize};
use crate::{
client::{api::PreparedTransactionData, secret::SecretManage},
types::block::{
address::Bech32Address,
output::{
unlock_condition::{AddressUnlockCondition, StorageDepositReturnUnlockCondition},
BasicOutputBuilder,
},
},
utils::serde::string,
wallet::{
operations::transaction::{prepare_output::ReturnStrategy, TransactionOptions, TransactionWithMetadata},
Wallet,
},
};
/// Params for `send_mana()`.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SendManaParams {
#[serde(with = "string")]
mana: u64,
address: Bech32Address,
return_strategy: Option<ReturnStrategy>,
}
impl SendManaParams {
pub fn new(mana: u64, address: Bech32Address) -> Self {
Self {
mana,
address,
return_strategy: None,
}
}
pub fn with_return_strategy(mut self, return_strategy: ReturnStrategy) -> Self {
self.return_strategy.replace(return_strategy);
self
}
}
impl<S: 'static + SecretManage> Wallet<S>
where
crate::wallet::Error: From<S::Error>,
crate::client::Error: From<S::Error>,
{
pub async fn send_mana(
&self,
params: SendManaParams,
options: impl Into<Option<TransactionOptions>> + Send,
) -> crate::wallet::Result<TransactionWithMetadata> {
let options = options.into();
let prepared_transaction = self.prepare_send_mana(params, options.clone()).await?;
self.sign_and_submit_transaction(prepared_transaction, options).await
}
pub async fn prepare_send_mana(
&self,
params: SendManaParams,
options: impl Into<Option<TransactionOptions>> + Send,
) -> crate::wallet::Result<PreparedTransactionData> {
log::debug!("[TRANSACTION] prepare_send_mana");
let return_strategy = params.return_strategy.unwrap_or_default();
let storage_score_params = self.client().get_storage_score_parameters().await?;
let mut output_builder = BasicOutputBuilder::new_with_minimum_amount(storage_score_params)
.with_mana(params.mana)
.add_unlock_condition(AddressUnlockCondition::new(params.address));
match return_strategy {
ReturnStrategy::Return => {
output_builder = output_builder.add_unlock_condition(StorageDepositReturnUnlockCondition::new(
self.address().await.inner().clone(),
1,
)?);
let return_amount = output_builder.clone().finish()?.amount();
output_builder = output_builder.replace_unlock_condition(StorageDepositReturnUnlockCondition::new(
self.address().await.inner().clone(),
return_amount,
)?);
}
ReturnStrategy::Gift => {}
}
let output = output_builder.finish_output()?;
self.prepare_transaction(vec![output], None, options).await
}
}