This commit is contained in:
sairajzero 2021-11-05 21:43:26 +05:30
parent 744979fb2a
commit b5cde90e89
2 changed files with 41 additions and 20 deletions

View File

@ -17,14 +17,19 @@ function getBestPairs(currentRate) {
return new Promise((resolve, reject) => {
DB.query("SELECT tag, sellPriority, buyPriority FROM TagList").then(result => {
//Sorted in Ascending (ie, stack; pop for highest)
let tags_buy = result.sort((a, b) => a.buyPriority > b.buyPriority).map(r => r.tag);
let tags_sell = result.sort((a, b) => a.sellPriority > b.sellPriority).map(r => r.tag);
let tags_buy = result.sort((a, b) => a.buyPriority > b.buyPriority ? 1 : -1).map(r => r.tag);
let tags_sell = result.sort((a, b) => a.sellPriority > b.sellPriority ? 1 : -1).map(r => r.tag);
resolve(new bestPair(currentRate, tags_buy, tags_sell));
}).catch(error => reject(error))
})
}
const bestPair = function(currentRate, tags_buy, tags_sell) {
const bestPair = function(cur_rate, tags_buy, tags_sell) {
const currentRate = cur_rate;
Object.defineProperty(this, 'cur_rate', {
get: () => currentRate
});
this.get = () => new Promise((resolve, reject) => {
Promise.all([getBuyOrder(), getSellOrder()]).then(results => {
@ -92,6 +97,7 @@ const bestPair = function(currentRate, tags_buy, tags_sell) {
} else if (!cache.end) { //Un-tagged floID's orders (do only once)
getUntaggedSellOrders(currentRate).then(orders => {
cache.orders = orders;
cache.cur_tag = null;
cache.end = true;
getSellOrder()
.then(result => resolve(result))
@ -100,7 +106,7 @@ const bestPair = function(currentRate, tags_buy, tags_sell) {
} else
reject(false);
});
getSeller.cache = {
getSellOrder.cache = {
tags: tags_sell
};
@ -143,6 +149,7 @@ const bestPair = function(currentRate, tags_buy, tags_sell) {
} else if (!cache.end) { //Un-tagged floID's orders (do only once)
getUntaggedBuyOrders(currentRate).then(orders => {
cache.orders = orders;
cache.cur_tag = null;
cache.end = true;
getBuyOrder()
.then(result => resolve(result))
@ -160,7 +167,7 @@ function getUntaggedSellOrders(cur_price) {
return new Promise((resolve, reject) => {
DB.query("SELECT SellOrder.id, SellOrder.floID, SellOrder.quantity FROM SellOrder" +
" LEFT JOIN Tags ON Tags.floID = SellOrder.floID" +
" WHERE Tags.floID IS NULL AND SellOrder.minPrice <=? ORDER BY SellOrder.time_placed", [cur_price])
" WHERE Tags.floID IS NULL AND SellOrder.minPrice <=? ORDER BY SellOrder.time_placed DESC", [cur_price])
.then(orders => resolve(orders))
.catch(error => reject(error))
})
@ -170,7 +177,7 @@ function getUntaggedBuyOrders(cur_price) {
return new Promise((resolve, reject) => {
DB.query("SELECT BuyOrder.id, BuyOrder.floID, BuyOrder.quantity FROM BuyOrder" +
" LEFT JOIN Tags ON Tags.floID = BuyOrder.floID" +
" WHERE Tags.floID IS NULL AND BuyOrder.maxPrice >=? ORDER BY BuyOrder.time_placed", [cur_price])
" WHERE Tags.floID IS NULL AND BuyOrder.maxPrice >=? ORDER BY BuyOrder.time_placed DESC", [cur_price])
.then(orders => resolve(orders))
.catch(error => reject(error))
})
@ -180,13 +187,13 @@ function getSellOrdersInTag(tag, cur_price) {
return new Promise((resolve, reject) => {
DB.query("SELECT SellOrder.id, SellOrder.floID, SellOrder.quantity FROM SellOrder" +
" INNER JOIN Tags ON Tags.floID = SellOrder.floID" +
" WHERE Tags.tag = ? AND SellOrder.minPrice <=? ORDER BY SellOrder.time_placed", [tag, cur_price]).then(orders => {
" WHERE Tags.tag = ? AND SellOrder.minPrice <=? ORDER BY SellOrder.time_placed DESC", [tag, cur_price]).then(orders => {
if (orders.length <= 1) // No (or) Only-one order, hence priority sort not required.
resolve(orders);
else
getPointsFromAPI(orders.map(o => o.floID)).then(points => {
getPointsFromAPI(tag, orders.map(o => o.floID)).then(points => {
let orders_sorted = orders.map(o => [o, points[o.floID]])
.sort((a, b) => a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0) //sort by points (ascending)
.sort((a, b) => a[1] > b[1] ? -1 : a[1] < b[1] ? 1 : 0) //sort by points (ascending)
.map(x => x[0]);
resolve(orders_sorted);
}).catch(error => reject(error))
@ -198,13 +205,13 @@ function getBuyOrdersInTag(tag, cur_price) {
return new Promise((resolve, reject) => {
DB.query("SELECT BuyOrder.id, BuyOrder.floID, BuyOrder.quantity FROM BuyOrder" +
" INNER JOIN Tags ON Tags.floID = BuyOrder.floID" +
" WHERE Tags.tag = ? AND BuyOrder.maxPrice >=? ORDER BY BuyOrder.time_placed", [tag, cur_price]).then(orders => {
" WHERE Tags.tag = ? AND BuyOrder.maxPrice >=? ORDER BY BuyOrder.time_placed DESC", [tag, cur_price]).then(orders => {
if (orders.length <= 1) // No (or) Only-one order, hence priority sort not required.
resolve(orders);
else
getPointsFromAPI(orders.map(o => o.floID)).then(points => {
getPointsFromAPI(tag, orders.map(o => o.floID)).then(points => {
let orders_sorted = orders.map(o => [o, points[o.floID]])
.sort((a, b) => a[1] < b[1] ? -1 : a[1] > b[1] ? 1 : 0) //sort by points (ascending)
.sort((a, b) => a[1] > b[1] ? -1 : a[1] < b[1] ? 1 : 0) //sort by points (ascending)
.map(x => x[0]);
resolve(orders_sorted);
}).catch(error => reject(error))
@ -212,7 +219,7 @@ function getBuyOrdersInTag(tag, cur_price) {
});
}
function getPointsFromAPI(floIDs) {
function getPointsFromAPI(tag, floIDs) {
floIDs = Array.from(new Set(floIDs));
return new Promise((resolve, reject) => {
DB.query("SELECT api FROM TagList WHERE tag=?", [tag]).then(result => {
@ -221,7 +228,7 @@ function getPointsFromAPI(floIDs) {
let points = {};
for (let i in result)
if (result[i].status === "fulfilled")
points[floIDs[i]] = result[i];
points[floIDs[i]] = result[i].value;
resolve(points);
}).catch(error => reject(error))
}).catch(error => reject(error))
@ -231,6 +238,15 @@ function getPointsFromAPI(floIDs) {
function fetch_api(api, id) {
return new Promise((resolve, reject) => {
//TODO: fetch data from API
let url = api.replace('<flo-id>', id);
global.fetch(url).then(response => {
if (response.ok)
response.text()
.then(result => resolve(result))
.catch(error => reject(error))
else
reject(response);
}).catch(error => reject(error))
})
}
@ -282,7 +298,6 @@ function verifySellOrder(sellOrder, cur_price) {
resolve(sellOrder);
}).catch(error => reject(error));
})
}
function getTopValidBuyOrder(orders, cur_price) {

View File

@ -171,16 +171,17 @@ function cancelOrder(type, id, floID) {
}
function initiateCoupling() {
group.getBestPairs()
group.getBestPairs(net_FLO_price)
.then(bestPairQueue => processCoupling(bestPairQueue))
.catch(error => reject(error))
.catch(error => console.error("initiateCoupling", error))
}
function processCoupling(bestPairQueue) {
bestPairQueue.get().then(result => {
let buyer_best = result.buyOrder,
seller_best = result.sellOrder;
console.debug("Sell:", seller_best.id, "Buy:", buyer_best.id);
console.debug("Sell:", seller_best);
console.debug("Buy:", buyer_best);
spendFLO(buyer_best, seller_best).then(txQueries => {
//process the Txn
var tx_quantity;
@ -190,7 +191,7 @@ function processCoupling(bestPairQueue) {
tx_quantity = processSellOrder(seller_best, buyer_best, txQueries);
else
tx_quantity = processBuyAndSellOrder(seller_best, buyer_best, txQueries);
updateBalance(seller_best, buyer_best, txQueries, cur_price, tx_quantity);
updateBalance(seller_best, buyer_best, txQueries, bestPairQueue.cur_rate, tx_quantity);
//process txn query in SQL
DB.transaction(txQueries).then(results => {
bestPairQueue.next();
@ -199,7 +200,12 @@ function processCoupling(bestPairQueue) {
processCoupling(bestPairQueue);
}).catch(error => console.error(error));
}).catch(error => console.error(error));
}).catch(error => console.error(error));
}).catch(error => {
if (error !== false)
console.error(error);
else
console.log("No valid orders.");
});
}
function spendFLO(buyOrder, sellOrder) {