1#include <xrpl/tx/transactors/lending/LoanBrokerSet.h>
3#include <xrpl/ledger/helpers/AccountRootHelpers.h>
4#include <xrpl/ledger/helpers/TokenHelpers.h>
5#include <xrpl/protocol/STTakesAsset.h>
6#include <xrpl/tx/transactors/lending/LendingHelpers.h>
19 using namespace Lending;
21 auto const& tx = ctx.
tx;
22 if (
auto const data = tx[~sfData];
34 if (tx.isFieldPresent(sfLoanBrokerID))
38 if (tx.isFieldPresent(sfManagementFeeRate) || tx.isFieldPresent(sfCoverRateMinimum) ||
39 tx.isFieldPresent(sfCoverRateLiquidation))
42 if (tx[sfLoanBrokerID] == beast::zero)
46 if (
auto const vaultID = tx.at(~sfVaultID))
48 if (*vaultID == beast::zero)
53 auto const minimumZero = tx[~sfCoverRateMinimum].value_or(0) == 0;
54 auto const liquidationZero = tx[~sfCoverRateLiquidation].value_or(0) == 0;
56 if (minimumZero != liquidationZero)
76 auto const& tx = ctx.
tx;
78 auto const account = tx[sfAccount];
79 auto const vaultID = tx[sfVaultID];
84 JLOG(ctx.
j.
warn()) <<
"Vault does not exist.";
87 Asset const asset = sleVault->at(sfAsset);
89 if (account != sleVault->at(sfOwner))
91 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the Vault.";
95 if (
auto const brokerID = tx[~sfLoanBrokerID])
102 JLOG(ctx.
j.
warn()) <<
"LoanBroker does not exist.";
105 if (vaultID != sleBroker->at(sfVaultID))
107 JLOG(ctx.
j.
warn()) <<
"Can not change VaultID on an existing LoanBroker.";
110 if (account != sleBroker->at(sfOwner))
112 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the LoanBroker.";
116 if (
auto const debtMax = tx[~sfDebtMaximum])
119 auto const currentDebtTotal = sleBroker->at(sfDebtTotal);
120 if (*debtMax != 0 && *debtMax < currentDebtTotal)
122 JLOG(ctx.
j.
warn()) <<
"Cannot reduce DebtMaximum below current DebtTotal.";
132 if (
auto const ter =
checkFrozen(ctx.
view, sleVault->at(sfAccount), sleVault->at(sfAsset)))
134 JLOG(ctx.
j.
warn()) <<
"Vault pseudo-account is frozen.";
143 if (
auto const value = tx[field]; value &&
STAmount{asset, *value} != *value)
145 JLOG(ctx.
j.
warn()) << field.f->getName() <<
" (" << *value
146 <<
") can not be represented as a(n) " <<
to_string(asset) <<
".";
160 if (
auto const brokerID = tx[~sfLoanBrokerID])
168 JLOG(
j_.
fatal()) <<
"LoanBroker does not exist.";
177 auto const vaultAsset = vault->at(sfAsset);
179 if (
auto const data = tx[~sfData])
180 broker->at(sfData) = *data;
181 if (
auto const debtMax = tx[~sfDebtMaximum])
182 broker->at(sfDebtMaximum) = *debtMax;
191 auto const vaultID = tx[sfVaultID];
197 JLOG(
j_.
fatal()) <<
"Vault does not exist.";
201 auto const vaultPseudoID = sleVault->at(sfAccount);
202 auto const vaultAsset = sleVault->at(sfAsset);
203 auto const sequence = tx.getSeqValue();
210 JLOG(
j_.
fatal()) <<
"Account does not exist.";
218 if (
auto const ter =
dirLink(
view, vaultPseudoID, broker, sfVaultNode))
224 auto const ownerCount = owner->at(sfOwnerCount);
230 return maybePseudo.error();
231 auto& pseudo = *maybePseudo;
232 auto pseudoId = pseudo->at(sfAccount);
238 broker->at(sfSequence) = sequence;
239 broker->at(sfVaultID) = vaultID;
241 broker->at(sfAccount) = pseudoId;
243 broker->at(sfLoanSequence) = 1;
244 if (
auto const data = tx[~sfData])
245 broker->at(sfData) = *data;
246 if (
auto const rate = tx[~sfManagementFeeRate])
247 broker->at(sfManagementFeeRate) = *rate;
248 if (
auto const debtMax = tx[~sfDebtMaximum])
249 broker->at(sfDebtMaximum) = *debtMax;
250 if (
auto const coverMin = tx[~sfCoverRateMinimum])
251 broker->at(sfCoverRateMinimum) = *coverMin;
252 if (
auto const coverLiq = tx[~sfCoverRateLiquidation])
253 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.
std::string to_string(base_uint< Bits, Tag > const &a)
TER addEmptyHolding(ApplyView &view, AccountID const &accountID, XRPAmount priorBalance, MPTIssue const &mptIssue, beast::Journal journal)
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 canAddHolding(ReadView const &view, MPTIssue const &mptIssue)
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.