1#include <xrpl/tx/transactors/lending/LoanBrokerCoverWithdraw.h>
3#include <xrpl/basics/Log.h>
4#include <xrpl/basics/Number.h>
5#include <xrpl/beast/utility/Zero.h>
6#include <xrpl/ledger/View.h>
7#include <xrpl/ledger/helpers/AccountRootHelpers.h>
8#include <xrpl/ledger/helpers/LendingHelpers.h>
9#include <xrpl/ledger/helpers/TokenHelpers.h>
10#include <xrpl/protocol/Feature.h>
11#include <xrpl/protocol/Indexes.h>
12#include <xrpl/protocol/Protocol.h>
13#include <xrpl/protocol/SField.h>
14#include <xrpl/protocol/STAmount.h>
15#include <xrpl/protocol/STLedgerEntry.h>
16#include <xrpl/protocol/STTakesAsset.h>
17#include <xrpl/protocol/STTx.h>
18#include <xrpl/protocol/TER.h>
19#include <xrpl/protocol/Units.h>
20#include <xrpl/protocol/XRPAmount.h>
21#include <xrpl/tx/Transactor.h>
34 if (ctx.
tx[sfLoanBrokerID] == beast::kZero)
37 auto const dstAmount = ctx.
tx[sfAmount];
38 if (dstAmount <= beast::kZero)
44 if (
auto const destination = ctx.
tx[~sfDestination])
46 if (*destination == beast::kZero)
60 auto const& tx = ctx.
tx;
62 auto const account = tx[sfAccount];
63 auto const brokerID = tx[sfLoanBrokerID];
64 auto const amount = tx[sfAmount];
66 auto const dstAcct = tx[~sfDestination].value_or(account);
70 JLOG(ctx.
j.
warn()) <<
"Trying to withdraw into a pseudo-account.";
76 JLOG(ctx.
j.
warn()) <<
"LoanBroker does not exist.";
79 if (account != sleBroker->at(sfOwner))
81 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the LoanBroker.";
88 JLOG(ctx.
j.
fatal()) <<
"Vault is missing for Broker " << brokerID;
93 auto const vaultAsset = vault->at(sfAsset);
94 if (amount.asset() != vaultAsset)
99 ctx.
view, sleBroker, vaultAsset, amount, ctx.
j,
"LoanBrokerCoverWithdraw"))
103 auto const pseudoAccountID = sleBroker->at(sfAccount);
109 if (
auto const ret =
canTransfer(ctx.
view, vaultAsset, pseudoAccountID, dstAcct, waive))
115 if (account != dstAcct)
126 if (
auto const ter =
requireAuth(ctx.
view, vaultAsset, dstAcct, authType))
137 if (dstAcct != vaultAsset.getIssuer())
140 if (
auto const ret =
checkFrozen(ctx.
view, pseudoAccountID, vaultAsset))
148 auto const coverAvail = sleBroker->at(sfCoverAvailable);
150 auto const currentDebtTotal = sleBroker->at(sfDebtTotal);
151 auto const minimumCover = [&]() {
155 currentDebtTotal,
TenthBips32{sleBroker->at(sfCoverRateMinimum)}, vault);
164 scale(currentDebtTotal, vaultAsset));
166 if (coverAvail < amount)
168 if ((coverAvail - amount) < minimumCover)
171 auto const freezeHandling = fix330Enabled && dstAcct == vaultAsset.getIssuer()
190 auto const& tx =
ctx_.tx;
192 auto const brokerID = tx[sfLoanBrokerID];
193 auto const amount = tx[sfAmount];
194 auto const dstAcct = tx[~sfDestination].value_or(
accountID_);
204 auto const vaultAsset = vault->at(sfAsset);
206 auto const brokerPseudoID = *broker->at(sfAccount);
209 broker->at(sfCoverAvailable) -= amount;
A generic endpoint for log messages.
virtual SLE::pointer peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
virtual void update(SLE::ref sle)=0
Indicate changes to a peeked SLE.
static bool checkExtraFeatures(PreflightContext const &ctx)
void visitInvariantEntry(bool isDelete, SLE::const_ref before, SLE::const_ref after) override
Inspect a single ledger entry modified by this transaction.
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 NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext 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 vault(AccountID const &owner, std::uint32_t seq) noexcept
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)
TER canApplyToBrokerCover(ReadView const &view, SLE::const_ref sleBroker, Asset const &vaultAsset, STAmount const &amount, beast::Journal j, std::string_view logPrefix)
Broker cover preclaim precision guard (fixCleanup3_2_0).
TER doWithdraw(ApplyView &view, STTx const &tx, AccountID const &senderAcct, AccountID const &dstAcct, AccountID const &sourceAcct, XRPAmount priorBalance, STAmount const &amount, beast::Journal j)
constexpr T tenthBipsOfValue(T value, TenthBips< TBips > bips)
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.
Number minimumBrokerCover(Number const &debtTotal, TenthBips32 coverRateMinimum, SLE::const_ref vaultSle)
bool isLegalNet(STAmount const &value)
TenthBips< std::uint32_t > TenthBips32
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to, WaiveMPTCanTransfer waive=WaiveMPTCanTransfer::No, std::uint8_t depth=0)
Check whether to may receive the given MPT from from.
TERSubset< CanCvtToNotTEC > NotTEC
TER checkWithdrawFreeze(ReadView const &view, AccountID const &srcAcct, AccountID const &submitterAcct, AccountID const &dstAcct, Asset const &asset)
Checks freeze compliance for withdrawing an asset from a pseudo-account (e.g.
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
TERSubset< CanCvtToTER > TER
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, std::uint8_t depth=0)
Check if the account lacks required authorization for MPT.
TER canWithdraw(ReadView const &view, AccountID const &from, AccountID const &to, SLE::const_ref toSle, STAmount const &amount, bool hasDestinationTag)
Checks that can withdraw funds from an object to itself or a destination.
bool isPseudoAccount(SLE::const_pointer sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
Returns true if and only if sleAcct is a pseudo-account or specific pseudo-accounts in pseudoFieldFil...
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
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.