use alloc::borrow::Cow;
use alloc::vec::Vec;
use serde::{Deserialize, Serialize};
use serde_with::skip_serializing_none;
use crate::models::amount::XRPAmount;
use crate::models::{
transactions::{Memo, Signer, Transaction, TransactionType},
Model, ValidateCurrencies,
};
use crate::models::{FlagCollection, NoFlags};
use super::{CommonFields, CommonTransactionBuilder};
#[skip_serializing_none]
#[derive(
Debug,
Default,
Serialize,
Deserialize,
PartialEq,
Eq,
Clone,
xrpl_rust_macros::ValidateCurrencies,
)]
#[serde(rename_all = "PascalCase")]
pub struct PaymentChannelCreate<'a> {
#[serde(flatten)]
pub common_fields: CommonFields<'a, NoFlags>,
pub amount: XRPAmount<'a>,
pub destination: Cow<'a, str>,
pub settle_delay: u32,
pub public_key: Cow<'a, str>,
pub cancel_after: Option<u32>,
pub destination_tag: Option<u32>,
}
impl<'a> Model for PaymentChannelCreate<'a> {
fn get_errors(&self) -> crate::models::XRPLModelResult<()> {
self.validate_currencies()
}
}
impl<'a> Transaction<'a, NoFlags> for PaymentChannelCreate<'a> {
fn get_transaction_type(&self) -> &TransactionType {
self.common_fields.get_transaction_type()
}
fn get_common_fields(&self) -> &CommonFields<'_, NoFlags> {
self.common_fields.get_common_fields()
}
fn get_mut_common_fields(&mut self) -> &mut CommonFields<'a, NoFlags> {
self.common_fields.get_mut_common_fields()
}
}
impl<'a> CommonTransactionBuilder<'a, NoFlags> for PaymentChannelCreate<'a> {
fn get_mut_common_fields(&mut self) -> &mut CommonFields<'a, NoFlags> {
&mut self.common_fields
}
fn into_self(self) -> Self {
self
}
}
impl<'a> PaymentChannelCreate<'a> {
pub fn new(
account: Cow<'a, str>,
account_txn_id: Option<Cow<'a, str>>,
fee: Option<XRPAmount<'a>>,
last_ledger_sequence: Option<u32>,
memos: Option<Vec<Memo>>,
sequence: Option<u32>,
signers: Option<Vec<Signer>>,
source_tag: Option<u32>,
ticket_sequence: Option<u32>,
amount: XRPAmount<'a>,
destination: Cow<'a, str>,
public_key: Cow<'a, str>,
settle_delay: u32,
cancel_after: Option<u32>,
destination_tag: Option<u32>,
) -> Self {
Self {
common_fields: CommonFields::new(
account,
TransactionType::PaymentChannelCreate,
account_txn_id,
fee,
Some(FlagCollection::default()),
last_ledger_sequence,
memos,
None,
sequence,
signers,
None,
source_tag,
ticket_sequence,
None,
),
amount,
destination,
settle_delay,
public_key,
cancel_after,
destination_tag,
}
}
pub fn with_cancel_after(mut self, cancel_after: u32) -> Self {
self.cancel_after = Some(cancel_after);
self
}
pub fn with_destination_tag(mut self, destination_tag: u32) -> Self {
self.destination_tag = Some(destination_tag);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_serde() {
let default_txn = PaymentChannelCreate {
common_fields: CommonFields {
account: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn".into(),
transaction_type: TransactionType::PaymentChannelCreate,
signing_pub_key: Some("".into()),
source_tag: Some(11747),
..Default::default()
},
amount: XRPAmount::from("10000"),
destination: "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW".into(),
settle_delay: 86400,
public_key: "32D2471DB72B27E3310F355BB33E339BF26F8392D5A93D3BC0FC3B566612DA0F0A".into(),
cancel_after: Some(533171558),
destination_tag: Some(23480),
};
let default_json_str = r#"{"Account":"rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn","TransactionType":"PaymentChannelCreate","Flags":0,"SigningPubKey":"","SourceTag":11747,"Amount":"10000","Destination":"rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW","SettleDelay":86400,"PublicKey":"32D2471DB72B27E3310F355BB33E339BF26F8392D5A93D3BC0FC3B566612DA0F0A","CancelAfter":533171558,"DestinationTag":23480}"#;
let default_json_value = serde_json::to_value(default_json_str).unwrap();
let serialized_string = serde_json::to_string(&default_txn).unwrap();
let serialized_value = serde_json::to_value(&serialized_string).unwrap();
assert_eq!(serialized_value, default_json_value);
let deserialized: PaymentChannelCreate = serde_json::from_str(default_json_str).unwrap();
assert_eq!(default_txn, deserialized);
}
#[test]
fn test_builder_pattern() {
let payment_channel_create = PaymentChannelCreate {
common_fields: CommonFields {
account: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn".into(),
transaction_type: TransactionType::PaymentChannelCreate,
..Default::default()
},
amount: XRPAmount::from("10000"),
destination: "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW".into(),
settle_delay: 86400,
public_key: "32D2471DB72B27E3310F355BB33E339BF26F8392D5A93D3BC0FC3B566612DA0F0A".into(),
..Default::default()
}
.with_cancel_after(533171558)
.with_destination_tag(23480)
.with_fee("12".into())
.with_sequence(123)
.with_last_ledger_sequence(7108682)
.with_source_tag(11747)
.with_memo(Memo {
memo_data: Some("creating payment channel".into()),
memo_format: None,
memo_type: Some("text".into()),
});
assert_eq!(payment_channel_create.amount.0, "10000");
assert_eq!(
payment_channel_create.destination,
"rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW"
);
assert_eq!(payment_channel_create.settle_delay, 86400);
assert_eq!(
payment_channel_create.public_key,
"32D2471DB72B27E3310F355BB33E339BF26F8392D5A93D3BC0FC3B566612DA0F0A"
);
assert_eq!(payment_channel_create.cancel_after, Some(533171558));
assert_eq!(payment_channel_create.destination_tag, Some(23480));
assert_eq!(
payment_channel_create.common_fields.fee.as_ref().unwrap().0,
"12"
);
assert_eq!(payment_channel_create.common_fields.sequence, Some(123));
assert_eq!(
payment_channel_create.common_fields.last_ledger_sequence,
Some(7108682)
);
assert_eq!(payment_channel_create.common_fields.source_tag, Some(11747));
assert_eq!(
payment_channel_create
.common_fields
.memos
.as_ref()
.unwrap()
.len(),
1
);
}
#[test]
fn test_default() {
let payment_channel_create = PaymentChannelCreate {
common_fields: CommonFields {
account: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn".into(),
transaction_type: TransactionType::PaymentChannelCreate,
..Default::default()
},
amount: XRPAmount::from("10000"),
destination: "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW".into(),
settle_delay: 86400,
public_key: "32D2471DB72B27E3310F355BB33E339BF26F8392D5A93D3BC0FC3B566612DA0F0A".into(),
..Default::default()
};
assert_eq!(
payment_channel_create.common_fields.account,
"rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn"
);
assert_eq!(
payment_channel_create.common_fields.transaction_type,
TransactionType::PaymentChannelCreate
);
assert_eq!(payment_channel_create.amount.0, "10000");
assert_eq!(
payment_channel_create.destination,
"rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW"
);
assert_eq!(payment_channel_create.settle_delay, 86400);
assert!(payment_channel_create.cancel_after.is_none());
assert!(payment_channel_create.destination_tag.is_none());
}
#[test]
fn test_without_optional_fields() {
let payment_channel_create = PaymentChannelCreate {
common_fields: CommonFields {
account: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn".into(),
transaction_type: TransactionType::PaymentChannelCreate,
fee: Some("12".into()),
sequence: Some(123),
..Default::default()
},
amount: XRPAmount::from("5000"),
destination: "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW".into(),
settle_delay: 3600, public_key: "32D2471DB72B27E3310F355BB33E339BF26F8392D5A93D3BC0FC3B566612DA0F0A".into(),
cancel_after: None,
destination_tag: None,
};
assert_eq!(payment_channel_create.amount.0, "5000");
assert_eq!(payment_channel_create.settle_delay, 3600);
assert!(payment_channel_create.cancel_after.is_none());
assert!(payment_channel_create.destination_tag.is_none());
assert_eq!(
payment_channel_create.common_fields.fee.as_ref().unwrap().0,
"12"
);
assert_eq!(payment_channel_create.common_fields.sequence, Some(123));
}
#[test]
fn test_ticket_sequence() {
let payment_channel_create = PaymentChannelCreate {
common_fields: CommonFields {
account: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn".into(),
transaction_type: TransactionType::PaymentChannelCreate,
..Default::default()
},
amount: XRPAmount::from("10000"),
destination: "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW".into(),
settle_delay: 86400,
public_key: "32D2471DB72B27E3310F355BB33E339BF26F8392D5A93D3BC0FC3B566612DA0F0A".into(),
..Default::default()
}
.with_ticket_sequence(456)
.with_fee("12".into());
assert_eq!(
payment_channel_create.common_fields.ticket_sequence,
Some(456)
);
assert_eq!(
payment_channel_create.common_fields.fee.as_ref().unwrap().0,
"12"
);
assert!(payment_channel_create.common_fields.sequence.is_none());
}
#[test]
fn test_long_lived_channel() {
let payment_channel_create = PaymentChannelCreate {
common_fields: CommonFields {
account: "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn".into(),
transaction_type: TransactionType::PaymentChannelCreate,
..Default::default()
},
amount: XRPAmount::from("100000000"), destination: "rsA2LpzuawewSBQXkiju3YQTMzW13pAAdW".into(),
settle_delay: 604800, public_key: "32D2471DB72B27E3310F355BB33E339BF26F8392D5A93D3BC0FC3B566612DA0F0A".into(),
..Default::default()
}
.with_cancel_after(1893456000) .with_destination_tag(98765)
.with_fee("15".into());
assert_eq!(payment_channel_create.amount.0, "100000000");
assert_eq!(payment_channel_create.settle_delay, 604800);
assert_eq!(payment_channel_create.cancel_after, Some(1893456000));
assert_eq!(payment_channel_create.destination_tag, Some(98765));
}
}