xrpld
Loading...
Searching...
No Matches
TokenHelpers.h
1#pragma once
2
3#include <xrpl/basics/Number.h>
4#include <xrpl/beast/utility/Journal.h>
5#include <xrpl/ledger/ApplyView.h>
6#include <xrpl/ledger/ReadView.h>
7#include <xrpl/protocol/AccountID.h>
8#include <xrpl/protocol/Asset.h>
9#include <xrpl/protocol/Issue.h>
10#include <xrpl/protocol/MPTIssue.h>
11#include <xrpl/protocol/Rate.h>
12#include <xrpl/protocol/STAmount.h>
13#include <xrpl/protocol/STLedgerEntry.h>
14#include <xrpl/protocol/TER.h>
15#include <xrpl/protocol/UintTypes.h>
16#include <xrpl/protocol/XRPAmount.h>
17
18#include <cstdint>
19#include <initializer_list>
20#include <utility>
21#include <vector>
22
23namespace xrpl {
24
25//------------------------------------------------------------------------------
26//
27// Enums for token handling
28//
29//------------------------------------------------------------------------------
30
35
40
46
51
52enum class WaiveTransferFee : bool { No = false, Yes };
53
57enum class AllowMPTOverflow : bool { No = false, Yes };
58
67enum class WaiveMPTCanTransfer : bool { No = false, Yes };
68
69/* Check if MPToken (for MPT) or trust line (for IOU) exists:
70 * - StrongAuth - before checking if authorization is required
71 * - WeakAuth
72 * for MPT - after checking lsfMPTRequireAuth flag
73 * for IOU - do not check if trust line exists
74 * - Legacy
75 * for MPT - before checking lsfMPTRequireAuth flag i.e. same as StrongAuth
76 * for IOU - do not check if trust line exists i.e. same as WeakAuth
77 */
79
80//------------------------------------------------------------------------------
81//
82// Freeze checking (Asset-based dispatchers)
83//
84//------------------------------------------------------------------------------
85
86[[nodiscard]] bool
87isGlobalFrozen(ReadView const& view, Asset const& asset);
88
89[[nodiscard]] TER
90checkGlobalFrozen(ReadView const& view, Asset const& asset);
91
92[[nodiscard]] bool
93isIndividualFrozen(ReadView const& view, AccountID const& account, Asset const& asset);
94
95[[nodiscard]] TER
96checkIndividualFrozen(ReadView const& view, AccountID const& account, Asset const& asset);
97
103[[nodiscard]] bool
105 ReadView const& view,
106 AccountID const& account,
107 Asset const& asset,
108 std::uint8_t depth = 0);
109
110[[nodiscard]] TER
111checkFrozen(ReadView const& view, AccountID const& account, Issue const& issue);
112
113[[nodiscard]] TER
114checkFrozen(ReadView const& view, AccountID const& account, MPTIssue const& mptIssue);
115
116[[nodiscard]] TER
117checkFrozen(ReadView const& view, AccountID const& account, Asset const& asset);
118
119[[nodiscard]] bool
121 ReadView const& view,
122 std::initializer_list<AccountID> const& accounts,
123 Issue const& issue);
124
125[[nodiscard]] bool
127 ReadView const& view,
128 std::initializer_list<AccountID> const& accounts,
129 Asset const& asset,
130 std::uint8_t depth = 0);
131
132[[nodiscard]] bool
134 ReadView const& view,
135 AccountID const& account,
136 MPTIssue const& mptIssue,
137 std::uint8_t depth = 0);
138
144[[nodiscard]] bool
146 ReadView const& view,
147 AccountID const& account,
148 Asset const& asset,
149 std::uint8_t depth = 0);
150
151[[nodiscard]] TER
152checkDeepFrozen(ReadView const& view, AccountID const& account, MPTIssue const& mptIssue);
153
154[[nodiscard]] TER
155checkDeepFrozen(ReadView const& view, AccountID const& account, Asset const& asset);
156
188[[nodiscard]] TER
190 ReadView const& view,
191 AccountID const& pseudoAcct,
192 AccountID const& submitterAcct,
193 AccountID const& dstAcct,
194 Asset const& asset);
195
215[[nodiscard]] TER
217 ReadView const& view,
218 AccountID const& srcAcct,
219 AccountID const& pseudoAcct,
220 Asset const& asset);
221
222//------------------------------------------------------------------------------
223//
224// Account balance functions (Asset-based dispatchers)
225//
226//------------------------------------------------------------------------------
227
228// Returns the amount an account can spend.
229//
230// If shSIMPLE_BALANCE is specified, this is the amount the account can spend
231// without going into debt.
232//
233// If shFULL_BALANCE is specified, this is the amount the account can spend
234// total. Specifically:
235// * The account can go into debt if using a trust line, and the other side has
236// a non-zero limit.
237// * If the account is the asset issuer the limit is defined by the asset /
238// issuance.
239//
240// <-- saAmount: amount of currency held by account. May be negative.
241[[nodiscard]] STAmount
243 ReadView const& view,
244 AccountID const& account,
245 Currency const& currency,
246 AccountID const& issuer,
247 FreezeHandling zeroIfFrozen,
250
251[[nodiscard]] STAmount
253 ReadView const& view,
254 AccountID const& account,
255 Issue const& issue,
256 FreezeHandling zeroIfFrozen,
259
260[[nodiscard]] STAmount
262 ReadView const& view,
263 AccountID const& account,
264 MPTIssue const& mptIssue,
265 FreezeHandling zeroIfFrozen,
266 AuthHandling zeroIfUnauthorized,
269
270[[nodiscard]] STAmount
272 ReadView const& view,
273 AccountID const& account,
274 Asset const& asset,
275 FreezeHandling zeroIfFrozen,
276 AuthHandling zeroIfUnauthorized,
279
280// Returns the amount an account can spend of the currency type saDefault, or
281// returns saDefault if this account is the issuer of the currency in
282// question. Should be used in favor of accountHolds when questioning how much
283// an account can spend while also allowing currency issuers to spend
284// unlimited amounts of their own currency (since they can always issue more).
285[[nodiscard]] STAmount
287 ReadView const& view,
288 AccountID const& id,
289 STAmount const& saDefault,
290 FreezeHandling freezeHandling,
292
293// Overload with AuthHandling to support IOU and MPT.
294[[nodiscard]] STAmount
296 ReadView const& view,
297 AccountID const& id,
298 STAmount const& saDefault,
299 FreezeHandling freezeHandling,
300 AuthHandling authHandling,
302
308[[nodiscard]] Rate
309transferRate(ReadView const& view, Asset const& asset);
310
316[[nodiscard]] Rate
317transferRate(ReadView const& view, STAmount const& amount);
318
319//------------------------------------------------------------------------------
320//
321// Holding operations (Asset-based dispatchers)
322//
323//------------------------------------------------------------------------------
324
325[[nodiscard]] TER
326canAddHolding(ReadView const& view, Asset const& asset);
327
331[[nodiscard]] bool
332holdingExists(ReadView const& view, AccountID const& account, Asset const& asset);
333
334[[nodiscard]] TER
336 ApplyViewContext ctx,
337 AccountID const& accountID,
338 XRPAmount priorBalance,
339 Asset const& asset,
340 beast::Journal journal);
341
342[[nodiscard]] TER
344 ApplyViewContext ctx,
345 AccountID const& accountID,
346 Asset const& asset,
347 beast::Journal journal);
348
349//------------------------------------------------------------------------------
350//
351// Authorization and transfer checks (Asset-based dispatchers)
352//
353//------------------------------------------------------------------------------
354
355[[nodiscard]] TER
357 ReadView const& view,
358 Asset const& asset,
359 AccountID const& account,
360 AuthType authType = AuthType::Legacy);
361
362[[nodiscard]] TER
364 ReadView const& view,
365 Asset const& asset,
366 AccountID const& from,
367 AccountID const& to,
369 std::uint8_t depth = 0);
370
371//------------------------------------------------------------------------------
372//
373// Money Transfers (Asset-based dispatchers)
374//
375//------------------------------------------------------------------------------
376
377// Direct send w/o fees:
378// - Redeeming IOUs and/or sending sender's own IOUs.
379// - Create trust line of needed.
380// --> bCheckIssuer : normally require issuer to be involved.
381// [[nodiscard]] // nodiscard commented out so DirectStep.cpp compiles.
382
387TER
389 ApplyView& view,
390 AccountID const& uSenderID,
391 AccountID const& uReceiverID,
392 STAmount const& saAmount,
393 bool bCheckIssuer,
395
400[[nodiscard]] TER
402 ApplyView& view,
403 AccountID const& from,
404 AccountID const& to,
405 STAmount const& saAmount,
407 SLE::Ref sponsorSle = {},
409 AllowMPTOverflow allowOverflow = AllowMPTOverflow::No);
410
419[[nodiscard]] TER
421 ApplyView& view,
422 AccountID const& senderID,
423 Asset const& asset,
424 MultiplePaymentDestinations const& receivers,
427
428[[nodiscard]] TER
430 ApplyView& view,
431 AccountID const& from,
432 AccountID const& to,
433 STAmount const& amount,
435
436} // namespace xrpl
A generic endpoint for log messages.
Definition Journal.h:44
Writeable view to a ledger, for applying a transaction.
Definition ApplyView.h:141
std::shared_ptr< STLedgerEntry > const & Ref
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)
FreezeHandling
Controls the treatment of frozen account balances.
SpendableHandling
Controls whether to include the account's full spendable balance.
TER checkIndividualFrozen(ReadView const &view, AccountID const &account, Asset const &asset)
bool isIndividualFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptIssue)
Returns true if account's MPToken for mptIssue carries the individual-lock flag (lsfMPTLocked).
TER checkDepositFreeze(ReadView const &view, AccountID const &srcAcct, AccountID const &pseudoAcct, Asset const &asset)
Checks freeze compliance for depositing an asset into a pseudo-account (e.g.
AllowMPTOverflow
Controls whether accountSend is allowed to overflow OutstandingAmount *.
TER removeEmptyHolding(ApplyViewContext ctx, AccountID const &accountID, MPTIssue const &mptIssue, beast::Journal journal)
TER checkFrozen(ReadView const &view, AccountID const &account, Issue const &issue)
BaseUInt< 160, detail::CurrencyTag > Currency
Currency is a hash representing a specific currency.
Definition UintTypes.h:42
ReserveHandling
Controls whether the recipient owner-reserve check is enforced when auto-creating a trustline or MPTo...
TER addEmptyHolding(ApplyViewContext ctx, AccountID const &accountID, XRPAmount priorBalance, MPTIssue const &mptIssue, beast::Journal journal)
WaiveTransferFee
TER canTransfer(ReadView const &view, MPTIssue const &mptIssue, AccountID const &from, AccountID const &to, WaiveMPTCanTransfer waive=WaiveMPTCanTransfer::No, std::uint8_t depth=0)
Check whether to may receive the given MPT from from.
bool isDeepFrozen(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer)
@ Yes
We have consensus along with the network.
@ No
We do not have consensus.
STAmount accountFunds(ReadView const &view, AccountID const &id, STAmount const &saDefault, FreezeHandling freezeHandling, beast::Journal j)
bool isGlobalFrozen(ReadView const &view, AccountID const &issuer)
Check if the issuer has the global freeze flag set.
TER canAddHolding(ReadView const &view, MPTIssue const &mptIssue)
TER accountSend(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &saAmount, beast::Journal j, SLE::Ref sponsorSle={}, WaiveTransferFee waiveFee=WaiveTransferFee::No, AllowMPTOverflow allowOverflow=AllowMPTOverflow::No)
Calls static accountSendIOU if saAmount represents Issue.
AuthHandling
Controls the treatment of unauthorized MPT balances.
Rate transferRate(ReadView const &view, AccountID const &issuer)
Returns IOU issuer transfer fee as Rate.
TER directSendNoFee(ApplyView &view, AccountID const &uSenderID, AccountID const &uReceiverID, STAmount const &saAmount, bool bCheckIssuer, beast::Journal j)
Calls static directSendNoFeeIOU if saAmount represents Issue.
TER transferXRP(ApplyView &view, AccountID const &from, AccountID const &to, STAmount const &amount, beast::Journal j)
WaiveMPTCanTransfer
Controls whether canTransfer enforces lsfMPTCanTransfer on MPTs.
bool isFrozen(ReadView const &view, AccountID const &account, MPTIssue const &mptIssue, std::uint8_t depth=0)
Returns true if account cannot send or receive tokens of mptIssue because a freeze applies.
TER checkGlobalFrozen(ReadView const &view, Asset const &asset)
BaseUInt< 160, detail::AccountIDTag > AccountID
A 160-bit unsigned that uniquely identifies an account.
Definition AccountID.h:34
bool isAnyFrozen(ReadView const &view, std::initializer_list< AccountID > const &accounts, MPTIssue const &mptIssue, std::uint8_t depth=0)
TERSubset< CanCvtToTER > TER
Definition TER.h:654
TER requireAuth(ReadView const &view, MPTIssue const &mptIssue, AccountID const &account, AuthType authType=AuthType::Legacy, std::uint8_t depth=0)
Check if the account lacks required authorization for MPT.
std::vector< std::pair< AccountID, Number > > MultiplePaymentDestinations
TER accountSendMulti(ApplyView &view, AccountID const &senderID, Asset const &asset, MultiplePaymentDestinations const &receivers, beast::Journal j, WaiveTransferFee waiveFee=WaiveTransferFee::No)
Like accountSend, except one account is sending multiple payments (with the same asset!...
TER checkWithdrawFreeze(ReadView const &view, AccountID const &pseudoAcct, AccountID const &submitterAcct, AccountID const &dstAcct, Asset const &asset)
Checks freeze compliance for withdrawing an asset from a pseudo-account (e.g.
STAmount accountHolds(ReadView const &view, AccountID const &account, Currency const &currency, AccountID const &issuer, FreezeHandling zeroIfFrozen, beast::Journal j, SpendableHandling includeFullBalance=SpendableHandling::SimpleBalance)
bool holdingExists(ReadView const &view, AccountID const &account, Asset const &asset)
True if the account already holds this asset (or is the issuer / XRP).