use miden_client::note::{
NoteConsumability as NativeNoteConsumability,
NoteConsumptionStatus as NativeNoteConsumptionStatus,
};
use miden_client::store::InputNoteRecord as NativeInputNoteRecord;
use wasm_bindgen::prelude::*;
use super::account_id::AccountId;
use super::input_note_record::InputNoteRecord;
#[derive(Clone)]
#[wasm_bindgen]
pub struct NoteConsumptionStatus(NativeNoteConsumptionStatus);
#[wasm_bindgen]
impl NoteConsumptionStatus {
#[wasm_bindgen(js_name = "consumable")]
pub fn consumable() -> Self {
Self(NativeNoteConsumptionStatus::Consumable)
}
#[wasm_bindgen(js_name = "consumableWithAuthorization")]
pub fn consumable_with_authorization() -> Self {
Self(NativeNoteConsumptionStatus::ConsumableWithAuthorization)
}
#[wasm_bindgen(js_name = "consumableAfter")]
pub fn consumable_after(block_height: u32) -> Self {
Self(NativeNoteConsumptionStatus::ConsumableAfter(block_height.into()))
}
#[wasm_bindgen(js_name = "neverConsumable")]
pub fn never_consumable(err: String) -> Self {
Self(NativeNoteConsumptionStatus::NeverConsumable(err.into()))
}
#[wasm_bindgen(js_name = "unconsumableConditions")]
pub fn unconsumable_conditions() -> Self {
Self(NativeNoteConsumptionStatus::UnconsumableConditions)
}
#[wasm_bindgen(js_name = "consumableAfterBlock")]
pub fn consumable_after_block(&self) -> Option<u32> {
match self.0 {
NativeNoteConsumptionStatus::ConsumableAfter(block_height) => {
Some(block_height.as_u32())
},
_ => None,
}
}
}
impl From<NativeNoteConsumptionStatus> for NoteConsumptionStatus {
fn from(native_note_consumption_status: NativeNoteConsumptionStatus) -> Self {
NoteConsumptionStatus(native_note_consumption_status)
}
}
#[derive(Clone)]
#[wasm_bindgen]
pub struct ConsumableNoteRecord {
input_note_record: InputNoteRecord,
note_consumability: Vec<NoteConsumability>,
}
#[derive(Clone)]
#[wasm_bindgen]
pub struct NoteConsumability {
account_id: AccountId,
consumption_status: NoteConsumptionStatus,
}
#[wasm_bindgen]
impl NoteConsumability {
pub(crate) fn new(
account_id: AccountId,
consumption_status: NoteConsumptionStatus,
) -> NoteConsumability {
NoteConsumability { account_id, consumption_status }
}
#[wasm_bindgen(js_name = "accountId")]
pub fn account_id(&self) -> AccountId {
self.account_id
}
#[wasm_bindgen(js_name = "consumptionStatus")]
pub fn consumption_status(&self) -> NoteConsumptionStatus {
self.consumption_status.clone()
}
}
#[wasm_bindgen]
impl ConsumableNoteRecord {
#[wasm_bindgen(constructor)]
pub fn new(
input_note_record: InputNoteRecord,
note_consumability: Vec<NoteConsumability>,
) -> ConsumableNoteRecord {
ConsumableNoteRecord { input_note_record, note_consumability }
}
#[wasm_bindgen(js_name = "inputNoteRecord")]
pub fn input_note_record(&self) -> InputNoteRecord {
self.input_note_record.clone()
}
#[wasm_bindgen(js_name = "noteConsumability")]
pub fn note_consumability(&self) -> Vec<NoteConsumability> {
self.note_consumability.clone()
}
}
impl From<(NativeInputNoteRecord, Vec<NativeNoteConsumability>)> for ConsumableNoteRecord {
fn from(
(input_note_record, note_consumability): (
NativeInputNoteRecord,
Vec<NativeNoteConsumability>,
),
) -> Self {
ConsumableNoteRecord::new(
input_note_record.into(),
note_consumability.into_iter().map(Into::into).collect(),
)
}
}
impl From<NativeNoteConsumability> for NoteConsumability {
fn from(note_consumability: NativeNoteConsumability) -> Self {
NoteConsumability::new(note_consumability.0.into(), note_consumability.1.into())
}
}