-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathindex.js
More file actions
422 lines (358 loc) · 16.9 KB
/
index.js
File metadata and controls
422 lines (358 loc) · 16.9 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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
const axios = require("axios")
const fs = require("fs").promises
const mime = require("mime")
const chainURLs = {
arbitrum: "https://arbitrum.idexo.io",
arbitrumnova: "https://arbitrumnova.idexo.io",
arweave: "https://ziparweave.idexo.io",
base: "https://base.idexo.io",
ethereum: "https://mainneteth.idexo.io",
filecoin: "https://filecoin.idexo.io",
optimism: "https://optimism.idexo.io",
utilsUrl: "https://transactions.idexo.io",
zkcandy: "https://zkcandy.api.idexo.com"
}
function headers(apiKey) {
return {
"Content-Type": "application/json",
"x-api-key": apiKey
}
}
async function sendRequest(apiKey, network, data, method = "post", params = null) {
let config = {
method: method,
url: chainURLs[network],
headers: headers(apiKey)
}
if (data) config.data = JSON.stringify(data)
if (params) config.params = params
return await axios.request(config).catch(function (error) {
if (error.response) {
return error.response
} else {
return error
}
})
}
function isBase64String(str) {
return str.length % 4 == 0 && /^[A-Za-z0-9+/]+[=]{0,2}$/.test(str)
}
function isStringURL(str) {
var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(str)
}
function detectMimeType(b64) {
for (var s in base64Signatures) {
if (b64.indexOf(s) === 0) {
return base64Signatures[s]
}
}
}
const base64Signatures = {
JVBERi0: "application/pdf",
R0lGODdh: "image/gif",
R0lGODlh: "image/gif",
iVBORw0KGgo: "image/png",
"/9j/": "image/jpg",
U: "image/webp"
}
const IdexoSDK = {
Bridges: {
async createRelayer(apiKey, network, type, tokenAddress, adminFee, bridgeWallet, threshold, signers, options) {
if(type === "origin") {
const transactionType = "createOriginRelayer"
return await sendRequest(apiKey, network, { tokenAddress, adminFee, bridgeWallet, threshold, signers, options, transactionType })
}
else if(type === "destination") {
const transactionType = "createDestinationRelayer"
return await sendRequest(apiKey, network, { tokenAddress, adminFee, bridgeWallet, threshold, signers, options, transactionType })
}
}
},
Common: {
async transferOwnership(apiKey, network, contractAddress, newOwnerAddress) {
const transactionType = "transferOwnership"
return await sendRequest(apiKey, network, { contractAddress, newOwnerAddress, transactionType })
}
},
Marketplace: {
async createSimpleMarketplace(apiKey, network, purchaseToken, saleStartTime, options) {
const transactionType = "createSimpleMarketplace"
return await sendRequest(apiKey, network, { purchaseToken, saleStartTime, options, transactionType })
},
async createAuctionMarketplace(apiKey, network, purchaseToken, maxDuration, options) {
const transactionType = "createAuctionMarketplace"
return await sendRequest(apiKey, network, { purchaseToken, maxDuration, options, transactionType })
},
async listNFTMeta(apiKey, network, contractAddress, sender, nftContractAddress, tokenId, price, nonce, request, signature, options) {
const transactionType = "listNFTMeta"
return await sendRequest(apiKey, network, { contractAddress, sender, nftContractAddress, tokenId, price, nonce, request, signature, options, transactionType })
}
},
Multi: {
// image (string) can be 1) path to file, 2) url, or 3) base64 string
async mintNFTWithImage(apiKey, network, contractAddress, mintToAddress, image, nftName, nftDescription, attributes, options) {
let contentType
// if image is string path to file
if (!isStringURL(image) && !isBase64String(image)) {
contentType = mime.getType(image)
image = await fs.readFile(image, { encoding: "base64" })
}
if (isBase64String(image)) contentType = detectMimeType(image)
if (contentType === undefined && !isStringURL(image)) throw "unsupported file type! image must be jpg, png, gif, webp, or pdf"
const transactionType = "mintNFTWithImage"
return await sendRequest(apiKey, network, {
contractAddress,
mintToAddress,
image,
contentType,
nftName,
nftDescription,
attributes,
transactionType,
options
})
},
async mintRoyaltyNFTWithImage(apiKey, network, contractAddress, mintToAddress, image, nftName, nftDescription, attributes, options) {
let contentType
// if image is string path to file
if (!isStringURL(image) && !isBase64String(image)) {
contentType = mime.getType(image)
image = await fs.readFile(image, { encoding: "base64" })
}
if (isBase64String(image)) contentType = detectMimeType(image)
if (contentType === undefined && !isStringURL(image)) throw "unsupported file type! image must be jpg, png, gif, webp, or pdf"
const transactionType = "mintRoyaltyNFTWithImage"
return await sendRequest(apiKey, network, {
contractAddress,
mintToAddress,
image,
contentType,
nftName,
nftDescription,
attributes,
transactionType,
options
})
}
},
NFTs: {
async createCollectionCapped(apiKey, network, name, symbol, cap, options) {
const transactionType = "createCollectionCapped"
return await sendRequest(apiKey, network, { transactionType, name, symbol, cap, options })
},
async createCollectionUncapped(apiKey, network, name, symbol, options) {
const transactionType = "createCollectionUncapped"
return await sendRequest(apiKey, network, { transactionType, name, symbol, options })
},
async createCappedRoyalty(apiKey, network, name, symbol, royaltyCollector, royaltyBP, cap, options) {
const transactionType = "createCappedRoyalty"
return await sendRequest(apiKey, network, { transactionType, name, symbol, royaltyCollector, royaltyBP, cap, options })
},
async createUncappedRoyalty(apiKey, network, name, symbol, royaltyCollector, royaltyBP, options) {
const transactionType = "createUncappedRoyalty"
return await sendRequest(apiKey, network, { transactionType, name, symbol, royaltyCollector, royaltyBP, options })
},
async mintNFT(apiKey, network, contractAddress, mintToAddress, tokenUri) {
const transactionType = "mintNFT"
return await sendRequest(apiKey, network, { transactionType, contractAddress, mintToAddress, tokenUri })
},
async mintRoyaltyNFT(apiKey, network, contractAddress, mintToAddress, tokenUri) {
const transactionType = "mintRoyaltyNFT"
return await sendRequest(apiKey, network, { transactionType, contractAddress, mintToAddress, tokenUri })
},
async mintNFTBatch(apiKey, network, contractAddress, recipients, tokenUris) {
const transactionType = "mintNFTBatch"
return await sendRequest(apiKey, network, { transactionType, contractAddress, recipients, tokenUris })
},
async setTokenURI(apiKey, network, contractAddress, tokenId, tokenUri) {
const transactionType = "setTokenURI"
return await sendRequest(apiKey, network, { contractAddress, tokenId, tokenUri, transactionType })
},
async getTokenURI(apiKey, network, contractAddress, tokenId) {
const transactionType = "getTokenURI"
return await sendRequest(apiKey, network, { contractAddress, tokenId, transactionType })
},
async getCollectionIds(apiKey, network, contractAddress, walletAddress, withURI = false) {
const transactionType = "getCollectionIds"
return await sendRequest(apiKey, network, { contractAddress, walletAddress, withURI, transactionType })
},
async getBalanceOf(apiKey, network, contractAddress, walletAddress) {
const transactionType = "getBalanceOf"
return await sendRequest(apiKey, network, { contractAddress, walletAddress, transactionType })
}
},
SBTs: {
async createSBTCapped(apiKey, network, name, symbol, baseUri, cap, options) {
const transactionType = "createSBTCapped"
return await sendRequest(apiKey, network, { transactionType, name, symbol, baseUri, cap, options })
},
async createSBTUncapped(apiKey, network, name, symbol, baseUri, options) {
const transactionType = "createSBTUncapped"
return await sendRequest(apiKey, network, { transactionType, name, symbol, baseUri, options })
},
async createLinkedSBTUncapped(apiKey, network, name, symbol, baseUri, options) {
const transactionType = "createUncappedLinkedSBT"
return await sendRequest(apiKey, network, { transactionType, name, symbol, baseUri, options })
},
// TODO: add abi and methods
// async createSBTCommunityUncapped(apiKey, network, name, symbol, options) {
// const transactionType = "createSBTCommunityUncapped"
// return await axios.post(chainURLs[network], JSON.stringify({ transactionType, name, symbol, options }), headers(apiKey))
// },
async mintSBT(apiKey, network, contractAddress, mintToAddress, tokenUri) {
const transactionType = "mintSBT"
return await sendRequest(apiKey, network, {
transactionType,
contractAddress,
mintToAddress,
tokenUri
})
},
async mintSBTBatch(apiKey, network, contractAddress, recipients, tokenUris) {
const transactionType = "mintSBTBatch"
return await sendRequest(apiKey, network, { transactionType, contractAddress, recipients, tokenUris })
},
// image (string) can be 1) path to file, 2) url, or 3) base64 string
async mintSBTWithImage(apiKey, network, contractAddress, mintToAddress, image, nftName, nftDescription, attributes, options) {
let contentType
// if image is string path to file
if (!isStringURL(image) && !isBase64String(image)) {
contentType = mime.getType(image)
image = await fs.readFile(image, { encoding: "base64" })
}
if (isBase64String(image)) contentType = detectMimeType(image)
if (contentType === undefined && !isStringURL(image)) throw "unsupported file type! image must be jpg, png, gif, webp, or pdf"
const transactionType = "mintSBTWithImage"
return await sendRequest(apiKey, network, {
contractAddress,
mintToAddress,
image,
contentType,
nftName,
nftDescription,
attributes,
transactionType,
options
})
}
},
Staking: {
async createStakePoolMultiple(apiKey, network, poolName, symbol, baseUri, depositToken, rewardToken, options) {
const transactionType = "createStakePoolMultipleRewards"
return await sendRequest(apiKey, network, {
transactionType,
poolName,
symbol,
baseUri,
depositToken,
rewardToken,
options
})
}
},
Storage: {
async uploadPlain(apiKey, network, data) {
const uploadType = "plainText"
return await sendRequest(apiKey, network, { uploadType, data, encoding: "null" })
},
async uploadHTML(apiKey, network, data) {
const uploadType = "HTML"
return await sendRequest(apiKey, network, { uploadType, data, encoding: "null" })
},
async uploadJSON(apiKey, network, data) {
const uploadType = "JSON"
return await sendRequest(apiKey, network, { uploadType, data, encoding: "null" })
},
async uploadBuffer(apiKey, network, data, encoding) {
// data must be string (should enforce that with type)
const uploadType = "buffer"
return await sendRequest(apiKey, network, { uploadType, data, encoding })
},
async uploadImage(apiKey, network, imagePath) {
// data must be string (should enforce that with type)
const uploadType = "image"
const contentType = mime.getType(imagePath)
const image = await fs.readFile(imagePath, { encoding: "base64" })
return await sendRequest(apiKey, network, { uploadType, image, contentType })
},
// image (string) can be 1) path to file, 2) url, or 3) base64 string
async uploadNFTMetadata(apiKey, network, image, nftName, nftDescription, attributes, options) {
let contentType
// if image is string path to file
if (!isStringURL(image) && !isBase64String(image)) {
contentType = mime.getType(image)
image = await fs.readFile(image, { encoding: "base64" })
}
if (isBase64String(image)) contentType = detectMimeType(image)
if (contentType === undefined && !isStringURL(image)) throw "unsupported file type! image must be jpg, png, gif, webp, or pdf"
const uploadType = "NFTMetadata"
return await sendRequest(apiKey, network, {
image,
contentType,
nftName,
nftDescription,
attributes,
uploadType,
options
})
}
},
Tokens: {
async createTokenCapped(apiKey, network, name, symbol, cap, options) {
const transactionType = "createTokenCapped"
return await sendRequest(apiKey, network, { transactionType, cap, name, symbol, options })
},
// TODO: add abi
// async createTokenUncapped(apiKey, network, name, symbol, options) {
// const transactionType = "createTokenUncapped"
// return await axios.post(chainURLs[network], JSON.stringify({ transactionType, name, symbol, options }), headers(apiKey))
// },
async mintToken(apiKey, network, contractAddress, mintToAddress, amount) {
const transactionType = "mintToken"
return await sendRequest(apiKey, network, { transactionType, contractAddress, mintToAddress, amount })
}
},
Vesting: {
async createVesting(apiKey, network, depositToken, beneficiary, startTime, cliffDays, durationDays, claimsPeriod, options) {
const transactionType = "createVesting"
return await sendRequest(apiKey, network, {
depositToken,
beneficiary,
startTime,
cliffDays,
durationDays,
claimsPeriod,
options,
transactionType
})
},
async getVestedAmount(apiKey, network, contractAddress) {
const transactionType = "getVestedAmount"
return await sendRequest(apiKey, network, { contractAddress, transactionType })
},
async getAvailableClaimAmount(apiKey, network, contractAddress) {
const transactionType = "getAvailableClaimAmount"
return await sendRequest(apiKey, network, { contractAddress, transactionType })
}
},
Utils: {
async getContractAddress(apiKey, network, transactionHash) {
let params = { path: "contract", network, hash: transactionHash }
return await sendRequest(apiKey, "utilsUrl", null, "get", params)
},
async getTransactions(apiKey, network, timestampFrom, timestampTo) {
let params = { path: "transactions", network, from: timestampFrom, to: timestampTo }
return await sendRequest(apiKey, "utilsUrl", null, "get", params)
},
async getTransactionsByGroup(apiKey, network, group, timestampFrom, timestampTo) {
let params = { path: "transactions", network, group, from: timestampFrom, to: timestampTo }
return await sendRequest(apiKey, "utilsUrl", null, "get", params)
},
async getTransactionsByFunction(apiKey, function_name) {
let params = { path: "functions", function_name }
return await sendRequest(apiKey, "utilsUrl", null, "get", params)
}
}
}
module.exports = IdexoSDK