Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

shapeshift Promise API library #2088

Merged
merged 6 commits into from
Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 71 additions & 0 deletions js/src/3rdparty/shapeshift/helpers.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// 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 <http://www.gnu.org/licenses/>.

import chai from 'chai';
import nock from 'nock';

global.expect = chai.expect; // eslint-disable-line no-undef

import 'isomorphic-fetch';
import es6Promise from 'es6-promise';
es6Promise.polyfill();

import initShapeshift from './';
import initRpc from './rpc';

const APIKEY = '0x123454321';

const shapeshift = initShapeshift(APIKEY);
const rpc = initRpc(APIKEY);

function mockget (requests) {
let scope = nock(rpc.ENDPOINT);

requests.forEach((request) => {
scope = scope
.get(`/${request.path}`)
.reply(request.code || 200, () => {
return request.reply;
});
});

return scope;
}

function mockpost (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;
}

export {
APIKEY,
mockget,
mockpost,
shapeshift,
rpc
};
22 changes: 22 additions & 0 deletions js/src/3rdparty/shapeshift/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// 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 <http://www.gnu.org/licenses/>.

import initRpc from './rpc';
import initShapeshift from './shapeshift';

export default function (apikey) {
return initShapeshift(initRpc(apikey));
}
67 changes: 67 additions & 0 deletions js/src/3rdparty/shapeshift/rpc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
// 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 <http://www.gnu.org/licenses/>.

const ENDPOINT = 'https://cors.shapeshift.io';

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 result;
});
}

export default function (apiKey) {
function get (method) {
return call(method, {
method: 'GET',
headers: {
'Accept': 'application/json'
}
});
}

function post (method, data) {
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': body.length
},
body
});
}

return {
ENDPOINT,
get,
post
};
}
77 changes: 77 additions & 0 deletions js/src/3rdparty/shapeshift/rpc.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
// 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 <http://www.gnu.org/licenses/>.

import { APIKEY, mockget, mockpost, rpc } from './helpers.spec.js';

describe('shapeshift/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);
});
});
});
44 changes: 44 additions & 0 deletions js/src/3rdparty/shapeshift/shapeshift.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// 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 <http://www.gnu.org/licenses/>.

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,
getMarketInfo,
getStatus,
shift
};
}
Loading