xrpld
Loading...
Searching...
No Matches
LoanBrokerSet.cpp
1#include <xrpl/tx/transactors/lending/LoanBrokerSet.h>
2
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>
22
23#include <memory>
24#include <vector>
25
26namespace xrpl {
27
28bool
33
36{
37 using namespace Lending;
38
39 auto const& tx = ctx.tx;
40 if (auto const data = tx[~sfData];
41 data && !data->empty() && !validDataLength(tx[~sfData], kMaxDataPayloadLength))
42 return temINVALID;
43 if (!validNumericRange(tx[~sfManagementFeeRate], kMaxManagementFeeRate))
44 return temINVALID;
45 if (!validNumericRange(tx[~sfCoverRateMinimum], kMaxCoverRate))
46 return temINVALID;
47 if (!validNumericRange(tx[~sfCoverRateLiquidation], kMaxCoverRate))
48 return temINVALID;
49 if (!validNumericRange(tx[~sfDebtMaximum], Number(kMaxMpTokenAmount), Number(0)))
50 return temINVALID;
51
52 if (tx.isFieldPresent(sfLoanBrokerID))
53 {
54 // Fixed fields can not be specified if we're modifying an existing
55 // LoanBroker Object
56 if (tx.isFieldPresent(sfManagementFeeRate) || tx.isFieldPresent(sfCoverRateMinimum) ||
57 tx.isFieldPresent(sfCoverRateLiquidation))
58 return temINVALID;
59
60 if (tx[sfLoanBrokerID] == beast::kZero)
61 return temINVALID;
62 }
63
64 if (auto const vaultID = tx.at(~sfVaultID))
65 {
66 if (*vaultID == beast::kZero)
67 return temINVALID;
68 }
69
70 {
71 auto const minimumZero = tx[~sfCoverRateMinimum].value_or(0) == 0;
72 auto const liquidationZero = tx[~sfCoverRateLiquidation].value_or(0) == 0;
73 // Both must be zero or non-zero.
74 if (minimumZero != liquidationZero)
75 {
76 return temINVALID;
77 }
78 }
79
80 return tesSUCCESS;
81}
82
85{
86 static std::vector<OptionaledField<STNumber>> const kValueFields{~sfDebtMaximum};
87
88 return kValueFields;
89}
90
91TER
93{
94 auto const& tx = ctx.tx;
95
96 auto const account = tx[sfAccount];
97 auto const vaultID = tx[sfVaultID];
98
99 auto const sleVault = ctx.view.read(keylet::vault(vaultID));
100 if (!sleVault)
101 {
102 JLOG(ctx.j.warn()) << "Vault does not exist.";
103 return tecNO_ENTRY;
104 }
105 Asset const asset = sleVault->at(sfAsset);
106
107 if (account != sleVault->at(sfOwner))
108 {
109 JLOG(ctx.j.warn()) << "Account is not the owner of the Vault.";
110 return tecNO_PERMISSION;
111 }
112
113 if (auto const brokerID = tx[~sfLoanBrokerID])
114 {
115 // Updating an existing Broker
116
117 auto const sleBroker = ctx.view.read(keylet::loanBroker(*brokerID));
118 if (!sleBroker)
119 {
120 JLOG(ctx.j.warn()) << "LoanBroker does not exist.";
121 return tecNO_ENTRY;
122 }
123 if (vaultID != sleBroker->at(sfVaultID))
124 {
125 JLOG(ctx.j.warn()) << "Can not change VaultID on an existing LoanBroker.";
126 return tecNO_PERMISSION;
127 }
128 if (account != sleBroker->at(sfOwner))
129 {
130 JLOG(ctx.j.warn()) << "Account is not the owner of the LoanBroker.";
131 return tecNO_PERMISSION;
132 }
133
134 if (auto const debtMax = tx[~sfDebtMaximum])
135 {
136 // Can't reduce the debt maximum below the current total debt
137 auto const currentDebtTotal = sleBroker->at(sfDebtTotal);
138 if (*debtMax != 0 && *debtMax < currentDebtTotal)
139 {
140 JLOG(ctx.j.warn()) << "Cannot reduce DebtMaximum below current DebtTotal.";
141 return tecLIMIT_EXCEEDED;
142 }
143 }
144 }
145 else
146 {
147 if (auto const ter = canAddHolding(ctx.view, asset))
148 return ter;
149
150 if (auto const ter = checkFrozen(ctx.view, sleVault->at(sfAccount), sleVault->at(sfAsset)))
151 {
152 JLOG(ctx.j.warn()) << "Vault pseudo-account is frozen.";
153 return ter;
154 }
155 }
156
157 // Check that relevant values can be represented as the vault asset
158 // type. This is mostly only relevant for integral (non-IOU) types
159 for (auto const& field : getValueFields())
160 {
161 if (auto const value = tx[field]; value && STAmount{asset, *value} != *value)
162 {
163 JLOG(ctx.j.warn()) << field.f->getName() << " (" << *value
164 << ") can not be represented as a(n) " << to_string(asset) << ".";
165 return tecPRECISION_LOSS;
166 }
167 }
168
169 return tesSUCCESS;
170}
171
172TER
174{
175 auto const& tx = ctx_.tx;
176 auto& view = ctx_.view();
177
178 if (auto const brokerID = tx[~sfLoanBrokerID])
179 {
180 // Modify an existing LoanBroker
181 auto broker = view.peek(keylet::loanBroker(*brokerID));
182 if (!broker)
183 {
184 // This should be impossible
185 // LCOV_EXCL_START
186 JLOG(j_.fatal()) << "LoanBroker does not exist.";
187 return tefBAD_LEDGER;
188 // LCOV_EXCL_STOP
189 }
190
191 auto const vault = view.read(keylet::vault(broker->at(sfVaultID)));
192 if (!vault)
193 return tecINTERNAL; // LCOV_EXCL_LINE
194
195 auto const vaultAsset = vault->at(sfAsset);
196
197 if (auto const data = tx[~sfData])
198 broker->at(sfData) = *data;
199 if (auto const debtMax = tx[~sfDebtMaximum])
200 broker->at(sfDebtMaximum) = *debtMax;
201
202 view.update(broker);
203
204 associateAsset(*broker, vaultAsset);
205 }
206 else
207 {
208 // Create a new LoanBroker pointing back to the given Vault
209 auto const vaultID = tx[sfVaultID];
210 auto const sleVault = view.read(keylet::vault(vaultID));
211 if (!sleVault)
212 {
213 // This should be impossible
214 // LCOV_EXCL_START
215 JLOG(j_.fatal()) << "Vault does not exist.";
216 return tefBAD_LEDGER;
217 // LCOV_EXCL_STOP
218 }
219 auto const vaultPseudoID = sleVault->at(sfAccount);
220 auto const vaultAsset = sleVault->at(sfAsset);
221 auto const sequence = tx.getSeqValue();
222
223 auto owner = view.peek(keylet::account(accountID_));
224 if (!owner)
225 {
226 // This should be impossible
227 // LCOV_EXCL_START
228 JLOG(j_.fatal()) << "Account does not exist.";
229 return tefBAD_LEDGER;
230 // LCOV_EXCL_STOP
231 }
232 auto broker = std::make_shared<SLE>(keylet::loanBroker(accountID_, sequence));
233
234 if (auto const ter = dirLink(view, accountID_, broker))
235 return ter; // LCOV_EXCL_LINE
236 if (auto const ter = dirLink(view, vaultPseudoID, broker, sfVaultNode))
237 return ter; // LCOV_EXCL_LINE
238
239 // Increases the owner count by two: one for the LoanBroker object, and
240 // one for the pseudo-account.
241 adjustOwnerCount(view, owner, 2, j_);
242 auto const ownerCount = owner->at(sfOwnerCount);
243 if (preFeeBalance_ < view.fees().accountReserve(ownerCount))
245
246 auto maybePseudo = createPseudoAccount(view, broker->key(), sfLoanBrokerID);
247 if (!maybePseudo)
248 return maybePseudo.error(); // LCOV_EXCL_LINE
249 auto& pseudo = *maybePseudo;
250 auto pseudoId = pseudo->at(sfAccount);
251
252 if (auto ter = addEmptyHolding(view, pseudoId, preFeeBalance_, sleVault->at(sfAsset), j_))
253 return ter;
254
255 // Initialize data fields:
256 broker->at(sfSequence) = sequence;
257 broker->at(sfVaultID) = vaultID;
258 broker->at(sfOwner) = accountID_;
259 broker->at(sfAccount) = pseudoId;
260 // The LoanSequence indexes loans created by this broker, starting at 1
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;
272
273 view.insert(broker);
274
275 associateAsset(*broker, vaultAsset);
276 }
277
278 return tesSUCCESS;
279}
280
281void
283{
284 // No transaction-specific invariants yet (future work).
285}
286
287bool
289 STTx const&,
290 TER,
291 XRPAmount,
292 ReadView const&,
293 beast::Journal const&)
294{
295 // No transaction-specific invariants yet (future work).
296 return true;
297}
298
299//------------------------------------------------------------------------------
300
301} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:38
Stream warn() const
Definition Journal.h:309
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()
TER doApply() override
static bool checkExtraFeatures(PreflightContext const &ctx)
Number is a floating point type that can represent a wide range of values.
Definition Number.h:306
A view into a ledger.
Definition ReadView.h:31
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
beast::Journal const j_
Definition Transactor.h:118
ApplyView & view()
Definition Transactor.h:136
AccountID const accountID_
Definition Transactor.h:120
XRPAmount preFeeBalance_
Definition Transactor.h:121
static bool validDataLength(std::optional< Slice > const &slice, std::size_t maxLength)
ApplyContext & ctx_
Definition Transactor.h:116
static bool validNumericRange(std::optional< T > value, T max, T min=T{})
Definition Transactor.h:559
T make_shared(T... args)
Keylet loanBroker(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:557
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:551
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:186
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
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)
@ tefBAD_LEDGER
Definition TER.h:160
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)
Definition base_uint.h:633
constexpr std::size_t kMaxDataPayloadLength
The maximum length of Data payload.
Definition Protocol.h:242
TER canAddHolding(ReadView const &view, MPTIssue const &mptIssue)
TERSubset< CanCvtToNotTEC > NotTEC
Definition TER.h:594
TER dirLink(ApplyView &view, AccountID const &owner, SLE::pointer &object, SF_UINT64 const &node=sfOwnerNode)
Definition View.cpp:334
void adjustOwnerCount(ApplyView &view, SLE::ref sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
@ temINVALID
Definition TER.h:96
TERSubset< CanCvtToTER > TER
Definition TER.h:634
@ tecNO_ENTRY
Definition TER.h:304
@ tecINTERNAL
Definition TER.h:308
@ tecPRECISION_LOSS
Definition TER.h:361
@ tecINSUFFICIENT_RESERVE
Definition TER.h:305
@ tecLIMIT_EXCEEDED
Definition TER.h:359
@ tecNO_PERMISSION
Definition TER.h:303
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.
Definition Protocol.h:238
@ tesSUCCESS
Definition TER.h:240
bool checkLendingProtocolDependencies(Rules const &rules, STTx const &tx)
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
beast::Journal const j
Definition Transactor.h:69
State information when preflighting a tx.
Definition Transactor.h:18