Skip to content

Commit

Permalink
feat: add check_fungible_history check_send_to_oneself
Browse files Browse the repository at this point in the history
  • Loading branch information
k0k0ne committed Sep 19, 2024
1 parent 48b6564 commit 4b03cc3
Show file tree
Hide file tree
Showing 3 changed files with 169 additions and 1 deletion.
130 changes: 130 additions & 0 deletions tests/transfers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
36 changes: 36 additions & 0 deletions tests/utils/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -835,6 +835,14 @@ impl TestWallet {
.collect()
}

pub fn fungible_history(
&self,
contract_id: ContractId,
iface: impl Into<IfaceRef>,
) -> HashMap<XWitnessId, IfaceOp<AmountChange>> {
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);

Expand Down Expand Up @@ -890,6 +898,34 @@ impl TestWallet {
println!("\nWallet total balance: {} ṩ", bp_runtime.balance());
}

pub fn debug_fungible_history(history: &HashMap<XWitnessId, IfaceOp<AmountChange>>) {
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,
Expand Down
4 changes: 3 additions & 1 deletion tests/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,16 @@ 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,
stl::ContractTerms,
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},
Expand Down

0 comments on commit 4b03cc3

Please sign in to comment.