From 718835c7fb3956095ab576cc14551250dd3c437a Mon Sep 17 00:00:00 2001 From: pbca26 Date: Sat, 2 Dec 2017 17:58:45 +0300 Subject: [PATCH] dex request, cache --- routes/shepherd.js | 4 ++++ routes/shepherd/dex/mmControl.js | 6 ++++++ routes/shepherd/dex/mmRequest.js | 35 +++++++++++++++++++++++++++----- 3 files changed, 40 insertions(+), 5 deletions(-) diff --git a/routes/shepherd.js b/routes/shepherd.js index 48efa5d..3fff883 100644 --- a/routes/shepherd.js +++ b/routes/shepherd.js @@ -47,6 +47,9 @@ shepherd.mmPublic = { asks: [], isAuth: false, rates: {}, + prices: [], + coinsHelper: [], + stats: [], }; // spv vars and libs @@ -98,6 +101,7 @@ shepherd = require('./shepherd/electrum/estimate.js')(shepherd); // dex shepherd = require('./shepherd/dex/coind.js')(shepherd); shepherd = require('./shepherd/dex/mmControl.js')(shepherd); +shepherd = require('./shepherd/dex/mmRequest.js')(shepherd); // core shepherd = require('./shepherd/addCoinShortcuts.js')(shepherd); diff --git a/routes/shepherd/dex/mmControl.js b/routes/shepherd/dex/mmControl.js index 5c89086..a34a7e2 100644 --- a/routes/shepherd/dex/mmControl.js +++ b/routes/shepherd/dex/mmControl.js @@ -43,6 +43,7 @@ module.exports = (shepherd) => { shepherd.mmPublic.coins = _parsedBody.coins; shepherd.log(`mm start success`); shepherd.log(`mm userpass ${_parsedBody.userpass}`); + shepherd.getCoinsHelper(); shepherd.getRates(); } } else { @@ -59,6 +60,11 @@ module.exports = (shepherd) => { }, 500); }); + shepherd.getCoinsHelper = () => { + const defaultCoinsListFile = path.join(__dirname, '../dex/coins.json'); + shepherd.mmPublic.coinsHelper = fs.readJsonSync(defaultCoinsListFile, { throws: false }); + } + shepherd.getRates = () => { function _getRates() { const options = { diff --git a/routes/shepherd/dex/mmRequest.js b/routes/shepherd/dex/mmRequest.js index bf261df..c91a9de 100644 --- a/routes/shepherd/dex/mmRequest.js +++ b/routes/shepherd/dex/mmRequest.js @@ -1,13 +1,38 @@ +const request = require('request'); + module.exports = (shepherd) => { // payload // record all calls - shepherd.get('/mm/request', (req, res, next) => { - const successObj = { - msg: 'success', - result: 'started', + shepherd.post('/mm/request', (req, res, next) => { + let _payload = req.body.payload; + _payload.userpass = shepherd.mmupass; + const options = { + url: `http://localhost:7783`, + method: 'POST', + body: JSON.stringify(_payload), }; - res.end(JSON.stringify(successObj)); + shepherd.log(_payload); + + // send back body on both success and error + // this bit replicates iguana core's behaviour + request(options, (error, response, body) => { + if (response && + response.statusCode && + response.statusCode === 200) { + const _parsedBody = JSON.parse(body); + shepherd.mmPublic[_payload.mapToProp] = _parsedBody; + res.end(body); + } else { + res.end(body ? body : JSON.stringify({ + result: 'error', + error: { + code: -777, + message: `unable to call method ${_payload.method} at port 7783`, + }, + })); + } + }); }); return shepherd;