1#include <xrpl/tx/transactors/lending/LoanBrokerDelete.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/ledger/helpers/AccountRootHelpers.h>
7#include <xrpl/ledger/helpers/LendingHelpers.h>
8#include <xrpl/ledger/helpers/TokenHelpers.h>
9#include <xrpl/protocol/Asset.h>
10#include <xrpl/protocol/Feature.h>
11#include <xrpl/protocol/Indexes.h>
12#include <xrpl/protocol/SField.h>
13#include <xrpl/protocol/STAmount.h>
14#include <xrpl/protocol/STLedgerEntry.h>
15#include <xrpl/protocol/STTakesAsset.h>
16#include <xrpl/protocol/STTx.h>
17#include <xrpl/protocol/TER.h>
18#include <xrpl/protocol/XRPAmount.h>
19#include <xrpl/tx/Transactor.h>
32 if (ctx.
tx[sfLoanBrokerID] == beast::kZero)
41 auto const& tx = ctx.
tx;
43 auto const account = tx[sfAccount];
44 auto const brokerID = tx[sfLoanBrokerID];
49 JLOG(ctx.
j.
warn()) <<
"LoanBroker does not exist.";
53 auto const brokerOwner = sleBroker->at(sfOwner);
55 if (account != brokerOwner)
57 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the LoanBroker.";
60 if (
auto const ownerCount = sleBroker->at(sfOwnerCount); ownerCount != 0)
62 JLOG(ctx.
j.
warn()) <<
"LoanBrokerDelete: Owner count is " << ownerCount;
70 JLOG(ctx.
j.
fatal()) <<
"Vault is missing for Broker " << brokerID;
75 Asset const asset = vault->at(sfAsset);
77 if (
auto const debtTotal = sleBroker->at(sfDebtTotal); debtTotal != beast::kZero)
86 if (rounded != beast::kZero)
89 JLOG(ctx.
j.
warn()) <<
"LoanBrokerDelete: Debt total is " << debtTotal
90 <<
", which rounds to " << rounded;
96 auto const coverAvailable =
STAmount{asset, sleBroker->at(sfCoverAvailable)};
99 if (coverAvailable > beast::kZero)
103 JLOG(ctx.
j.
warn()) <<
"Broker owner account is frozen.";
110 if (coverAvailable > beast::kZero)
112 auto const brokerPseudo = sleBroker->at(sfAccount);
115 JLOG(ctx.
j.
warn()) <<
"Broker pseudo-account is frozen/locked.";
127 auto const& tx =
ctx_.tx;
129 auto const brokerID = tx[sfLoanBrokerID];
135 auto const vaultID = broker->at(sfVaultID);
139 auto const vaultPseudoID = sleVault->at(sfAccount);
140 auto const vaultAsset = sleVault->at(sfAsset);
142 auto const brokerPseudoID = broker->at(sfAccount);
144 if (!
view().dirRemove(
149 if (!
view().dirRemove(
150 keylet::ownerDir(vaultPseudoID), broker->at(sfVaultNode), broker->key(),
false))
156 auto const coverAvailable =
STAmount{vaultAsset, broker->at(sfCoverAvailable)};
166 if (!brokerPseudoSLE)
171 if (*brokerPseudoSLE->at(sfBalance))
173 JLOG(
j_.warn()) <<
"LoanBrokerDelete: Pseudo-account has a balance";
176 if (brokerPseudoSLE->at(sfOwnerCount) != 0)
178 JLOG(
j_.warn()) <<
"LoanBrokerDelete: Pseudo-account still owns objects";
183 JLOG(
j_.warn()) <<
"LoanBrokerDelete: Pseudo-account has a directory";
A generic endpoint for log messages.
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void erase(SLE::ref sle)=0
Remove a peeked SLE.
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.
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 bool checkExtraFeatures(PreflightContext const &ctx)
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 & const_ref
AccountID const accountID_
Keylet loanBroker(AccountID const &owner, std::uint32_t seq) noexcept
Keylet ownerDir(AccountID const &id) noexcept
The root page of an account's directory.
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 checkDeepFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
int getAssetsTotalScale(SLE::const_ref vaultSle)
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
int scale(Number const &number, Asset const &asset)
Get the scale of a Number for a given asset.
TERSubset< CanCvtToNotTEC > NotTEC
void adjustOwnerCount(ApplyView &view, SLE::ref sle, std::int32_t amount, beast::Journal j)
Adjust the owner count up or down.
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No, AllowMPTOverflow allowOverflow=AllowMPTOverflow::No)
Calls static accountSendIOU if saAmount represents Issue.
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
TERSubset< CanCvtToTER > TER
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
TER removeEmptyHolding(ApplyView &view, AccountID const &accountID, MPTIssue const &mptIssue, beast::Journal journal)
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.