Skip to content

Commit 7d97a59

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

12 files changed

Lines changed: 227 additions & 64 deletions

File tree

web/apps/p2p/app.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ function copyToClipboard(event, text) {
7171
function transferWithMetamask(total, usdtAddress, recipient, callback) {
7272
if (window.ethereum == null) {
7373
showError("MetaMask not installed");
74+
stopWaitProcess();
7475
return;
7576
}
7677
window.provider = new ethers.BrowserProvider(window.ethereum);
@@ -88,10 +89,12 @@ function transferWithMetamask(total, usdtAddress, recipient, callback) {
8889
}).catch(err => {
8990
console.error(err)
9091
showError("Error creating wallet transaction");
92+
stopWaitProcess();
9193
})
9294
}).catch(err => {
9395
console.error(err)
9496
showError("Error connecting metamask");
97+
stopWaitProcess();
9598
})
9699
}
97100

@@ -260,4 +263,45 @@ function closeBuyCard() {
260263
function closeSellCard() {
261264
$("#sell-card").removeClass("open")
262265
$("body .container").css("padding-bottom", 0)
266+
}
267+
268+
function startWaitForProcess() {
269+
$("body").addClass("wait-process-body");
270+
}
271+
function stopWaitProcess() {
272+
$("body").removeClass("wait-process-body");
273+
}
274+
function openHistoryTab() {
275+
$('#my-offers-card .nav-link').each(function() {
276+
$(this).removeClass('active')
277+
});
278+
$('#my-offers-card .tab-pane').each(function() {
279+
$(this).removeClass('active')
280+
});
281+
$("#my-offers-card .nav-link[href='#offers-history']").addClass('active')
282+
$('#my-offers-card .tab-pane#offers-history').addClass('active')
283+
}
284+
function flashOfferRow(id) {
285+
$('#my-offers-card .tab-pane#offers-history tr[data-offer-id="'+id+'"]').addClass('flash')
286+
setTimeout(()=>{
287+
$('#my-offers-card .tab-pane#offers-history tr[data-offer-id="'+id+'"]').removeClass('flash')
288+
}, 2500)
289+
}
290+
291+
function focusOffer(id) {
292+
stopWaitProcess()
293+
openHistoryTab()
294+
flashOfferRow(id)
295+
openOffer(id)
296+
}
297+
298+
function cancelOffer(event, id) {
299+
startWaitForProcess();
300+
paction(event, 'cancelOffer', [id], {afterUpdate:()=>{
301+
focusOffer(id)
302+
}})
303+
}
304+
function depositFromWallet(event) {
305+
startWaitForProcess();
306+
paction(event, 'depositFromWallet', {}, {update: '#offer-modal .modal-content'})
263307
}

web/apps/p2p/cli.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,14 @@
6464
}
6565
$service = OfferService::getService($asset['service']);
6666
CliService::processDepositTxId($service, $asset, $deposit_tx_id);
67+
} else if ($command == "returnDeposit") {
68+
$symbol = @$argv[2];
69+
$deposit_tx_id = @$argv[3];
70+
if(!$symbol || !$deposit_tx_id) {
71+
_log("Missing parameters");
72+
exit;
73+
}
74+
CliService::returnDeposit($symbol, $deposit_tx_id);
6775
} else {
6876
_log("Unknown command $command");
6977
}

web/apps/p2p/css/style.css

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/apps/p2p/css/style.css.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

web/apps/p2p/css/style.scss

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,44 @@ body {
2323
}
2424
}
2525

26+
.wait-process-div {
27+
display: none;
28+
}
29+
30+
&.wait-process-body {
31+
.wait-process-div {
32+
position: fixed;
33+
top: 0;
34+
bottom: 0;
35+
left: 0;
36+
right: 0;
37+
background-color: rgba(0, 0, 0, 0.2);
38+
z-index: 1000;
39+
display: flex !important;
40+
flex-direction: column;
41+
.wait-process-div-inner {
42+
background-color: var(--bs-primary);
43+
padding: 1rem;
44+
text-align: center;
45+
* {
46+
color: #fff;
47+
}
48+
}
49+
}
50+
}
51+
52+
}
53+
54+
@keyframes flashFade {
55+
0% { background-color: var(--bs-primary); } /* bright yellow */
56+
100% { background-color: transparent; }
57+
}
58+
59+
.table tbody tr.flash {
60+
animation: flashFade 2s ease-out;
61+
}
62+
63+
64+
.flash {
65+
animation: flashFade 2s ease-out;
2666
}

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,10 @@ function createBuyOffer() {
140140
OfferService::storeUserCoinAddress(OfferService::userAddress(), $this->base, $this->base_receive_address);
141141
$this->clearTradeInputs();
142142
$this->success("Your offer has been created");
143+
Pajax::executeScript('stopWaitProcess');
144+
Pajax::executeScript('openHistoryTab');
143145
Pajax::executeScript('openOffer', $offer_id);
146+
Pajax::executeScript('flashOfferRow', $offer_id);
144147
}
145148
function createSellOffer() {
146149
if(empty($this->sell_base_amount)) {
@@ -332,20 +335,26 @@ function depositFromWallet() {
332335
if($offer['type']==OfferService::TYPE_SELL) {
333336
$amount = $offer['base_amount'] + $offer['base_dust_amount'];
334337
$service = OfferService::getService($offer['base_service']);
338+
$symbol = $offer['base'];
335339
} else {
336340
$amount = $offer['base_amount']*$offer['base_price'] + $offer['quote_dust_amount'];
337341
$service = OfferService::getService($offer['quote_service']);
342+
$symbol = $offer['quote'];
338343
}
339344
$_SESSION['offer'] = $offer;
345+
_log("depositFromWallet: $symbol Offer #".$offer['id']." amount=$amount");
340346
$service->depositFromWallet($amount, $offer);
341347
}
342348
function depositFromWalletCallback($data) {
343349
$offer = $_SESSION['offer'];
344350
if($offer['type']==OfferService::TYPE_SELL) {
345351
$service = OfferService::getService($offer['base_service']);
352+
$symbol = $offer['base'];
346353
} else {
347354
$service = OfferService::getService($offer['quote_service']);
355+
$symbol = $offer['quote'];
348356
}
357+
_log("depositFromWalletCallback: $symbol Offer #".$offer['id']." data=".json_encode($data));
349358
$service->depositFromWalletCallback($offer, $data);
350359
}
351360
function transferFromWallet() {
@@ -420,11 +429,7 @@ function createdViewTemplate($offer) {
420429
You have 2 hours to make a deposit. After that time offer will expire.
421430
</p>
422431
<div class="d-flex align-items-baseline gap-2 flex-wrap">
423-
<?php if($offer['type']==OfferService::TYPE_SELL) { ?>
424-
<a href="" class="btn btn-primary" onclick="paction(event, 'depositFromWallet'); return false">Deposit from wallet</a>
425-
<?php } else { ?>
426-
<a href="" class="btn btn-primary" onclick="paction(event, 'depositFromWallet', {}, {update: '#offer-modal .modal-content'}); return false">Deposit from wallet</a>
427-
<?php } ?>
432+
<a href="" class="btn btn-primary" onclick="depositFromWallet(event); return false">Deposit from wallet</a>
428433
<p class="text-muted">
429434
You can also deposit directly from your wallet
430435
</p>

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

Lines changed: 29 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static function checkAssetDeposit($asset) {
3939
}
4040
$last_block_number = $last_block_number - 10;
4141
$depositTxIds = $service->findTransfers($last_block_number);
42-
_log("Checking $symbol deposits last_block_number=$last_block_number deposits: found " . count($depositTxIds));
42+
_log("$symbol Checking deposits last_block_number=$last_block_number deposits: found " . count($depositTxIds));
4343
$block_height = $service->getLastHeight();
4444
foreach ($depositTxIds as $depositTxId) {
4545
self::processDepositTxId($service, $asset, $depositTxId, $block_height);
@@ -71,28 +71,23 @@ static function processDepositTxId($service, $asset, $depositTxId, $block_height
7171
OfferService::setOfferOpen($offer['id']);
7272
_log($asset['symbol']. " Offer #{$offer['id']} Deposit #{$depositTxId} - set status opened");
7373
} else {
74-
_log("Not found offer by deposit tx id {$depositTxId}");
75-
if($depositTxId == "0x07fbf96ce3b9e25ccfa09e2511c5e43be9c7f882cad09a51f09a12bdcd296aca") {
76-
$a=1;
77-
}
7874
$depositTx = $service->findTransaction($depositTxId);
7975
if(!$depositTx) {
80-
_log("Transaction #{$depositTxId} not found");
76+
_log($asset['symbol']. " Deposit $depositTxId not found");
8177
return;
8278
}
8379
$amount = $depositTx['amount'];
8480
$offers = OfferService::getOfferByDepositAmount(floatval($amount), $asset['id']);
85-
_log("Found offers by amount {$amount}: found " . count($offers));
8681
if(count($offers)>1) {
87-
_log("Multiple offers found for amount $amount");
82+
_log($asset['symbol']. " Deposit $depositTxId - Multiple offers found for amount $amount");
8883
return;
8984
}
9085
if(count($offers)==0) {
91-
_log("No offer found for amount $amount");
86+
_log($asset['symbol']. " Deposit $depositTxId - No offer found for amount $amount");
9287
return;
9388
}
9489
$offer = $offers[0];
95-
_log("Set offer #{$offer['id']} deposition for #{$depositTxId}");
90+
_log($asset['symbol']. " Deposit $depositTxId - amount $amount - found offer #{$offer['id']} - set status depositing");
9691
OfferService::setOfferDepositing($offer['id'], $depositTxId);
9792
}
9893
}
@@ -164,19 +159,18 @@ public static function checkAssetTransferring($asset)
164159
_log("Transaction check failed");
165160
continue;
166161
}
167-
_log("No offer found for transfer transaction $txId - looking for amount $amount");
168162
$offers = OfferService::getAcceptedOfferByAmount($amount, $asset['id']);
169163
if(count($offers)>1) {
170-
_log("Found multiple offers for $amount");
164+
_log($asset['symbol'] . " Found multiple offers for $amount");
171165
continue;
172166
}
173167
if(count($offers)==0) {
174-
_log("No offer found for amount $amount");
168+
_log($asset['symbol'] . " No offer found for amount $amount");
175169
continue;
176170
}
177171
$offer = $offers[0];
178-
_log("Found offer #{$offer['id']} for transfer transaction $txId");
179172
OfferService::setAcceptedOfferTransferring($offer['id'], $txId);
173+
_log($asset['symbol']." Found offer #{$offer['id']} for transfer transaction $txId - set status transferring");
180174
}
181175
}
182176
}
@@ -312,4 +306,25 @@ public static function checkIncompleteTransferLogs()
312306
OfferService::updateTransferLogWithTransaction($log['id'], $tx);
313307
}
314308
}
309+
310+
public static function returnDeposit(string $symbol, string $deposit_tx_id)
311+
{
312+
_log("$symbol Return deposit tx $deposit_tx_id");
313+
$asset = OfferService::getAssetBySymbol($symbol);
314+
$service = OfferService::getService($asset['service']);
315+
$tx = $service->findTransaction($deposit_tx_id);
316+
if(!$tx) {
317+
_log("$symbol Not found transaction $deposit_tx_id");
318+
return;
319+
}
320+
if($tx['to'] != $service->getEscrowAddress()) {
321+
_log("$symbol transaction $deposit_tx_id - not deposit tx");
322+
return;
323+
}
324+
$amount = $tx['amount'];
325+
$dst = $tx['from'];
326+
_log("$symbol Return deposit tx $deposit_tx_id amount = $amount to = $dst");
327+
$hash = $service->createPayment($amount, $dst, null);
328+
_log("$symbol Return deposit tx $deposit_tx_id - created transaction $hash");
329+
}
315330
}

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ public static function getOfferByReceiveTx($tx_id)
9999

100100
public static function setOfferDepositing($id, $deposit_tx_id)
101101
{
102+
_log("setOfferDepositing: Offer #$id deposit_tx_id=$deposit_tx_id");
102103
DBService::runInTransaction(function() use ($id, $deposit_tx_id) {
103104
self::exec("update p2p_offers set status=?, deposit_tx_id = ? where id=?",[OfferService::STATUS_DEPOSITING,$deposit_tx_id, $id]);
104105
self::saveOfferLog($id);
@@ -218,34 +219,37 @@ static function cancelCreatedOffer($id) {
218219
}
219220

220221
static function cancelOpenOffer($offer, $status = OfferService::STATUS_CANCELED) {
221-
_log("Returning funds to offer creator");
222222
$market = OfferService::getMarket($offer['market_id']);
223223
if($offer['type']==OfferService::TYPE_SELL) {
224224
$service = OfferService::getService($market['base_service']);
225+
$symbol = $market['base'];
225226
} else {
226227
$service = OfferService::getService($market['quote_service']);
228+
$symbol = $market['quote'];
227229
}
228230
$depositTx = $service->findTransaction($offer['deposit_tx_id']);
229231
if(!$depositTx) {
232+
_log("cancelOpenOffer: $symbol Offer #{$offer['id']} - Cannot find deposit transaction ".$offer['deposit_tx_id']);
230233
throw new \Exception("Cannot find deposit transaction");
231234
}
232235
$amount = $depositTx['amount'];
233236
$toAddress = $depositTx['from'];
234-
_log("Return deposited amount=".$amount." to src: ".$toAddress);
235237
$return_tx_id = $service->createPayment($amount,$toAddress,$offer);
236238
if(!$return_tx_id) {
239+
_log("cancelOpenOffer: $symbol Offer #{$offer['id']} - Cannot create cancel transaction");
237240
throw new \Exception("Cannot create return transaction");
238241
}
242+
_log("cancelOpenOffer: $symbol Offer #{$offer['id']} - Created cancel transaction $return_tx_id amount=$amount to src: ".$toAddress);
239243
DBService::runInTransaction(function() use ($status,$return_tx_id,$offer,&$res) {
240244
$res = self::exec('update p2p_offers set status=?, return_tx_id =?, canceled_at=now() where id=?',
241245
[$status,$return_tx_id,$offer['id']]);
242246
OfferService::saveOfferLog($offer['id']);
243247
OfferService::saveReturnTransferLog($offer['id'],$return_tx_id);
244248
});
245249
if(!$res) {
246-
_log("Cannot update offer status");
250+
_log("cancelOpenOffer: $symbol Offer #{$offer['id']} - Cannot update offer status");
247251
} else {
248-
_log("Offer #".$offer['id']." updated status to: ".$status);
252+
_log("cancelOpenOffer: $symbol Offer #{$offer['id']} - updated status to: ".$status);
249253
}
250254
}
251255

@@ -331,7 +335,7 @@ public static function checkQuoteDustAmount($quote_total, AssetService $service,
331335
{
332336
$limit = 100;
333337
$counter = 0;
334-
$quote_dust_amount = 0;
338+
$quote_dust_amount = rand(1, 99) / pow(10, $service->getDecimals());
335339
while(true) {
336340
$counter++;
337341
if($counter >= $limit) {
@@ -357,7 +361,7 @@ public static function checkBaseDustAmount($base_amount, AssetService $service,
357361
{
358362
$limit = 100;
359363
$counter = 0;
360-
$base_dust_amount = 0;
364+
$base_dust_amount = rand(1, 99) / pow(10, $service->getDecimals());
361365
while(true) {
362366
$counter++;
363367
if($counter >= $limit) {

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ public function createPayment(mixed $amount, mixed $toAddress, $offer)
7373
$msg = json_encode([
7474
"app"=>"p2p-trader",
7575
"action"=>"return",
76-
"offer"=>$offer['id'],
77-
"accept_tx_id"=>$offer['accept_tx_id']
76+
"offer"=>$offer['id'] ?? null,
77+
"accept_tx_id"=>$offer['accept_tx_id'] ?? null,
7878
]);
7979
$tx = new Transaction($public_key, $toAddress, $amount, TX_TYPE_SEND, time(), $msg);
8080
$tx->sign(self::getEscrowPrivateKey());

0 commit comments

Comments
 (0)