1#include <xrpl/tx/transactors/lending/LoanBrokerCoverWithdraw.h>
3#include <xrpl/ledger/helpers/AccountRootHelpers.h>
4#include <xrpl/ledger/helpers/CredentialHelpers.h>
5#include <xrpl/ledger/helpers/TokenHelpers.h>
6#include <xrpl/protocol/STTakesAsset.h>
7#include <xrpl/tx/transactors/lending/LendingHelpers.h>
8#include <xrpl/tx/transactors/payment/Payment.h>
21 if (ctx.
tx[sfLoanBrokerID] == beast::zero)
24 auto const dstAmount = ctx.
tx[sfAmount];
25 if (dstAmount <= beast::zero)
31 if (
auto const destination = ctx.
tx[~sfDestination])
33 if (*destination == beast::zero)
45 auto const& tx = ctx.
tx;
47 auto const account = tx[sfAccount];
48 auto const brokerID = tx[sfLoanBrokerID];
49 auto const amount = tx[sfAmount];
51 auto const dstAcct = tx[~sfDestination].value_or(account);
55 JLOG(ctx.
j.
warn()) <<
"Trying to withdraw into a pseudo-account.";
61 JLOG(ctx.
j.
warn()) <<
"LoanBroker does not exist.";
64 if (account != sleBroker->at(sfOwner))
66 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the LoanBroker.";
73 JLOG(ctx.
j.
fatal()) <<
"Vault is missing for Broker " << brokerID;
78 auto const vaultAsset = vault->at(sfAsset);
79 if (amount.asset() != vaultAsset)
83 auto const pseudoAccountID = sleBroker->at(sfAccount);
85 if (
auto const ret =
canTransfer(ctx.
view, vaultAsset, pseudoAccountID, dstAcct))
91 if (account != dstAcct)
102 if (
auto const ter =
requireAuth(ctx.
view, vaultAsset, dstAcct, authType))
106 if (dstAcct != vaultAsset.getIssuer())
109 if (
auto const ret =
checkFrozen(ctx.
view, pseudoAccountID, vaultAsset))
116 auto const coverAvail = sleBroker->at(sfCoverAvailable);
118 auto const currentDebtTotal = sleBroker->at(sfDebtTotal);
119 auto const minimumCover = [&]() {
126 currentDebtTotal.exponent());
128 if (coverAvail < amount)
130 if ((coverAvail - amount) < minimumCover)
150 auto const brokerID = tx[sfLoanBrokerID];
151 auto const amount = tx[sfAmount];
152 auto const dstAcct = tx[~sfDestination].value_or(
account_);
162 auto const vaultAsset = vault->at(sfAsset);
164 auto const brokerPseudoID = *broker->at(sfAccount);
167 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={})
Returns true if and only if sleAcct is a pseudo-account or specific pseudo-accounts in pseudoFieldFil...
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.
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, int depth=0)
Check if the account lacks required authorization for MPT.
State information when determining if a tx is likely to claim a fee.
State information when preflighting a tx.