Corrections to parameters handling and sorting.
This commit is contained in:
parent
88e5b03430
commit
98cc81c53d
@ -89,6 +89,7 @@ export interface BisqTrade {
|
|||||||
payment_method: string;
|
payment_method: string;
|
||||||
trade_id: string;
|
trade_id: string;
|
||||||
trade_date: number;
|
trade_date: number;
|
||||||
|
market?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Currencies { [txid: string]: Currency; }
|
export interface Currencies { [txid: string]: Currency; }
|
||||||
@ -97,7 +98,8 @@ export interface Currency {
|
|||||||
code: string;
|
code: string;
|
||||||
name: string;
|
name: string;
|
||||||
precision: number;
|
precision: number;
|
||||||
type: string;
|
|
||||||
|
_type: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export interface Depth { [market: string]: Market; }
|
export interface Depth { [market: string]: Market; }
|
||||||
|
@ -15,8 +15,8 @@ class BisqMarketsApi {
|
|||||||
this.offersData = offers;
|
this.offersData = offers;
|
||||||
this.tradesData = trades;
|
this.tradesData = trades;
|
||||||
|
|
||||||
this.fiatCurrencyData.forEach((currency) => currency.type = 'fiat');
|
this.fiatCurrencyData.forEach((currency) => currency._type = 'fiat');
|
||||||
this.cryptoCurrencyData.forEach((currency) => currency.type = 'crypto');
|
this.cryptoCurrencyData.forEach((currency) => currency._type = 'crypto');
|
||||||
this.tradesData.forEach((trade) => {
|
this.tradesData.forEach((trade) => {
|
||||||
trade._market = trade.currencyPair.toLowerCase().replace('/', '_');
|
trade._market = trade.currencyPair.toLowerCase().replace('/', '_');
|
||||||
});
|
});
|
||||||
@ -51,13 +51,13 @@ class BisqMarketsApi {
|
|||||||
const currencyPair = market.replace('_', '/').toUpperCase();
|
const currencyPair = market.replace('_', '/').toUpperCase();
|
||||||
|
|
||||||
const buys = this.offersData
|
const buys = this.offersData
|
||||||
.filter((offer) => offer.currencyPair === currencyPair && offer.direction === 'BUY')
|
.filter((offer) => offer.currencyPair === currencyPair && offer.primaryMarketDirection === 'BUY')
|
||||||
.map((offer) => offer.price)
|
.map((offer) => offer.price)
|
||||||
.sort((a, b) => b - a)
|
.sort((a, b) => b - a)
|
||||||
.map((price) => this.intToBtc(price));
|
.map((price) => this.intToBtc(price));
|
||||||
|
|
||||||
const sells = this.offersData
|
const sells = this.offersData
|
||||||
.filter((offer) => offer.currencyPair === currencyPair && offer.direction === 'SELL')
|
.filter((offer) => offer.currencyPair === currencyPair && offer.primaryMarketDirection === 'SELL')
|
||||||
.map((offer) => offer.price)
|
.map((offer) => offer.price)
|
||||||
.sort((a, b) => a - b)
|
.sort((a, b) => a - b)
|
||||||
.map((price) => this.intToBtc(price));
|
.map((price) => this.intToBtc(price));
|
||||||
@ -72,22 +72,24 @@ class BisqMarketsApi {
|
|||||||
|
|
||||||
getOffers(
|
getOffers(
|
||||||
market: string,
|
market: string,
|
||||||
direction?: 'BUY' | 'SELL',
|
direction?: 'buy' | 'sell',
|
||||||
): Offers {
|
): Offers {
|
||||||
const currencyPair = market.replace('_', '/').toUpperCase();
|
const currencyPair = market.replace('_', '/').toUpperCase();
|
||||||
|
|
||||||
let buys: Offer[] | null = null;
|
let buys: Offer[] | null = null;
|
||||||
let sells: Offer[] | null = null;
|
let sells: Offer[] | null = null;
|
||||||
|
|
||||||
if (!direction || direction === 'BUY') {
|
if (!direction || direction === 'buy') {
|
||||||
buys = this.offersData
|
buys = this.offersData
|
||||||
.filter((offer) => offer.currencyPair === currencyPair && offer.direction === 'BUY')
|
.filter((offer) => offer.currencyPair === currencyPair && offer.primaryMarketDirection === 'BUY')
|
||||||
|
.sort((a, b) => b.price - a.price)
|
||||||
.map((offer) => this.offerDataToOffer(offer));
|
.map((offer) => this.offerDataToOffer(offer));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!direction || direction === 'SELL') {
|
if (!direction || direction === 'sell') {
|
||||||
sells = this.offersData
|
sells = this.offersData
|
||||||
.filter((offer) => offer.currencyPair === currencyPair && offer.direction === 'SELL')
|
.filter((offer) => offer.currencyPair === currencyPair && offer.primaryMarketDirection === 'SELL')
|
||||||
|
.sort((a, b) => a.price - b.price)
|
||||||
.map((offer) => this.offerDataToOffer(offer));
|
.map((offer) => this.offerDataToOffer(offer));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -108,14 +110,14 @@ class BisqMarketsApi {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
const isFiat = allCurrencies[currency].type === 'fiat';
|
const isFiat = allCurrencies[currency]._type === 'fiat';
|
||||||
const pmarketname = allCurrencies['BTC']['name'];
|
const pmarketname = allCurrencies['BTC']['name'];
|
||||||
|
|
||||||
const lsymbol = isFiat ? 'BTC' : currency;
|
const lsymbol = isFiat ? 'BTC' : currency;
|
||||||
const rsymbol = isFiat ? currency : 'BTC';
|
const rsymbol = isFiat ? currency : 'BTC';
|
||||||
const lname = isFiat ? pmarketname : allCurrencies[currency].name;
|
const lname = isFiat ? pmarketname : allCurrencies[currency].name;
|
||||||
const rname = isFiat ? allCurrencies[currency].name : pmarketname;
|
const rname = isFiat ? allCurrencies[currency].name : pmarketname;
|
||||||
const ltype = isFiat ? 'crypto' : allCurrencies[currency].type;
|
const ltype = isFiat ? 'crypto' : allCurrencies[currency]._type;
|
||||||
const rtype = isFiat ? 'fiat' : 'crypto';
|
const rtype = isFiat ? 'fiat' : 'crypto';
|
||||||
const lprecision = 8;
|
const lprecision = 8;
|
||||||
const rprecision = isFiat ? 2 : 8;
|
const rprecision = isFiat ? 2 : 8;
|
||||||
@ -155,9 +157,13 @@ class BisqMarketsApi {
|
|||||||
|
|
||||||
if (!timestamp_from) {
|
if (!timestamp_from) {
|
||||||
timestamp_from = new Date('2016-01-01').getTime();
|
timestamp_from = new Date('2016-01-01').getTime();
|
||||||
|
} else {
|
||||||
|
timestamp_from = timestamp_from * 1000;
|
||||||
}
|
}
|
||||||
if (!timestamp_to) {
|
if (!timestamp_to) {
|
||||||
timestamp_to = new Date().getTime();
|
timestamp_to = new Date().getTime();
|
||||||
|
} else {
|
||||||
|
timestamp_to = timestamp_to * 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
const allCurrencies = this.getCurrencies();
|
const allCurrencies = this.getCurrencies();
|
||||||
@ -206,12 +212,16 @@ class BisqMarketsApi {
|
|||||||
const currencyLeft = allCurrencies[currencyPairs[0]];
|
const currencyLeft = allCurrencies[currencyPairs[0]];
|
||||||
const currencyRight = allCurrencies[currencyPairs[1]];
|
const currencyRight = allCurrencies[currencyPairs[1]];
|
||||||
|
|
||||||
trade.tradePrice = trade.primaryMarketTradePrice * Math.pow(10, 8 - currencyRight.precision);
|
if (!currencyLeft || !currencyRight) {
|
||||||
trade.tradeAmount = trade.primaryMarketTradeAmount * Math.pow(10, 8 - currencyLeft.precision);
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tradePrice = trade.primaryMarketTradePrice * Math.pow(10, 8 - currencyRight.precision);
|
||||||
|
const tradeAmount = trade.primaryMarketTradeAmount * Math.pow(10, 8 - currencyLeft.precision);
|
||||||
const tradeVolume = trade.primaryMarketTradeVolume * Math.pow(10, 8 - currencyRight.precision);
|
const tradeVolume = trade.primaryMarketTradeVolume * Math.pow(10, 8 - currencyRight.precision);
|
||||||
|
|
||||||
trade._tradePrice = this.intToBtc(trade.tradePrice);
|
trade._tradePrice = this.intToBtc(tradePrice);
|
||||||
trade._tradeAmount = this.intToBtc(trade.tradeAmount);
|
trade._tradeAmount = this.intToBtc(tradeAmount);
|
||||||
trade._tradeVolume = this.intToBtc(tradeVolume);
|
trade._tradeVolume = this.intToBtc(tradeVolume);
|
||||||
trade._offerAmount = this.intToBtc(trade.offerAmount);
|
trade._offerAmount = this.intToBtc(trade.offerAmount);
|
||||||
|
|
||||||
@ -226,17 +236,27 @@ class BisqMarketsApi {
|
|||||||
matches = [];
|
matches = [];
|
||||||
}
|
}
|
||||||
|
|
||||||
return matches.map((trade) => {
|
if (sort === 'asc') {
|
||||||
return {
|
matches.sort((a, b) => a.tradeDate - b.tradeDate);
|
||||||
direction: trade.direction,
|
} else {
|
||||||
price: trade._tradePrice,
|
matches.sort((a, b) => b.tradeDate - a.tradeDate);
|
||||||
amount: trade._tradeAmount,
|
}
|
||||||
volume: trade._tradeVolume,
|
|
||||||
payment_method: trade.paymentMethod,
|
return matches.map((trade) => {
|
||||||
trade_id: trade.offerId,
|
const bsqTrade: BisqTrade = {
|
||||||
trade_date: trade.tradeDate,
|
direction: trade.primaryMarketDirection,
|
||||||
};
|
price: trade._tradePrice,
|
||||||
});
|
amount: trade._tradeAmount,
|
||||||
|
volume: trade._tradeVolume,
|
||||||
|
payment_method: trade.paymentMethod,
|
||||||
|
trade_id: trade.offerId,
|
||||||
|
trade_date: trade.tradeDate,
|
||||||
|
};
|
||||||
|
if (market === 'all') {
|
||||||
|
bsqTrade.market = trade._market;
|
||||||
|
}
|
||||||
|
return bsqTrade;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
getVolumes(
|
getVolumes(
|
||||||
@ -262,9 +282,13 @@ class BisqMarketsApi {
|
|||||||
): HighLowOpenClose[] {
|
): HighLowOpenClose[] {
|
||||||
if (!timestamp_from) {
|
if (!timestamp_from) {
|
||||||
timestamp_from = new Date('2016-01-01').getTime();
|
timestamp_from = new Date('2016-01-01').getTime();
|
||||||
|
} else {
|
||||||
|
timestamp_from = timestamp_from * 1000;
|
||||||
}
|
}
|
||||||
if (!timestamp_to) {
|
if (!timestamp_to) {
|
||||||
timestamp_to = new Date().getTime();
|
timestamp_to = new Date().getTime();
|
||||||
|
} else {
|
||||||
|
timestamp_to = timestamp_to * 1000;
|
||||||
}
|
}
|
||||||
return [];
|
return [];
|
||||||
}
|
}
|
||||||
|
@ -277,7 +277,7 @@ class Routes {
|
|||||||
},
|
},
|
||||||
'direction': {
|
'direction': {
|
||||||
required: false,
|
required: false,
|
||||||
types: ['BUY', 'SELL']
|
types: ['buy', 'sell']
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -287,7 +287,7 @@ class Routes {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = bisqMarket.getCurrencies(p.type);
|
const result = bisqMarket.getOffers(p.market, p.direction);
|
||||||
if (result) {
|
if (result) {
|
||||||
res.json(result);
|
res.json(result);
|
||||||
} else {
|
} else {
|
||||||
@ -295,30 +295,6 @@ class Routes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private parseRequestParameters(req: Request, params: RequiredSpec): { [name: string]: any; } {
|
|
||||||
const final = {};
|
|
||||||
for (const i in params) {
|
|
||||||
if (params.hasOwnProperty(i)) {
|
|
||||||
if (params[i].required && !req.query[i]) {
|
|
||||||
return { error: i + ' parameter missing'};
|
|
||||||
}
|
|
||||||
if (typeof req.query[i] === 'string') {
|
|
||||||
if (params[i].types.indexOf('@number') > -1) {
|
|
||||||
const number = parseInt((req.query[i] || '0').toString(), 10);
|
|
||||||
final[i] = number;
|
|
||||||
} else if (params[i].types.indexOf('@string') > -1) {
|
|
||||||
final[i] = req.query[i];
|
|
||||||
} else if (params[i].types.indexOf((req.query[i] || '').toString()) > -1) {
|
|
||||||
final[i] = req.query[i];
|
|
||||||
} else {
|
|
||||||
return { error: i + ' parameter invalid'};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return final;
|
|
||||||
}
|
|
||||||
|
|
||||||
public getBisqMarketVolumes(req: Request, res: Response) {
|
public getBisqMarketVolumes(req: Request, res: Response) {
|
||||||
const constraints: RequiredSpec = {
|
const constraints: RequiredSpec = {
|
||||||
'market': {
|
'market': {
|
||||||
@ -409,6 +385,31 @@ class Routes {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private parseRequestParameters(req: Request, params: RequiredSpec): { [name: string]: any; } {
|
||||||
|
const final = {};
|
||||||
|
for (const i in params) {
|
||||||
|
if (params.hasOwnProperty(i)) {
|
||||||
|
if (params[i].required && !req.query[i]) {
|
||||||
|
return { error: i + ' parameter missing'};
|
||||||
|
}
|
||||||
|
if (typeof req.query[i] === 'string') {
|
||||||
|
const str = (req.query[i] || '').toString().toLowerCase();
|
||||||
|
if (params[i].types.indexOf('@number') > -1) {
|
||||||
|
const number = parseInt((str).toString(), 10);
|
||||||
|
final[i] = number;
|
||||||
|
} else if (params[i].types.indexOf('@string') > -1) {
|
||||||
|
final[i] = str;
|
||||||
|
} else if (params[i].types.indexOf(str) > -1) {
|
||||||
|
final[i] = str;
|
||||||
|
} else {
|
||||||
|
return { error: i + ' parameter invalid'};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return final;
|
||||||
|
}
|
||||||
|
|
||||||
private getBisqMarketErrorResponse(message: string): MarketsApiError {
|
private getBisqMarketErrorResponse(message: string): MarketsApiError {
|
||||||
return {
|
return {
|
||||||
'success': 0,
|
'success': 0,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user