rippled
Loading...
Searching...
No Matches
Credit.cpp
1//------------------------------------------------------------------------------
2/*
3 This file is part of rippled: https://github.com/ripple/rippled
4 Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6 Permission to use, copy, modify, and/or distribute this software for any
7 purpose with or without fee is hereby granted, provided that the above
8 copyright notice and this permission notice appear in all copies.
9
10 THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/ledger/ReadView.h>
21#include <xrpl/protocol/AmountConversions.h>
22#include <xrpl/protocol/Indexes.h>
23#include <xrpl/protocol/STAmount.h>
24
25namespace ripple {
26
27STAmount
29 ReadView const& view,
30 AccountID const& account,
31 AccountID const& issuer,
32 Currency const& currency)
33{
34 STAmount result(Issue{currency, account});
35
36 auto sleRippleState = view.read(keylet::line(account, issuer, currency));
37
38 if (sleRippleState)
39 {
40 result = sleRippleState->getFieldAmount(
41 account < issuer ? sfLowLimit : sfHighLimit);
42 result.setIssuer(account);
43 }
44
45 XRPL_ASSERT(
46 result.getIssuer() == account,
47 "ripple::creditLimit : result issuer match");
48 XRPL_ASSERT(
49 result.getCurrency() == currency,
50 "ripple::creditLimit : result currency match");
51 return result;
52}
53
54IOUAmount
56 ReadView const& v,
57 AccountID const& acc,
58 AccountID const& iss,
59 Currency const& cur)
60{
61 return toAmount<IOUAmount>(creditLimit(v, acc, iss, cur));
62}
63
64STAmount
66 ReadView const& view,
67 AccountID const& account,
68 AccountID const& issuer,
69 Currency const& currency)
70{
71 STAmount result(Issue{currency, account});
72
73 auto sleRippleState = view.read(keylet::line(account, issuer, currency));
74
75 if (sleRippleState)
76 {
77 result = sleRippleState->getFieldAmount(sfBalance);
78 if (account < issuer)
79 result.negate();
80 result.setIssuer(account);
81 }
82
83 XRPL_ASSERT(
84 result.getIssuer() == account,
85 "ripple::creditBalance : result issuer match");
86 XRPL_ASSERT(
87 result.getCurrency() == currency,
88 "ripple::creditBalance : result currency match");
89 return result;
90}
91
92} // namespace ripple
A currency issued by an account.
Definition Issue.h:33
A view into a ledger.
Definition ReadView.h:51
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
Keylet line(AccountID const &id0, AccountID const &id1, Currency const &currency) noexcept
The index of a trust line for a given currency.
Definition Indexes.cpp:244
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:25
STAmount creditLimit(ReadView const &view, AccountID const &account, AccountID const &issuer, Currency const &currency)
Calculate the maximum amount of IOUs that an account can hold.
Definition Credit.cpp:28
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
STAmount creditBalance(ReadView const &view, AccountID const &account, AccountID const &issuer, Currency const &currency)
Returns the amount of IOUs issued by issuer that are held by an account.
Definition Credit.cpp:65
IOUAmount creditLimit2(ReadView const &v, AccountID const &acc, AccountID const &iss, Currency const &cur)
Definition Credit.cpp:55