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/protocol/Asset.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/Protocol.h>
14#include <xrpl/protocol/SField.h>
15#include <xrpl/protocol/STLedgerEntry.h>
16#include <xrpl/protocol/STNumber.h>
17#include <xrpl/protocol/STTakesAsset.h>
18#include <xrpl/protocol/STTx.h>
19#include <xrpl/protocol/TER.h>
20#include <xrpl/protocol/XRPAmount.h>
21#include <xrpl/tx/Transactor.h>
39 auto const& tx = ctx.
tx;
40 if (
auto const data = tx[~sfData];
52 if (tx.isFieldPresent(sfLoanBrokerID))
56 if (tx.isFieldPresent(sfManagementFeeRate) || tx.isFieldPresent(sfCoverRateMinimum) ||
57 tx.isFieldPresent(sfCoverRateLiquidation))
60 if (tx[sfLoanBrokerID] == beast::kZero)
64 if (
auto const vaultID = tx.at(~sfVaultID))
66 if (*vaultID == beast::kZero)
71 auto const minimumZero = tx[~sfCoverRateMinimum].value_or(0) == 0;
72 auto const liquidationZero = tx[~sfCoverRateLiquidation].value_or(0) == 0;
74 if (minimumZero != liquidationZero)
94 auto const& tx = ctx.
tx;
96 auto const account = tx[sfAccount];
97 auto const vaultID = tx[sfVaultID];
102 JLOG(ctx.
j.
warn()) <<
"Vault does not exist.";
105 Asset const asset = sleVault->at(sfAsset);
107 if (account != sleVault->at(sfOwner))
109 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the Vault.";
113 if (
auto const brokerID = tx[~sfLoanBrokerID])
120 JLOG(ctx.
j.
warn()) <<
"LoanBroker does not exist.";
123 if (vaultID != sleBroker->at(sfVaultID))
125 JLOG(ctx.
j.
warn()) <<
"Can not change VaultID on an existing LoanBroker.";
128 if (account != sleBroker->at(sfOwner))
130 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the LoanBroker.";
134 if (
auto const debtMax = tx[~sfDebtMaximum])
137 auto const currentDebtTotal = sleBroker->at(sfDebtTotal);
138 if (*debtMax != 0 && *debtMax < currentDebtTotal)
140 JLOG(ctx.
j.
warn()) <<
"Cannot reduce DebtMaximum below current DebtTotal.";
150 if (
auto const ter =
checkFrozen(ctx.
view, sleVault->at(sfAccount), sleVault->at(sfAsset)))
152 JLOG(ctx.
j.
warn()) <<
"Vault pseudo-account is frozen.";
161 if (
auto const value = tx[field]; value &&
STAmount{asset, *value} != *value)
163 JLOG(ctx.
j.
warn()) << field.f->getName() <<
" (" << *value
164 <<
") can not be represented as a(n) " <<
to_string(asset) <<
".";
175 auto const& tx =
ctx_.tx;
178 if (
auto const brokerID = tx[~sfLoanBrokerID])
186 JLOG(
j_.fatal()) <<
"LoanBroker does not exist.";
195 auto const vaultAsset = vault->at(sfAsset);
197 if (
auto const data = tx[~sfData])
198 broker->at(sfData) = *data;
199 if (
auto const debtMax = tx[~sfDebtMaximum])
200 broker->at(sfDebtMaximum) = *debtMax;
209 auto const vaultID = tx[sfVaultID];
215 JLOG(
j_.fatal()) <<
"Vault does not exist.";
219 auto const vaultPseudoID = sleVault->at(sfAccount);
220 auto const vaultAsset = sleVault->at(sfAsset);
221 auto const sequence = tx.getSeqValue();
228 JLOG(
j_.fatal()) <<
"Account does not exist.";
236 if (
auto const ter =
dirLink(
view, vaultPseudoID, broker, sfVaultNode))
242 auto const ownerCount = owner->at(sfOwnerCount);
248 return maybePseudo.error();
249 auto& pseudo = *maybePseudo;
250 auto pseudoId = pseudo->at(sfAccount);
256 broker->at(sfSequence) = sequence;
257 broker->at(sfVaultID) = vaultID;
259 broker->at(sfAccount) = pseudoId;
261 broker->at(sfLoanSequence) = 1;
262 if (
auto const data = tx[~sfData])
263 broker->at(sfData) = *data;
264 if (
auto const rate = tx[~sfManagementFeeRate])
265 broker->at(sfManagementFeeRate) = *rate;
266 if (
auto const debtMax = tx[~sfDebtMaximum])
267 broker->at(sfDebtMaximum) = *debtMax;
268 if (
auto const coverMin = tx[~sfCoverRateMinimum])
269 broker->at(sfCoverRateMinimum) = *coverMin;
270 if (
auto const coverLiq = tx[~sfCoverRateLiquidation])
271 broker->at(sfCoverRateLiquidation) = *coverLiq;
A generic endpoint for log messages.
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
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 SLE::const_pointer read(Keylet const &k) const =0
Return the state item associated with a key.
std::shared_ptr< STLedgerEntry const > const & const_ref
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 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, MPTIssue const &mptIssue, beast::Journal journal)
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.
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)
void adjustOwnerCount(ApplyView &view, SLE::ref sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
TERSubset< CanCvtToTER > TER
@ 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.