1#include <xrpld/app/tx/detail/LoanBrokerCoverWithdraw.h>
3#include <xrpld/app/misc/LendingHelpers.h>
4#include <xrpld/app/tx/detail/Payment.h>
6#include <xrpl/ledger/CredentialHelpers.h>
7#include <xrpl/protocol/STTakesAsset.h>
20 if (ctx.
tx[sfLoanBrokerID] == beast::zero)
23 auto const dstAmount = ctx.
tx[sfAmount];
24 if (dstAmount <= beast::zero)
30 if (
auto const destination = ctx.
tx[~sfDestination])
32 if (*destination == beast::zero)
44 auto const& tx = ctx.
tx;
46 auto const account = tx[sfAccount];
47 auto const brokerID = tx[sfLoanBrokerID];
48 auto const amount = tx[sfAmount];
50 auto const dstAcct = tx[~sfDestination].value_or(account);
54 JLOG(ctx.
j.
warn()) <<
"Trying to withdraw into a pseudo-account.";
60 JLOG(ctx.
j.
warn()) <<
"LoanBroker does not exist.";
63 if (account != sleBroker->at(sfOwner))
65 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the LoanBroker.";
72 JLOG(ctx.
j.
fatal()) <<
"Vault is missing for Broker " << brokerID;
77 auto const vaultAsset = vault->at(sfAsset);
78 if (amount.asset() != vaultAsset)
82 auto const pseudoAccountID = sleBroker->at(sfAccount);
84 if (
auto const ret =
canTransfer(ctx.
view, vaultAsset, pseudoAccountID, dstAcct))
90 if (account != dstAcct)
101 if (
auto const ter =
requireAuth(ctx.
view, vaultAsset, dstAcct, authType))
105 if (dstAcct != vaultAsset.getIssuer())
108 if (
auto const ret =
checkFrozen(ctx.
view, pseudoAccountID, vaultAsset))
115 auto const coverAvail = sleBroker->at(sfCoverAvailable);
117 auto const currentDebtTotal = sleBroker->at(sfDebtTotal);
118 auto const minimumCover = [&]() {
125 currentDebtTotal.exponent());
127 if (coverAvail < amount)
129 if ((coverAvail - amount) < minimumCover)
149 auto const brokerID = tx[sfLoanBrokerID];
150 auto const amount = tx[sfAmount];
151 auto const dstAcct = tx[~sfDestination].value_or(
account_);
161 auto const vaultAsset = vault->at(sfAsset);
163 auto const brokerPseudoID = *broker->at(sfAccount);
166 broker->at(sfCoverAvailable) -= amount;
virtual void update(std::shared_ptr< SLE > const &sle)=0
Indicate changes to a peeked SLE.
virtual std::shared_ptr< SLE > peek(Keylet const &k)=0
Prepare to modify the SLE associated with key.
static bool checkExtraFeatures(PreflightContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
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 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)
bool isLegalNet(STAmount const &value)
bool checkLendingProtocolDependencies(PreflightContext const &ctx)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const ¤cy, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=shSIMPLE_BALANCE)
TenthBips< std::uint32_t > TenthBips32
bool isPseudoAccount(std::shared_ptr< SLE const > sleAcct, std::set< SField const * > const &pseudoFieldFilter={})
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to)
Check if the destination account is allowed to receive MPT.
void roundToAsset(A const &asset, Number &value)
Round an arbitrary precision Number IN PLACE to the precision of a given Asset.
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.
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.