-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathindex.js
200 lines (169 loc) · 4.98 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
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
const axios = require("axios");
const BigNumber = require("bignumber.js");
const chunk = require("lodash.chunk");
const SLPSDK = require("slp-sdk/lib/SLP").default;
const SLP = new SLPSDK();
async function getBalances(bitdbApiKey, tokenId) {
try {
const utxos = await getSlpUtxos(bitdbApiKey, tokenId);
if (utxos.length === 0) return [];
const txidsToValidate = [...new Set(utxos.map(utxo => utxo.txid))];
const validTxidArrays = await Promise.all(
chunk(txidsToValidate, 20).map(txids => {
return axios({
method: "GET",
url: `https://tokengraph.network/verify/${txids.join(",")}`,
json: true
}).then(res => res.data.response.filter(i => !i.errors).map(i => i.tx));
})
);
const validTxids = [].concat(...validTxidArrays);
const validUtxos = utxos.filter(utxo => validTxids.includes(utxo.txid));
const balances = validUtxos
.reduce((bals, utxo) => {
const existingBal = bals.find(bal => bal.address === utxo.address);
if (existingBal) {
existingBal.amount = existingBal.amount.plus(utxo.amount);
} else {
bals.push({
cashAddress: SLP.Address.toCashAddress(utxo.address),
slpAddress: SLP.Conversion.toSLPAddress(utxo.address),
legacyAddress: SLP.Address.toLegacyAddress(utxo.address),
balance: utxo.amount
});
}
return bals;
}, [])
.map(bal => {
bal.balance = bal.balance.toString();
return bal;
});
return balances;
} catch (err) {
console.error("slp-balances", err);
return [];
}
}
async function getSlpUtxos(bitdbApiKey, tokenId) {
try {
var query = {
v: 3,
q: {
find: {
"out.h1": "534c5000",
$or: [
{
$and: [
{
$or: [{ "out.s3": "MINT" }, { "out.s3": "SEND" }],
"out.h4": tokenId
}
]
},
{
"tx.h": tokenId
}
]
},
limit: 1000
}
};
var s = JSON.stringify(query);
var b64 = Buffer.from(s).toString("base64");
var url = "https://bitdb.network/q/" + b64;
var header = {
headers: { key: bitdbApiKey }
};
const tokenTxRes = await axios.get(url, header);
const tokenTxs = tokenTxRes.data.c;
if (tokenTxRes.data.u && tokenTxRes.data.u.length) {
tokenTxs.concat(tokenTxRes.u);
}
const outputs = parseSlpOutputs(tokenId, tokenTxs);
const unspentOutputs = outputs.filter(output => {
return !tokenTxs.some(tokenTx => {
return tokenTx.in.some(input => {
return input.e.h === output.txid && input.e.i === output.vout;
});
});
});
return unspentOutputs;
} catch (err) {
console.error("slp-balances", err);
return [];
}
}
function parseSlpOutputs(tokenId, txs) {
const genesisTx = txs.filter(tx => tx.tx.h === tokenId)[0];
const decimals = parseGenesisDecimals(genesisTx);
const genesisOutput = parseGenesisOutput(genesisTx, decimals);
const mintTxs = txs.filter(
tx => tx.tx.h !== tokenId && tx.out[0].s3 === "MINT"
);
const mintOutputs = mintTxs.map(tx => parseMintOutput(tx, decimals));
const sendTxs = txs.filter(
tx => tx.tx.h !== tokenId && tx.out[0].s3 === "SEND"
);
const sendOutputArrays = sendTxs.map(tx => parseSendOutputs(tx, decimals));
const sendOutputs = [].concat(...sendOutputArrays);
const outputs = [genesisOutput].concat(mintOutputs, sendOutputs);
return outputs;
}
function parseGenesisDecimals(tx) {
const opReturn = tx.out[0];
const decimals = parseInt(opReturn.h8, 16) || 0;
return decimals;
}
function parseAmount(amountHex, decimals) {
let amount = new BigNumber(amountHex, 16);
amount = decimals ? amount.div(10 ** decimals) : amount;
return amount;
}
function parseGenesisOutput(tx, decimals) {
const opReturn = tx.out[0];
const amount = opReturn.h10;
const address = tx.out[1].e.a;
const output = {
txid: tx.tx.h,
vout: 1,
amount: parseAmount(amount, decimals),
address: address
};
return output;
}
function parseMintOutput(tx, decimals) {
const opReturn = tx.out[0];
const amount = opReturn.h6;
const address = tx.out[1].e.a;
const output = {
txid: tx.tx.h,
vout: 1,
amount: parseAmount(amount, decimals),
address: address
};
return output;
}
function parseSendOutputs(tx, decimals) {
const outputs = [];
try {
const opReturn = tx.out[0];
for (var i = 5; i < 20; i++) {
const amount = opReturn[`h${i}`];
if (typeof amount === "undefined" || amount === null) break;
const address = tx.out[i - 4].e.a;
const output = {
txid: tx.tx.h,
vout: i - 4,
amount: parseAmount(amount, decimals),
address: address
};
outputs.push(output);
}
} catch (err) {
console.error("slp-balances", err);
}
return outputs;
}
module.exports = {
getBalances
};