From 9ed04e817446c3de16b80b34a42dd8f4bd2df8dc Mon Sep 17 00:00:00 2001 From: Dmytro Kozhevin Date: Fri, 30 Sep 2022 14:13:02 -0400 Subject: [PATCH 1/2] Respect trustline limit in `to_classic` conversions --- soroban-env-host/src/host.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/soroban-env-host/src/host.rs b/soroban-env-host/src/host.rs index 6e7407bf2..e8dfc81e1 100644 --- a/soroban-env-host/src/host.rs +++ b/soroban-env-host/src/host.rs @@ -1002,6 +1002,7 @@ impl Host { let max_balance = i64::MAX; (min_balance, max_balance) }; + let max_balance = min(max_balance, tl.limit); let new_balance = if amount <= 0 { tl.balance + amount From a7d4552ce7d40389eb624a32585371c0373aa5cd Mon Sep 17 00:00:00 2001 From: Dmytro Kozhevin Date: Mon, 3 Oct 2022 18:33:47 -0400 Subject: [PATCH 2/2] Account for trustline limit before buying liabilities and not after. --- soroban-env-host/src/host.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/soroban-env-host/src/host.rs b/soroban-env-host/src/host.rs index e8dfc81e1..1503f772c 100644 --- a/soroban-env-host/src/host.rs +++ b/soroban-env-host/src/host.rs @@ -995,14 +995,16 @@ impl Host { let base_reserve = self.with_ledger_info(|li| Ok(li.base_reserve))? as i64; let (min_balance, max_balance) = if let TrustLineEntryExt::V1(ext1) = &tl.ext { let min_balance = ext1.liabilities.selling; - let max_balance = i64::MAX - ext1.liabilities.buying; + if tl.limit < ext1.liabilities.buying { + return Err(self.err_general("limit is lower than liabilities")); + } + let max_balance = tl.limit - ext1.liabilities.buying; (min_balance, max_balance) } else { let min_balance = 0; - let max_balance = i64::MAX; + let max_balance = tl.limit; (min_balance, max_balance) }; - let max_balance = min(max_balance, tl.limit); let new_balance = if amount <= 0 { tl.balance + amount