1#include <xrpld/app/tx/detail/LoanBrokerSet.h>
3#include <xrpld/app/misc/LendingHelpers.h>
5#include <xrpl/protocol/STTakesAsset.h>
18 using namespace Lending;
20 auto const& tx = ctx.
tx;
32 if (tx.isFieldPresent(sfLoanBrokerID))
36 if (tx.isFieldPresent(sfManagementFeeRate) || tx.isFieldPresent(sfCoverRateMinimum) ||
37 tx.isFieldPresent(sfCoverRateLiquidation))
40 if (tx[sfLoanBrokerID] == beast::zero)
44 if (
auto const vaultID = tx.at(~sfVaultID))
46 if (*vaultID == beast::zero)
51 auto const minimumZero = tx[~sfCoverRateMinimum].value_or(0) == 0;
52 auto const liquidationZero = tx[~sfCoverRateLiquidation].value_or(0) == 0;
54 if (minimumZero != liquidationZero)
74 auto const& tx = ctx.
tx;
76 auto const account = tx[sfAccount];
77 auto const vaultID = tx[sfVaultID];
82 JLOG(ctx.
j.
warn()) <<
"Vault does not exist.";
85 Asset const asset = sleVault->at(sfAsset);
87 if (account != sleVault->at(sfOwner))
89 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the Vault.";
93 if (
auto const brokerID = tx[~sfLoanBrokerID])
100 JLOG(ctx.
j.
warn()) <<
"LoanBroker does not exist.";
103 if (vaultID != sleBroker->at(sfVaultID))
105 JLOG(ctx.
j.
warn()) <<
"Can not change VaultID on an existing LoanBroker.";
108 if (account != sleBroker->at(sfOwner))
110 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the LoanBroker.";
114 if (
auto const debtMax = tx[~sfDebtMaximum])
117 auto const currentDebtTotal = sleBroker->at(sfDebtTotal);
118 if (*debtMax != 0 && *debtMax < currentDebtTotal)
120 JLOG(ctx.
j.
warn()) <<
"Cannot reduce DebtMaximum below current DebtTotal.";
130 if (
auto const ter =
checkFrozen(ctx.
view, sleVault->at(sfAccount), sleVault->at(sfAsset)))
132 JLOG(ctx.
j.
warn()) <<
"Vault pseudo-account is frozen.";
141 if (
auto const value = tx[field]; value &&
STAmount{asset, *value} != *value)
143 JLOG(ctx.
j.
warn()) << field.f->getName() <<
" (" << *value <<
") can not be represented as a(n) "
158 if (
auto const brokerID = tx[~sfLoanBrokerID])
166 JLOG(
j_.
fatal()) <<
"LoanBroker does not exist.";
175 auto const vaultAsset = vault->at(sfAsset);
177 if (
auto const data = tx[~sfData])
178 broker->at(sfData) = *data;
179 if (
auto const debtMax = tx[~sfDebtMaximum])
180 broker->at(sfDebtMaximum) = *debtMax;
189 auto const vaultID = tx[sfVaultID];
195 JLOG(
j_.
fatal()) <<
"Vault does not exist.";
199 auto const vaultPseudoID = sleVault->at(sfAccount);
200 auto const vaultAsset = sleVault->at(sfAsset);
201 auto const sequence = tx.getSeqValue();
208 JLOG(
j_.
fatal()) <<
"Account does not exist.";
216 if (
auto const ter =
dirLink(
view, vaultPseudoID, broker, sfVaultNode))
222 auto const ownerCount = owner->at(sfOwnerCount);
228 return maybePseudo.error();
229 auto& pseudo = *maybePseudo;
230 auto pseudoId = pseudo->at(sfAccount);
236 broker->at(sfSequence) = sequence;
237 broker->at(sfVaultID) = vaultID;
239 broker->at(sfAccount) = pseudoId;
241 broker->at(sfLoanSequence) = 1;
242 if (
auto const data = tx[~sfData])
243 broker->at(sfData) = *data;
244 if (
auto const rate = tx[~sfManagementFeeRate])
245 broker->at(sfManagementFeeRate) = *rate;
246 if (
auto const debtMax = tx[~sfDebtMaximum])
247 broker->at(sfDebtMaximum) = *debtMax;
248 if (
auto const coverMin = tx[~sfCoverRateMinimum])
249 broker->at(sfCoverRateMinimum) = *coverMin;
250 if (
auto const coverLiq = tx[~sfCoverRateLiquidation])
251 broker->at(sfCoverRateLiquidation) = *coverLiq;
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual void insert(std::shared_ptr< SLE > const &sle)=0
Insert a new state SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
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 Fees const & fees() const =0
Returns the fees for the base ledger.
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
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 loanbroker(AccountID const &owner, std::uint32_t seq) noexcept
Keylet vault(AccountID const &owner, std::uint32_t 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 addEmptyHolding(ApplyView &view, AccountID const &accountID, XRPAmount priorBalance, Issue const &issue, beast::Journal journal)
Any transactors that call addEmptyHolding() in doApply must call canAddHolding() in preflight with th...
TER canAddHolding(ReadView const &view, Asset const &asset)
std::string to_string(base_uint< Bits, Tag > const &a)
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
std::uint64_t constexpr maxMPTokenAmount
The maximum amount of MPTokenIssuance.
bool checkLendingProtocolDependencies(PreflightContext const &ctx)
std::size_t constexpr maxDataPayloadLength
The maximum length of Data payload.
void adjustOwnerCount(ApplyView &view, std::shared_ptr< SLE > const &sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
TER dirLink(ApplyView &view, AccountID const &owner, std::shared_ptr< SLE > &object, SF_UINT64 const &node=sfOwnerNode)
@ tecINSUFFICIENT_RESERVE
Expected< std::shared_ptr< SLE >, TER > createPseudoAccount(ApplyView &view, uint256 const &pseudoOwnerKey, SField const &ownerField)
Create pseudo-account, storing pseudoOwnerKey into ownerField.
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.