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 <xrpld/ledger/ReadView.h>
21
22#include <xrpl/protocol/AmountConversions.h>
23#include <xrpl/protocol/Indexes.h>
24#include <xrpl/protocol/STAmount.h>
25
26namespace ripple {
27
28STAmount
30 ReadView const& view,
31 AccountID const& account,
32 AccountID const& issuer,
33 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(
42 account < issuer ? sfLowLimit : sfHighLimit);
43 result.setIssuer(account);
44 }
45
46 XRPL_ASSERT(
47 result.getIssuer() == account,
48 "ripple::creditLimit : result issuer match");
49 XRPL_ASSERT(
50 result.getCurrency() == currency,
51 "ripple::creditLimit : result currency match");
52 return result;
53}
54
55IOUAmount
57 ReadView const& v,
58 AccountID const& acc,
59 AccountID const& iss,
60 Currency const& cur)
61{
62 return toAmount<IOUAmount>(creditLimit(v, acc, iss, cur));
63}
64
65STAmount
67 ReadView const& view,
68 AccountID const& account,
69 AccountID const& issuer,
70 Currency const& currency)
71{
72 STAmount result(Issue{currency, account});
73
74 auto sleRippleState = view.read(keylet::line(account, issuer, currency));
75
76 if (sleRippleState)
77 {
78 result = sleRippleState->getFieldAmount(sfBalance);
79 if (account < issuer)
80 result.negate();
81 result.setIssuer(account);
82 }
83
84 XRPL_ASSERT(
85 result.getIssuer() == account,
86 "ripple::creditBalance : result issuer match");
87 XRPL_ASSERT(
88 result.getCurrency() == currency,
89 "ripple::creditBalance : result currency match");
90 return result;
91}
92
93} // namespace ripple
A currency issued by an account.
Definition: Issue.h:36
A view into a ledger.
Definition: ReadView.h:52
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:236
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: algorithm.h:26
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:29
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:66
IOUAmount creditLimit2(ReadView const &v, AccountID const &acc, AccountID const &iss, Currency const &cur)
Definition: Credit.cpp:56