-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathxcp.js
149 lines (133 loc) · 3.67 KB
/
xcp.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
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
/**
* Copyright (c) 2017 Chiguireitor
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*/
'use strict'
const getJSON = require('get-json')
const xcpAPI = 'https://counterpartychain.io/api/'
var currentBlock = 0
function getAsset(asset) {
asset = asset.trim()
return new Promise((resolve,reject) => {
if (asset.length <= 12) {
getJSON(xcpAPI + 'asset/' + asset, function(error, response){
if (error) {
reject(error)
} else {
resolve(response)
}
})
} else {
reject(new Error('Assets must be 12 characters or less'))
}
})
}
function getAssetHolders(asset) {
asset = asset.trim()
return new Promise((resolve,reject) => {
if (asset.length <= 12) {
let page = 1
let processed = 0
let total = 1
let holders = {}
function getPage() {
getJSON(xcpAPI + 'holders/' + asset + '/' + page, function(error, response){
console.log('Getting page ' + page + ' for asset ' + asset)
if (error) {
reject(error)
} else {
total = parseInt(response.total)
response.data.forEach((holder) => {
let tb = {}
tb[asset] = parseFloat(holder.amount)
holders[holder.address] = tb
})
processed += response.data.length
page++
if (processed < total) {
getPage()
} else {
resolve(holders)
}
}
})
}
getPage()
} else {
reject(new Error('Assets must be 12 characters or less'))
}
})
}
function getUserBalance(addr, asset) {
asset = asset.trim()
addr = addr.trim()
return new Promise((resolve,reject) => {
if (asset.length <= 12) {
getJSON(xcpAPI + 'balances/' + addr, function(error, response){
if (error) {
reject(error)
} else {
let result = response.data.find(x => x.asset == asset)
resolve(result)
}
})
} else {
reject(new Error('Assets must be 12 characters or less'))
}
})
}
function getUser(addr) {
return new Promise((resolve,reject) => {
if (addr) {
addr = addr.trim()
getJSON(xcpAPI + 'balances/' + addr, function(error, response){
if (error) {
reject(error)
} else {
let result = {}
response.data.forEach((x) => {
result[x.asset] = parseFloat(x.amount)
})
let ob = {}
ob[addr] = result
resolve(ob)
}
})
} else {
resolve({})
}
})
}
function hasNewBlock() {
return new Promise((resolve, reject) => {
getJSON('http://public.coindaddy.io:4100/', function(error, response){
if (!error) {
let lastbi = response.counterblock_last_processed_block.block_index
if (currentBlock < lastbi) {
currentBlock = lastbi
resolve({isNew: true, height: lastbi})
} else {
resolve({isNew: false, height: lastbi})
}
} else {
reject(error)
}
})
})
}
module.exports = {
getAsset,
getAssetHolders,
getUserBalance,
getUser,
hasNewBlock
}