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/CredentialHelpers.h>
9#include <xrpl/ledger/helpers/LendingHelpers.h>
10#include <xrpl/ledger/helpers/TokenHelpers.h>
11#include <xrpl/protocol/Feature.h>
12#include <xrpl/protocol/Indexes.h>
13#include <xrpl/protocol/Protocol.h>
14#include <xrpl/protocol/SField.h>
15#include <xrpl/protocol/STAmount.h>
16#include <xrpl/protocol/STLedgerEntry.h>
17#include <xrpl/protocol/STTakesAsset.h>
18#include <xrpl/protocol/STTx.h>
19#include <xrpl/protocol/TER.h>
20#include <xrpl/protocol/Units.h>
21#include <xrpl/protocol/XRPAmount.h>
22#include <xrpl/tx/Transactor.h>
42 auto const dstAmount = ctx.
tx[sfAmount];
49 if (
auto const destination = ctx.
tx[~sfDestination])
69 auto const& tx = ctx.
tx;
71 auto const account = tx[sfAccount];
72 auto const brokerID = tx[sfLoanBrokerID];
73 auto const amount = tx[sfAmount];
75 auto const dstAcct = tx[~sfDestination].value_or(account);
79 JLOG(ctx.
j.
warn()) <<
"Trying to withdraw into a pseudo-account.";
85 JLOG(ctx.
j.
warn()) <<
"LoanBroker does not exist.";
88 if (account != sleBroker->at(sfOwner))
90 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the LoanBroker.";
97 JLOG(ctx.
j.
fatal()) <<
"Vault is missing for Broker " << brokerID;
102 auto const vaultAsset = vault->at(sfAsset);
103 if (amount.asset() != vaultAsset)
108 ctx.
view, sleBroker, vaultAsset, amount, ctx.
j,
"LoanBrokerCoverWithdraw"))
112 auto const pseudoAccountID = sleBroker->at(sfAccount);
118 if (
auto const ret =
canTransfer(ctx.
view, vaultAsset, pseudoAccountID, dstAcct, waive))
130 if (account != dstAcct)
141 if (
auto const ter =
requireAuth(ctx.
view, vaultAsset, dstAcct, authType))
144 if (fix340Enabled && account == dstAcct && !
holdingExists(ctx.
view, dstAcct, vaultAsset))
158 if (dstAcct != vaultAsset.getIssuer())
161 if (
auto const ret =
checkFrozen(ctx.
view, pseudoAccountID, vaultAsset))
169 auto const coverAvail = sleBroker->at(sfCoverAvailable);
171 auto const currentDebtTotal = sleBroker->at(sfDebtTotal);
172 auto const minimumCover = [&]() {
176 currentDebtTotal,
TenthBips32{sleBroker->at(sfCoverRateMinimum)}, vault);
185 scale(currentDebtTotal, vaultAsset));
187 if (coverAvail < amount)
189 if ((coverAvail - amount) < minimumCover)
192 auto const freezeHandling = fix330Enabled && dstAcct == vaultAsset.getIssuer()
211 auto const& tx =
ctx_.tx;
213 auto const brokerID = tx[sfLoanBrokerID];
214 auto const amount = tx[sfAmount];
215 auto const dstAcct = tx[~sfDestination].value_or(
accountID_);
225 auto const vaultAsset = vault->at(sfAsset);
227 auto const brokerPseudoID = *broker->at(sfAccount);
230 broker->at(sfCoverAvailable) -= amount;
236 ctx_.getApplyViewContext(),
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)
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)
void visitInvariantEntry(bool isDelete, SLE::ConstRef before, SLE::ConstRef after) override
Inspect a single ledger entry modified by this transaction.
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 & ConstRef
bool isFieldPresent(SField const &field) const
AccountID const accountID_
NotTEC checkFields(STTx const &tx, Rules const &rules, beast::Journal j)
TER valid(STTx const &tx, ReadView const &view, AccountID const &src, beast::Journal j)
Keylet vault(AccountID const &owner, SeqProxy const &seq) noexcept
Keylet loanBroker(AccountID const &owner, SeqProxy const &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)
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.
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.
TER doWithdraw(ApplyViewContext ctx, AccountID const &senderAcct, AccountID const &dstAcct, AccountID const &sourceAcct, XRPAmount priorBalance, STAmount const &amount, beast::Journal j)
Number minimumBrokerCover(Number const &debtTotal, TenthBips32 coverRateMinimum, SLE::ConstRef vaultSle)
TER canAddHolding(ReadView const &view, MPTIssue const &mptIssue)
TERSubset< CanCvtToNotTEC > NotTEC
TER canWithdraw(ReadView const &view, AccountID const &from, AccountID const &to, SLE::ConstRef toSle, STAmount const &amount, bool hasDestinationTag, std::optional< std::vector< UInt256 > > const &credentialIDs=std::nullopt)
Checks that can withdraw funds from an object to itself or a destination.
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
bool isPseudoAccount(SLE::const_pointer sleAcct)
Returns true if and only if sleAcct is a pseudo-account of any kind (i.e.
bool isTesSuccess(TER x) noexcept
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.
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
TER checkWithdrawFreeze(ReadView const &view, AccountID const &pseudoAcct, AccountID const &submitterAcct, AccountID const &dstAcct, Asset const &asset)
Checks freeze compliance for withdrawing an asset from a pseudo-account (e.g.
TER canApplyToBrokerCover(ReadView const &view, SLE::ConstRef sleBroker, Asset const &vaultAsset, STAmount const &amount, beast::Journal j, std::string_view logPrefix)
Broker cover preclaim precision guard (fixCleanup3_2_0).
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
bool holdingExists(ReadView const &view, AccountID const &account, Asset const &asset)
True if the account already holds this asset (or is the issuer / XRP).
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.