-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.js
118 lines (95 loc) · 3.35 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const rp = require('request-promise')
const crypto = require('crypto')
const nonce = require('nonce')()
class Cryptopia {
constructor (key, secret) {
this.key = key
this.secret = secret
this.API_URL = 'https://www.cryptopia.co.nz/api/'
}
_public (endpoint, parameters) {
let httpParam = '/'
for (let key in parameters) {
if (parameters[key]) {
httpParam += parameters[key] + '/'
}
}
const options = {
method: 'GET',
uri: this.API_URL + endpoint + httpParam,
json: true
}
return rp(options)
}
async GetCurrencies () {
return this._public('GetCurrencies')
}
async GetTradePairs () {
return this._public('GetTradePairs')
}
async GetMarkets (baseMarket, hours) {
return this._public('GetMarkets', {baseMarket, hours})
}
async GetMarket (market, hours) {
return this._public('GetMarket', {market, hours})
}
async GetMarketHistory (market, hours) {
return this._public('GetMarketHistory', {market, hours})
}
async GetMarketOrders (market, orderCount) {
return this._public('GetMarketOrders', {market, orderCount})
}
async GetMarketOrderGroups (markets, orderCount) {
return this._public('GetMarketOrderGroups', {markets, orderCount})
}
_private (endpoint, parameters) {
const _nonce = nonce()
const HASHED_POST_PARAMS = crypto.createHash('md5').update(JSON.stringify(parameters)).digest('base64')
const requestSignature = this.key + 'POST' + encodeURIComponent(this.API_URL + endpoint).toLowerCase() + _nonce + HASHED_POST_PARAMS
const hmacSignature = crypto.createHmac('sha256', Buffer.from(this.secret, 'base64')).update(requestSignature).digest('base64')
const authorization = 'amx ' + this.key + ':' + hmacSignature + ':' + _nonce
const options = {
method: 'POST',
uri: this.API_URL + endpoint,
body: parameters,
headers: {
'Authorization': authorization,
'Content-Type': 'application/json; charset=utf-8',
'Content-Length': Buffer.byteLength(JSON.stringify(parameters))
},
json: true
}
return rp(options)
}
async GetBalance (Currency) {
return this._private('GetBalance', {Currency})
}
async GetDepositAddress (Currency) {
return this._private('GetDepositAddress', {Currency})
}
async GetOpenOrders (Market, TradePairId, Count) {
return this._private('GetOpenOrders', {Market, TradePairId, Count})
}
async GetTradeHistory (Market, TradePairId, Count) {
return this._private('GetTradeHistory', {Market, TradePairId, Count})
}
async GetTransactions (Type, Count) {
return this._private('GetTransactions', {Type, Count})
}
async SubmitTrade (Market, TradePairId, Type, Rate, Amount) {
return this._private('SubmitTrade', {Market, TradePairId, Type, Rate, Amount})
}
async CancelTrade (Type, OrderId, TradePairId) {
return this._private('CancelTrade', {Type, OrderId, TradePairId})
}
async SubmitTip (Currency, ActiveUsers, Amount) {
return this._private('SubmitTip', {Currency, ActiveUsers, Amount})
}
async SubmitWithdraw (Currency, Address, PaymentId, Amount) {
return this._private('SubmitWithdraw', {Currency, Address, PaymentId, Amount})
}
async SubmitTransfer (Currency, Username, Amount) {
return this._private('SubmitTransfer', {Currency, Username, Amount})
}
}
module.exports = Cryptopia