-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathCredit.cpp
109 lines (85 loc) · 2.82 KB
/
Credit.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
This file is part of the Zero Reserve Plugin for Retroshare.
Zero Reserve is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Zero Reserve is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Zero Reserve. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Credit.h"
#include "zrdb.h"
#include "Currency.h"
#include "ZeroReservePlugin.h"
#include "p3ZeroReserverRS.h"
#include <stdexcept>
void Credit::getCreditList( CreditList & outList, const std::string & id )
{
ZrDB::Instance()->loadPeer( id, outList );
}
Credit::Credit( const std::string & id, const std::string & currencySym ) :
m_id( id ),
m_currency( currencySym )
{
if( Currency::getCurrencyBySymbol( currencySym) == Currency::INVALID ){
throw std::runtime_error( "Credit::Credit(): Invalid currency symbol" );
}
}
ZR::RetVal Credit::allocate( const ZR::ZR_Number & amount)
{
if( amount > getPeerAvailable() ){
return ZR::ZR_FAILURE;
}
m_allocated += amount;
ZrDB::Instance()->updatePeerCredit( *this, "allocation", m_allocated );
return ZR::ZR_SUCCESS;
}
void Credit::deallocate( const ZR::ZR_Number & amount)
{
m_allocated -= amount;
try{
ZrDB::Instance()->updatePeerCredit( *this, "allocation", m_allocated );
}
catch( std::exception e ){
g_ZeroReservePlugin->placeMsg( std::string( "Exception caught: " ) + e.what() + " Cannot deallocate funds" );
}
}
void Credit::updateCredit()
{
try{
ZrDB * db = ZrDB::Instance();
if( !db->peerExists( *this )){
db->createPeerRecord( *this );
}
db->updatePeerCredit( *this, "credit", m_credit );
}
catch( std::exception e ){
g_ZeroReservePlugin->placeMsg( std::string( "Exception caught: " ) + e.what() + " Cannot update credit" );
}
}
void Credit::updateOurCredit()
{
try{
ZrDB * db = ZrDB::Instance();
if( !db->peerExists( *this )){
db->createPeerRecord( *this );
}
db->updatePeerCredit( *this, "our_credit", m_our_credit );
}
catch( std::exception e ){
g_ZeroReservePlugin->placeMsg( e.what() );
}
}
void Credit::loadPeer()
{
ZrDB::Instance()->loadPeer( *this );
}
void Credit::publish()
{
p3ZeroReserveRS * p3zr = static_cast< p3ZeroReserveRS* >( g_ZeroReservePlugin->rs_pqi_service() );
p3zr->sendCredit( this );
}