From 21c135f7f2ffa57b6d5367a2bc186fd063336b71 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 13 Sep 2016 23:32:35 +0200 Subject: [PATCH 1/6] initial import before tweaking starts --- js/src/3rdparty/shapeshift/helpers.spec.js | 53 +++++++++ js/src/3rdparty/shapeshift/index.js | 5 + js/src/3rdparty/shapeshift/lib/rpc.js | 56 +++++++++ js/src/3rdparty/shapeshift/lib/rpc.spec.js | 62 ++++++++++ js/src/3rdparty/shapeshift/lib/shapeshift.js | 23 ++++ .../shapeshift/lib/shapeshift.spec.js | 109 ++++++++++++++++++ 6 files changed, 308 insertions(+) create mode 100644 js/src/3rdparty/shapeshift/helpers.spec.js create mode 100644 js/src/3rdparty/shapeshift/index.js create mode 100644 js/src/3rdparty/shapeshift/lib/rpc.js create mode 100644 js/src/3rdparty/shapeshift/lib/rpc.spec.js create mode 100644 js/src/3rdparty/shapeshift/lib/shapeshift.js create mode 100644 js/src/3rdparty/shapeshift/lib/shapeshift.spec.js diff --git a/js/src/3rdparty/shapeshift/helpers.spec.js b/js/src/3rdparty/shapeshift/helpers.spec.js new file mode 100644 index 00000000000..86d7d40b628 --- /dev/null +++ b/js/src/3rdparty/shapeshift/helpers.spec.js @@ -0,0 +1,53 @@ +'use strict'; + +const APIKEY = '0x123454321'; + +const chai = require('chai'); +const nock = require('nock'); + +require('es6-promise').polyfill(); +require('isomorphic-fetch'); + +const shapeshift = require('./index.js')(APIKEY); +const rpc = require('./lib/rpc')(APIKEY); + +const mockget = function(requests) { + let scope = nock(rpc.ENDPOINT); + + requests.forEach((request) => { + scope = scope + .get(`/${request.path}`) + .reply(request.code || 200, () => { + return request.reply; + }); + }); + + return scope; +}; + +const mockpost = function(requests) { + let scope = nock(rpc.ENDPOINT); + + requests.forEach((request) => { + scope = scope + .post(`/${request.path}`) + .reply(request.code || 200, (uri, body) => { + scope.body = scope.body || {}; + scope.body[request.path] = body; + + return request.reply; + }); + }); + + return scope; +}; + +global.expect = chai.expect; // eslint-disable-line no-undef + +module.exports = { + APIKEY: APIKEY, + mockget: mockget, + mockpost: mockpost, + shapeshift: shapeshift, + rpc: rpc +}; diff --git a/js/src/3rdparty/shapeshift/index.js b/js/src/3rdparty/shapeshift/index.js new file mode 100644 index 00000000000..ee1ebfd670d --- /dev/null +++ b/js/src/3rdparty/shapeshift/index.js @@ -0,0 +1,5 @@ +module.exports = function(apikey) { + const rpc = require('./lib/rpc')(apikey); + + return require('./lib/shapeshift')(rpc); +}; diff --git a/js/src/3rdparty/shapeshift/lib/rpc.js b/js/src/3rdparty/shapeshift/lib/rpc.js new file mode 100644 index 00000000000..ad5ce64455b --- /dev/null +++ b/js/src/3rdparty/shapeshift/lib/rpc.js @@ -0,0 +1,56 @@ +const ENDPOINT = 'https://cors.shapeshift.io'; + +module.exports = function(apikey) { + const call = function(method, options) { + return fetch(`${ENDPOINT}/${method}`, options) + .then((response) => { + if (response.status !== 200) { + throw { code: response.status, message: response.statusText }; // eslint-disable-line + } + + return response.json(); + }) + .then((result) => { + if (result.error) { + throw { code: -1, message: result.error }; // eslint-disable-line + } + + return result; + }); + }; + + return { + ENDPOINT: ENDPOINT, + + get: function(method) { + return call(method, { + method: 'GET', + headers: { + 'Accept': 'application/json' + } + }); + }, + + post: function(method, data) { + const params = { + apiKey: apikey + }; + + Object.keys(data).forEach((key) => { + params[key] = data[key]; + }); + + const json = JSON.stringify(params); + + return call(method, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'Content-Length': json.length + }, + body: json + }); + } + }; +}; diff --git a/js/src/3rdparty/shapeshift/lib/rpc.spec.js b/js/src/3rdparty/shapeshift/lib/rpc.spec.js new file mode 100644 index 00000000000..3aa44f26235 --- /dev/null +++ b/js/src/3rdparty/shapeshift/lib/rpc.spec.js @@ -0,0 +1,62 @@ +const helpers = require('../helpers.spec.js'); +const { APIKEY, mockget, mockpost, rpc } = helpers; + +describe('lib/rpc', () => { + describe('GET', () => { + const REPLY = { test: 'this is some result' }; + + let scope; + let result; + + beforeEach(() => { + scope = mockget([{ path: 'test', reply: REPLY }]); + + return rpc + .get('test') + .then((_result) => { + result = _result; + }); + }); + + it('does GET', () => { + expect(scope.isDone()).to.be.true; + }); + + it('retrieves the info', () => { + expect(result).to.deep.equal(REPLY); + }); + }); + + describe('POST', () => { + const REPLY = { test: 'this is some result' }; + + let scope; + let result; + + beforeEach(() => { + scope = mockpost([{ path: 'test', reply: REPLY }]); + + return rpc + .post('test', { input: 'stuff' }) + .then((_result) => { + result = _result; + }); + }); + + it('does POST', () => { + expect(scope.isDone()).to.be.true; + }); + + it('retrieves the info', () => { + expect(result).to.deep.equal(REPLY); + }); + + it('passes the input object', () => { + expect(scope.body.test.input).to.equal('stuff'); + }); + + it('passes the apikey specified', () => { + expect(scope.body.test.apiKey).to.equal(APIKEY); + }); + }); +}); diff --git a/js/src/3rdparty/shapeshift/lib/shapeshift.js b/js/src/3rdparty/shapeshift/lib/shapeshift.js new file mode 100644 index 00000000000..01f61fa02db --- /dev/null +++ b/js/src/3rdparty/shapeshift/lib/shapeshift.js @@ -0,0 +1,23 @@ +module.exports = function(rpc) { + return { + getCoins: function() { + return rpc.get('getcoins'); + }, + + getMarketInfo: function(pair) { + return rpc.get(`marketinfo/${pair}`); + }, + + getStatus: function(depositAddress) { + return rpc.get(`txStat/${depositAddress}`); + }, + + shift: function(toAddress, returnAddress, pair) { + return rpc.post('shift', { + withdrawal: toAddress, + pair: pair, + returnAddress: returnAddress + }); + } + }; +}; diff --git a/js/src/3rdparty/shapeshift/lib/shapeshift.spec.js b/js/src/3rdparty/shapeshift/lib/shapeshift.spec.js new file mode 100644 index 00000000000..7e596a0059e --- /dev/null +++ b/js/src/3rdparty/shapeshift/lib/shapeshift.spec.js @@ -0,0 +1,109 @@ +const helpers = require('../helpers.spec.js'); +const { mockget, mockpost, shapeshift } = helpers; + +describe('lib/calls', () => { + describe('getCoins', () => { + const REPLY = { + BTC: { + name: 'Bitcoin', + symbol: 'BTC', + image: 'https://shapeshift.io/images/coins/bitcoin.png', + status: 'available' + }, + ETH: { + name: 'Ether', + symbol: 'ETH', + image: 'https://shapeshift.io/images/coins/ether.png', + status: 'available' + } + }; + + let scope; + + before(() => { + scope = mockget([{ path: 'getcoins', reply: REPLY }]); + + return shapeshift.getCoins(); + }); + + it('makes the call', () => { + expect(scope.isDone()).to.be.ok; + }); + }); + + describe('getMarketInfo', () => { + const REPLY = { + pair: 'btc_ltc', + rate: 128.17959917, + minerFee: 0.003, + limit: 0, + minimum: 0.00004632 + }; + + let scope; + + before(() => { + scope = mockget([{ path: 'marketinfo/btc_ltc', reply: REPLY }]); + + return shapeshift.getMarketInfo('btc_ltc'); + }); + + it('makes the call', () => { + expect(scope.isDone()).to.be.ok; + }); + }); + + describe('getStatus', () => { + const REPLY = { + status: '0x123', + address: '0x123' + }; + + let scope; + + before(() => { + scope = mockget([{ path: 'txStat/0x123', reply: REPLY }]); + + return shapeshift.getStatus('0x123'); + }); + + it('makes the call', () => { + expect(scope.isDone()).to.be.ok; + }); + }); + + describe('shift', () => { + const REPLY = { + deposit: '1BTC', + depositType: 'btc', + withdrawal: '0x456', + withdrawalType: 'eth' + }; + + let scope; + + before(() => { + scope = mockpost([{ path: 'shift', reply: REPLY }]); + + return shapeshift.shift('0x456', '1BTC', 'btc_eth'); + }); + + it('makes the call', () => { + expect(scope.isDone()).to.be.ok; + }); + + describe('body', () => { + it('has withdrawal set', () => { + expect(scope.body.shift.withdrawal).to.equal('0x456'); + }); + + it('has returnAddress set', () => { + expect(scope.body.shift.returnAddress).to.equal('1BTC'); + }); + + it('has pair set', () => { + expect(scope.body.shift.pair).to.equal('btc_eth'); + }); + }); + }); +}); From fc304c0aa9f13c01a0baff2450778367a0663c21 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 13 Sep 2016 23:33:22 +0200 Subject: [PATCH 2/6] GPL header --- js/src/3rdparty/shapeshift/helpers.spec.js | 16 ++++++++++++++++ js/src/3rdparty/shapeshift/index.js | 16 ++++++++++++++++ js/src/3rdparty/shapeshift/lib/rpc.js | 16 ++++++++++++++++ js/src/3rdparty/shapeshift/lib/rpc.spec.js | 16 ++++++++++++++++ js/src/3rdparty/shapeshift/lib/shapeshift.js | 16 ++++++++++++++++ .../3rdparty/shapeshift/lib/shapeshift.spec.js | 16 ++++++++++++++++ 6 files changed, 96 insertions(+) diff --git a/js/src/3rdparty/shapeshift/helpers.spec.js b/js/src/3rdparty/shapeshift/helpers.spec.js index 86d7d40b628..aa389a30d34 100644 --- a/js/src/3rdparty/shapeshift/helpers.spec.js +++ b/js/src/3rdparty/shapeshift/helpers.spec.js @@ -1,3 +1,19 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + 'use strict'; const APIKEY = '0x123454321'; diff --git a/js/src/3rdparty/shapeshift/index.js b/js/src/3rdparty/shapeshift/index.js index ee1ebfd670d..bee544e4716 100644 --- a/js/src/3rdparty/shapeshift/index.js +++ b/js/src/3rdparty/shapeshift/index.js @@ -1,3 +1,19 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + module.exports = function(apikey) { const rpc = require('./lib/rpc')(apikey); diff --git a/js/src/3rdparty/shapeshift/lib/rpc.js b/js/src/3rdparty/shapeshift/lib/rpc.js index ad5ce64455b..91b4490b5d7 100644 --- a/js/src/3rdparty/shapeshift/lib/rpc.js +++ b/js/src/3rdparty/shapeshift/lib/rpc.js @@ -1,3 +1,19 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + const ENDPOINT = 'https://cors.shapeshift.io'; module.exports = function(apikey) { diff --git a/js/src/3rdparty/shapeshift/lib/rpc.spec.js b/js/src/3rdparty/shapeshift/lib/rpc.spec.js index 3aa44f26235..57128806b74 100644 --- a/js/src/3rdparty/shapeshift/lib/rpc.spec.js +++ b/js/src/3rdparty/shapeshift/lib/rpc.spec.js @@ -1,3 +1,19 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + const helpers = require('../helpers.spec.js'); const { APIKEY, mockget, mockpost, rpc } = helpers; diff --git a/js/src/3rdparty/shapeshift/lib/shapeshift.js b/js/src/3rdparty/shapeshift/lib/shapeshift.js index 01f61fa02db..bb333e76da1 100644 --- a/js/src/3rdparty/shapeshift/lib/shapeshift.js +++ b/js/src/3rdparty/shapeshift/lib/shapeshift.js @@ -1,3 +1,19 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + module.exports = function(rpc) { return { getCoins: function() { diff --git a/js/src/3rdparty/shapeshift/lib/shapeshift.spec.js b/js/src/3rdparty/shapeshift/lib/shapeshift.spec.js index 7e596a0059e..922ef271cf7 100644 --- a/js/src/3rdparty/shapeshift/lib/shapeshift.spec.js +++ b/js/src/3rdparty/shapeshift/lib/shapeshift.spec.js @@ -1,3 +1,19 @@ +// Copyright 2015, 2016 Ethcore (UK) Ltd. +// This file is part of Parity. + +// Parity is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// Parity is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. + +// You should have received a copy of the GNU General Public License +// along with Parity. If not, see . + const helpers = require('../helpers.spec.js'); const { mockget, mockpost, shapeshift } = helpers; From 6faff8f3c1f5cf37d9ed57e9d0697c5c3c425c97 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 13 Sep 2016 23:44:13 +0200 Subject: [PATCH 3/6] es6-ify library & flatten --- js/src/3rdparty/shapeshift/helpers.spec.js | 44 ++++++++++--------- js/src/3rdparty/shapeshift/index.js | 9 ++-- js/src/3rdparty/shapeshift/{lib => }/rpc.js | 12 ++--- .../3rdparty/shapeshift/{lib => }/rpc.spec.js | 3 +- .../shapeshift/{lib => }/shapeshift.js | 12 ++--- .../shapeshift/{lib => }/shapeshift.spec.js | 3 +- 6 files changed, 42 insertions(+), 41 deletions(-) rename js/src/3rdparty/shapeshift/{lib => }/rpc.js (92%) rename js/src/3rdparty/shapeshift/{lib => }/rpc.spec.js (95%) rename js/src/3rdparty/shapeshift/{lib => }/shapeshift.js (83%) rename js/src/3rdparty/shapeshift/{lib => }/shapeshift.spec.js (96%) diff --git a/js/src/3rdparty/shapeshift/helpers.spec.js b/js/src/3rdparty/shapeshift/helpers.spec.js index aa389a30d34..04d85f8d06c 100644 --- a/js/src/3rdparty/shapeshift/helpers.spec.js +++ b/js/src/3rdparty/shapeshift/helpers.spec.js @@ -14,20 +14,24 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -'use strict'; +import chai from 'chai'; +import nock from 'nock'; -const APIKEY = '0x123454321'; +global.expect = chai.expect; // eslint-disable-line no-undef -const chai = require('chai'); -const nock = require('nock'); +import 'isomorphic-fetch'; +import es6Promise from 'es6-promise'; +es6Promise.polyfill(); -require('es6-promise').polyfill(); -require('isomorphic-fetch'); +import libShapeshift from './'; +import libRpc from './lib/rpc'; -const shapeshift = require('./index.js')(APIKEY); -const rpc = require('./lib/rpc')(APIKEY); +const APIKEY = '0x123454321'; -const mockget = function(requests) { +const shapeshift = libShapeshift(APIKEY); +const rpc = libRpc(APIKEY); + +function mockget (requests) { let scope = nock(rpc.ENDPOINT); requests.forEach((request) => { @@ -39,9 +43,9 @@ const mockget = function(requests) { }); return scope; -}; +} -const mockpost = function(requests) { +function mockpost (requests) { let scope = nock(rpc.ENDPOINT); requests.forEach((request) => { @@ -56,14 +60,12 @@ const mockpost = function(requests) { }); return scope; -}; - -global.expect = chai.expect; // eslint-disable-line no-undef - -module.exports = { - APIKEY: APIKEY, - mockget: mockget, - mockpost: mockpost, - shapeshift: shapeshift, - rpc: rpc +} + +export { + APIKEY, + mockget, + mockpost, + shapeshift, + rpc }; diff --git a/js/src/3rdparty/shapeshift/index.js b/js/src/3rdparty/shapeshift/index.js index bee544e4716..5dd91e4f42e 100644 --- a/js/src/3rdparty/shapeshift/index.js +++ b/js/src/3rdparty/shapeshift/index.js @@ -14,8 +14,9 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -module.exports = function(apikey) { - const rpc = require('./lib/rpc')(apikey); +import initRpc from './rpc'; +import initShapeshift from './shapeshift'; - return require('./lib/shapeshift')(rpc); -}; +export default function (apikey) { + return initShapeshift(initRpc(apikey)); +} diff --git a/js/src/3rdparty/shapeshift/lib/rpc.js b/js/src/3rdparty/shapeshift/rpc.js similarity index 92% rename from js/src/3rdparty/shapeshift/lib/rpc.js rename to js/src/3rdparty/shapeshift/rpc.js index 91b4490b5d7..0e28d0f73c7 100644 --- a/js/src/3rdparty/shapeshift/lib/rpc.js +++ b/js/src/3rdparty/shapeshift/rpc.js @@ -16,8 +16,8 @@ const ENDPOINT = 'https://cors.shapeshift.io'; -module.exports = function(apikey) { - const call = function(method, options) { +export default function (apikey) { + function call (method, options) { return fetch(`${ENDPOINT}/${method}`, options) .then((response) => { if (response.status !== 200) { @@ -33,12 +33,12 @@ module.exports = function(apikey) { return result; }); - }; + } return { ENDPOINT: ENDPOINT, - get: function(method) { + get: function (method) { return call(method, { method: 'GET', headers: { @@ -47,7 +47,7 @@ module.exports = function(apikey) { }); }, - post: function(method, data) { + post: function (method, data) { const params = { apiKey: apikey }; @@ -69,4 +69,4 @@ module.exports = function(apikey) { }); } }; -}; +} diff --git a/js/src/3rdparty/shapeshift/lib/rpc.spec.js b/js/src/3rdparty/shapeshift/rpc.spec.js similarity index 95% rename from js/src/3rdparty/shapeshift/lib/rpc.spec.js rename to js/src/3rdparty/shapeshift/rpc.spec.js index 57128806b74..cdcbe9c61cc 100644 --- a/js/src/3rdparty/shapeshift/lib/rpc.spec.js +++ b/js/src/3rdparty/shapeshift/rpc.spec.js @@ -14,8 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -const helpers = require('../helpers.spec.js'); -const { APIKEY, mockget, mockpost, rpc } = helpers; +import { APIKEY, mockget, mockpost, rpc } from './helpers.spec.js'; describe('lib/rpc', () => { describe('GET', () => { diff --git a/js/src/3rdparty/shapeshift/lib/shapeshift.js b/js/src/3rdparty/shapeshift/shapeshift.js similarity index 83% rename from js/src/3rdparty/shapeshift/lib/shapeshift.js rename to js/src/3rdparty/shapeshift/shapeshift.js index bb333e76da1..a094e450710 100644 --- a/js/src/3rdparty/shapeshift/lib/shapeshift.js +++ b/js/src/3rdparty/shapeshift/shapeshift.js @@ -14,21 +14,21 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -module.exports = function(rpc) { +export default function (rpc) { return { - getCoins: function() { + getCoins: function () { return rpc.get('getcoins'); }, - getMarketInfo: function(pair) { + getMarketInfo: function (pair) { return rpc.get(`marketinfo/${pair}`); }, - getStatus: function(depositAddress) { + getStatus: function (depositAddress) { return rpc.get(`txStat/${depositAddress}`); }, - shift: function(toAddress, returnAddress, pair) { + shift: function (toAddress, returnAddress, pair) { return rpc.post('shift', { withdrawal: toAddress, pair: pair, @@ -36,4 +36,4 @@ module.exports = function(rpc) { }); } }; -}; +} diff --git a/js/src/3rdparty/shapeshift/lib/shapeshift.spec.js b/js/src/3rdparty/shapeshift/shapeshift.spec.js similarity index 96% rename from js/src/3rdparty/shapeshift/lib/shapeshift.spec.js rename to js/src/3rdparty/shapeshift/shapeshift.spec.js index 922ef271cf7..1997535d405 100644 --- a/js/src/3rdparty/shapeshift/lib/shapeshift.spec.js +++ b/js/src/3rdparty/shapeshift/shapeshift.spec.js @@ -14,8 +14,7 @@ // You should have received a copy of the GNU General Public License // along with Parity. If not, see . -const helpers = require('../helpers.spec.js'); -const { mockget, mockpost, shapeshift } = helpers; +import { mockget, mockpost, shapeshift } from './helpers.spec.js'; describe('lib/calls', () => { describe('getCoins', () => { From 1a6daa8ec2a8dea9fde9314c19e9b871f19ce39c Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 13 Sep 2016 23:48:22 +0200 Subject: [PATCH 4/6] fix tests with new structure, proper naming --- js/src/3rdparty/shapeshift/helpers.spec.js | 8 ++++---- js/src/3rdparty/shapeshift/rpc.spec.js | 2 +- js/src/3rdparty/shapeshift/shapeshift.spec.js | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/js/src/3rdparty/shapeshift/helpers.spec.js b/js/src/3rdparty/shapeshift/helpers.spec.js index 04d85f8d06c..a82b2f6c39f 100644 --- a/js/src/3rdparty/shapeshift/helpers.spec.js +++ b/js/src/3rdparty/shapeshift/helpers.spec.js @@ -23,13 +23,13 @@ import 'isomorphic-fetch'; import es6Promise from 'es6-promise'; es6Promise.polyfill(); -import libShapeshift from './'; -import libRpc from './lib/rpc'; +import initShapeshift from './'; +import initRpc from './rpc'; const APIKEY = '0x123454321'; -const shapeshift = libShapeshift(APIKEY); -const rpc = libRpc(APIKEY); +const shapeshift = initShapeshift(APIKEY); +const rpc = initRpc(APIKEY); function mockget (requests) { let scope = nock(rpc.ENDPOINT); diff --git a/js/src/3rdparty/shapeshift/rpc.spec.js b/js/src/3rdparty/shapeshift/rpc.spec.js index cdcbe9c61cc..8de9e86413f 100644 --- a/js/src/3rdparty/shapeshift/rpc.spec.js +++ b/js/src/3rdparty/shapeshift/rpc.spec.js @@ -16,7 +16,7 @@ import { APIKEY, mockget, mockpost, rpc } from './helpers.spec.js'; -describe('lib/rpc', () => { +describe('shapeshift/rpc', () => { describe('GET', () => { const REPLY = { test: 'this is some result' }; diff --git a/js/src/3rdparty/shapeshift/shapeshift.spec.js b/js/src/3rdparty/shapeshift/shapeshift.spec.js index 1997535d405..36b1506a271 100644 --- a/js/src/3rdparty/shapeshift/shapeshift.spec.js +++ b/js/src/3rdparty/shapeshift/shapeshift.spec.js @@ -16,7 +16,7 @@ import { mockget, mockpost, shapeshift } from './helpers.spec.js'; -describe('lib/calls', () => { +describe('shapeshift/calls', () => { describe('getCoins', () => { const REPLY = { BTC: { From cab8a6b1b8be5b7c6e83139f895b38f3ffff7754 Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Tue, 13 Sep 2016 23:55:30 +0200 Subject: [PATCH 5/6] define functions explicitly withing closure --- js/src/3rdparty/shapeshift/rpc.js | 58 ++++++++++++------------ js/src/3rdparty/shapeshift/shapeshift.js | 43 ++++++++++-------- 2 files changed, 54 insertions(+), 47 deletions(-) diff --git a/js/src/3rdparty/shapeshift/rpc.js b/js/src/3rdparty/shapeshift/rpc.js index 0e28d0f73c7..087a69214f4 100644 --- a/js/src/3rdparty/shapeshift/rpc.js +++ b/js/src/3rdparty/shapeshift/rpc.js @@ -35,38 +35,40 @@ export default function (apikey) { }); } - return { - ENDPOINT: ENDPOINT, + function get (method) { + return call(method, { + method: 'GET', + headers: { + 'Accept': 'application/json' + } + }); + } - get: function (method) { - return call(method, { - method: 'GET', - headers: { - 'Accept': 'application/json' - } - }); - }, + function post (method, data) { + const params = { + apiKey: apikey + }; - post: function (method, data) { - const params = { - apiKey: apikey - }; + Object.keys(data).forEach((key) => { + params[key] = data[key]; + }); - Object.keys(data).forEach((key) => { - params[key] = data[key]; - }); + const json = JSON.stringify(params); - const json = JSON.stringify(params); + return call(method, { + method: 'POST', + headers: { + 'Accept': 'application/json', + 'Content-Type': 'application/json', + 'Content-Length': json.length + }, + body: json + }); + } - return call(method, { - method: 'POST', - headers: { - 'Accept': 'application/json', - 'Content-Type': 'application/json', - 'Content-Length': json.length - }, - body: json - }); - } + return { + ENDPOINT, + get, + post }; } diff --git a/js/src/3rdparty/shapeshift/shapeshift.js b/js/src/3rdparty/shapeshift/shapeshift.js index a094e450710..7d20f155b36 100644 --- a/js/src/3rdparty/shapeshift/shapeshift.js +++ b/js/src/3rdparty/shapeshift/shapeshift.js @@ -15,25 +15,30 @@ // along with Parity. If not, see . export default function (rpc) { + function getCoins () { + return rpc.get('getcoins'); + } + + function getMarketInfo (pair) { + return rpc.get(`marketinfo/${pair}`); + } + + function getStatus (depositAddress) { + return rpc.get(`txStat/${depositAddress}`); + } + + function shift (toAddress, returnAddress, pair) { + return rpc.post('shift', { + withdrawal: toAddress, + pair: pair, + returnAddress: returnAddress + }); + } + return { - getCoins: function () { - return rpc.get('getcoins'); - }, - - getMarketInfo: function (pair) { - return rpc.get(`marketinfo/${pair}`); - }, - - getStatus: function (depositAddress) { - return rpc.get(`txStat/${depositAddress}`); - }, - - shift: function (toAddress, returnAddress, pair) { - return rpc.post('shift', { - withdrawal: toAddress, - pair: pair, - returnAddress: returnAddress - }); - } + getCoins, + getMarketInfo, + getStatus, + shift }; } From 186baff7ebaf9b48ead6ce0c23c52140ee25be8a Mon Sep 17 00:00:00 2001 From: Jaco Greeff Date: Wed, 14 Sep 2016 00:13:11 +0200 Subject: [PATCH 6/6] call is not needed within closure --- js/src/3rdparty/shapeshift/rpc.js | 47 +++++++++++++------------------ 1 file changed, 20 insertions(+), 27 deletions(-) diff --git a/js/src/3rdparty/shapeshift/rpc.js b/js/src/3rdparty/shapeshift/rpc.js index 087a69214f4..d2df7f3e8c5 100644 --- a/js/src/3rdparty/shapeshift/rpc.js +++ b/js/src/3rdparty/shapeshift/rpc.js @@ -16,25 +16,25 @@ const ENDPOINT = 'https://cors.shapeshift.io'; -export default function (apikey) { - function call (method, options) { - return fetch(`${ENDPOINT}/${method}`, options) - .then((response) => { - if (response.status !== 200) { - throw { code: response.status, message: response.statusText }; // eslint-disable-line - } +function call (method, options) { + return fetch(`${ENDPOINT}/${method}`, options) + .then((response) => { + if (response.status !== 200) { + throw { code: response.status, message: response.statusText }; // eslint-disable-line + } - return response.json(); - }) - .then((result) => { - if (result.error) { - throw { code: -1, message: result.error }; // eslint-disable-line - } + return response.json(); + }) + .then((result) => { + if (result.error) { + throw { code: -1, message: result.error }; // eslint-disable-line + } - return result; - }); - } + return result; + }); +} +export default function (apiKey) { function get (method) { return call(method, { method: 'GET', @@ -45,24 +45,17 @@ export default function (apikey) { } function post (method, data) { - const params = { - apiKey: apikey - }; - - Object.keys(data).forEach((key) => { - params[key] = data[key]; - }); - - const json = JSON.stringify(params); + const params = Object.assign({}, { apiKey }, data); + const body = JSON.stringify(params); return call(method, { method: 'POST', headers: { 'Accept': 'application/json', 'Content-Type': 'application/json', - 'Content-Length': json.length + 'Content-Length': body.length }, - body: json + body }); }