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 18, 2024
1 parent 48b6564 commit a019ea1
Show file tree
Hide file tree
Showing 2 changed files with 184 additions and 0 deletions.
174 changes: 174 additions & 0 deletions tests/transfers.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
pub mod utils;

use rgb::{
interface::{AmountChange, IfaceOp},
XOutputSeal,
};
use utils::*;

type TT = TransferType;
Expand Down Expand Up @@ -767,3 +771,173 @@ fn tapret_wlt_receiving_opret() {
1000,
);
}

fn print_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);
}
}

#[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())
.unwrap();
print_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())
.unwrap();
println!("History 1:");
print_history(&history);

assert_eq!(
history.values().next().unwrap().state_change,
AmountChange::Dec(Amount::from(200 as 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())
.unwrap();
println!("History 2:");
print_history(&history);
assert_eq!(
history.values().next().unwrap().state_change,
AmountChange::Inc(Amount::from(200 as u64))
);

println!("Balance 2:");
wlt_2.debug_logs(contract_id, &iface_type_name.clone());
}

#[test]
fn check_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())
.unwrap();

print_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())
.unwrap();
println!("Final History:");
print_history(&history);
assert!(history.len() > 0);

println!("Balance:");
let balance = wlt.debug_logs(contract_id, &iface_type_name.clone());
println!("{:?}", balance);
}
10 changes: 10 additions & 0 deletions tests/utils/helpers.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use rgb::{interface::{AmountChange, IfaceOp, IfaceRef}, WalletError};

use super::*;

pub struct TestWallet {
Expand Down Expand Up @@ -835,6 +837,14 @@ impl TestWallet {
.collect()
}

pub fn fungible_history(
&self,
contract_id: ContractId,
iface: impl Into<IfaceRef>,
) -> Result<HashMap<XWitnessId, IfaceOp<AmountChange>>, WalletError> {
return self.wallet.fungible_history(contract_id, iface);
}

pub fn debug_logs(&self, contract_id: ContractId, iface_type_name: &TypeName) {
let contract = self.contract_iface(contract_id, iface_type_name);

Expand Down

0 comments on commit a019ea1

Please sign in to comment.