Ensure vault asset cap is not exceeded (#6124)

This commit is contained in:
Vito Tumas
2025-12-17 23:38:10 +01:00
committed by GitHub
parent 14790724af
commit 28fd66097b
2 changed files with 31 additions and 5 deletions

View File

@@ -3590,6 +3590,26 @@ protected:
fee(env.current()->fees().base * 5));
},
CaseArgs{.requireAuth = true, .authorizeBorrower = true});
testCase(
[&, this](Env& env, BrokerInfo const& broker, auto&) {
using namespace loan;
Number const principalRequest = broker.asset(1'000).value();
Vault vault{env};
auto tx = vault.set({.owner = lender, .id = broker.vaultID});
tx[sfAssetsMaximum] = BrokerParameters::defaults().vaultDeposit;
env(tx);
env.close();
testcase("Vault maximum value exceeded");
env(set(issuer, broker.brokerID, principalRequest),
counterparty(lender),
interestRate(TenthBips32(10'000)),
sig(sfCounterpartySignature, lender),
fee(env.current()->fees().base * 5),
ter(tecLIMIT_EXCEEDED));
},
nullptr);
}
void

View File

@@ -407,6 +407,17 @@ LoanSet::doApply()
vaultScale,
j_);
LoanState const state = constructLoanState(
properties.totalValueOutstanding,
principalRequested,
properties.managementFeeOwedToBroker);
if (vaultSle->at(sfAssetsMaximum) != 0 &&
vaultTotalProxy + state.interestDue > vaultSle->at(sfAssetsMaximum))
{
JLOG(j_.warn()) << "Loan would exceed the maximum assets of the vault";
return tecLIMIT_EXCEEDED;
}
// Check that relevant values won't lose precision. This is mostly only
// relevant for IOU assets.
{
@@ -449,11 +460,6 @@ LoanSet::doApply()
// LCOV_EXCL_STOP
}
LoanState const state = constructLoanState(
properties.totalValueOutstanding,
principalRequested,
properties.managementFeeOwedToBroker);
auto const originationFee = tx[~sfLoanOriginationFee].value_or(Number{});
auto const loanAssetsToBorrower = principalRequested - originationFee;