rippled
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 <ripple/ledger/ReadView.h>
21 #include <ripple/protocol/AmountConversions.h>
22 #include <ripple/protocol/Indexes.h>
23 #include <ripple/protocol/STAmount.h>
24 
25 namespace ripple {
26 
27 STAmount
29  ReadView const& view,
30  AccountID const& account,
31  AccountID const& issuer,
32  Currency const& currency)
33 {
34  STAmount result({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  assert(result.getIssuer() == account);
46  assert(result.getCurrency() == currency);
47  return result;
48 }
49 
50 IOUAmount
52  ReadView const& v,
53  AccountID const& acc,
54  AccountID const& iss,
55  Currency const& cur)
56 {
57  return toAmount<IOUAmount>(creditLimit(v, acc, iss, cur));
58 }
59 
60 STAmount
62  ReadView const& view,
63  AccountID const& account,
64  AccountID const& issuer,
65  Currency const& currency)
66 {
67  STAmount result({currency, account});
68 
69  auto sleRippleState = view.read(keylet::line(account, issuer, currency));
70 
71  if (sleRippleState)
72  {
73  result = sleRippleState->getFieldAmount(sfBalance);
74  if (account < issuer)
75  result.negate();
76  result.setIssuer(account);
77  }
78 
79  assert(result.getIssuer() == account);
80  assert(result.getCurrency() == currency);
81  return result;
82 }
83 
84 } // namespace ripple
ripple::creditBalance
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:61
ripple::creditLimit2
IOUAmount creditLimit2(ReadView const &v, AccountID const &acc, AccountID const &iss, Currency const &cur)
Definition: Credit.cpp:51
ripple::toAmount< IOUAmount >
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
Definition: AmountConversions.h:77
ripple::base_uint
Integers of any length that is a multiple of 32-bits.
Definition: base_uint.h:82
ripple::sfLowLimit
const SF_AMOUNT sfLowLimit
ripple::creditLimit
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
ripple::STAmount
Definition: STAmount.h:45
ripple::sfHighLimit
const SF_AMOUNT sfHighLimit
ripple::keylet::line
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:193
ripple::ReadView::read
virtual std::shared_ptr< SLE const > read(Keylet const &k) const =0
Return the state item associated with a key.
ripple::ReadView
A view into a ledger.
Definition: ReadView.h:125
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::sfBalance
const SF_AMOUNT sfBalance