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/STAmount.h>
23 #include <ripple/protocol/Indexes.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(
37  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  assert (result.getIssuer () == account);
47  assert (result.getCurrency () == currency);
48  return result;
49 }
50 
51 IOUAmount
53  ReadView const& v,
54  AccountID const& acc,
55  AccountID const& iss,
56  Currency const& cur)
57 {
58  return toAmount<IOUAmount> (creditLimit (v, acc, iss, cur));
59 }
60 
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(
70  keylet::line(account, issuer, currency));
71 
72  if (sleRippleState)
73  {
74  result = sleRippleState->getFieldAmount (sfBalance);
75  if (account < issuer)
76  result.negate ();
77  result.setIssuer (account);
78  }
79 
80  assert (result.getIssuer () == account);
81  assert (result.getCurrency () == currency);
82  return result;
83 }
84 
85 } // 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:52
ripple::sfHighLimit
const SF_Amount sfHighLimit(access, STI_AMOUNT, 7, "HighLimit")
Definition: SField.h:429
ripple::toAmount< IOUAmount >
IOUAmount toAmount< IOUAmount >(STAmount const &amt)
Definition: AmountConversions.h:82
ripple::sfLowLimit
const SF_Amount sfLowLimit(access, STI_AMOUNT, 6, "LowLimit")
Definition: SField.h:428
ripple::base_uint
Definition: base_uint.h:65
ripple::keylet::line
static const line_t line
Definition: Indexes.h:176
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:42
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:186
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(access, STI_AMOUNT, 2, "Balance")
Definition: SField.h:424