Skip to content

Commit 7269d6a

Browse files
committed
fixed a few more bugs, partially implemented refunds (now refunds any XCP tokens if sending BTC amount and BTC is below dust limit)
1 parent 5167ee2 commit 7269d6a

File tree

3 files changed

+112
-55
lines changed

3 files changed

+112
-55
lines changed

crypto-gateway.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@
2020
echo "Gateway(s) in progress \n";
2121
while(true){
2222
foreach($gateways as $name => $gateway){
23-
$gateway->init();
23+
try{
24+
$gateway->init();
25+
}
26+
catch(Exception $e){
27+
echo 'Error: '.$e->getMessage()."\n";
28+
continue;
29+
}
30+
sleep(10);
2431
}
2532
//wait a few minutes before running the loop again
2633
sleep(300);

lib/BitcoinRPC.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,13 +163,12 @@ public function __call($method,$params) {
163163
// check
164164

165165
if (!isset($response['id']) OR $response['id'] != $currentId) {
166-
debug($response);
167-
debug($request);
168-
throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.$response['id'].')');
166+
if(isset($response['data'])){
167+
throw new Exception('Server Error: '.$response['data']);
168+
}
169+
throw new Exception('Incorrect response id (request id: '.$currentId.', response id: '.@$response['id'].')');
169170
}
170-
if (isset($response['error']) AND !is_null($response['error'])) {
171-
debug($response);
172-
debug($request);
171+
if (isset($response['error']) AND !is_null($response['error'])) {
173172
throw new Exception('Request error: '.$response['error']['message']);
174173
}
175174

lib/Gateway.php

Lines changed: 99 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,10 @@ private function verifySources()
9696
//verify source/watch addresses
9797
$verify = new BTCValidate;
9898
if(!$verify->checkAddress($this->source_address)){
99-
echo "Error: Invalid source address!\n";
100-
die();
99+
throw new Exception("Error: Invalid source address!\n");
101100
}
102101
if(!$verify->checkAddress($this->watch_address)){
103-
echo "Error: Invalid watch address!\n";
104-
die();
102+
throw new Exception("Error: Invalid watch address!\n");
105103
}
106104

107105
if(!$this->source_pubkey){
@@ -114,12 +112,10 @@ private function verifySources()
114112
$this->source_pubkey = $validate['pubkey'];
115113
}
116114
catch(Exception $e){
117-
echo 'Error getting source address ['.$this->source_address.'] pubkey:'. $e->getMessage()."\n";
118-
die();
115+
throw new Exception('Error getting source address ['.$this->source_address.'] pubkey:'. $e->getMessage()."\n");
119116
}
120117
if(!$this->source_pubkey){
121-
echo "Could not get source address pubkey [".$this->source_address."]\n";
122-
die();
118+
throw new Exception("Could not get source address pubkey [".$this->source_address."]\n");
123119
}
124120
}
125121
}
@@ -166,8 +162,7 @@ protected function grabTokenInfo()
166162
}
167163
}
168164
catch(Exception $e){
169-
echo "Error obtaining asset info: ".$e->getMessage()."\n";
170-
die();
165+
throw new Exception("Error obtaining asset info: ".$e->getMessage()."\n");
171166
}
172167
}
173168

@@ -186,8 +181,7 @@ protected function getIncomingSends()
186181

187182
}
188183
catch(Exception $e){
189-
echo "Error checking watch address balances: ".$e->getMessage()." ".timestamp()."\n";
190-
die();
184+
throw new Exception("Error checking watch address balances: ".$e->getMessage()." ".timestamp()."\n");
191185
}
192186

193187
return $sendsFound;
@@ -429,16 +423,14 @@ protected function vend($sends)
429423
}
430424
}
431425
catch(Exception $e){
432-
echo 'Error getting balances: '.$e->getMessage()." ".timestamp()."\n";
433-
die();
426+
throw new Exception('Error getting balances: '.$e->getMessage()." ".timestamp()."\n");
434427
}
435428

436429
foreach($vendingTokens as $vendAsset => $vendAmount){
437430

438431
if($vendAsset == 'BTC'){
439432
if($btc_balance < $vendAmount){
440-
echo "Insufficient balance for ".$vendAsset." (need ".convertFloat($vendAmount).") ".timestamp()."\n";
441-
die();
433+
throw new Exception("Insufficient balance for ".$vendAsset." (need ".convertFloat($vendAmount).") ".timestamp()."\n");
442434
}
443435
}
444436
else{
@@ -460,8 +452,7 @@ protected function vend($sends)
460452
$this->autoInflateToken($vendAsset, $needed);
461453
return array();
462454
}
463-
echo "Insufficient balance for ".$vendAsset." (need ".convertFloat($vendAmount).") ".timestamp()."\n";
464-
die();
455+
throw new Exception("Insufficient balance for ".$vendAsset." (need ".convertFloat($vendAmount).") ".timestamp()."\n");
465456
}
466457
}
467458
}
@@ -480,25 +471,33 @@ protected function vend($sends)
480471
$this->btc->walletpassphrase(XCP_WALLET, 300);
481472
}
482473
catch(Exception $e){
483-
echo "Could not unlock wallet: ".$e->getMessage()." ".timestamp()."\n";
484-
die();
474+
throw new Exception("Could not unlock wallet: ".$e->getMessage()." ".timestamp()."\n");
485475
}
486476
echo "Wallet unlocked\n";
487477

488478
//loop through pending sends
489479
foreach($sends as $send){
490-
480+
$sendTX = false;
481+
$refunded = false;
491482
try{
492483
switch($send['vend_token']){
493484
case 'BTC':
494485
echo "Sending BTC TX\n";
495-
$this->btc->settxfee($this->miner_fee);
496-
$sendTX = $this->btc->sendfromaddress($this->source_address, $send['amount'], $send['send_to']);
486+
if($send['amount'] < $this->dust_size){
487+
echo "BTC TX below dust limit (".$send['amount'].")\n";
488+
$this->refund($send);
489+
$sendTX = false;
490+
$refunded = true;
491+
}
492+
else{
493+
$this->btc->settxfee($this->miner_fee);
494+
$sendTX = $this->btc->sendfromaddress($this->source_address, $send['amount'], $send['send_to']);
495+
}
497496
break;
498497
default:
499498
echo "Sending XCP TX\n";
500499
//send out counterparty tokens
501-
$quantity = round($send['amount'] * SATOSHI_MOD);
500+
$quantity = (int)round(round($send['amount'], 8) * SATOSHI_MOD);
502501
$sendData = array('source' => $this->source_address, 'destination' => $send['send_to'],
503502
'asset' => $send['vend_token'], 'quantity' => $quantity, 'allow_unconfirmed_inputs' => true,
504503
'pubkey' => $this->source_pubkey,
@@ -514,36 +513,39 @@ protected function vend($sends)
514513
}
515514
}
516515
catch(Exception $e){
517-
echo 'Error sending '.$send['vend_token'].': '.$e->getMessage()." ".timestamp()."\n";
518-
die();
516+
throw new Exception('Error sending '.$send['vend_token'].': '.$e->getMessage()." ".timestamp()."\n");
519517
}
520518

521519

522520
//save incoming/outgoing transactions
523521
$time = timestamp();
524-
foreach($send['income']['tx'] as $income){
525-
$saveReceive = $this->insert('transactions', array('type' => 'gateway_receive',
526-
'source' => $income['source'],
527-
'destination' => $this->watch_address,
528-
'amount' => $income['real_quantity'],
529-
'txId' => $income['tx_hash'],
530-
'confirmed' => 1,
522+
if(($refunded AND !$sendTX) OR ($sendTX)){
523+
foreach($send['income']['tx'] as $income){
524+
$saveReceive = $this->insert('transactions', array('type' => 'gateway_receive',
525+
'source' => $income['source'],
526+
'destination' => $this->watch_address,
527+
'amount' => $income['real_quantity'],
528+
'txId' => $income['tx_hash'],
529+
'confirmed' => 1,
530+
'txDate' => $time,
531+
'asset' => $income['asset']));
532+
echo $income['real_quantity'].' '.$income['asset']." received!\n";
533+
}
534+
}
535+
536+
if($sendTX){
537+
$saveSend = $this->insert('transactions', array('type' => 'gateway_send',
538+
'source' => $this->source_address,
539+
'destination' => $send['send_to'],
540+
'amount' => $send['amount'],
541+
'txId' => $sendTX,
542+
'confirmed' => 0,
531543
'txDate' => $time,
532-
'asset' => $income['asset']));
533-
echo $income['real_quantity'].' '.$income['asset']." received!\n";
544+
'asset' => $send['vend_token']));
545+
echo 'Vended '.$send['amount'].' '.$send['vend_token'].' to '.$send['send_to'].': '.$sendTX." ".timestamp()."\n";
534546
}
535-
536-
537-
$saveSend = $this->insert('transactions', array('type' => 'gateway_send',
538-
'source' => $this->source_address,
539-
'destination' => $send['send_to'],
540-
'amount' => $send['amount'],
541-
'txId' => $sendTX,
542-
'confirmed' => 0,
543-
'txDate' => $time,
544-
'asset' => $send['vend_token']));
545547

546-
echo 'Vended '.$send['amount'].' '.$send['vend_token'].' to '.$send['send_to'].': '.$sendTX." ".timestamp()."\n";
548+
547549
//wait a few seconds to avoid sending transactions too fast and causing errors
548550
sleep(10);
549551
}
@@ -635,8 +637,7 @@ protected function autoInflateToken($token, $needed = 0)
635637
$this->btc->walletpassphrase(XCP_WALLET, 300);
636638
}
637639
catch(Exception $e){
638-
echo "Could not unlock wallet: ".$e->getMessage()."\n";
639-
die();
640+
throw new Exception("Could not unlock wallet: ".$e->getMessage()."\n");
640641
}
641642

642643
$issueData = array('source' => $this->source_address, 'quantity' => $newTokens,
@@ -810,4 +811,54 @@ public function getLatestRate($rate, $token)
810811
}
811812
return floatval($rate);
812813
}
814+
815+
public function refund($send)
816+
{
817+
$refunds = array();
818+
$send_to = $send['send_to'];
819+
foreach($send['income']['tx'] as $tx){
820+
if(!isset($refunds[$tx['asset']])){
821+
$refunds[$tx['asset']] = 0;
822+
}
823+
$refunds[$tx['asset']] += $tx['quantity'];
824+
}
825+
826+
foreach($refunds as $asset => $amount){
827+
$sendTX = false;
828+
if($asset == 'BTC'){
829+
830+
}
831+
else{
832+
$sendData = array('source' => $this->source_address, 'destination' => $send_to,
833+
'asset' => $asset, 'quantity' => $amount, 'allow_unconfirmed_inputs' => true,
834+
'pubkey' => $this->source_pubkey,
835+
'fee' => ($this->miner_fee * SATOSHI_MOD),
836+
'regular_dust_size' => (($this->dust_size / 2) * SATOSHI_MOD),
837+
'multisig_dust_size' => (($this->dust_size / 2) * SATOSHI_MOD)
838+
);
839+
840+
$getRaw = $this->xcp->create_send($sendData);
841+
$sign = $this->xcp->sign_tx(array('unsigned_tx_hex' => $getRaw));
842+
$sendTX = $this->xcp->broadcast_tx(array('signed_tx_hex' => $sign));
843+
}
844+
845+
if($sendTX){
846+
$float_amount = round($amount / SATOSHI_MOD, 8);
847+
$saveSend = $this->insert('transactions', array('type' => 'gateway_refund',
848+
'source' => $this->source_address,
849+
'destination' => $send_to,
850+
'amount' => $float_amount,
851+
'txId' => $sendTX,
852+
'confirmed' => 0,
853+
'txDate' => timestamp(),
854+
'asset' => $asset));
855+
856+
echo 'Refunded '.$float_amount.' '.$asset.' to '.$send_to.': '.$sendTX." ".timestamp()."\n";
857+
sleep(10);
858+
}
859+
else{
860+
echo 'Refund failed.. '.$send_to."\n";
861+
}
862+
}
863+
}
813864
}

0 commit comments

Comments
 (0)