This repository has been archived by the owner on Jun 15, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bernard Labno
committed
May 21, 2018
1 parent
eea7498
commit 911f6ca
Showing
3 changed files
with
108 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const _ = require('lodash'); | ||
const $http = require('http-as-promised'); | ||
|
||
const threshold = 0.1; | ||
|
||
//tag::getPriceFilter[] | ||
function getPriceFilter(marketPrice) { | ||
const maxPrice = marketPrice * (1 - threshold) * 10000; | ||
return offer => { | ||
if (offer.useMarketBasedPrice) | ||
return offer.marketPriceMargin >= threshold; | ||
return offer.price < maxPrice; | ||
} | ||
} | ||
//end::getPriceFilter[] | ||
|
||
//tag::getMarketPrice[] | ||
function getMarketPrice() { | ||
return $http.get('http://localhost:8080/api/v1/currencies/prices?currencyCodes=EUR', {resolve: 'body', json: true}) | ||
.then(body => _.get(body, 'prices.EUR')) | ||
} | ||
//end::getMarketPrice[] | ||
|
||
//tag::getOffers[] | ||
function getOffers() { | ||
return $http.get('http://localhost:8080/api/v1/offers', {resolve: 'body', json: true}).then(body => body.offers); | ||
} | ||
//end::getOffers[] | ||
|
||
function notify(offers) { | ||
if (!offers.length) { | ||
console.log('No interesting offers found'); | ||
return; | ||
} | ||
|
||
const text = _.map(offers, offer => `${offer.amount / 100000000} BTC (-${_.round(offer.margin * 100, 2)}%)`).join('\n'); | ||
console.info(text); | ||
} | ||
|
||
//tag::filterOffers[] | ||
function filterOffers([offers, marketPrice]) { | ||
return _(offers) | ||
.filter({ | ||
baseCurrencyCode: 'BTC', | ||
counterCurrencyCode: 'EUR', | ||
direction: 'SELL', | ||
paymentMethodId: 'SEPA' | ||
}) | ||
.filter(i => _.includes(i.acceptedCountryCodes, 'PL')) | ||
.filter(getPriceFilter(marketPrice)) | ||
.map(i => _.pick(i, 'baseCurrencyCode', 'counterCurrencyCode', 'direction', 'paymentMethodId', 'id', 'useMarketBasedPrice', 'price', 'marketPriceMargin', 'amount', 'minAmont')) | ||
.map(i => ({amount: i.amount, margin: i.useMarketBasedPrice ? i.marketPriceMargin : marketPrice / i.price})) | ||
.value(); | ||
} | ||
//end::filterOffers[] | ||
|
||
//tag::flow[] | ||
Promise.all([getOffers(), getMarketPrice()]) | ||
.then(filterOffers) | ||
.then(notify) | ||
.catch(e => console.error(e)); | ||
//end::flow[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters