rippled
Loading...
Searching...
No Matches
LoanBrokerCoverDeposit.cpp
1#include <xrpld/app/tx/detail/LoanBrokerCoverDeposit.h>
2//
3#include <xrpld/app/misc/LendingHelpers.h>
4
5#include <xrpl/protocol/STTakesAsset.h>
6
7namespace xrpl {
8
9bool
14
17{
18 if (ctx.tx[sfLoanBrokerID] == beast::zero)
19 return temINVALID;
20
21 auto const dstAmount = ctx.tx[sfAmount];
22 if (dstAmount <= beast::zero)
23 return temBAD_AMOUNT;
24
25 if (!isLegalNet(dstAmount))
26 return temBAD_AMOUNT;
27
28 return tesSUCCESS;
29}
30
31TER
33{
34 auto const& tx = ctx.tx;
35
36 auto const account = tx[sfAccount];
37 auto const brokerID = tx[sfLoanBrokerID];
38 auto const amount = tx[sfAmount];
39
40 auto const sleBroker = ctx.view.read(keylet::loanbroker(brokerID));
41 if (!sleBroker)
42 {
43 JLOG(ctx.j.warn()) << "LoanBroker does not exist.";
44 return tecNO_ENTRY;
45 }
46 if (account != sleBroker->at(sfOwner))
47 {
48 JLOG(ctx.j.warn()) << "Account is not the owner of the LoanBroker.";
49 return tecNO_PERMISSION;
50 }
51 auto const vault = ctx.view.read(keylet::vault(sleBroker->at(sfVaultID)));
52 if (!vault)
53 {
54 // LCOV_EXCL_START
55 JLOG(ctx.j.fatal()) << "Vault is missing for Broker " << brokerID;
56 return tefBAD_LEDGER;
57 // LCOV_EXCL_STOP
58 }
59
60 auto const vaultAsset = vault->at(sfAsset);
61 if (amount.asset() != vaultAsset)
62 return tecWRONG_ASSET;
63
64 auto const pseudoAccountID = sleBroker->at(sfAccount);
65 // Cannot transfer a non-transferable Asset
66 if (auto const ret = canTransfer(ctx.view, vaultAsset, account, pseudoAccountID))
67 return ret;
68 // Cannot transfer a frozen Asset
69 if (auto const ret = checkFrozen(ctx.view, account, vaultAsset))
70 return ret;
71 // Pseudo-account cannot receive if asset is deep frozen
72 if (auto const ret = checkDeepFrozen(ctx.view, pseudoAccountID, vaultAsset))
73 return ret;
74 // Cannot transfer unauthorized asset
75 if (auto const ret = requireAuth(ctx.view, vaultAsset, account, AuthType::StrongAuth))
76 return ret;
77
78 if (accountHolds(
79 ctx.view,
80 account,
81 vaultAsset,
84 ctx.j,
87
88 return tesSUCCESS;
89}
90
91TER
93{
94 auto const& tx = ctx_.tx;
95
96 auto const brokerID = tx[sfLoanBrokerID];
97 auto const amount = tx[sfAmount];
98
99 auto broker = view().peek(keylet::loanbroker(brokerID));
100 if (!broker)
101 return tecINTERNAL; // LCOV_EXCL_LINE
102
103 auto const vault = view().read(keylet::vault(broker->at(sfVaultID)));
104 if (!vault)
105 return tecINTERNAL; // LCOV_EXCL_LINE
106
107 auto const vaultAsset = vault->at(sfAsset);
108
109 auto const brokerPseudoID = broker->at(sfAccount);
110
111 // Transfer assets from depositor to pseudo-account.
112 if (auto ter = accountSend(view(), account_, brokerPseudoID, amount, j_, WaiveTransferFee::Yes))
113 return ter;
114
115 // Increase the LoanBroker's CoverAvailable by Amount
116 broker->at(sfCoverAvailable) += amount;
117 view().update(broker);
118
119 associateAsset(*broker, vaultAsset);
120
121 return tesSUCCESS;
122}
123
124//------------------------------------------------------------------------------
125
126} // namespace xrpl
Stream fatal() const
Definition Journal.h:324
Stream warn() const
Definition Journal.h:312
STTx const & tx
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 TER preclaim(PreclaimContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
AccountID const account_
Definition Transactor.h:112
beast::Journal const j_
Definition Transactor.h:110
ApplyView & view()
Definition Transactor.h:128
ApplyContext & ctx_
Definition Transactor.h:108
Keylet loanbroker(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:498
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:492
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
TER checkDeepFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
Definition View.h:198
@ fhZERO_IF_FROZEN
Definition View.h:58
@ shFULL_BALANCE
Definition View.h:64
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
Definition View.h:121
@ tefBAD_LEDGER
Definition TER.h:150
bool isLegalNet(STAmount const &value)
Definition STAmount.h:566
bool checkLendingProtocolDependencies(PreflightContext const &ctx)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=shSIMPLE_BALANCE)
Definition View.cpp:392
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Calls static accountSendIOU if saAmount represents Issue.
Definition View.cpp:2446
@ ahZERO_IF_UNAUTHORIZED
Definition View.h:61
TER requireAuth(ReadView const &view, Issue const &issue, AccountID const &account, AuthType authType=AuthType::Legacy)
Check if the account lacks required authorization.
Definition View.cpp:2710
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to)
Check if the destination account is allowed to receive MPT.
Definition View.cpp:2914
@ temINVALID
Definition TER.h:90
@ temBAD_AMOUNT
Definition TER.h:69
@ tecWRONG_ASSET
Definition TER.h:341
@ tecNO_ENTRY
Definition TER.h:287
@ tecINTERNAL
Definition TER.h:291
@ tecINSUFFICIENT_FUNDS
Definition TER.h:306
@ tecNO_PERMISSION
Definition TER.h:286
void associateAsset(STLedgerEntry &sle, Asset const &asset)
Associate an Asset with all sMD_NeedsAsset fields in a ledger entry.
@ tesSUCCESS
Definition TER.h:225
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:53
ReadView const & view
Definition Transactor.h:56
beast::Journal const j
Definition Transactor.h:61
State information when preflighting a tx.
Definition Transactor.h:15