1#include <xrpl/tx/transactors/lending/LoanBrokerCoverDeposit.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/LendingHelpers.h>
7#include <xrpl/ledger/helpers/TokenHelpers.h>
8#include <xrpl/protocol/Feature.h>
9#include <xrpl/protocol/Indexes.h>
10#include <xrpl/protocol/SField.h>
11#include <xrpl/protocol/STAmount.h>
12#include <xrpl/protocol/STLedgerEntry.h>
13#include <xrpl/protocol/STTakesAsset.h>
14#include <xrpl/protocol/STTx.h>
15#include <xrpl/protocol/TER.h>
16#include <xrpl/protocol/XRPAmount.h>
17#include <xrpl/tx/Transactor.h>
30 if (ctx.
tx[sfLoanBrokerID] == beast::kZero)
33 auto const dstAmount = ctx.
tx[sfAmount];
34 if (dstAmount <= beast::kZero)
48 auto const& tx = ctx.
tx;
50 auto const account = tx[sfAccount];
51 auto const brokerID = tx[sfLoanBrokerID];
52 auto const amount = tx[sfAmount];
57 JLOG(ctx.
j.
warn()) <<
"LoanBroker does not exist.";
60 if (account != sleBroker->at(sfOwner))
62 JLOG(ctx.
j.
warn()) <<
"Account is not the owner of the LoanBroker.";
69 JLOG(ctx.
j.
fatal()) <<
"Vault is missing for Broker " << brokerID;
74 auto const vaultAsset = vault->at(sfAsset);
75 if (amount.asset() != vaultAsset)
78 auto const pseudoAccountID = sleBroker->at(sfAccount);
80 if (
auto const ret =
canTransfer(ctx.
view, vaultAsset, account, pseudoAccountID))
106 auto const roundedAmount = [&]() ->
STAmount {
112 scale(sleBroker->at(sfCoverAvailable), vaultAsset),
116 if (fix320Enabled && roundedAmount == beast::kZero)
118 JLOG(ctx.
j.
warn()) <<
"LoanBrokerCoverDeposit: deposit amount: " << tx[sfAmount]
119 <<
" is zero at loan broker scale";
139 auto const& tx =
ctx_.tx;
141 auto const brokerID = tx[sfLoanBrokerID];
150 auto const vaultAsset = vault->at(sfAsset);
151 auto const brokerPseudoID = broker->at(sfAccount);
157 auto const amount = [&]() ->
STAmount {
163 scale(broker->at(sfCoverAvailable), vaultAsset),
168 if (amount == beast::kZero)
171 JLOG(
j_.error()) <<
"LoanBrokerCoverDeposit: deposit amount: " << tx[sfAmount]
183 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.
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 bool checkExtraFeatures(PreflightContext const &ctx)
static TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(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.
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 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)
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.
STAmount roundToScale(STAmount const &value, std::int32_t scale, Number::RoundingMode rounding=Number::getround())
Round an arbitrary precision Amount to the precision of an STAmount that has a given exponent.
TERSubset< CanCvtToNotTEC > NotTEC
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.
TER checkDepositFreeze(ReadView const &view, AccountID const &srcAcct, AccountID const &dstAcct, Asset const &asset)
Checks freeze compliance for depositing an asset into a pseudo-account (e.g.
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.
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.