-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathindex.js
More file actions
215 lines (174 loc) · 5.68 KB
/
index.js
File metadata and controls
215 lines (174 loc) · 5.68 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/**
* CryptAPI's NodeJS Library
* @author CryptAPI <info@cryptapi.io>
*/
class CryptAPI {
static #baseURL = 'https://api.cryptapi.io'
constructor(coin, ownAddress, callbackUrl, parameters = {}, caParams = {}) {
if (!coin || !ownAddress || !callbackUrl) {
throw new Error('Missing required parameters')
}
coin = coin.replace('/', '_')
CryptAPI.getSupportedCoins().then(validCoins => {
if (!validCoins.hasOwnProperty(coin)) {
throw new Error('The cryptocurrency/token requested is not supported.')
}
})
this.coin = coin
this.ownAddress = ownAddress
this.callbackUrl = callbackUrl
this.parameters = parameters
this.caParams = caParams
this.paymentAddress = ''
}
/**
* Gets all the supported cryptocurrencies and tokens from the API
* @returns {Promise<{}|null>}
*/
static async getSupportedCoins() {
const info = await this.getInfo(null, true)
if (!info) {
return null
}
delete info['fee_tiers']
const coins = {}
for (const chain of Object.keys(info)) {
const data = info[chain]
const isBaseCoin = data.hasOwnProperty('ticker')
if (isBaseCoin) {
coins[chain] = data
} else {
const baseTicker = `${chain}_`
Object.entries(data).forEach(([token, subData]) => {
coins[baseTicker + token] = subData
})
}
}
return coins
}
/**
* Actually makes the request to the API returning the address.
* It's necessary to run this before running the other non-static functions
* @returns {Promise<*|null>}
*/
async getAddress() {
if (!this.coin || !this.callbackUrl || !this.ownAddress) {
return null
}
let callbackUrl = new URL(this.callbackUrl)
const parameters = this.parameters
if (Object.entries(parameters).length > 0) {
Object.entries(parameters).forEach(([k, v]) => callbackUrl.searchParams.append(k, v))
}
let params = {...this.caParams, ...{
callback: encodeURI(callbackUrl.toString()),
address: this.ownAddress,
}}
const response = await CryptAPI.#_request(this.coin, 'create', params)
const addressIn = response.address_in
this.paymentAddress = addressIn
return addressIn
}
/**
* Checks the logs related to a request.
* (Can be used to check for callbacks)
* @returns {Promise<any|null>}
*/
async checkLogs() {
if (!this.coin || !this.callbackUrl) {
return null
}
let callbackUrl = new URL(this.callbackUrl)
const parameters = this.parameters
if (Object.entries(parameters).length > 0) {
Object.entries(parameters).forEach(([k, v]) => callbackUrl.searchParams.append(k, v))
}
callbackUrl = encodeURI(callbackUrl.toString())
return await CryptAPI.#_request(this.coin, 'logs', {
callback: callbackUrl
})
}
/**
* Gets the QRCode for a payment.
* @param value
* @param size
* @returns {Promise<any|null>}
*/
async getQrcode(value = null, size = 512) {
const params = {
address: this.paymentAddress,
}
if (value) {
params['value'] = value
}
params['size'] = size
return await CryptAPI.#_request(this.coin, 'qrcode', params)
}
/**
* Get information related to a cryptocurrency/token.
* If coin=null it calls the /info/ endpoint returning general information
* @param coin
* @returns {Promise<any|null>}
*/
static async getInfo(coin = null) {
const params = {}
if (!coin) {
params['prices'] = 0
}
return await this.#_request(coin, 'info', params)
}
/**
* Gets an estimate of the blockchain fees for the coin provided.
* @param coin
* @param addresses
* @param priority
* @returns {Promise<any|null>}
*/
static async getEstimate(coin, addresses = 1, priority = 'default') {
return await CryptAPI.#_request(coin, 'estimate', {
addresses,
priority,
})
}
/**
* This method allows you to easily convert prices from FIAT to Crypto or even between cryptocurrencies
* @param coin
* @param value
* @param from
* @returns {Promise<any|null>}
*/
static async getConvert(coin, value, from) {
let params = {
value,
from,
}
return await CryptAPI.#_request(coin, 'convert', params)
}
/**
* Helper function to make a request to API
* @param coin
* @param endpoint
* @param params
* @returns {Promise<any>}
*/
static async #_request(coin, endpoint, params = {}) {
const url = coin ? new URL(`${this.#baseURL}/${coin.replace('_', '/')}/${endpoint}/`) : new URL(`${this.#baseURL}/${endpoint}/`)
if (params) {
Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))
}
const fetchParams = {
method: 'GET',
headers: {
referer: this.#baseURL
},
credentials: 'include'
}
const response = await fetch(url, fetchParams)
const response_obj = await response.json()
if ( response_obj.status === 'error' ) {
throw new Error(response_obj.error)
}
return response_obj
}
}
module.exports = CryptAPI