From 4b03cc39cf076782bfea53c23b53d9a102e6465b Mon Sep 17 00:00:00 2001 From: k0k0ne Date: Wed, 18 Sep 2024 12:07:47 +0800 Subject: [PATCH] feat: add check_fungible_history check_send_to_oneself --- tests/transfers.rs | 130 +++++++++++++++++++++++++++++++++++++++++ tests/utils/helpers.rs | 36 ++++++++++++ tests/utils/mod.rs | 4 +- 3 files changed, 169 insertions(+), 1 deletion(-) diff --git a/tests/transfers.rs b/tests/transfers.rs index 083b3c5..e999efa 100644 --- a/tests/transfers.rs +++ b/tests/transfers.rs @@ -767,3 +767,133 @@ fn tapret_wlt_receiving_opret() { 1000, ); } + +#[test] +fn check_fungible_history() { + initialize(); + + let mut wlt_1 = get_wallet(&DescriptorType::Wpkh); + let mut wlt_2 = get_wallet(&DescriptorType::Wpkh); + + println!("Wallets created: wlt_1 and wlt_2"); + + let amount = 100000; + println!("Initial amount: {}", amount); + + let (contract_id, iface_type_name) = wlt_1.issue_nia(amount, wlt_1.close_method(), None); + println!( + "Contract issued - ID: {:?}, Interface type: {}", + contract_id, iface_type_name + ); + + let initial_height = get_height(); + println!("Initial height: {}", initial_height); + + let history = wlt_1.fungible_history(contract_id, iface_type_name.clone()); + TestWallet::debug_fungible_history(&history); + assert_eq!(history.len(), 0); + + let amount_transfer = 200; + println!("Transfer amount: {}", amount_transfer); + + let invoice = wlt_2.invoice( + contract_id, + &iface_type_name, + amount_transfer, + wlt_2.close_method(), + InvoiceType::Witness, + ); + println!("Invoice created: {:?}", invoice); + + let (consignment, _) = wlt_1.transfer(invoice.clone(), None, Some(1234)); + + let height = get_height(); + println!("Height: {}", height); + + wlt_2.accept_transfer(consignment); + + mine(false); + wlt_1.sync(); + wlt_2.sync(); + + let height = get_height(); + println!("Height: {}", height); + + let history = wlt_1.fungible_history(contract_id, iface_type_name.clone()); + println!("History 1:"); + TestWallet::debug_fungible_history(&history); + + assert_eq!( + history.values().next().unwrap().state_change, + AmountChange::Dec(Amount::from(200_u64)) + ); + + println!("Balance 1:"); + wlt_1.debug_logs(contract_id, &iface_type_name.clone()); + + let history = wlt_2.fungible_history(contract_id, iface_type_name.clone()); + println!("History 2:"); + TestWallet::debug_fungible_history(&history); + assert_eq!( + history.values().next().unwrap().state_change, + AmountChange::Inc(Amount::from(200_u64)) + ); + + println!("Balance 2:"); + wlt_2.debug_logs(contract_id, &iface_type_name.clone()); +} + +#[test] +fn send_to_oneself() { + println!("Starting test: self_transfer_example"); + initialize(); + + let mut wlt = get_wallet(&DescriptorType::Wpkh); + println!("Wallet created: wlt"); + + let initial_amount = 100000; + println!("Initial amount: {}", initial_amount); + + let (contract_id, iface_type_name) = wlt.issue_nia(initial_amount, wlt.close_method(), None); + println!( + "Contract issued - ID: {:?}, Interface type: {}", + contract_id, iface_type_name + ); + + let history = wlt.fungible_history(contract_id, iface_type_name.clone()); + + TestWallet::debug_fungible_history(&history); + + let amount_transfer = 300; // Amount to transfer to self + println!("Transfer amount: {}", amount_transfer); + + let invoice = wlt.invoice( + contract_id, + &iface_type_name, + amount_transfer, + wlt.close_method(), + InvoiceType::Witness, + ); + println!("Invoice created: {:?}", invoice); + + let (consignment, _) = wlt.transfer(invoice.clone(), None, None); + + let height = get_height(); + println!("Height: {}", height); + + wlt.accept_transfer(consignment); // Accepting transfer to self + + mine(false); + wlt.sync(); + + let height = get_height(); + println!("Height after sync: {}", height); + + let history = wlt.fungible_history(contract_id, iface_type_name.clone()); + println!("Final History:"); + TestWallet::debug_fungible_history(&history); + assert!(!history.is_empty()); + + println!("Balance:"); + wlt.debug_logs(contract_id, &iface_type_name.clone()); +} diff --git a/tests/utils/helpers.rs b/tests/utils/helpers.rs index 9aaf6b1..a7c8734 100644 --- a/tests/utils/helpers.rs +++ b/tests/utils/helpers.rs @@ -835,6 +835,14 @@ impl TestWallet { .collect() } + pub fn fungible_history( + &self, + contract_id: ContractId, + iface: impl Into, + ) -> HashMap> { + self.wallet.fungible_history(contract_id, iface).unwrap() + } + pub fn debug_logs(&self, contract_id: ContractId, iface_type_name: &TypeName) { let contract = self.contract_iface(contract_id, iface_type_name); @@ -890,6 +898,34 @@ impl TestWallet { println!("\nWallet total balance: {} ṩ", bp_runtime.balance()); } + pub fn debug_fungible_history(history: &HashMap>) { + println!("Amount\tCounterparty\tWitness Id"); + for (id, op) in history { + let (amount, cparty, more) = match op.state_change { + AmountChange::Dec(amt) => ( + format!("-{}", amt.value()), + op.beneficiaries.first(), + op.beneficiaries.len().saturating_sub(1), + ), + AmountChange::Zero => continue, + AmountChange::Inc(amt) => ( + format!("{}", amt.value()), + op.payers.first(), + op.payers.len().saturating_sub(1), + ), + }; + let more = if more > 0 { + format!(" (+{more})") + } else { + s!("") + }; + let cparty = cparty + .map(XOutputSeal::to_string) + .unwrap_or_else(|| s!("none")); + println!("{},{}\t{}{}\t{}", amount, op.state_change, cparty, more, id); + } + } + pub fn send( &mut self, recv_wlt: &mut TestWallet, diff --git a/tests/utils/mod.rs b/tests/utils/mod.rs index ea34756..4727c0c 100644 --- a/tests/utils/mod.rs +++ b/tests/utils/mod.rs @@ -54,6 +54,7 @@ pub use psbt::{ pub use psrgbt::{RgbExt, RgbInExt, RgbPsbt, TxParams}; pub use rand::RngCore; pub use rgb::{ + interface::{AmountChange, IfaceOp, IfaceRef}, invoice::Pay2Vout, persistence::{ContractStateRead, MemContract, MemContractState, Stock}, resolvers::AnyResolver, @@ -61,7 +62,8 @@ pub use rgb::{ validation::{Failure, ResolveWitness, Scripts, Validity, WitnessResolverError}, vm::{WitnessOrd, WitnessPos, XWitnessTx}, BlindingFactor, DescriptorRgb, GenesisSeal, GraphSeal, Identity, RgbDescr, RgbKeychain, - RgbWallet, TapretKey, TransferParams, Transition, WalletProvider, XOutpoint, XWitnessId, + RgbWallet, TapretKey, TransferParams, Transition, WalletProvider, XOutpoint, XOutputSeal, + XWitnessId, }; pub use rgbstd::{ containers::{BuilderSeal, ConsignmentExt, Fascia, FileContent, Kit, Transfer, ValidKit},