xrpld
Loading...
Searching...
No Matches
PaymentSandbox.h
1#pragma once
2
3#include <xrpl/ledger/ApplyView.h>
4#include <xrpl/ledger/OwnerCounts.h>
5#include <xrpl/ledger/RawView.h>
6#include <xrpl/ledger/ReadView.h>
7#include <xrpl/ledger/detail/ApplyViewBase.h>
8#include <xrpl/protocol/AccountID.h>
9#include <xrpl/protocol/MPTIssue.h>
10#include <xrpl/protocol/STAmount.h>
11#include <xrpl/protocol/UintTypes.h>
12#include <xrpl/protocol/XRPAmount.h>
13
14#include <cstdint>
15#include <map>
16#include <optional>
17#include <tuple>
18#include <utility>
19
20namespace xrpl {
21
22namespace detail {
23
24// VFALCO TODO Inline this implementation
25// into the PaymentSandbox class itself
27{
28private:
37
39 {
40 HolderValueMPT() = default;
41 // Debit to issuer
44 };
45
47 {
48 IssuerValueMPT() = default;
50 // Credit to holder
52 // OutstandingAmount might overflow when MPTs are credited to a holder.
53 // Consider A1 paying 100MPT to A2 and A1 already having maximum MPTs.
54 // Since the payment engine executes a payment in revers, A2 is
55 // credited first and OutstandingAmount is going to be equal
56 // to MaximumAmount + 100MPT. In the next step A1 redeems 100MPT
57 // to the issuer and OutstandingAmount balances out.
59 // Self debit on offer selling MPT. Since the payment engine executes
60 // a payment in reverse, a crediting/buying step may overflow
61 // OutstandingAmount. A sell MPT offer owned by a holder can redeem any
62 // amount up to the offer's amount and holder's available funds,
63 // balancing out OutstandingAmount. But if the offer's owner is issuer
64 // then it issues more MPT. In this case the available amount to issue
65 // is the initial issuer's available amount less all offer sell amounts
66 // by the issuer. This is self-debit, where the offer's owner,
67 // issuer in this case, debits to self.
69 };
71
72public:
83
84 // Get the adjustments for the balance between main and other.
85 // Returns the debits, credits and the original balance
87 adjustmentsIOU(AccountID const& main, AccountID const& other, Currency const& currency) const;
88
90 adjustmentsMPT(MPTID const& mptID) const;
91
92 void
94 AccountID const& sender,
95 AccountID const& receiver,
96 STAmount const& amount,
97 STAmount const& preCreditSenderBalance);
98
99 void
100 creditMPT(
101 AccountID const& sender,
102 AccountID const& receiver,
103 STAmount const& amount,
104 std::uint64_t preCreditBalanceHolder,
105 std::int64_t preCreditBalanceIssuer);
106
107 void
108 issuerSelfDebitMPT(MPTIssue const& issue, std::uint64_t amount, std::int64_t origBalance);
109
110 void
111 ownerCount(AccountID const& id, OwnerCounts const& cur, OwnerCounts const& next);
112
113 // Get the adjusted owner count. Since DeferredCredits is meant to be used
114 // in payments, and payments only decrease owner counts, return the max
115 // remembered owner count.
116 [[nodiscard]] std::optional<OwnerCounts>
117 ownerCount(AccountID const& id) const;
118
119 void
121
122private:
123 static KeyIOU
124 makeKeyIOU(AccountID const& a1, AccountID const& a2, Currency const& currency);
125
129};
130
131} // namespace detail
132
133//------------------------------------------------------------------------------
134
149{
150public:
151 PaymentSandbox() = delete;
156 operator=(PaymentSandbox const&) = delete;
157
159
161 {
162 }
163
164 PaymentSandbox(ApplyView const* base) : ApplyViewBase(base, base->flags())
165 {
166 }
167
179 // VFALCO If we are constructing on top of a PaymentSandbox,
180 // or a PaymentSandbox-derived class, we MUST go through
181 // one of these constructors or invariants will be broken.
183 explicit PaymentSandbox(PaymentSandbox const* base)
184 : ApplyViewBase(base, base->flags()), ps_(base)
185 {
186 }
187
188 explicit PaymentSandbox(PaymentSandbox* base) : ApplyViewBase(base, base->flags()), ps_(base)
189 {
190 }
191
192
193 [[nodiscard]] STAmount
194 balanceHookIOU(AccountID const& account, AccountID const& issuer, STAmount const& amount)
195 const override;
196
197 [[nodiscard]] STAmount
198 balanceHookMPT(AccountID const& account, MPTIssue const& issue, std::int64_t amount)
199 const override;
200
201 [[nodiscard]] STAmount
202 balanceHookSelfIssueMPT(MPTIssue const& issue, std::int64_t amount) const override;
203
204 void
206 AccountID const& from,
207 AccountID const& to,
208 STAmount const& amount,
209 STAmount const& preCreditBalance) override;
210
211 void
213 AccountID const& from,
214 AccountID const& to,
215 STAmount const& amount,
216 std::uint64_t preCreditBalanceHolder,
217 std::int64_t preCreditBalanceIssuer) override;
218
219 void
220 issuerSelfDebitHookMPT(MPTIssue const& issue, std::uint64_t amount, std::int64_t origBalance)
221 override;
222
223 void
224 adjustOwnerCountHook(AccountID const& account, OwnerCounts const& cur, OwnerCounts const& next)
225 override;
226
227 [[nodiscard]] OwnerCounts
228 ownerCountHook(AccountID const& account, OwnerCounts const& count) const override;
229
238 void
239 apply(RawView& to);
240
241 void
244
245 [[nodiscard]] XRPAmount
246 xrpDestroyed() const;
247
248private:
250 PaymentSandbox const* ps_ = nullptr;
251};
252
253} // namespace xrpl
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:134
A wrapper which makes credits unavailable to balances.
PaymentSandbox(ReadView const *base, ApplyFlags flags)
STAmount balanceHookMPT(AccountID const &account, MPTIssue const &issue, std::int64_t amount) const override
PaymentSandbox(PaymentSandbox *base)
void creditHookMPT(AccountID const &from, AccountID const &to, STAmount const &amount, std::uint64_t preCreditBalanceHolder, std::int64_t preCreditBalanceIssuer) override
STAmount balanceHookIOU(AccountID const &account, AccountID const &issuer, STAmount const &amount) const override
void creditHookIOU(AccountID const &from, AccountID const &to, STAmount const &amount, STAmount const &preCreditBalance) override
void adjustOwnerCountHook(AccountID const &account, OwnerCounts const &cur, OwnerCounts const &next) override
PaymentSandbox & operator=(PaymentSandbox &&)=delete
PaymentSandbox const * ps_
PaymentSandbox(PaymentSandbox const &)=delete
void issuerSelfDebitHookMPT(MPTIssue const &issue, std::uint64_t amount, std::int64_t origBalance) override
Facilitate tracking of MPT sold by an issuer owning MPT sell offer.
void apply(RawView &to)
Apply changes to base view.
PaymentSandbox(PaymentSandbox &&)=default
detail::DeferredCredits tab_
PaymentSandbox(ApplyView const *base)
OwnerCounts ownerCountHook(AccountID const &account, OwnerCounts const &count) const override
STAmount balanceHookSelfIssueMPT(MPTIssue const &issue, std::int64_t amount) const override
PaymentSandbox(PaymentSandbox const *base)
Construct on top of existing PaymentSandbox.
XRPAmount xrpDestroyed() const
PaymentSandbox & operator=(PaymentSandbox const &)=delete
Interface for ledger entry changes.
Definition RawView.h:18
A view into a ledger.
Definition ReadView.h:41
ApplyFlags flags() const override
Returns the tx apply flags.
std::optional< AdjustmentIOU > adjustmentsIOU(AccountID const &main, AccountID const &other, Currency const &currency) const
std::map< MPTID, IssuerValueMPT > creditsMPT_
void ownerCount(AccountID const &id, OwnerCounts const &cur, OwnerCounts const &next)
void apply(DeferredCredits &to)
static KeyIOU makeKeyIOU(AccountID const &a1, AccountID const &a2, Currency const &currency)
void creditIOU(AccountID const &sender, AccountID const &receiver, STAmount const &amount, STAmount const &preCreditSenderBalance)
std::map< KeyIOU, ValueIOU > creditsIOU_
void creditMPT(AccountID const &sender, AccountID const &receiver, STAmount const &amount, std::uint64_t preCreditBalanceHolder, std::int64_t preCreditBalanceIssuer)
std::map< AccountID, OwnerCounts > ownerCounts_
std::tuple< AccountID, AccountID, Currency > KeyIOU
std::optional< AdjustmentMPT > adjustmentsMPT(MPTID const &mptID) const
void issuerSelfDebitMPT(MPTIssue const &issue, std::uint64_t amount, std::int64_t origBalance)
STL namespace.
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:5
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:42
BaseUInt< 192 > MPTID
MPTID is a 192-bit value representing MPT Issuance ID, which is a concatenation of a 32-bit sequence ...
Definition UintTypes.h:54
ApplyFlags
Definition ApplyView.h:27
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
AdjustmentIOU(STAmount d, STAmount c, STAmount b)
std::map< AccountID, HolderValueMPT > holders