Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions contracts/split/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15276,6 +15276,36 @@ impl SplitContract {
Self::_validate_parent(env, grandparent_id, depth + 1);
}
}

/// Get the creator address for an invoice.
pub fn get_invoice_creator(env: Env, invoice_id: u64) -> Address {
let invoice = load_invoice(&env, invoice_id);
invoice.creator
}

/// Get the list of recipient addresses for an invoice.
pub fn get_invoice_recipients(env: Env, invoice_id: u64) -> Vec<Address> {
let invoice = load_invoice(&env, invoice_id);
invoice.recipients
}

/// Get the number of payments made toward an invoice.
pub fn get_invoice_payment_count(env: Env, invoice_id: u64) -> u32 {
let invoice = load_invoice(&env, invoice_id);
invoice.payments.len() as u32
}

/// Get the funding percentage of an invoice as basis points.
/// Returns (funded * 10_000 / total) as u32, or 0 if total is 0.
pub fn get_invoice_funding_percentage(env: Env, invoice_id: u64) -> u32 {
let invoice = load_invoice(&env, invoice_id);
let total: i128 = invoice.amounts.iter().sum();
if total == 0 {
0
} else {
((invoice.funded * 10_000) / total) as u32
}
}
}

/// Move a finalised invoice from hot storage to cold archival storage.
Expand Down
98 changes: 98 additions & 0 deletions contracts/split/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8017,3 +8017,101 @@ fn test_cancel_invoice_on_deleted_invoice_panics() {
c.delete_invoice(&creator, &id);
c.cancel_invoice(&creator, &id);
}

#[test]
fn test_get_invoice_creator() {
let (env, contract_id, token_id) = setup_initialized();
let c = client(&env, &contract_id);

let creator = Address::generate(&env);
let recipient = Address::generate(&env);

env.ledger().set_timestamp(1_000);

let id = make_invoice(&env, &c, &creator, &recipient, 100, &token_id, 2_000);
assert_eq!(c.get_invoice_creator(&id), creator);
}

#[test]
fn test_get_invoice_recipients() {
let (env, contract_id, token_id) = setup_initialized();
let c = client(&env, &contract_id);

let creator = Address::generate(&env);
let recipient1 = Address::generate(&env);
let recipient2 = Address::generate(&env);

env.ledger().set_timestamp(1_000);

let mut recipients = Vec::new(&env);
recipients.push_back(recipient1.clone());
recipients.push_back(recipient2.clone());
let mut amounts = Vec::new(&env);
amounts.push_back(100);
amounts.push_back(200);

let id = c.create_invoice(&creator, &recipients, &amounts, &token_id, &2_000, &default_options(&env));

let retrieved_recipients = c.get_invoice_recipients(&id);
assert_eq!(retrieved_recipients.len(), 2);
assert_eq!(retrieved_recipients.get(0), recipient1);
assert_eq!(retrieved_recipients.get(1), recipient2);
}

#[test]
fn test_get_invoice_payment_count() {
let (env, contract_id, token_id) = setup_initialized();
let c = client(&env, &contract_id);
let tk = token_client(&env, &token_id);

let creator = Address::generate(&env);
let payer1 = Address::generate(&env);
let payer2 = Address::generate(&env);
let recipient = Address::generate(&env);

StellarAssetClient::new(&env, &token_id).mint(&payer1, &500);
StellarAssetClient::new(&env, &token_id).mint(&payer2, &500);

env.ledger().set_timestamp(1_000);

let id = make_invoice(&env, &c, &creator, &recipient, 200, &token_id, 9_999);

// Fresh invoice should have 0 payments
assert_eq!(c.get_invoice_payment_count(&id), 0);

// After first payment, count should be 1
c.pay(&payer1, &id, &100_i128, &0_u64, &false, &false, &None);
assert_eq!(c.get_invoice_payment_count(&id), 1);

// After second payment, count should be 2
c.pay(&payer2, &id, &100_i128, &0_u64, &false, &false, &None);
assert_eq!(c.get_invoice_payment_count(&id), 2);
}

#[test]
fn test_get_invoice_funding_percentage() {
let (env, contract_id, token_id) = setup_initialized();
let c = client(&env, &contract_id);

let creator = Address::generate(&env);
let payer = Address::generate(&env);
let recipient = Address::generate(&env);

StellarAssetClient::new(&env, &token_id).mint(&payer, &1000);

env.ledger().set_timestamp(1_000);

let id = make_invoice(&env, &c, &creator, &recipient, 100, &token_id, 9_999);

// 0% funded
assert_eq!(c.get_invoice_funding_percentage(&id), 0);

// 50% funded
c.pay(&payer, &id, &50_i128, &0_u64, &false, &false, &None);
assert_eq!(c.get_invoice_funding_percentage(&id), 5000);

// Create new invoice for 100% test
let id2 = make_invoice(&env, &c, &creator, &recipient, 200, &token_id, 9_999);
c.pay(&payer, &id2, &200_i128, &0_u64, &false, &false, &None);
assert_eq!(c.get_invoice_funding_percentage(&id2), 10000);
}