xrpld
Loading...
Searching...
No Matches
PaymentSandbox.h
1#pragma once
2
3#include <xrpl/ledger/RawView.h>
4#include <xrpl/ledger/Sandbox.h>
5#include <xrpl/ledger/detail/ApplyViewBase.h>
6#include <xrpl/protocol/AccountID.h>
7
8#include <map>
9#include <utility>
10
11namespace xrpl {
12
13namespace detail {
14
15// VFALCO TODO Inline this implementation
16// into the PaymentSandbox class itself
18{
19private:
28
30 {
31 HolderValueMPT() = default;
32 // Debit to issuer
35 };
36
38 {
39 IssuerValueMPT() = default;
41 // Credit to holder
43 // OutstandingAmount might overflow when MPTs are credited to a holder.
44 // Consider A1 paying 100MPT to A2 and A1 already having maximum MPTs.
45 // Since the payment engine executes a payment in revers, A2 is
46 // credited first and OutstandingAmount is going to be equal
47 // to MaximumAmount + 100MPT. In the next step A1 redeems 100MPT
48 // to the issuer and OutstandingAmount balances out.
50 // Self debit on offer selling MPT. Since the payment engine executes
51 // a payment in reverse, a crediting/buying step may overflow
52 // OutstandingAmount. A sell MPT offer owned by a holder can redeem any
53 // amount up to the offer's amount and holder's available funds,
54 // balancing out OutstandingAmount. But if the offer's owner is issuer
55 // then it issues more MPT. In this case the available amount to issue
56 // is the initial issuer's available amount less all offer sell amounts
57 // by the issuer. This is self-debit, where the offer's owner,
58 // issuer in this case, debits to self.
60 };
62
63public:
74
75 // Get the adjustments for the balance between main and other.
76 // Returns the debits, credits and the original balance
78 adjustmentsIOU(AccountID const& main, AccountID const& other, Currency const& currency) const;
79
81 adjustmentsMPT(MPTID const& mptID) const;
82
83 void
85 AccountID const& sender,
86 AccountID const& receiver,
87 STAmount const& amount,
88 STAmount const& preCreditSenderBalance);
89
90 void
92 AccountID const& sender,
93 AccountID const& receiver,
94 STAmount const& amount,
95 std::uint64_t preCreditBalanceHolder,
96 std::int64_t preCreditBalanceIssuer);
97
98 void
99 issuerSelfDebitMPT(MPTIssue const& issue, std::uint64_t amount, std::int64_t origBalance);
100
101 void
102 ownerCount(AccountID const& id, std::uint32_t cur, std::uint32_t next);
103
104 // Get the adjusted owner count. Since DeferredCredits is meant to be used
105 // in payments, and payments only decrease owner counts, return the max
106 // remembered owner count.
107 [[nodiscard]] std::optional<std::uint32_t>
108 ownerCount(AccountID const& id) const;
109
110 void
112
113private:
114 static KeyIOU
115 makeKeyIOU(AccountID const& a1, AccountID const& a2, Currency const& currency);
116
120};
121
122} // namespace detail
123
124//------------------------------------------------------------------------------
125
139{
140public:
141 PaymentSandbox() = delete;
146 operator=(PaymentSandbox const&) = delete;
147
149
151 {
152 }
153
154 PaymentSandbox(ApplyView const* base) : ApplyViewBase(base, base->flags())
155 {
156 }
157
168 // VFALCO If we are constructing on top of a PaymentSandbox,
169 // or a PaymentSandbox-derived class, we MUST go through
170 // one of these constructors or invariants will be broken.
172 explicit PaymentSandbox(PaymentSandbox const* base)
173 : ApplyViewBase(base, base->flags()), ps_(base)
174 {
175 }
176
177 explicit PaymentSandbox(PaymentSandbox* base) : ApplyViewBase(base, base->flags()), ps_(base)
178 {
179 }
180
181
182 [[nodiscard]] STAmount
183 balanceHookIOU(AccountID const& account, AccountID const& issuer, STAmount const& amount)
184 const override;
185
186 [[nodiscard]] STAmount
187 balanceHookMPT(AccountID const& account, MPTIssue const& issue, std::int64_t amount)
188 const override;
189
190 [[nodiscard]] STAmount
191 balanceHookSelfIssueMPT(MPTIssue const& issue, std::int64_t amount) const override;
192
193 void
195 AccountID const& from,
196 AccountID const& to,
197 STAmount const& amount,
198 STAmount const& preCreditBalance) override;
199
200 void
202 AccountID const& from,
203 AccountID const& to,
204 STAmount const& amount,
205 std::uint64_t preCreditBalanceHolder,
206 std::int64_t preCreditBalanceIssuer) override;
207
208 void
209 issuerSelfDebitHookMPT(MPTIssue const& issue, std::uint64_t amount, std::int64_t origBalance)
210 override;
211
212 void
213 adjustOwnerCountHook(AccountID const& account, std::uint32_t cur, std::uint32_t next) override;
214
215 [[nodiscard]] std::uint32_t
216 ownerCountHook(AccountID const& account, std::uint32_t count) const override;
217
225 void
226 apply(RawView& to);
227
228 void
231
232 [[nodiscard]] XRPAmount
233 xrpDestroyed() const;
234
235private:
237 PaymentSandbox const* ps_ = nullptr;
238};
239
240} // namespace xrpl
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:118
A wrapper which makes credits unavailable to balances.
PaymentSandbox(ReadView const *base, ApplyFlags flags)
void adjustOwnerCountHook(AccountID const &account, std::uint32_t cur, std::uint32_t next) override
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
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)
std::uint32_t ownerCountHook(AccountID const &account, std::uint32_t 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:14
A view into a ledger.
Definition ReadView.h:31
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 apply(DeferredCredits &to)
static KeyIOU makeKeyIOU(AccountID const &a1, AccountID const &a2, Currency const &currency)
std::map< AccountID, std::uint32_t > ownerCounts_
void creditIOU(AccountID const &sender, AccountID const &receiver, STAmount const &amount, STAmount const &preCreditSenderBalance)
void ownerCount(AccountID const &id, std::uint32_t cur, std::uint32_t next)
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::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:36
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:44
ApplyFlags
Definition ApplyView.h:12
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:28
AdjustmentIOU(STAmount d, STAmount c, STAmount b)
std::map< AccountID, HolderValueMPT > holders