use js_export_macro::js_export;
use miden_client::pswap::{
PswapLineageRecord as NativePswapLineageRecord,
PswapLineageState as NativePswapLineageState,
};
use super::account_id::AccountId;
use super::note_id::NoteId;
use crate::platform::u64_to_js_u64;
#[js_export]
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[repr(u8)]
pub enum PswapLineageState {
Active = 0,
FullyFilled = 1,
Reclaimed = 2,
}
#[derive(Clone)]
#[js_export]
pub struct PswapLineageRecord(NativePswapLineageRecord);
#[js_export]
impl PswapLineageRecord {
#[js_export(js_name = "orderId")]
pub fn order_id(&self) -> String {
self.0.order_id().as_canonical_u64().to_string()
}
#[js_export(js_name = "creatorAccountId")]
pub fn creator_account_id(&self) -> AccountId {
self.0.creator_account_id().into()
}
#[js_export(js_name = "remainingOffered")]
pub fn remaining_offered(&self) -> JsU64 {
u64_to_js_u64(self.0.remaining_offered.as_u64())
}
#[js_export(js_name = "remainingRequested")]
pub fn remaining_requested(&self) -> JsU64 {
u64_to_js_u64(self.0.remaining_requested.as_u64())
}
#[js_export(js_name = "currentDepth")]
pub fn current_depth(&self) -> u32 {
self.0.current_depth
}
#[js_export(js_name = "currentTipNoteId")]
pub fn current_tip_note_id(&self) -> NoteId {
self.0.current_tip_note_id.into()
}
pub fn state(&self) -> PswapLineageState {
self.0.state.into()
}
}
impl From<NativePswapLineageState> for PswapLineageState {
fn from(value: NativePswapLineageState) -> Self {
match value {
NativePswapLineageState::Active => PswapLineageState::Active,
NativePswapLineageState::FullyFilled => PswapLineageState::FullyFilled,
NativePswapLineageState::Reclaimed => PswapLineageState::Reclaimed,
}
}
}
impl From<NativePswapLineageRecord> for PswapLineageRecord {
fn from(record: NativePswapLineageRecord) -> Self {
PswapLineageRecord(record)
}
}
impl_napi_from_value!(PswapLineageRecord);