rippled
Loading...
Searching...
No Matches
PaymentChannelFund.cpp
1#include <xrpl/ledger/ApplyView.h>
2#include <xrpl/ledger/View.h>
3#include <xrpl/protocol/Indexes.h>
4#include <xrpl/tx/transactors/payment_channel/PaymentChannelFund.h>
5
6#include <libxrpl/tx/transactors/payment_channel/PaymentChannelHelpers.h>
7
8namespace xrpl {
9
10TxConsequences
12{
13 return TxConsequences{ctx.tx, ctx.tx[sfAmount].xrp()};
14}
15
18{
19 if (!isXRP(ctx.tx[sfAmount]) || (ctx.tx[sfAmount] <= beast::zero))
20 return temBAD_AMOUNT;
21
22 return tesSUCCESS;
23}
24
25TER
27{
28 Keylet const k(ltPAYCHAN, ctx_.tx[sfChannel]);
29 auto const slep = ctx_.view().peek(k);
30 if (!slep)
31 return tecNO_ENTRY;
32
33 AccountID const src = (*slep)[sfAccount];
34 auto const txAccount = ctx_.tx[sfAccount];
35 auto const expiration = (*slep)[~sfExpiration];
36
37 {
38 auto const cancelAfter = (*slep)[~sfCancelAfter];
39 auto const closeTime = ctx_.view().header().parentCloseTime.time_since_epoch().count();
40 if ((cancelAfter && closeTime >= *cancelAfter) || (expiration && closeTime >= *expiration))
41 return closeChannel(slep, ctx_.view(), k.key, ctx_.registry.get().getJournal("View"));
42 }
43
44 if (src != txAccount)
45 {
46 // only the owner can add funds or extend
47 return tecNO_PERMISSION;
48 }
49
50 if (auto extend = ctx_.tx[~sfExpiration])
51 {
52 auto minExpiration = ctx_.view().header().parentCloseTime.time_since_epoch().count() +
53 (*slep)[sfSettleDelay];
54 if (expiration && *expiration < minExpiration)
55 minExpiration = *expiration;
56
57 if (*extend < minExpiration)
58 return temBAD_EXPIRATION;
59 (*slep)[~sfExpiration] = *extend;
60 ctx_.view().update(slep);
61 }
62
63 auto const sle = ctx_.view().peek(keylet::account(txAccount));
64 if (!sle)
65 return tefINTERNAL; // LCOV_EXCL_LINE
66
67 {
68 // Check reserve and funds availability
69 auto const balance = (*sle)[sfBalance];
70 auto const reserve = ctx_.view().fees().accountReserve((*sle)[sfOwnerCount]);
71
72 if (balance < reserve)
74
75 if (balance < reserve + ctx_.tx[sfAmount])
76 return tecUNFUNDED;
77 }
78
79 // do not allow adding funds if dst does not exist
80 if (AccountID const dst = (*slep)[sfDestination]; !ctx_.view().read(keylet::account(dst)))
81 {
82 return tecNO_DST;
83 }
84
85 (*slep)[sfAmount] = (*slep)[sfAmount] + ctx_.tx[sfAmount];
86 ctx_.view().update(slep);
87
88 (*sle)[sfBalance] = (*sle)[sfBalance] - ctx_.tx[sfAmount];
89 ctx_.view().update(sle);
90
91 return tesSUCCESS;
92}
93
94} // namespace xrpl
STTx const & tx
std::reference_wrapper< ServiceRegistry > registry
ApplyView & view()
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 TxConsequences makeTxConsequences(PreflightContext const &ctx)
static NotTEC preflight(PreflightContext const &ctx)
virtual Fees const & fees() const =0
Returns the fees for the base ledger.
virtual LedgerHeader const & header() const =0
Returns information about the ledger.
ApplyContext & ctx_
Definition Transactor.h:112
Class describing the consequences to the account of applying a transaction if the transaction consume...
Definition applySteps.h:38
Keylet account(AccountID const &id) noexcept
AccountID root.
Definition Indexes.cpp:165
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
bool isXRP(AccountID const &c)
Definition AccountID.h:70
@ tefINTERNAL
Definition TER.h:153
TER closeChannel(std::shared_ptr< SLE > const &slep, ApplyView &view, uint256 const &key, beast::Journal j)
@ temBAD_EXPIRATION
Definition TER.h:71
@ temBAD_AMOUNT
Definition TER.h:69
@ tecNO_ENTRY
Definition TER.h:287
@ tecINSUFFICIENT_RESERVE
Definition TER.h:288
@ tecNO_PERMISSION
Definition TER.h:286
@ tecNO_DST
Definition TER.h:271
@ tecUNFUNDED
Definition TER.h:276
@ tesSUCCESS
Definition TER.h:225
XRPAmount accountReserve(std::size_t ownerCount) const
Returns the account reserve given the owner count, in drops.
A pair of SHAMap key and LedgerEntryType.
Definition Keylet.h:19
uint256 key
Definition Keylet.h:20
NetClock::time_point parentCloseTime
State information when preflighting a tx.
Definition Transactor.h:14
T time_since_epoch(T... args)