rippled
Loading...
Searching...
No Matches
LoanBrokerCoverDeposit.cpp
1#include <xrpld/app/tx/detail/LoanBrokerCoverDeposit.h>
2//
3#include <xrpld/app/misc/LendingHelpers.h>
4
5namespace xrpl {
6
7bool
12
15{
16 if (ctx.tx[sfLoanBrokerID] == beast::zero)
17 return temINVALID;
18
19 auto const dstAmount = ctx.tx[sfAmount];
20 if (dstAmount <= beast::zero)
21 return temBAD_AMOUNT;
22
23 if (!isLegalNet(dstAmount))
24 return temBAD_AMOUNT;
25
26 return tesSUCCESS;
27}
28
29TER
31{
32 auto const& tx = ctx.tx;
33
34 auto const account = tx[sfAccount];
35 auto const brokerID = tx[sfLoanBrokerID];
36 auto const amount = tx[sfAmount];
37
38 auto const sleBroker = ctx.view.read(keylet::loanbroker(brokerID));
39 if (!sleBroker)
40 {
41 JLOG(ctx.j.warn()) << "LoanBroker does not exist.";
42 return tecNO_ENTRY;
43 }
44 if (account != sleBroker->at(sfOwner))
45 {
46 JLOG(ctx.j.warn()) << "Account is not the owner of the LoanBroker.";
47 return tecNO_PERMISSION;
48 }
49 auto const vault = ctx.view.read(keylet::vault(sleBroker->at(sfVaultID)));
50 if (!vault)
51 {
52 // LCOV_EXCL_START
53 JLOG(ctx.j.fatal()) << "Vault is missing for Broker " << brokerID;
54 return tefBAD_LEDGER;
55 // LCOV_EXCL_STOP
56 }
57
58 auto const vaultAsset = vault->at(sfAsset);
59 if (amount.asset() != vaultAsset)
60 return tecWRONG_ASSET;
61
62 auto const pseudoAccountID = sleBroker->at(sfAccount);
63 // Cannot transfer a non-transferable Asset
64 if (auto const ret =
65 canTransfer(ctx.view, vaultAsset, account, pseudoAccountID))
66 return ret;
67 // Cannot transfer a frozen Asset
68 if (auto const ret = checkFrozen(ctx.view, account, vaultAsset))
69 return ret;
70 // Pseudo-account cannot receive if asset is deep frozen
71 if (auto const ret = checkDeepFrozen(ctx.view, pseudoAccountID, vaultAsset))
72 return ret;
73 // Cannot transfer unauthorized asset
74 if (auto const ret =
75 requireAuth(ctx.view, vaultAsset, account, AuthType::StrongAuth))
76 return ret;
77
78 if (accountHolds(
79 ctx.view,
80 account,
81 vaultAsset,
84 ctx.j) < amount)
86
87 return tesSUCCESS;
88}
89
90TER
92{
93 auto const& tx = ctx_.tx;
94
95 auto const brokerID = tx[sfLoanBrokerID];
96 auto const amount = tx[sfAmount];
97
98 auto broker = view().peek(keylet::loanbroker(brokerID));
99 if (!broker)
100 return tecINTERNAL; // LCOV_EXCL_LINE
101
102 auto const brokerPseudoID = broker->at(sfAccount);
103
104 // Transfer assets from depositor to pseudo-account.
105 if (auto ter = accountSend(
106 view(),
107 account_,
108 brokerPseudoID,
109 amount,
110 j_,
112 return ter;
113
114 // Increase the LoanBroker's CoverAvailable by Amount
115 broker->at(sfCoverAvailable) += amount;
116 view().update(broker);
117
118 return tesSUCCESS;
119}
120
121//------------------------------------------------------------------------------
122
123} // namespace xrpl
Stream fatal() const
Definition Journal.h:333
Stream warn() const
Definition Journal.h:321
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:128
beast::Journal const j_
Definition Transactor.h:126
ApplyView & view()
Definition Transactor.h:144
ApplyContext & ctx_
Definition Transactor.h:124
Keylet loanbroker(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:552
Keylet vault(AccountID const &owner, std::uint32_t seq) noexcept
Definition Indexes.cpp:546
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
TER checkDeepFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
Definition View.h:269
@ fhZERO_IF_FROZEN
Definition View.h:59
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
Definition View.h:160
@ tefBAD_LEDGER
Definition TER.h:151
bool isLegalNet(STAmount const &value)
Definition STAmount.h:592
bool checkLendingProtocolDependencies(PreflightContext const &ctx)
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j)
Definition View.cpp:461
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:2777
@ ahZERO_IF_UNAUTHORIZED
Definition View.h:62
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:3096
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:3325
@ temINVALID
Definition TER.h:91
@ temBAD_AMOUNT
Definition TER.h:70
@ tecWRONG_ASSET
Definition TER.h:342
@ tecNO_ENTRY
Definition TER.h:288
@ tecINTERNAL
Definition TER.h:292
@ tecINSUFFICIENT_FUNDS
Definition TER.h:307
@ tecNO_PERMISSION
Definition TER.h:287
@ tesSUCCESS
Definition TER.h:226
State information when determining if a tx is likely to claim a fee.
Definition Transactor.h:61
ReadView const & view
Definition Transactor.h:64
beast::Journal const j
Definition Transactor.h:69
State information when preflighting a tx.
Definition Transactor.h:16