Skip to content

Commit c893f11

Browse files
committed
Trader app - testing
1 parent 7d97a59 commit c893f11

9 files changed

Lines changed: 139 additions & 63 deletions

File tree

web/apps/p2p/app.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,11 +297,27 @@ function focusOffer(id) {
297297

298298
function cancelOffer(event, id) {
299299
startWaitForProcess();
300-
paction(event, 'cancelOffer', [id], {afterUpdate:()=>{
301-
focusOffer(id)
302-
}})
300+
paction(event, 'cancelOffer', [id], {onComplete: stopWaitProcess})
303301
}
304302
function depositFromWallet(event) {
305303
startWaitForProcess();
306304
paction(event, 'depositFromWallet', {}, {update: '#offer-modal .modal-content'})
305+
}
306+
function transferFromWallet(event) {
307+
startWaitForProcess();
308+
paction(event, 'transferFromWallet', {}, {update: '#offer-modal .modal-content'})
309+
}
310+
311+
function createBuyOffer(event) {
312+
paction(event, 'createBuyOffer', {}, {beforeSend: startWaitForProcess, onComplete: stopWaitProcess})
313+
}
314+
function createSellOffer(event) {
315+
paction(event, 'createSellOffer', {}, {beforeSend: startWaitForProcess, onComplete: stopWaitProcess})
316+
}
317+
318+
function acceptBuyOffer(event, id) {
319+
paction(event, 'acceptBuyOffer', id, {beforeSend: startWaitForProcess, onComplete: stopWaitProcess} )
320+
}
321+
function acceptSellOffer(event, id) {
322+
paction(event, 'acceptSellOffer', id, {beforeSend: startWaitForProcess, onComplete: stopWaitProcess} )
307323
}

web/apps/p2p/cli.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
CliService::checkAssetsTransferring();
5555
CliService::checkExpiredAcceptedOffers();
5656
CliService::checkExpiredCreatedOffers();
57+
CliService::checkDepositingOffers();
5758
CliService::checkAssetsDeposits();
5859
} else if ($command == "processDeposit") {
5960
$symbol = @$argv[2];

web/apps/p2p/inc/class/AppView.php

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,13 @@ function cancelOffer($offer_id) {
6464
if($offer['status']==OfferService::STATUS_CREATED) {
6565
OfferService::cancelCreatedOffer($offer['id']);
6666
$this->success("Offer #$offer_id has been cancelled");
67+
Pajax::executeScript('focusOffer', $offer['id']);
6768
return;
6869
}
6970
if($offer['status']==OfferService::STATUS_OPEN) {
7071
OfferService::cancelOpenOffer($offer);
7172
$this->success("Offer #$offer_id has been cancelled");
73+
Pajax::executeScript('focusOffer', $offer['id']);
7274
return;
7375
}
7476
if($offer['status']==OfferService::STATUS_ACCEPTED) {
@@ -140,10 +142,7 @@ function createBuyOffer() {
140142
OfferService::storeUserCoinAddress(OfferService::userAddress(), $this->base, $this->base_receive_address);
141143
$this->clearTradeInputs();
142144
$this->success("Your offer has been created");
143-
Pajax::executeScript('stopWaitProcess');
144-
Pajax::executeScript('openHistoryTab');
145-
Pajax::executeScript('openOffer', $offer_id);
146-
Pajax::executeScript('flashOfferRow', $offer_id);
145+
Pajax::executeScript('focusOffer', $offer_id);
147146
}
148147
function createSellOffer() {
149148
if(empty($this->sell_base_amount)) {
@@ -206,7 +205,7 @@ function createSellOffer() {
206205
OfferService::storeUserCoinAddress(OfferService::userAddress(), $this->quote, $this->quote_receive_address);
207206
$this->clearTradeInputs();
208207
$this->success("Your offer has been created");
209-
Pajax::executeScript('openOffer', $offer_id);
208+
Pajax::executeScript('focusOffer', $offer_id);
210209
}
211210

212211
function clearTradeInputs() {
@@ -362,20 +361,26 @@ function transferFromWallet() {
362361
if($offer['type']==OfferService::TYPE_SELL) {
363362
$service = OfferService::getService($offer['quote_service']);
364363
$amount = $offer['base_amount']*$offer['base_price'] + $offer['quote_dust_amount'];
364+
$symbol = $offer['base'];
365365
} else {
366366
$service = OfferService::getService($offer['base_service']);
367367
$amount = $offer['base_amount'] + $offer['base_dust_amount'];
368+
$symbol = $offer['quote'];
368369
}
369370
$_SESSION['offer'] = $offer;
371+
_log("transferFromWallet: $symbol Offer #".$offer['id']." amount=$amount");
370372
$service->transferFromWallet($amount, $offer);
371373
}
372374
function transferFromWalletCallback($data) {
373375
$offer = $_SESSION['offer'];
374376
if($offer['type']==OfferService::TYPE_SELL) {
375377
$service = OfferService::getService($offer['quote_service']);
378+
$symbol = $offer['base'];
376379
} else {
377380
$service = OfferService::getService($offer['base_service']);
381+
$symbol = $offer['quote'];
378382
}
383+
_log("transferFromWalletCallback: $symbol Offer #".$offer['id']." data=".json_encode($data));
379384
$service->transferFromWalletCallback($offer, $data);
380385
}
381386

@@ -593,7 +598,11 @@ function acceptedViewTemplate($offer) {
593598
<h6>You need to transfer your <?= $transferCoin ?> to escrow address</h6>
594599
<dl class="row my-3">
595600
<dt class="col-sm-3">Transfer amount:</dt>
596-
<dd class="col-sm-9 fw-bold"><?= $transferAmount ?> <?= $transferCoin ?></dd>
601+
<dd class="col-sm-9 fw-bold">
602+
<span><?= $transferAmount ?></span>
603+
<span class="fa fa-copy" data-bs-toggle="tooltip" title="Copy" onclick="copyToClipboard(event, '<?=$transferAmount ?>')"
604+
style="cursor: pointer"></span>
605+
<?= $transferCoin ?></dd>
597606
<dt class="col-sm-3">Transfer to address:</dt>
598607
<dd class="col-sm-9">
599608
<a class="fw-bold text-break" href="<?= $transferAddressLink ?>" target="_blank"><?=$transferAddress ?></a>
@@ -616,13 +625,8 @@ function acceptedViewTemplate($offer) {
616625
You have 2 hours to make transfer. After that time offer will expire
617626
</p>
618627
<div class="d-flex align-items-baseline gap-2 flex-wrap">
619-
<?php if($offer['type']==OfferService::TYPE_SELL) { ?>
620-
<a href="" class="btn btn-primary" onclick="paction(event, 'transferFromWallet', {}, {update: '#offer-modal .modal-content'}); return false">Transfer from wallet</a>
621-
<?php } else { ?>
622-
<a href="" class="btn btn-primary" onclick="paction(event, 'transferFromWallet'); return false">Transfer from wallet</a>
623-
<?php } ?>
624-
You can also transfer directly from your wallet
625-
</p>
628+
<a href="" class="btn btn-primary" onclick="transferFromWallet(event); return false">Transfer from wallet</a>
629+
You can also transfer directly from your wallet
626630
</div>
627631
</div>
628632
<div class="text-sm-center text-start">
@@ -916,7 +920,8 @@ function acceptSellOffer($offer_id) {
916920
$this->clearTradeInputs();
917921
OfferService::storeUserCoinAddress(OfferService::userAddress(), $this->base, $this->base_receive_address);
918922
$this->success("You accepted offer");
919-
Pajax::executeScript('openOffer', $offer['id']);
923+
_log("acceptSellOffer: " .$offer['base']." User ".OfferService::userAddress()." accepted offer #".$offer['id']);
924+
Pajax::executeScript('focusOffer', $offer['id']);
920925
}
921926

922927
function acceptBuyOffer($offer_id) {
@@ -954,7 +959,8 @@ function acceptBuyOffer($offer_id) {
954959
$this->clearTradeInputs();
955960
OfferService::storeUserCoinAddress(OfferService::userAddress(), $this->quote, $this->quote_receive_address);
956961
$this->success("You accepted offer");
957-
Pajax::executeScript('openOffer', $offer['id']);
962+
_log("acceptBuyOffer: " .$offer['base']." User ".OfferService::userAddress()." accepted offer #".$offer['id']);
963+
Pajax::executeScript('focusOffer', $offer['id']);
958964
}
959965

960966
function getChartData($params) {

web/apps/p2p/inc/class/CliService.php

Lines changed: 83 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -178,9 +178,8 @@ public static function checkAssetTransferring($asset)
178178
public static function processTreansferred()
179179
{
180180
$offers = OfferService::getTransferredOffers();
181-
_log("Processing treansferred offers count=".count($offers));
181+
_log("Processing transferred offers count=".count($offers));
182182
foreach ($offers as $offer) {
183-
_log("Processing treansferred offer #{$offer['id']}");
184183
$base_transfer_tx_id = null;
185184
$quote_transfer_tx_id = null;
186185
$market = OfferService::getMarket($offer['market_id']);
@@ -189,32 +188,36 @@ public static function processTreansferred()
189188
$amount = $offer['base_amount'];
190189
$toAddress = $offer['base_receive_address'];
191190
$base_transfer_tx_id = $baseService->createPayment($amount, $toAddress, $offer);
191+
$symbol = $market['base'];
192192
if(!$base_transfer_tx_id) {
193-
_log("Failed to create PHPCoin Payment");
193+
_log("$symbol Offer #{$offer['id']} - Failed to create $symbol Payment");
194194
} else {
195195
$res = OfferService::setBaseTransferTxId($base_transfer_tx_id, $offer['id'], $market['base_asset_id']);
196196
if(!$res) {
197-
_log("Failed to set base_transfer_tx_id");
197+
_log("$symbol Offer #{$offer['id']} - Failed to set base_transfer_tx_id");
198198
}
199+
_log("$symbol Offer #{$offer['id']} - Created $symbol payment txid=$base_transfer_tx_id");
199200
}
200201
}
201202
if(empty($offer['quote_transfer_tx_id'])) {
202203
$quoteService = OfferService::getService($market['quote_service']);
203204
$amount = $offer['base_amount'] * $offer['base_price'];
204205
$toAddress = $offer['quote_receive_address'];
205206
$quote_transfer_tx_id = $quoteService->createPayment($amount, $toAddress, $offer);
207+
$symbol = $market['quote'];
206208
if(!$quote_transfer_tx_id) {
207-
_log("Failed to create Coin Payment");
209+
_log("$symbol Offer #{$offer['id']} - Failed to create $symbol Payment");
208210
} else {
209211
$res = OfferService::setQuoteTransferTxId($quote_transfer_tx_id, $offer['id'], $market['quote_asset_id']);
210212
if(!$res) {
211-
_log("Failed to set quote_transfer_tx_id");
213+
_log("$symbol Offer #{$offer['id']} - Failed to set quote_transfer_tx_id");
212214
}
215+
_log("$symbol Offer #{$offer['id']} - Created $symbol payment txid=$quote_transfer_tx_id");
213216
}
214217
}
215218
$res = OfferService::setOfferPaying($offer['id']);
216219
if(!$res) {
217-
_log("Failed to set offer paying");
220+
_log("Offer #{$offer['id']} - Failed to set offer paying");
218221
}
219222
}
220223
}
@@ -225,42 +228,52 @@ public static function processPayingOffers()
225228
_log("Processing paying offers count=".count($offers));
226229
foreach ($offers as $offer) {
227230
$market = OfferService::getMarket($offer['market_id']);
231+
$base_completed = false;
228232
if(empty($offer['base_transfer_tx_id'])) {
229-
_log("No base_transfer_tx_id");
230-
continue;
231-
}
232-
$baseService = OfferService::getService($market['base_service']);
233-
$tx = $baseService->findTransaction($offer['base_transfer_tx_id']);
234-
if(!$tx) {
235-
_log("Not found php payment tx id");
236-
continue;
237-
}
238-
$block_height = $baseService->getLastHeight();
239-
$confirmations= $block_height - $tx['height'];
240-
$requiredConfirmations=$baseService->getConfirmations('paying');
241-
if($confirmations <= $requiredConfirmations) {
242-
_log("Offer #".$offer['id']." base=".$market['base']." tx=".$offer['base_transfer_tx_id']." Waiting confirmations $confirmations / ".$requiredConfirmations);
243-
continue;
233+
_log("Offer #{$offer['id']} - No base_transfer_tx_id");
234+
} else {
235+
$baseService = OfferService::getService($market['base_service']);
236+
$tx = $baseService->findTransaction($offer['base_transfer_tx_id']);
237+
if(!$tx) {
238+
_log("Offer #{$offer['id']} Not found php payment tx id");
239+
} else {
240+
$block_height = $baseService->getLastHeight();
241+
$confirmations= $block_height - $tx['height'];
242+
$requiredConfirmations=$baseService->getConfirmations('paying');
243+
if($confirmations <= $requiredConfirmations) {
244+
_log("Offer #".$offer['id']." base=".$market['base']." tx=".$offer['base_transfer_tx_id']." Waiting confirmations $confirmations / ".$requiredConfirmations);
245+
} else {
246+
_log("Offer #".$offer['id']." base=".$market['base']." tx=".$offer['base_transfer_tx_id']." - completed");
247+
$base_completed = true;
248+
}
249+
}
250+
244251
}
252+
253+
$quote_completed = false;
245254
if(empty($offer['quote_transfer_tx_id'])) {
246-
_log("No quote_transfer_tx_id");
247-
continue;
248-
}
249-
$quoteService = OfferService::getService($market['quote_service']);
250-
$tx = $quoteService->findTransaction($offer['quote_transfer_tx_id']);
251-
if(!$tx) {
252-
_log('Not found coin payment tx id');
253-
continue;
255+
_log("Offer #{$offer['id']} - No quote_transfer_tx_id");
256+
} else {
257+
$quoteService = OfferService::getService($market['quote_service']);
258+
$tx = $quoteService->findTransaction($offer['quote_transfer_tx_id']);
259+
if(!$tx) {
260+
_log("Offer #{$offer['id']} - Not found coin payment tx id");
261+
} else {
262+
$block_height = $quoteService->getLastHeight();
263+
$confirmations= $block_height - $tx['height'];
264+
$requiredConfirmations=$baseService->getConfirmations('paying');
265+
if($confirmations <= $requiredConfirmations) {
266+
_log("Offer #".$offer['id']." quote=".$market['quote']." tx=".$offer['quote_transfer_tx_id']." Waiting confirmations $confirmations / ".$requiredConfirmations);
267+
} else {
268+
_log("Offer #".$offer['id']." quote=".$market['quote']." tx=".$offer['quote_transfer_tx_id']." - completed");
269+
$quote_completed = true;
270+
}
271+
}
254272
}
255-
$block_height = $quoteService->getLastHeight();
256-
$confirmations= $block_height - $tx['height'];
257-
$requiredConfirmations=$baseService->getConfirmations('paying');
258-
if($confirmations <= $requiredConfirmations) {
259-
_log("Offer #".$offer['id']." base=".$market['quote']." tx=".$offer['quote_transfer_tx_id']." Waiting confirmations $confirmations / ".$requiredConfirmations);
260-
continue;
273+
if($base_completed && $quote_completed) {
274+
_log("Offer #".$offer['id']." payments completed - closed");
275+
OfferService::setOfferClosed($offer);
261276
}
262-
_log("Offer #".$offer['id']." payments completed - closed");
263-
OfferService::setOfferClosed($offer);
264277
}
265278
}
266279

@@ -327,4 +340,36 @@ public static function returnDeposit(string $symbol, string $deposit_tx_id)
327340
$hash = $service->createPayment($amount, $dst, null);
328341
_log("$symbol Return deposit tx $deposit_tx_id - created transaction $hash");
329342
}
343+
344+
public static function checkDepositingOffers()
345+
{
346+
$offers = OfferService::getDepositingOffers();
347+
_log("Found ".count($offers)." depositing offers");
348+
foreach($offers as $offer) {
349+
$deposit_tx_id = $offer['deposit_tx_id'];
350+
$market = OfferService::getMarket($offer['market_id']);
351+
if($offer['type']==OfferService::TYPE_SELL) {
352+
$symbol = $market['base'];
353+
$service = OfferService::getService($market['base_service']);
354+
} else {
355+
$symbol = $market['quote'];
356+
$service = OfferService::getService($market['quote_service']);
357+
}
358+
$depositTx = $service->findTransaction($deposit_tx_id);
359+
if(!$depositTx) {
360+
_log("$symbol Offer #{$offer['id']} Not found deposit tx $deposit_tx_id");
361+
continue;
362+
}
363+
$block_height = $service->getLastHeight();
364+
$last_height = $depositTx['height'];
365+
$confirmations= $block_height - $last_height;
366+
$requiredConfirmations=$service->getConfirmations('wait');
367+
if($confirmations <= $requiredConfirmations) {
368+
_log($symbol. " Offer #{$offer['id']} deposit {$deposit_tx_id} waiting confirmations $confirmations/".$requiredConfirmations);
369+
return;
370+
}
371+
OfferService::setOfferOpen($offer['id']);
372+
_log($symbol. " Offer #{$offer['id']} Deposit #{$deposit_tx_id} - set status opened");
373+
}
374+
}
330375
}

web/apps/p2p/inc/class/OfferService.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ public static function cancelAcceptedOffer($offer)
142142
} else {
143143
OfferService::cancelAcceptedBuyOffer($offer['id']);
144144
}
145+
_log("cancelAcceptedOffer: ".$offer['base']." User ".OfferService::userAddress()." cancel accepted offer #{$offer['id']}");
145146
OfferService::saveOfferLog($offer['id']);
146147
});
147148
}
@@ -784,4 +785,10 @@ public static function getOfferHistory(mixed $id)
784785
return $rows;
785786
}
786787

788+
public static function getDepositingOffers()
789+
{
790+
$sql="select * from p2p_offers o where o.status = ? order by o.created_at";
791+
return self::rows($sql, ['depositing']);
792+
}
793+
787794
}

web/apps/p2p/inc/class/PhpCoinService.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ public function depositFromWalletCallback(mixed $offer, $data)
124124
$deposit_tx_id = $data['res'];
125125
$offer_id = $offer['id'];
126126
OfferService::setOfferDepositing($offer_id, $deposit_tx_id);
127+
$_SESSION['offer_id']=$offer_id;
127128
}
128129
Pajax::redirect(AppView::BASE_URL);
129130
}

web/apps/p2p/inc/class/UsdtPolygonService.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ public function transferFromWalletCallback(mixed $offer, $data)
168168
{
169169
$hash = $data['hash'];
170170
OfferService::setAcceptedOfferTransferring($offer['id'], $hash);
171-
$_SESSION['offer_id'] = $offer['id'];
172-
Pajax::redirect(AppView::BASE_URL);
171+
Pajax::executeScript('focusOffer', $offer['id']);
173172
}
174173

175174
public function getEscrowAddress()

0 commit comments

Comments
 (0)