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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
use miden_client::{
    notes::get_input_note_with_id_prefix,
    transactions::{
        build_swap_tag,
        request::{PaymentTransactionData, SwapTransactionData, TransactionRequest},
    },
};
use miden_objects::{accounts::AccountId, assets::FungibleAsset, notes::NoteType as MidenNoteType};
use wasm_bindgen::prelude::*;

use crate::{
    models::transactions::{NewSwapTransactionResult, NewTransactionResult},
    WebClient,
};

#[wasm_bindgen]
impl WebClient {
    pub async fn new_mint_transaction(
        &mut self,
        target_account_id: String,
        faucet_id: String,
        note_type: String,
        amount: String,
    ) -> Result<NewTransactionResult, JsValue> {
        if let Some(client) = self.get_mut_inner() {
            let target_account_id = AccountId::from_hex(&target_account_id).unwrap();
            let faucet_id = AccountId::from_hex(&faucet_id).unwrap();
            let amount_as_u64: u64 = amount.parse::<u64>().map_err(|err| err.to_string())?;
            let fungible_asset =
                FungibleAsset::new(faucet_id, amount_as_u64).map_err(|err| err.to_string())?;
            let note_type = match note_type.as_str() {
                "Public" => MidenNoteType::Public,
                "Private" => MidenNoteType::Private,
                _ => MidenNoteType::Private,
            };

            let mint_transaction_request = TransactionRequest::mint_fungible_asset(
                fungible_asset,
                target_account_id,
                note_type,
                client.rng(),
            )
            .unwrap();

            let mint_transaction_execution_result =
                client.new_transaction(faucet_id, mint_transaction_request).await.unwrap();

            let result = NewTransactionResult::new(
                mint_transaction_execution_result.executed_transaction().id().to_string(),
                mint_transaction_execution_result
                    .created_notes()
                    .iter()
                    .map(|note| note.id().to_string())
                    .collect(),
            );

            client.submit_transaction(mint_transaction_execution_result).await.unwrap();

            Ok(result)
        } else {
            Err(JsValue::from_str("Client not initialized"))
        }
    }

    pub async fn new_send_transaction(
        &mut self,
        sender_account_id: String,
        target_account_id: String,
        faucet_id: String,
        note_type: String,
        amount: String,
        recall_height: Option<String>,
    ) -> Result<NewTransactionResult, JsValue> {
        if let Some(client) = self.get_mut_inner() {
            let sender_account_id = AccountId::from_hex(&sender_account_id).unwrap();
            let target_account_id = AccountId::from_hex(&target_account_id).unwrap();
            let faucet_id = AccountId::from_hex(&faucet_id).unwrap();
            let amount_as_u64: u64 = amount.parse::<u64>().map_err(|err| err.to_string())?;
            let fungible_asset = FungibleAsset::new(faucet_id, amount_as_u64)
                .map_err(|err| err.to_string())?
                .into();

            let note_type = match note_type.as_str() {
                "Public" => MidenNoteType::Public,
                "Private" => MidenNoteType::Private,
                _ => MidenNoteType::Private,
            };
            let payment_transaction =
                PaymentTransactionData::new(fungible_asset, sender_account_id, target_account_id);

            let send_transaction_request = if let Some(recall_height) = recall_height {
                let recall_height_as_u32: u32 =
                    recall_height.parse::<u32>().map_err(|err| err.to_string())?;

                TransactionRequest::pay_to_id(
                    payment_transaction,
                    Some(recall_height_as_u32),
                    note_type,
                    client.rng(),
                )
                .unwrap()
            } else {
                TransactionRequest::pay_to_id(payment_transaction, None, note_type, client.rng())
                    .unwrap()
            };

            let send_transaction_execution_result = client
                .new_transaction(sender_account_id, send_transaction_request)
                .await
                .unwrap();
            let result = NewTransactionResult::new(
                send_transaction_execution_result.executed_transaction().id().to_string(),
                send_transaction_execution_result
                    .created_notes()
                    .iter()
                    .map(|note| note.id().to_string())
                    .collect(),
            );

            client.submit_transaction(send_transaction_execution_result).await.unwrap();

            Ok(result)
        } else {
            Err(JsValue::from_str("Client not initialized"))
        }
    }

    pub async fn new_consume_transaction(
        &mut self,
        account_id: String,
        list_of_notes: Vec<String>,
    ) -> Result<NewTransactionResult, JsValue> {
        if let Some(client) = self.get_mut_inner() {
            let account_id = AccountId::from_hex(&account_id).unwrap();
            let mut result = Vec::new();
            for note_id in list_of_notes {
                match get_input_note_with_id_prefix(client, &note_id).await {
                    Ok(note_record) => result.push(note_record.id()),
                    Err(err) => return Err(JsValue::from_str(&err.to_string())),
                }
            }

            let consume_transaction_request = TransactionRequest::consume_notes(result);
            let consume_transaction_execution_result =
                client.new_transaction(account_id, consume_transaction_request).await.unwrap();
            let result = NewTransactionResult::new(
                consume_transaction_execution_result.executed_transaction().id().to_string(),
                consume_transaction_execution_result
                    .created_notes()
                    .iter()
                    .map(|note| note.id().to_string())
                    .collect(),
            );

            client.submit_transaction(consume_transaction_execution_result).await.unwrap();

            Ok(result)
        } else {
            Err(JsValue::from_str("Client not initialized"))
        }
    }

    pub async fn new_swap_transaction(
        &mut self,
        sender_account_id: String,
        offered_asset_faucet_id: String,
        offered_asset_amount: String,
        requested_asset_faucet_id: String,
        requested_asset_amount: String,
        note_type: String,
    ) -> Result<NewSwapTransactionResult, JsValue> {
        if let Some(client) = self.get_mut_inner() {
            let sender_account_id = AccountId::from_hex(&sender_account_id).unwrap();

            let offered_asset_faucet_id = AccountId::from_hex(&offered_asset_faucet_id).unwrap();
            let offered_asset_amount_as_u64: u64 =
                offered_asset_amount.parse::<u64>().map_err(|err| err.to_string())?;
            let offered_fungible_asset =
                FungibleAsset::new(offered_asset_faucet_id, offered_asset_amount_as_u64)
                    .map_err(|err| err.to_string())?
                    .into();

            let requested_asset_faucet_id =
                AccountId::from_hex(&requested_asset_faucet_id).unwrap();
            let requested_asset_amount_as_u64: u64 =
                requested_asset_amount.parse::<u64>().map_err(|err| err.to_string())?;
            let requested_fungible_asset =
                FungibleAsset::new(requested_asset_faucet_id, requested_asset_amount_as_u64)
                    .map_err(|err| err.to_string())?
                    .into();

            let note_type = match note_type.as_str() {
                "Public" => MidenNoteType::Public,
                "Private" => MidenNoteType::Private,
                _ => MidenNoteType::Private,
            };

            let swap_transaction = SwapTransactionData::new(
                sender_account_id,
                offered_fungible_asset,
                requested_fungible_asset,
            );

            let swap_transaction_request =
                TransactionRequest::swap(swap_transaction.clone(), note_type, client.rng())
                    .unwrap();
            let swap_transaction_execution_result = client
                .new_transaction(sender_account_id, swap_transaction_request.clone())
                .await
                .unwrap();
            let mut result = NewSwapTransactionResult::new(
                swap_transaction_execution_result.executed_transaction().id().to_string(),
                swap_transaction_request
                    .expected_output_notes()
                    .map(|note| note.id().to_string())
                    .collect(),
                swap_transaction_request
                    .expected_future_notes()
                    .map(|note| note.id().to_string())
                    .collect(),
                None,
            );

            client.submit_transaction(swap_transaction_execution_result).await.unwrap();

            let payback_note_tag_u32: u32 = build_swap_tag(
                note_type,
                swap_transaction.offered_asset().faucet_id(),
                swap_transaction.requested_asset().faucet_id(),
            )
            .map_err(|err| err.to_string())?
            .into();

            result.set_note_tag(payback_note_tag_u32.to_string());

            Ok(result)
        } else {
            Err(JsValue::from_str("Client not initialized"))
        }
    }
}