-
Notifications
You must be signed in to change notification settings - Fork 1.7k
fund account via ShapeShift #2099
Changes from 33 commits
21c135f
fc304c0
6faff8f
1a6daa8
cab8a6b
186baff
ea5e54a
b2771fe
cc87e5e
24fff44
be99b09
bfebf03
39f736a
3cca307
0b71fc8
b09c7c9
03b3a36
937f3d2
3e5c8df
62c4fc7
581efd8
7698927
81c8eed
885d321
ab7faf2
8b6b07c
902c8ac
38e2e42
26d7b27
5adc791
a3bea1f
9703f4b
369334c
f1fac42
268f4ef
2981c87
001d888
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 | ||
}; |
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)); | ||
} |
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As people often check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Actually needs to be done to align with the other APIs as well. |
||
} | ||
|
||
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 | ||
}; | ||
} |
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); | ||
}); | ||
}); | ||
}); |
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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. As above – using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only issue here is that there are 2 types of errors - recoverable and non-recoverable. The fatal flag indicates that. So not 100% sure how to handle that. |
||
message: status.error, | ||
fatal: true | ||
}); | ||
subscriptions[subscription.idx] = null; | ||
return; | ||
} | ||
}) | ||
.catch(subscription.callback); | ||
} | ||
|
||
function _pollStatus () { | ||
subscriptions.map(_getSubscriptionStatus); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch, fixed. |
||
} | ||
|
||
setInterval(_pollStatus, 2000); | ||
|
||
return { | ||
getCoins, | ||
getMarketInfo, | ||
getStatus, | ||
shift, | ||
subscribe | ||
}; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think checking for
200 <= status && status < 300
is safer, as the HTTP RFC defines2xx
as "client's request was successfully received, understood, and accepted.".Edit: Check for
response.ok
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
response.ok :)