@@ -96,12 +96,10 @@ private function verifySources()
96
96
//verify source/watch addresses
97
97
$ verify = new BTCValidate ;
98
98
if (!$ verify ->checkAddress ($ this ->source_address )){
99
- echo "Error: Invalid source address! \n" ;
100
- die ();
99
+ throw new Exception ("Error: Invalid source address! \n" );
101
100
}
102
101
if (!$ verify ->checkAddress ($ this ->watch_address )){
103
- echo "Error: Invalid watch address! \n" ;
104
- die ();
102
+ throw new Exception ("Error: Invalid watch address! \n" );
105
103
}
106
104
107
105
if (!$ this ->source_pubkey ){
@@ -114,12 +112,10 @@ private function verifySources()
114
112
$ this ->source_pubkey = $ validate ['pubkey ' ];
115
113
}
116
114
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" );
119
116
}
120
117
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" );
123
119
}
124
120
}
125
121
}
@@ -166,8 +162,7 @@ protected function grabTokenInfo()
166
162
}
167
163
}
168
164
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" );
171
166
}
172
167
}
173
168
@@ -186,8 +181,7 @@ protected function getIncomingSends()
186
181
187
182
}
188
183
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" );
191
185
}
192
186
193
187
return $ sendsFound ;
@@ -429,16 +423,14 @@ protected function vend($sends)
429
423
}
430
424
}
431
425
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" );
434
427
}
435
428
436
429
foreach ($ vendingTokens as $ vendAsset => $ vendAmount ){
437
430
438
431
if ($ vendAsset == 'BTC ' ){
439
432
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" );
442
434
}
443
435
}
444
436
else {
@@ -460,8 +452,7 @@ protected function vend($sends)
460
452
$ this ->autoInflateToken ($ vendAsset , $ needed );
461
453
return array ();
462
454
}
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" );
465
456
}
466
457
}
467
458
}
@@ -480,25 +471,33 @@ protected function vend($sends)
480
471
$ this ->btc ->walletpassphrase (XCP_WALLET , 300 );
481
472
}
482
473
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" );
485
475
}
486
476
echo "Wallet unlocked \n" ;
487
477
488
478
//loop through pending sends
489
479
foreach ($ sends as $ send ){
490
-
480
+ $ sendTX = false ;
481
+ $ refunded = false ;
491
482
try {
492
483
switch ($ send ['vend_token ' ]){
493
484
case 'BTC ' :
494
485
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
+ }
497
496
break ;
498
497
default :
499
498
echo "Sending XCP TX \n" ;
500
499
//send out counterparty tokens
501
- $ quantity = round ($ send ['amount ' ] * SATOSHI_MOD );
500
+ $ quantity = ( int ) round (round ( $ send ['amount ' ], 8 ) * SATOSHI_MOD );
502
501
$ sendData = array ('source ' => $ this ->source_address , 'destination ' => $ send ['send_to ' ],
503
502
'asset ' => $ send ['vend_token ' ], 'quantity ' => $ quantity , 'allow_unconfirmed_inputs ' => true ,
504
503
'pubkey ' => $ this ->source_pubkey ,
@@ -514,36 +513,39 @@ protected function vend($sends)
514
513
}
515
514
}
516
515
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" );
519
517
}
520
518
521
519
522
520
//save incoming/outgoing transactions
523
521
$ 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 ,
531
543
'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" ;
534
546
}
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 ' ]));
545
547
546
- echo ' Vended ' . $ send [ ' amount ' ]. ' ' . $ send [ ' vend_token ' ]. ' to ' . $ send [ ' send_to ' ]. ' : ' . $ sendTX . " " . timestamp (). "\n" ;
548
+
547
549
//wait a few seconds to avoid sending transactions too fast and causing errors
548
550
sleep (10 );
549
551
}
@@ -635,8 +637,7 @@ protected function autoInflateToken($token, $needed = 0)
635
637
$ this ->btc ->walletpassphrase (XCP_WALLET , 300 );
636
638
}
637
639
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" );
640
641
}
641
642
642
643
$ issueData = array ('source ' => $ this ->source_address , 'quantity ' => $ newTokens ,
@@ -810,4 +811,54 @@ public function getLatestRate($rate, $token)
810
811
}
811
812
return floatval ($ rate );
812
813
}
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
+ }
813
864
}
0 commit comments