@@ -67,7 +67,8 @@ pub use wallet::{
|
|||||||
get_wallet_for_mut, GlobalWallet, Wallet, WalletError, WalletType, WalletUIState,
|
get_wallet_for_mut, GlobalWallet, Wallet, WalletError, WalletType, WalletUIState,
|
||||||
};
|
};
|
||||||
pub use zaps::{
|
pub use zaps::{
|
||||||
AnyZapState, NoteZapTarget, NoteZapTargetOwned, ZapTarget, ZapTargetOwned, ZappingError,
|
AnyZapState, DefaultZapError, DefaultZapMsats, NoteZapTarget, NoteZapTargetOwned,
|
||||||
|
PendingDefaultZapState, ZapTarget, ZapTargetOwned, ZappingError,
|
||||||
};
|
};
|
||||||
|
|
||||||
// export libs
|
// export libs
|
||||||
|
|||||||
104
crates/notedeck/src/zaps/default_zap.rs
Normal file
104
crates/notedeck/src/zaps/default_zap.rs
Normal file
@@ -0,0 +1,104 @@
|
|||||||
|
use tokenator::{ParseError, TokenParser, TokenSerializable};
|
||||||
|
|
||||||
|
const DEFAULT_ZAP_MSATS: u64 = 10_000;
|
||||||
|
|
||||||
|
#[derive(Debug, Default)]
|
||||||
|
pub struct DefaultZapMsats {
|
||||||
|
pub msats: Option<u64>,
|
||||||
|
pub pending: PendingDefaultZapState,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DefaultZapMsats {
|
||||||
|
pub fn from_user(value: Option<UserZapMsats>) -> Self {
|
||||||
|
let mut obj = match value {
|
||||||
|
Some(user_msats) => {
|
||||||
|
let mut val = DefaultZapMsats::default();
|
||||||
|
val.set_user_selection(user_msats.msats);
|
||||||
|
val
|
||||||
|
}
|
||||||
|
None => DefaultZapMsats::default(),
|
||||||
|
};
|
||||||
|
|
||||||
|
obj.pending.write_msats(obj.get_default_zap_msats());
|
||||||
|
obj
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_user_selection(&mut self, msats: u64) {
|
||||||
|
self.msats = Some(msats);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_default_zap_msats(&self) -> u64 {
|
||||||
|
let Some(default_zap_msats) = self.msats else {
|
||||||
|
return DEFAULT_ZAP_MSATS;
|
||||||
|
};
|
||||||
|
|
||||||
|
default_zap_msats
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn has_user_selection(&self) -> bool {
|
||||||
|
self.msats.is_some()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn try_into_user(&self) -> Option<UserZapMsats> {
|
||||||
|
let user_zap_amount = self.msats?;
|
||||||
|
|
||||||
|
Some(UserZapMsats {
|
||||||
|
msats: user_zap_amount,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct UserZapMsats {
|
||||||
|
pub msats: u64,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl TokenSerializable for UserZapMsats {
|
||||||
|
fn parse_from_tokens<'a>(parser: &mut TokenParser<'a>) -> Result<Self, ParseError<'a>> {
|
||||||
|
parser.parse_token("default_zap")?;
|
||||||
|
|
||||||
|
let msats: u64 = parser
|
||||||
|
.pull_token()?
|
||||||
|
.parse()
|
||||||
|
.map_err(|_| ParseError::DecodeFailed)?;
|
||||||
|
|
||||||
|
Ok(UserZapMsats { msats })
|
||||||
|
}
|
||||||
|
|
||||||
|
fn serialize_tokens(&self, writer: &mut tokenator::TokenWriter) {
|
||||||
|
writer.write_token("default_zap");
|
||||||
|
writer.write_token(&self.msats.to_string());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct PendingDefaultZapState {
|
||||||
|
pub amount_sats: String,
|
||||||
|
pub error_message: Option<DefaultZapError>,
|
||||||
|
pub is_rewriting: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for PendingDefaultZapState {
|
||||||
|
fn default() -> Self {
|
||||||
|
Self {
|
||||||
|
amount_sats: msats_to_sats_string(DEFAULT_ZAP_MSATS),
|
||||||
|
error_message: Default::default(),
|
||||||
|
is_rewriting: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PendingDefaultZapState {
|
||||||
|
pub fn write_msats(&mut self, msats: u64) {
|
||||||
|
self.amount_sats = msats_to_sats_string(msats);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn msats_to_sats_string(msats: u64) -> String {
|
||||||
|
(msats / 1000).to_string()
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub enum DefaultZapError {
|
||||||
|
InvalidUserInput,
|
||||||
|
}
|
||||||
@@ -1,7 +1,10 @@
|
|||||||
mod cache;
|
mod cache;
|
||||||
|
mod default_zap;
|
||||||
mod networking;
|
mod networking;
|
||||||
mod zap;
|
mod zap;
|
||||||
|
|
||||||
pub use cache::{
|
pub use cache::{
|
||||||
AnyZapState, NoteZapTarget, NoteZapTargetOwned, ZapTarget, ZapTargetOwned, ZappingError, Zaps,
|
AnyZapState, NoteZapTarget, NoteZapTargetOwned, ZapTarget, ZapTargetOwned, ZappingError, Zaps,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub use default_zap::{DefaultZapError, DefaultZapMsats, PendingDefaultZapState};
|
||||||
|
|||||||
Reference in New Issue
Block a user