1#include <xrpl/tx/transactors/lending/LoanBrokerSet.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/core/ServiceRegistry.h>
7#include <xrpl/ledger/View.h>
8#include <xrpl/ledger/helpers/AccountRootHelpers.h>
9#include <xrpl/ledger/helpers/LendingHelpers.h>
10#include <xrpl/ledger/helpers/TokenHelpers.h>
11#include <xrpl/ledger/helpers/VaultHelpers.h>
12#include <xrpl/protocol/Asset.h>
13#include <xrpl/protocol/Feature.h>
14#include <xrpl/protocol/Indexes.h>
15#include <xrpl/protocol/Protocol.h>
16#include <xrpl/protocol/SField.h>
17#include <xrpl/protocol/STLedgerEntry.h>
18#include <xrpl/protocol/STNumber.h>
19#include <xrpl/protocol/STTakesAsset.h>
20#include <xrpl/protocol/STTx.h>
21#include <xrpl/protocol/TER.h>
22#include <xrpl/protocol/XRPAmount.h>
23#include <xrpl/tx/Transactor.h>
41 auto const& tx = ctx.
tx;
42 if (
auto const data = tx[~sfData];
54 if (tx.isFieldPresent(sfLoanBrokerID))
58 if (tx.isFieldPresent(sfManagementFeeRate) || tx.isFieldPresent(sfCoverRateMinimum) ||
59 tx.isFieldPresent(sfCoverRateLiquidation))
66 if (
auto const vaultID = tx.at(~sfVaultID))
73 auto const minimumZero = tx[~sfCoverRateMinimum].value_or(0) == 0;
74 auto const liquidationZero = tx[~sfCoverRateLiquidation].value_or(0) == 0;
76 if (minimumZero != liquidationZero)
96 auto const& tx = ctx.
tx;
98 auto const account = tx[sfAccount];
99 auto const vaultID = tx[sfVaultID];
104 JLOG(ctx.
j.
warn()) <<
"Vault does not exist.";
107 Asset const asset = sleVault->at(sfAsset);
109 if (account != sleVault->at(sfOwner))
111 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the Vault.";
115 if (
auto const brokerID = tx[~sfLoanBrokerID])
122 JLOG(ctx.
j.
warn()) <<
"LoanBroker does not exist.";
125 if (vaultID != sleBroker->at(sfVaultID))
127 JLOG(ctx.
j.
warn()) <<
"Can not change VaultID on an existing LoanBroker.";
130 if (account != sleBroker->at(sfOwner))
132 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the LoanBroker.";
136 if (
auto const debtMax = tx[~sfDebtMaximum])
139 auto const currentDebtTotal = sleBroker->at(sfDebtTotal);
140 if (*debtMax != 0 && *debtMax < currentDebtTotal)
142 JLOG(ctx.
j.
warn()) <<
"Cannot reduce DebtMaximum below current DebtTotal.";
159 JLOG(ctx.
j.
warn()) <<
"LoanBroker requires a closed-ended Vault.";
166 if (
auto const ter =
checkFrozen(ctx.
view, sleVault->at(sfAccount), sleVault->at(sfAsset)))
168 JLOG(ctx.
j.
warn()) <<
"Vault pseudo-account is frozen.";
177 if (
auto const value = tx[field]; value &&
STAmount{asset, *value} != *value)
179 JLOG(ctx.
j.
warn()) << field.f->getName() <<
" (" << *value
180 <<
") can not be represented as a(n) " <<
to_string(asset) <<
".";
191 auto const& tx =
ctx_.tx;
194 if (
auto const brokerID = tx[~sfLoanBrokerID])
202 JLOG(
j_.fatal()) <<
"LoanBroker does not exist.";
211 auto const vaultAsset = vault->at(sfAsset);
213 if (
auto const data = tx[~sfData])
214 broker->at(sfData) = *data;
215 if (
auto const debtMax = tx[~sfDebtMaximum])
216 broker->at(sfDebtMaximum) = *debtMax;
225 auto const vaultID = tx[sfVaultID];
231 JLOG(
j_.fatal()) <<
"Vault does not exist.";
235 auto const vaultPseudoID = sleVault->at(sfAccount);
236 auto const vaultAsset = sleVault->at(sfAsset);
237 auto const sequence = tx.getSeqProxy();
244 JLOG(
j_.fatal()) <<
"Account does not exist.";
252 if (
auto const ter =
dirLink(
view, vaultPseudoID, broker, sfVaultNode))
263 return maybePseudo.error();
264 auto& pseudo = *maybePseudo;
265 auto pseudoId = pseudo->at(sfAccount);
272 broker->at(sfSequence) = sequence.value();
273 broker->at(sfVaultID) = vaultID;
275 broker->at(sfAccount) = pseudoId;
277 broker->at(sfLoanSequence) = 1;
278 if (
auto const data = tx[~sfData])
279 broker->at(sfData) = *data;
280 if (
auto const rate = tx[~sfManagementFeeRate])
281 broker->at(sfManagementFeeRate) = *rate;
282 if (
auto const debtMax = tx[~sfDebtMaximum])
283 broker->at(sfDebtMaximum) = *debtMax;
284 if (
auto const coverMin = tx[~sfCoverRateMinimum])
285 broker->at(sfCoverRateMinimum) = *coverMin;
286 if (
auto const coverLiq = tx[~sfCoverRateLiquidation])
287 broker->at(sfCoverRateLiquidation) = *coverLiq;
A generic endpoint for log messages.
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
bool finalizeInvariants(STTx const &tx, TER result, XRPAmount fee, ReadView const &view, beast::Journal const &j) override
Check transaction-specific post-conditions after all entries have been visited.
static std::vector< OptionaledField< STNumber > > const & getValueFields()
static bool checkExtraFeatures(PreflightContext const &ctx)
Number is a floating point type that can represent a wide range of values.
virtual Rules const & rules() const =0
Returns the tx processing rules.
virtual SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
bool enabled(UInt256 const &feature) const
Returns true if a feature is enabled.
std::shared_ptr< STLedgerEntry const > const & ConstRef
AccountID const accountID_
static bool validDataLength(std::optional< Slice > const &slice, std::size_t maxLength)
static bool validNumericRange(std::optional< T > value, T max, T min=T{})
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Keylet loanBroker(AccountID const &owner, SeqProxy const &seq) noexcept
Keylet account(AccountID const &id) noexcept
AccountID root.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
std::expected< SLE::pointer, TER > createPseudoAccount(ApplyView &view, UInt256 const &pseudoOwnerKey, SField const &ownerField)
Create pseudo-account, storing pseudoOwnerKey into ownerField.
TER addEmptyHolding(ApplyViewContext ctx, AccountID const &accountID, XRPAmount priorBalance, MPTIssue const &mptIssue, beast::Journal journal)
void increaseOwnerCount(ApplyView &view, SLE::Ref accountSle, SLE::Ref sponsorSle, std::uint32_t count, beast::Journal j)
Increase owner-count fields when the caller supplies the sponsor.
std::string to_string(BaseUInt< Bits, Tag > const &a)
constexpr std::size_t kMaxDataPayloadLength
The maximum length of Data payload.
TER canAddHolding(ReadView const &view, MPTIssue const &mptIssue)
TERSubset< CanCvtToNotTEC > NotTEC
TER dirLink(ApplyView &view, AccountID const &owner, SLE::pointer &object, SF_UINT64 const &node=sfOwnerNode)
VaultKind getVaultKind(SLE::ConstRef vault)
Resolves the VaultKind of a vault SLE.
TERSubset< CanCvtToTER > TER
XRPAmount accountReserve(ReadView const &view, SLE::ConstRef sle, beast::Journal j, Adjustment adj={})
Returns the account reserve, in drops.
@ tecINSUFFICIENT_RESERVE
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
constexpr std::uint64_t kMaxMpTokenAmount
The maximum amount of MPTokenIssuance.
bool checkLendingProtocolDependencies(Rules const &rules, STTx const &tx)
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.