Skip to content

Commit d527253

Browse files
committed
Buy and sell market ledger balance update added.
1 parent e8b1e09 commit d527253

File tree

1 file changed

+132
-31
lines changed

1 file changed

+132
-31
lines changed

routes/orders.js

Lines changed: 132 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -271,10 +271,17 @@ const marketOrders = async() => {
271271

272272
}else if(direction === "sell"){
273273
const marketSell = await db.query('SELECT * FROM marketOrders WHERE direction = ? ORDER BY time ASC;',"sell")
274-
const updateBids = await db.query('select size,price from bidLimitOrders ORDER BY price DESC,time ASC;')
274+
const updateBids = await db.query('select size,price,userId from bidLimitOrders ORDER BY price DESC,time ASC;')
275275
const marketSellArr = marketSell[0];
276276
const updateBidsArr = updateBids[0];
277277

278+
279+
//fetch wallet balance
280+
281+
const walletBalance = await db.query("select usd,bitcoin from ledger where userId = ?",[userId]);
282+
const walletBalanceArr = walletBalance[0];
283+
284+
278285
let limitSize = updateBidsArr[0].size;
279286
let index = 1;
280287
let costBasis = updateBidsArr[0].price * updateBidsArr[0].size;
@@ -302,11 +309,26 @@ const marketOrders = async() => {
302309
if(sub !== 0){
303310

304311
if(marketSellArr[0].size > limitSize){
305-
const avgPrice = costBasis / limitSize;
306-
const queryStringTrades = "insert into trades(symbol,direction,price,size,time,userId) values(?,?,?,?,?,?);"
312+
313+
if(walletBalanceArr[0].bitcoin >= limitSize){
307314

308-
const deleteRows = await db.query('DELETE FROM bidLimitOrders ORDER BY price DESC,time ASC LIMIT ?;',index);
309-
if(deleteRows[0].affectedRows !== 0){
315+
for(let y = 0; y < index; y++){
316+
317+
const fetchLimitWallet = await db.query("select bitcoin from ledger where userId = ?;",[updateBidsArr[y].userId]);
318+
const updateBtcVal = fetchLimitWallet[0][0].bitcoin + updateBidsArr[y].size;
319+
const updateWallet = await db.query("update ledger set bitcoin = ? where userId = ?;",[updateBtcVal,updateBidsArr[y].userId]);
320+
}
321+
322+
const avgPrice = costBasis / limitSize;
323+
324+
const updateBtcVal = walletBalanceArr[0].bitcoin - limitSize - (limitSize/100 * feesTaker);
325+
const updateUsdVal = walletBalanceArr[0].usd + avgPrice * limitSize;
326+
const updateWallet = await db.query("update ledger set bitcoin = ?, usd = ? where userId = ?;",[updateBtcVal,updateUsdVal,userId]);
327+
328+
const queryStringTrades = "insert into trades(symbol,direction,price,size,time,userId) values(?,?,?,?,?,?);"
329+
330+
const deleteRows = await db.query('DELETE FROM bidLimitOrders ORDER BY price DESC,time ASC LIMIT ?;',index);
331+
if(deleteRows[0].affectedRows !== 0){
310332

311333
const tradesInsert = await db.query(queryStringTrades,[symbol,direction,avgPrice,limitSize,time,userId]);
312334

@@ -315,41 +337,91 @@ const marketOrders = async() => {
315337
const deleteRows = await db.query('DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;',1);
316338
res.status(200).json({message : "Market Order Partially Filled Successfully", code : 200})
317339

340+
}
341+
342+
}else{
343+
const deleteRows = await db.query('DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;',1);
344+
res.status(403).json({message : "Not enough balance to fulfil this order.",code : 403});
318345
}
319346

320347
}else if(limitSize > marketSellArr[0].size){
321348
if(index === 1){
322-
const avgPrice = updateBidsArr[0].price;
323-
const queryStringTrades = "insert into trades(symbol,direction,price,size,time,userId) values(?,?,?,?,?,?);"
324-
325-
const updateRow = await db.query('UPDATE bidLimitOrders SET size = ? ORDER BY price DESC,time ASC LIMIT ?',[sub,1]);
326-
327349

328-
if(updateRow[0].changedRows !== 0){
350+
if(walletBalanceArr[0].bitcoin >= size){
351+
352+
const fetchWalletLimit = await db.query("select bitcoin from ledger where userId = ?;",[updateBidsArr[0].userId]);
353+
const updateBtcVal = fetchWalletLimit[0][0].bitcoin + sub;
354+
const updateWalletLimit = await db.query("update ledger set bitcoin = ? where userId = ?;",[updateBtcVal,updateBidsArr[0].userId]);
355+
356+
if(updateWalletLimit[0].changedRows !== 0){
357+
const avgPrice = updateBidsArr[0].price;
358+
359+
const queryStringTrades = "insert into trades(symbol,direction,price,size,time,userId) values(?,?,?,?,?,?);"
360+
361+
const updateRow = await db.query('UPDATE bidLimitOrders SET size = ? ORDER BY price DESC,time ASC LIMIT ?',[sub,1]);
362+
363+
364+
if(updateRow[0].changedRows !== 0){
365+
366+
const updateValBtc = walletBalanceArr[0].bitcoin - size - (size/100 * feesTaker);
367+
const updateValUsd = walletBalanceArr[0].usd + avgPrice * size;
368+
const updateWallet = await db.query("update ledger set bitcoin = ?, usd = ? where userId = ?; ",[updateValBtc,updateValUsd,userId]);
369+
329370
const tradesInsert = await db.query(queryStringTrades,[symbol,direction,avgPrice,size,time,userId]);
330371

331372
//Deleting market order from the database after the trade execution
332373

333374
const deleteRows = await db.query('DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;',1);
334375
res.status(200).json({message : "Market Order Filled Successfully", code : 200})
335376

377+
}
378+
}
379+
380+
}else{
381+
const deleteRows = await db.query('DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;',1);
382+
res.status(403).json({message : "Not enough balance to fulfil this order.",code : 403});
336383
}
384+
337385
}else{
338-
const rows = index - 1
339-
340-
const deleteRows = await db.query('DELETE FROM bidLimitOrders ORDER BY price DESC,time ASC LIMIT ?;',rows);
386+
const rows = index - 1;
387+
388+
const bidSub = costBasis - updateBidsArr[rows].price * sub;
389+
const avgPrice = bidSub / marketSellArr[0].size;
390+
console.log(avgPrice);
391+
392+
if(walletBalanceArr[0].bitcoin >= size){
393+
394+
for(let a = 0; a < rows; a++){
395+
396+
const fetchLimitWallet = await db.query("select bitcoin from ledger where userId = ?;",[updateBidsArr[a].userId]);
397+
const updateBtcVal = fetchLimitWallet[0][0].bitcoin + updateBidsArr[a].size;
398+
const updateWallet = await db.query("update ledger set bitcoin = ? where userId = ?;",[updateBtcVal,updateBidsArr[a].userId]);
399+
400+
}
401+
402+
const fetchLimitWallet = await db.query("select bitcoin from ledger where userId = ?;",[updateBidsArr[rows].userId]);
403+
const updateBtcVal = fetchLimitWallet[0][0].bitcoin + sub;
404+
const updateWallet = await db.query("update ledger set bitcoin = ? where userId = ?;",[updateBtcVal,updateBidsArr[rows].userId]);
405+
406+
if(updateWallet[0].changedRows !== 0){
407+
408+
const deleteRows = await db.query('DELETE FROM bidLimitOrders ORDER BY price DESC,time ASC LIMIT ?;',rows);
341409

342-
if(deleteRows[0].affectedRows !== 0){
410+
if(deleteRows[0].affectedRows !== 0){
343411
const queryStringTrades = "insert into trades(symbol,direction,price,size,time,userId) values(?,?,?,?,?,?);"
344412

345413
const updateRow = await db.query('UPDATE bidLimitOrders SET size = ? ORDER BY price DESC,time ASC LIMIT ?',[sub,1]);
346414

347415

348416

349417
if(updateRow[0].changedRows !== 0){
350-
const fetchBids = await db.query('select size,price from bidLimitOrders ORDER BY price DESC,time ASC;');
351-
const bidSub = costBasis - fetchBids[0][0].price * fetchBids[0][0].size
352-
const avgPrice = bidSub / marketSellArr[0].size;
418+
// const fetchBids = await db.query('select size,price from bidLimitOrders ORDER BY price DESC,time ASC;');
419+
// const bidSub = costBasis - fetchBids[0][0].price * fetchBids[0][0].size
420+
// const avgPrice = bidSub / marketSellArr[0].size;
421+
const updateBtcValue = walletBalanceArr[0].bitcoin - size - (size/100 * feesTaker);
422+
const updateUsdValue = walletBalanceArr[0].usd + avgPrice * size;
423+
const updateWallet = await db.query("update ledger set bitcoin = ?, usd = ? where userId = ?;",[updateBtcValue,updateUsdValue,userId]);
424+
353425
const tradesInsert = await db.query(queryStringTrades,[symbol,direction,avgPrice,size,time,userId]);
354426

355427
//Deleting market order from the database after the trade execution
@@ -360,9 +432,15 @@ const marketOrders = async() => {
360432

361433
}
362434

435+
}
436+
437+
}
438+
439+
}else{
440+
const deleteRows = await db.query('DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;',1);
441+
res.status(403).json({message : "Not enough balance to fulfil this order.",code : 403});
363442
}
364-
365-
443+
366444
}
367445

368446
}
@@ -371,21 +449,44 @@ const marketOrders = async() => {
371449

372450

373451
}else if(sub === 0){
374-
const avgPrice = costBasis / limitSize;
375-
const deleteRows = await db.query('DELETE FROM bidLimitOrders ORDER BY price DESC,time ASC LIMIT ?;',index);
376-
377-
const queryStringTrades = "insert into trades(symbol,direction,price,size,time,userId) values(?,?,?,?,?,?);"
378-
379-
380-
if(deleteRows[0].affectedRows !== 0){
381-
const tradesInsert = await db.query(queryStringTrades,[symbol,direction,avgPrice,size,time,userId])
452+
453+
if(walletBalanceArr[0].bitcoin >= size){
454+
455+
for(let y = 0; y < index; y++){
456+
457+
const fetchLimitWallet = await db.query("select bitcoin from ledger where userId = ?;",[updateBidsArr[y].userId]);
458+
const updateBtcVal = fetchLimitWallet[0][0].bitcoin + updateBidsArr[y].size;
459+
const updateWallet = await db.query("update ledger set bitcoin = ? where userId = ?;",[updateBtcVal,updateBidsArr[y].userId]);
460+
461+
};
462+
463+
const avgPrice = costBasis / limitSize;
382464

383-
//Deleting market order from the database after the trade execution
465+
const deleteRows = await db.query('DELETE FROM bidLimitOrders ORDER BY price DESC,time ASC LIMIT ?;',index);
384466

385-
const deleteRows = await db.query('DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;',1);
386-
res.status(200).json({message : "Market Order Filled Successfully", code : 200})
467+
const queryStringTrades = "insert into trades(symbol,direction,price,size,time,userId) values(?,?,?,?,?,?);"
468+
469+
470+
if(deleteRows[0].affectedRows !== 0){
471+
472+
const updateBtcValue = walletBalanceArr[0].bitcoin - size - (size/100 * feesTaker);
473+
const updateUsdValue = walletBalanceArr[0].usd + avgPrice * size;
474+
const updateWallet = await db.query("update ledger set bitcoin = ?, usd = ? where userId = ?;",[updateBtcValue,updateUsdValue,userId]);
387475

476+
const tradesInsert = await db.query(queryStringTrades,[symbol,direction,avgPrice,size,time,userId])
477+
478+
//Deleting market order from the database after the trade execution
479+
480+
const deleteRows = await db.query('DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;',1);
481+
res.status(200).json({message : "Market Order Filled Successfully", code : 200})
482+
483+
}
484+
485+
}else{
486+
const deleteRows = await db.query('DELETE FROM marketOrders ORDER BY time ASC LIMIT ?;',1);
487+
res.status(403).json({message : "Not enough balance to fulfil this order.",code : 403});
388488
}
489+
389490
}
390491

391492

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy