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
34 changes: 34 additions & 0 deletions contracts/split/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4032,6 +4032,40 @@ impl SplitContract {
}
}

pub fn get_invoice_deadline(env: Env, invoice_id: u64) -> Result<u64, ContractError> {
if let Some(core) = env.storage().persistent().get(&invoice_key(invoice_id)) {
Ok(core.deadline)
} else if let Some(core) = env.storage().instance().get(&invoice_key(invoice_id)) {
Ok(core.deadline)
} else {
Err(ContractError::InvoiceNotFound)
}
}

pub fn get_invoice_funded(env: Env, invoice_id: u64) -> Result<i128, ContractError> {
if let Some(hot) = env.storage().instance().get(&invoice_hot_key(invoice_id)) {
Ok(hot.funded)
} else if let Some(core) = env.storage().persistent().get(&invoice_key(invoice_id)) {
Ok(core.funded)
} else if let Some(core) = env.storage().instance().get(&invoice_key(invoice_id)) {
Ok(core.funded)
} else {
Err(ContractError::InvoiceNotFound)
}
}

pub fn get_invoice_status(env: Env, invoice_id: u64) -> Result<InvoiceStatus, ContractError> {
if let Some(hot) = env.storage().instance().get(&invoice_hot_key(invoice_id)) {
Ok(hot.status)
} else if let Some(core) = env.storage().persistent().get(&invoice_key(invoice_id)) {
Ok(core.status)
} else if let Some(core) = env.storage().instance().get(&invoice_key(invoice_id)) {
Ok(core.status)
} else {
Err(ContractError::InvoiceNotFound)
}
}

/// Get a consolidated invoice snapshot for off-chain audit.
pub fn get_invoice_snapshot(env: Env, invoice_id: u64) -> types::InvoiceSnapshot {
let core: types::InvoiceCore = env
Expand Down
92 changes: 92 additions & 0 deletions contracts/split/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8017,3 +8017,95 @@ fn test_cancel_invoice_on_deleted_invoice_panics() {
c.delete_invoice(&creator, &id);
c.cancel_invoice(&creator, &id);
}

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

let creator = Address::generate(&env);
let recipient = Address::generate(&env);
let deadline: u64 = 5_000;

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

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

let returned_deadline = c.get_invoice_deadline(&id).expect("should return deadline");
assert_eq!(returned_deadline, deadline);
}

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

let result = c.try_get_invoice_deadline(&999);
assert!(result.is_err());
}

#[test]
fn test_get_invoice_funded() {
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 payer = Address::generate(&env);
let recipient = Address::generate(&env);

StellarAssetClient::new(&env, &token_id).mint(&payer, &500);
env.ledger().set_timestamp(1_000);

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

let funded_before = c.get_invoice_funded(&id).expect("should return funded");
assert_eq!(funded_before, 0);

c.pay(&payer, &id, &150_i128, &0_u64, &false, &false, &None);

let funded_after = c.get_invoice_funded(&id).expect("should return funded");
assert_eq!(funded_after, 150);
}

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

let result = c.try_get_invoice_funded(&999);
assert!(result.is_err());
}

#[test]
fn test_get_invoice_status() {
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 payer = Address::generate(&env);
let recipient = Address::generate(&env);

StellarAssetClient::new(&env, &token_id).mint(&payer, &500);
env.ledger().set_timestamp(1_000);

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

let status_pending = c.get_invoice_status(&id).expect("should return status");
assert_eq!(status_pending, InvoiceStatus::Pending);

c.pay(&payer, &id, &200_i128, &0_u64, &false, &false, &None);

let status_released = c.get_invoice_status(&id).expect("should return status");
assert_eq!(status_released, InvoiceStatus::Released);
}

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

let result = c.try_get_invoice_status(&999);
assert!(result.is_err());
}
24 changes: 23 additions & 1 deletion tests/event_log_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,28 @@ fn test_event_log_with_multiple_recipients() {

// Create invoice with 3 recipients
// Full fund and release

// Verify release event includes all 3 recipient addresses
}

#[test]
fn fee_paid_event_carries_amount_and_treasury() {
let env = Env::default();
env.mock_all_auths();

let treasury = Address::generate(&env);
let expected_amount: i128 = 500;

env.ledger().set_sequence(100);

split_contracts::events::fee_paid(&env, 1, expected_amount, &treasury);

let events = env.events().all();
assert!(!events.is_empty(), "should have at least one event");

let fee_paid_event = events.last().expect("should have fee_paid event");
let (topics, data): (Vec<Symbol>, (i128, Address, u32)) = fee_paid_event.parsed_data();

assert_eq!(data.0, expected_amount, "amount should match");
assert_eq!(data.1, treasury, "treasury should match");
}