Skip to content
Merged
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
26 changes: 26 additions & 0 deletions contracts/invoice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1629,6 +1629,32 @@ impl InvoiceContract {
Self::get_invoice(&env, invoice_id).discount_bps
}

/// Returns (status, face_value, discount_bps) in a single cross-contract call.
///
/// # Arguments
/// - `invoice_id` - The invoice to query.
///
/// # Auth
/// - None.
///
/// # Panics
/// - If the invoice does not exist (`InvoiceError::NotFound`).
///
/// # Returns
/// A tuple of `(invoice_status as u32, face_value, discount_bps)`.
pub fn get_funding_terms(env: Env, invoice_id: BytesN<32>) -> (u32, u128, u32) {
let invoice: Invoice = env
.storage()
.persistent()
.get(&DataKey::Invoice(invoice_id))
.unwrap_or_else(|| panic_with_error!(&env, InvoiceError::NotFound));
(
invoice.status as u32,
invoice.face_value,
invoice.discount_bps,
)
}

/// Returns the funding asset for an invoice.
///
/// # Arguments
Expand Down
22 changes: 22 additions & 0 deletions contracts/invoice/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,20 @@ fn test_get_funding_asset_returns_correct_asset() {
assert_eq!(asset, usdc);
}

#[test]
fn test_get_funding_terms_returns_correct_values() {
let (env, client, issuer, buyer, _, usdc) = setup();
let due_date = env.ledger().timestamp() + 86400;
let invoice_id = client.create(&issuer, &buyer, &1_000_000_000, &due_date, &usdc);
attest(&env, &client, &invoice_id);
client.list_for_financing(&invoice_id, &200);

let (status, face_value, discount_bps) = client.get_funding_terms(&invoice_id);
assert_eq!(status, 1, "status should be 1 (Listed)");
assert_eq!(face_value, 1_000_000_000);
assert_eq!(discount_bps, 200);
}

#[test]
fn test_expire_listing_succeeds_by_issuer() {
let (env, client, issuer, buyer, _, usdc) = setup();
Expand Down Expand Up @@ -2634,6 +2648,14 @@ fn test_create_fails_uninitialized_registry() {
client.create(&issuer, &buyer, &DEFAULT_FACE_VALUE, &due_date, &usdc);
}

#[test]
#[should_panic(expected = "Error(Contract, #2)")]
fn test_get_funding_terms_unknown_panics() {
let (env, client, _, _, _, _) = setup();
let fake_id = BytesN::from_array(&env, &[0u8; 32]);
client.get_funding_terms(&fake_id);
}

#[test]
#[should_panic(expected = "Error(Contract, #2)")]
fn test_create_fails_missing_counter() {
Expand Down
22 changes: 5 additions & 17 deletions contracts/pool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -534,8 +534,11 @@ impl PoolContract {

let mut args = Vec::new(&env);
args.push_back(invoice_id.clone().into_val(&env));
let invoice_status: u32 =
env.invoke_contract(&invoice_contract, &Symbol::new(&env, "get_status"), args);
let (invoice_status, face_value, discount_bps): (u32, u128, u32) = env.invoke_contract(
&invoice_contract,
&Symbol::new(&env, "get_funding_terms"),
args,
);
if invoice_status != 1 {
panic_with_error!(&env, PoolError::InvoiceNotListed);
}
Expand Down Expand Up @@ -582,21 +585,6 @@ impl PoolContract {
panic_with_error!(&env, PoolError::AssetMismatch);
}

let mut args = Vec::new(&env);
args.push_back(invoice_id.clone().into_val(&env));
let face_value: u128 = env.invoke_contract(
&invoice_contract,
&Symbol::new(&env, "get_face_value"),
args,
);
let mut args = Vec::new(&env);
args.push_back(invoice_id.clone().into_val(&env));
let discount_bps: u32 = env.invoke_contract(
&invoice_contract,
&Symbol::new(&env, "get_discount_bps"),
args,
);

// `face_value` is read from the invoice contract via a cross-contract
// call and is not bounded by this pool, so the scaling multiplication
// must be guarded just like the utilization check below (#585).
Expand Down
14 changes: 14 additions & 0 deletions contracts/pool/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,20 @@ impl MockHugeFaceInvoice {
1 // Listed
}

pub fn get_funding_terms(env: Env, _invoice_id: BytesN<32>) -> (u32, u128, u32) {
(
1, // Listed
env.storage()
.instance()
.get(&InvKey(Symbol::new(&env, "face")))
.unwrap(),
env.storage()
.instance()
.get(&InvKey(Symbol::new(&env, "disc")))
.unwrap(),
)
}

pub fn get_issuer(env: Env, _invoice_id: BytesN<32>) -> Address {
env.storage()
.instance()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4907,7 +4907,7 @@
"bytes": "0000000000000000000000000000000000000000000000000000000000000008"
},
{
"symbol": "get_status"
"symbol": "get_funding_terms"
}
],
"data": {
Expand All @@ -4930,11 +4930,24 @@
"symbol": "fn_return"
},
{
"symbol": "get_status"
"symbol": "get_funding_terms"
}
],
"data": {
"u32": 1
"vec": [
{
"u32": 1
},
{
"u128": {
"hi": 0,
"lo": 10000000000
}
},
{
"u32": 200
}
]
}
}
}
Expand Down Expand Up @@ -5186,107 +5199,6 @@
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "000000000000000000000000000000000000000000000000000000000000000a",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_call"
},
{
"bytes": "0000000000000000000000000000000000000000000000000000000000000008"
},
{
"symbol": "get_face_value"
}
],
"data": {
"bytes": "55e1249079d8b7d4ed5bdaf46f740a2e626d2bea6a824d555b95ed73c34b337d"
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000008",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_return"
},
{
"symbol": "get_face_value"
}
],
"data": {
"u128": {
"hi": 0,
"lo": 10000000000
}
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "000000000000000000000000000000000000000000000000000000000000000a",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_call"
},
{
"bytes": "0000000000000000000000000000000000000000000000000000000000000008"
},
{
"symbol": "get_discount_bps"
}
],
"data": {
"bytes": "55e1249079d8b7d4ed5bdaf46f740a2e626d2bea6a824d555b95ed73c34b337d"
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
"contract_id": "0000000000000000000000000000000000000000000000000000000000000008",
"type_": "diagnostic",
"body": {
"v0": {
"topics": [
{
"symbol": "fn_return"
},
{
"symbol": "get_discount_bps"
}
],
"data": {
"u32": 200
}
}
}
},
"failed_call": false
},
{
"event": {
"ext": "v0",
Expand Down
Loading