rippled
Loading...
Searching...
No Matches
Credit.cpp
1#include <xrpl/ledger/ReadView.h>
2#include <xrpl/protocol/AmountConversions.h>
3#include <xrpl/protocol/Indexes.h>
4#include <xrpl/protocol/STAmount.h>
5
6namespace xrpl {
7
8STAmount
9creditLimit(ReadView const& view, AccountID const& account, AccountID const& issuer, Currency const& currency)
10{
11 STAmount result(Issue{currency, account});
12
13 auto sleRippleState = view.read(keylet::line(account, issuer, currency));
14
15 if (sleRippleState)
16 {
17 result = sleRippleState->getFieldAmount(account < issuer ? sfLowLimit : sfHighLimit);
18 result.setIssuer(account);
19 }
20
21 XRPL_ASSERT(result.getIssuer() == account, "xrpl::creditLimit : result issuer match");
22 XRPL_ASSERT(result.getCurrency() == currency, "xrpl::creditLimit : result currency match");
23 return result;
24}
25
26IOUAmount
27creditLimit2(ReadView const& v, AccountID const& acc, AccountID const& iss, Currency const& cur)
28{
29 return toAmount<IOUAmount>(creditLimit(v, acc, iss, cur));
30}
31
32STAmount
33creditBalance(ReadView const& view, AccountID const& account, AccountID const& issuer, Currency const& currency)
34{
35 STAmount result(Issue{currency, account});
36
37 auto sleRippleState = view.read(keylet::line(account, issuer, currency));
38
39 if (sleRippleState)
40 {
41 result = sleRippleState->getFieldAmount(sfBalance);
42 if (account < issuer)
43 result.negate();
44 result.setIssuer(account);
45 }
46
47 XRPL_ASSERT(result.getIssuer() == account, "xrpl::creditBalance : result issuer match");
48 XRPL_ASSERT(result.getCurrency() == currency, "xrpl::creditBalance : result currency match");
49 return result;
50}
51
52} // namespace xrpl
A currency issued by an account.
Definition Issue.h:14
A view into a ledger.
Definition ReadView.h:32
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:214
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition algorithm.h:6
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:9
IOUAmount creditLimit2(ReadView const &v, AccountID const &acc, AccountID const &iss, Currency const &cur)
Definition Credit.cpp:27
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:33
IOUAmount toAmount< IOUAmount >(STAmount const &amt)