-
Notifications
You must be signed in to change notification settings - Fork 15
/
BtcContract.cpp
202 lines (171 loc) · 6.56 KB
/
BtcContract.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
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 "BtcContract.h"
#include "ZRBitcoin.h"
#include "Payment.h"
#include "zrdb.h"
#include "ZeroReservePlugin.h"
#ifdef ZR_TESTNET
const unsigned int BtcContract::reqConfirmations = 2;
#else
const unsigned int BtcContract::reqConfirmations = 6;
#endif
std::list< BtcContract* > BtcContract::contracts;
RsMutex BtcContract::m_contractMutex("ContractMutex");
const static qint64 contract_timeout = 86400000; // one day
void BtcContract::pollContracts()
{
RsStackMutex contractMutex( m_contractMutex );
for( ContractIterator it = contracts.begin(); it != contracts.end(); ){
BtcContract * contract = *it;
if( contract->poll() ){
delete contract;
it = contracts.erase( it );
}
else{
it++;
}
}
}
void BtcContract::rmContract( BtcContract * contract )
{
RsStackMutex contractMutex( m_contractMutex );
for( ContractIterator it = contracts.begin(); it != contracts.end(); it++){
BtcContract * c = *it;
if( c->m_counterParty == contract->m_counterParty && c->m_btcTxId == contract->m_btcTxId ){
if( !contract->m_btcTxId.empty() ){
try{
ZrDB::Instance()->rmBtcContract( contract->m_btcTxId, contract->m_party );
}
catch( std::exception e ){
g_ZeroReservePlugin->placeMsg( std::string( "Exception caught: " ) + e.what() + " Can't remove contract " + contract->m_btcTxId );
}
}
if( contract->m_party == RECEIVER )
c->deallocateFunds( c->getFiatAmount() );
delete c;
contracts.erase( it );
break;
}
}
}
///// End static functions
BtcContract::BtcContract( const ZR::ZR_Number & btcAmount, const ZR::ZR_Number & fee, const ZR::ZR_Number & price , const std::string & currencySym , Party party, const std::string & counterParty, const qint64 creationtime ):
m_btcAmount( btcAmount ),
m_price( price ),
m_currencySym( currencySym ),
m_party( party ),
m_counterParty( counterParty ),
m_activated( false ),
m_fee( fee )
{
if( creationtime == 0 ){
m_creationtime = QDateTime::currentMSecsSinceEpoch(); // newly created contract gets a current timestamp
if( party == RECEIVER ){ // the payment receiver needs to make sure account is not overdrawn
ZR::ZR_Number fiatAmount = btcAmount * price;
Credit c( counterParty, currencySym );
c.loadPeer();
if( c.getPeerAvailable() <= fee ) throw std::runtime_error( "Insufficient funds" );
if( c.getPeerAvailable() < fiatAmount + fee ){
fiatAmount = c.getPeerAvailable() - fee;
m_btcAmount = fiatAmount / price;
}
c.allocate( fiatAmount );
}
}
else{
m_creationtime = creationtime; // contract loaded from DB
}
RsStackMutex contractMutex( m_contractMutex );
contracts.push_back( this );
}
BtcContract::~BtcContract()
{
}
bool BtcContract::poll()
{
// is it timed out?
if( QDateTime::currentMSecsSinceEpoch() - m_creationtime > contract_timeout ){
if( !m_btcTxId.empty() ){
try{
ZrDB::Instance()->rmBtcContract( m_btcTxId, m_party );
}
catch( std::exception e ){
g_ZeroReservePlugin->placeMsg( std::string( "Exception caught: " ) + e.what() + " Can't remove contract " + m_btcTxId );
}
}
return true;
}
if( !m_activated ) return false; // not yet active
// is the condition for settlement met?
unsigned int confirmations = ZR::Bitcoin::Instance()->getConfirmations( m_btcTxId );
std::cerr << "Zero Reserve: Contract: " << m_btcTxId << " : " << confirmations << " confirmations." << std::endl;
if( confirmations >= reqConfirmations ){
// TODO: Check BTC Address and amount
ZrDB::Instance()->beginTx();
execute();
if( !m_btcTxId.empty() ){
try{
ZrDB::Instance()->rmBtcContract( m_btcTxId, m_party );
ZrDB::Instance()->commitTx();
}
catch( std::exception e ){
g_ZeroReservePlugin->placeMsg( std::string( "Exception caught: " ) + e.what() + " Can't remove contract " + m_btcTxId );
ZrDB::Instance()->rollbackTx();
}
}
return true;
}
return false;
}
void BtcContract::deallocateFunds( const ZR::ZR_Number & amount )
{
Credit c( m_counterParty, m_currencySym );
c.loadPeer();
c.deallocate( amount );
}
void BtcContract::persist()
{
ZrDB::Instance()->addBtcContract( this );
}
void BtcContract::execute()
{
std::cerr << "Zero Reserve: Contract: Settling for ID: " << m_btcTxId << std::endl;
ZR::ZR_Number fiatAmount = m_btcAmount * m_price;
if( m_party == SENDER ){
PaymentSpender p( m_counterParty, fiatAmount, m_currencySym, Payment::PAYMENT );
if( p.init() == ZR::ZR_FAILURE ){
g_ZeroReservePlugin->placeMsg( std::string( "Cannot execute payment of " ) + fiatAmount.toDecimalStdString() + " to " + m_counterParty );
return;
}
p.commit();
}
else {
deallocateFunds( getFiatAmount() );
PaymentReceiver p( m_counterParty, fiatAmount, m_currencySym, Payment::PAYMENT );
if( p.init() == ZR::ZR_FAILURE ){
g_ZeroReservePlugin->placeMsg( std::string( "Cannot execute payment of " ) + fiatAmount.toDecimalStdString() + " to " + m_counterParty );
return;
}
p.commit();
}
}
void BtcContract::setBtcAmount( const ZR::ZR_Number & btcAmount )
{
ZR::ZR_Number fiatAmount1 = getFiatAmount();
m_btcAmount = btcAmount;
ZR::ZR_Number fiatAmount2 = getFiatAmount();
if( m_party == RECEIVER )
deallocateFunds( fiatAmount1 - fiatAmount2 );
}