crypto-exchange-manager
Advanced tools
Comparing version 4.0.4 to 5.0.0
const fetch = require('isomorphic-fetch'); | ||
const crypto = require('crypto') | ||
const crypto = require('crypto'); | ||
const {convertToUSDIfPossible} = require('../utils'); | ||
const wait = (...args) => new Promise((resolve, reject) => { | ||
setTimeout( () => resolve(...args), 1000) | ||
}); | ||
module.exports = class { | ||
@@ -53,27 +58,50 @@ constructor(apiKey) { | ||
getWallet() { | ||
return this.authReq('v2/auth/r/wallets') | ||
return wait() | ||
.then(() => this.authReq('v2/auth/r/wallets')) | ||
.then(res => { | ||
return res.map(p => ({ | ||
currency: p[1], | ||
wallet: p[2] | ||
})) | ||
return res.map(p => ({ | ||
currency: p[1], | ||
wallet: p[2] | ||
})) | ||
}); | ||
} | ||
getBook(currency) { | ||
return this.authReq('v2/auth/r/movements/'+currency+'/hist') | ||
.then(res => { | ||
return res.map(c => ({ | ||
currency: c[1], | ||
value: c[12], | ||
completed: c[9] === 'COMPLETED', | ||
issued: ''+(c[5] / 1000) | ||
})); | ||
}) | ||
getBook() { | ||
let finalResult = []; | ||
let finalResultConverted = []; | ||
return this.getWallet().then(tab => Promise.all(tab.map(r => { | ||
return wait() | ||
.then(() => { | ||
return this.authReq('v2/auth/r/movements/'+r.currency+'/hist') | ||
}) | ||
.then(res => { | ||
return res.map(c => { | ||
return ({ | ||
currency: c[1], | ||
value: c[12], | ||
completed: c[9] === 'COMPLETED', | ||
timestamp: ''+(c[5] / 1000) | ||
}); | ||
}); | ||
}) | ||
.then((res) => { | ||
return Promise.all(res.map(r => { | ||
const oldR = Object.assign({}, r); | ||
return convertToUSDIfPossible(r).then(newR => { | ||
oldR.nativeCurrency = newR.currency; | ||
oldR.nativeValue = newR.value; | ||
finalResultConverted.push(oldR); | ||
}) | ||
})) | ||
}) | ||
}))) | ||
.then(() => { | ||
return finalResultConverted; | ||
}); | ||
} | ||
authReq(path, body = {}) { | ||
authReq(path, body = {}, retry = 0) { | ||
if(this.apiKey) { | ||
const apiPath = path; | ||
const nonce = Date.now().toString() | ||
const nonce = ''+((new Date()).getTime() * 1000 + 2000) | ||
let signature = `/api/${apiPath}${nonce}${JSON.stringify(body)}` | ||
@@ -103,4 +131,18 @@ | ||
}) | ||
.then(res => { | ||
if(res[0] === 'error') { | ||
console.error('Error while retrieving BitFinex result. retrying...') | ||
if(retry < 3) | ||
return wait().then(() => this.authReq(path, body, retry + 1)) | ||
else { | ||
console.error('Error while retrieving BitFinex result. To many retry. Abort.') | ||
return []; | ||
} | ||
} | ||
else { | ||
return res; | ||
} | ||
}) | ||
} | ||
} | ||
} |
const fetch = require('isomorphic-fetch'); | ||
const crypto = require('crypto'); | ||
const VERSION_DATE = '2017-06-17'; | ||
let lastNonce = 0; | ||
module.exports = class { | ||
constructor(apiKey) { | ||
this.apiKey = apiKey; | ||
} | ||
getPairs() { | ||
@@ -64,2 +70,80 @@ return Promise.resolve([ | ||
} | ||
getWallet() { | ||
return this.authReq('GET', 'accounts') | ||
.then(result => { | ||
result = result.map(r => { | ||
r.wallet = r.balance.amount; | ||
delete r.balance; | ||
delete r.resource_path; | ||
delete r.resource; | ||
delete r.primary; | ||
delete r.native_balance; | ||
delete r.created_at; | ||
return r; | ||
}); | ||
return result; | ||
}) | ||
} | ||
getBook() { | ||
return this.getWallet().then(tab => Promise.all(tab.map(r => { | ||
return this.authReq('GET', `accounts/${r.id}/transactions`); | ||
}))).then(result => { | ||
const finalResult = []; | ||
result.map(account => { | ||
account.map(r => { | ||
finalResult.push({ | ||
value: r.amount.amount, | ||
nativeValue: r.native_amount.amount, | ||
completed: r.completed, | ||
currency: r.amount.currency, | ||
nativeCurrency: r.native_amount.currency, | ||
timestamp: r.updated_at | ||
}); | ||
}) | ||
}); | ||
return finalResult; | ||
}); | ||
} | ||
authReq(method, uri, body) { | ||
if(this.apiKey) { | ||
let host = "https://api.coinbase.com/v2/"; | ||
let nonce = Math.floor(Date.now() / 1000);; | ||
let opts = { | ||
method: method, | ||
uri: host + uri, | ||
headers: { | ||
"CB-ACCESS-TIMESTAMP": nonce, | ||
'CB-VERSION': '2016-02-18' | ||
} | ||
}; | ||
opts["headers"]["CB-ACCESS-KEY"] = this.apiKey.key; | ||
let bodyStr = body ? JSON.stringify(body) : ''; | ||
if (body) { | ||
opts.headers["Content-Type"] = "application/json"; | ||
opts["body"] = bodyStr; | ||
} | ||
let hmac = crypto.createHmac("sha256", this.apiKey.secret); | ||
let signature = nonce + opts.method + '/v2/' + uri + bodyStr; | ||
opts["headers"]["CB-ACCESS-SIGN"] = hmac.update(signature).digest("hex"); | ||
return fetch(opts.uri, opts) | ||
.then(res => { | ||
return res.json(); | ||
}).then( (r) => { | ||
return r.data; | ||
}) | ||
} | ||
} | ||
} |
@@ -19,2 +19,18 @@ const fetch = require('isomorphic-fetch'); | ||
} | ||
tickAtTimestamp(pair, timestamp) { | ||
const pairArr = pair.split('_'); | ||
const asset1 = pairArr[1] === 'USD' ? 'USDT' : pairArr[1]; | ||
const asset2 = pairArr[0] === 'USD' ? 'USDT' : pairArr[0]; | ||
const formattedPair = asset1 + '_' + asset2; | ||
return fetch('https://poloniex.com/public?command=returnChartData¤cyPair='+formattedPair+'&start='+timestamp+'&end='+(timestamp+14401)+'&period=14400') | ||
.then((res) => { | ||
return res.json(); | ||
}) | ||
.then((res) => { | ||
return res; | ||
}) | ||
.catch(err => console.error('poloniex error:', err)); | ||
} | ||
@@ -21,0 +37,0 @@ tick(pair) { |
{ | ||
"name": "crypto-exchange-manager", | ||
"version": "4.0.4", | ||
"version": "5.0.0", | ||
"description": "Wrapper for cryptocurrencies exchanges apis.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -79,2 +79,3 @@ [![CircleCI](https://circleci.com/gh/azukaar/crypto-exchange-manager.svg?style=svg)](https://circleci.com/gh/azukaar/crypto-exchange-manager) | ||
- bifinex | ||
- coinbase | ||
``` | ||
@@ -95,2 +96,3 @@ | ||
- bifinex | ||
- coinbase | ||
``` | ||
@@ -118,3 +120,3 @@ | ||
**get wallet** | ||
**get book** | ||
@@ -124,2 +126,3 @@ ``` | ||
- bifinex | ||
- coinbase | ||
``` | ||
@@ -134,3 +137,3 @@ | ||
bifinex.getBook('ETH').then(result => { | ||
bifinex.getBook().then(result => { | ||
console.log(result); | ||
@@ -140,8 +143,27 @@ }) | ||
Will return an array of movement in / out of the account of the user for a specific currency. | ||
Will return an array of movement in / out of the account of the user for a specific currency. Native value and currency are values in USD (calculated with historical values). | ||
``` | ||
[ | ||
{currency: 'ETH', value: '1.8', completed: true, issued: '154787488'} | ||
{ | ||
currency: 'ETH', | ||
value: '1.8', | ||
completed: true, | ||
timestamp: '154787488', | ||
nativeCurrency: 'USD', | ||
nativeValue: 500, | ||
} | ||
] | ||
``` | ||
**Utils** | ||
Convert values of currency (live or historical). | ||
```js | ||
convertToUSDIfPossible({ | ||
value : 1, | ||
currency : 'ETH', | ||
timestamp : '1519293784' (optionnal) | ||
}) | ||
``` |
const cryptoManager = require('../index.js'); | ||
const apiKeys = { | ||
'bitfinex' : { key : '123', secret : '456'} | ||
'exchange' : { key : 'key', secret : 'secret'} | ||
}; | ||
test('Should return array of all available exchanges', () => { | ||
@@ -13,3 +15,3 @@ expect(cryptoManager).toHaveProperty('binance'); | ||
const exchange = new cryptoManager[Exchange](apiKeys[Exchange]); | ||
if(exchange.getWallet) { | ||
if(apiKeys[Exchange] && apiKeys[Exchange].key && exchange.getWallet) { | ||
return exchange.getWallet().then(result => { | ||
@@ -25,4 +27,4 @@ expect(Array.isArray(result)).toBe(true); | ||
const exchange = new cryptoManager[Exchange](apiKeys[Exchange]); | ||
if(exchange.getBook) { | ||
return exchange.getBook('ETH').then(result => { | ||
if(apiKeys[Exchange] && apiKeys[Exchange].key && exchange.getBook) { | ||
return exchange.getBook().then(result => { | ||
expect(Array.isArray(result)).toBe(true); | ||
@@ -32,2 +34,2 @@ }); | ||
}); | ||
} | ||
} |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
30684
22
888
163
28