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

Commit

Permalink
Merge pull request #2099 from ethcore/jg-fund-account
Browse files Browse the repository at this point in the history
fund account via ShapeShift
  • Loading branch information
ngotchac authored Sep 15, 2016
2 parents 90811af + 001d888 commit 7921fa8
Show file tree
Hide file tree
Showing 39 changed files with 1,582 additions and 111 deletions.
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.ok) {
throw new Error(response.statusText);
}

return response.json();
})
.then((result) => {
if (result.error) {
throw new Error(result.error);
}

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);
});
});
});
93 changes: 93 additions & 0 deletions js/src/3rdparty/shapeshift/shapeshift.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
// 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) {
const subscriptions = [];

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

function subscribe (depositAddress, callback) {
const idx = subscriptions.length;

subscriptions.push({
depositAddress,
callback,
idx
});
}

function _getSubscriptionStatus (subscription) {
if (!subscription) {
return;
}

getStatus(subscription.depositAddress)
.then((result) => {
switch (result.status) {
case 'no_deposits':
case 'received':
subscription.callback(null, result);
return;

case 'complete':
subscription.callback(null, result);
subscriptions[subscription.idx] = null;
return;

case 'failed':
subscription.callback({
message: status.error,
fatal: true
});
subscriptions[subscription.idx] = null;
return;
}
})
.catch(subscription.callback);
}

function _pollStatus () {
subscriptions.forEach(_getSubscriptionStatus);
}

setInterval(_pollStatus, 2000);

return {
getCoins,
getMarketInfo,
getStatus,
shift,
subscribe
};
}
Loading

0 comments on commit 7921fa8

Please sign in to comment.