mainnet-js 1.1.34 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (117) hide show
  1. package/dist/index.html +1 -1
  2. package/dist/{mainnet-1.1.34.js → mainnet-2.0.0.js} +25 -337
  3. package/dist/module/db/SqlProvider.d.ts +2 -2
  4. package/dist/module/db/SqlProvider.d.ts.map +1 -1
  5. package/dist/module/db/SqlProvider.js +6 -24
  6. package/dist/module/db/SqlProvider.js.map +1 -1
  7. package/dist/module/index.d.ts +0 -3
  8. package/dist/module/index.d.ts.map +1 -1
  9. package/dist/module/index.js +3 -6
  10. package/dist/module/index.js.map +1 -1
  11. package/dist/module/transaction/Wif.d.ts +4 -8
  12. package/dist/module/transaction/Wif.d.ts.map +1 -1
  13. package/dist/module/transaction/Wif.js +6 -10
  14. package/dist/module/transaction/Wif.js.map +1 -1
  15. package/dist/module/util/deriveCashaddr.d.ts +1 -0
  16. package/dist/module/util/deriveCashaddr.d.ts.map +1 -1
  17. package/dist/module/util/deriveCashaddr.js +8 -1
  18. package/dist/module/util/deriveCashaddr.js.map +1 -1
  19. package/dist/module/wallet/Base.d.ts +1 -1
  20. package/dist/module/wallet/Base.js +1 -1
  21. package/dist/module/wallet/Util.d.ts +2 -2
  22. package/dist/module/wallet/Util.js +2 -2
  23. package/dist/module/wallet/Wif.d.ts +0 -28
  24. package/dist/module/wallet/Wif.d.ts.map +1 -1
  25. package/dist/module/wallet/Wif.js +2 -109
  26. package/dist/module/wallet/Wif.js.map +1 -1
  27. package/dist/module/wallet/createWallet.d.ts +0 -12
  28. package/dist/module/wallet/createWallet.d.ts.map +1 -1
  29. package/dist/module/wallet/createWallet.js +0 -43
  30. package/dist/module/wallet/createWallet.js.map +1 -1
  31. package/dist/module/wallet/interface.d.ts +0 -2
  32. package/dist/module/wallet/interface.d.ts.map +1 -1
  33. package/dist/module/webhook/Webhook.d.ts +1 -6
  34. package/dist/module/webhook/Webhook.d.ts.map +1 -1
  35. package/dist/module/webhook/Webhook.js +0 -4
  36. package/dist/module/webhook/Webhook.js.map +1 -1
  37. package/dist/module/webhook/index.d.ts +0 -1
  38. package/dist/module/webhook/index.d.ts.map +1 -1
  39. package/dist/module/webhook/index.js +0 -1
  40. package/dist/module/webhook/index.js.map +1 -1
  41. package/dist/module/webhook/interface.d.ts +0 -1
  42. package/dist/module/webhook/interface.d.ts.map +1 -1
  43. package/dist/tsconfig.tsbuildinfo +1 -1
  44. package/package.json +5 -10
  45. package/src/db/SqlProvider.test.ts +2 -18
  46. package/src/db/SqlProvider.ts +5 -26
  47. package/src/index.ts +3 -7
  48. package/src/transaction/Wif.ts +2 -19
  49. package/src/util/deriveCashaddr.test.ts +8 -0
  50. package/src/util/deriveCashaddr.ts +9 -0
  51. package/src/wallet/Base.ts +1 -1
  52. package/src/wallet/Util.ts +2 -2
  53. package/src/wallet/Wif.test.ts +0 -23
  54. package/src/wallet/Wif.ts +2 -148
  55. package/src/wallet/createWallet.ts +0 -53
  56. package/src/wallet/interface.ts +0 -2
  57. package/src/webhook/Webhook.ts +4 -10
  58. package/src/webhook/index.ts +0 -1
  59. package/src/webhook/interface.ts +0 -1
  60. package/webpack.config.cjs +7 -7
  61. package/dist/module/slp/GsppProvider.d.ts +0 -55
  62. package/dist/module/slp/GsppProvider.d.ts.map +0 -1
  63. package/dist/module/slp/GsppProvider.js +0 -204
  64. package/dist/module/slp/GsppProvider.js.map +0 -1
  65. package/dist/module/slp/SlpDbProvider.d.ts +0 -52
  66. package/dist/module/slp/SlpDbProvider.d.ts.map +0 -1
  67. package/dist/module/slp/SlpDbProvider.js +0 -175
  68. package/dist/module/slp/SlpDbProvider.js.map +0 -1
  69. package/dist/module/slp/SlpDbTemplates.d.ts +0 -313
  70. package/dist/module/slp/SlpDbTemplates.d.ts.map +0 -1
  71. package/dist/module/slp/SlpDbTemplates.js +0 -375
  72. package/dist/module/slp/SlpDbTemplates.js.map +0 -1
  73. package/dist/module/slp/SlpLibAuth.d.ts +0 -210
  74. package/dist/module/slp/SlpLibAuth.d.ts.map +0 -1
  75. package/dist/module/slp/SlpLibAuth.js +0 -451
  76. package/dist/module/slp/SlpLibAuth.js.map +0 -1
  77. package/dist/module/slp/SlpProvider.d.ts +0 -24
  78. package/dist/module/slp/SlpProvider.d.ts.map +0 -1
  79. package/dist/module/slp/SlpProvider.js +0 -29
  80. package/dist/module/slp/SlpProvider.js.map +0 -1
  81. package/dist/module/slp/index.d.ts +0 -6
  82. package/dist/module/slp/index.d.ts.map +0 -1
  83. package/dist/module/slp/index.js +0 -5
  84. package/dist/module/slp/index.js.map +0 -1
  85. package/dist/module/slp/interface.d.ts +0 -114
  86. package/dist/module/slp/interface.d.ts.map +0 -1
  87. package/dist/module/slp/interface.js +0 -7
  88. package/dist/module/slp/interface.js.map +0 -1
  89. package/dist/module/util/bchaddr.d.ts +0 -2
  90. package/dist/module/util/bchaddr.d.ts.map +0 -1
  91. package/dist/module/util/bchaddr.js +0 -16
  92. package/dist/module/util/bchaddr.js.map +0 -1
  93. package/dist/module/wallet/Slp.d.ts +0 -431
  94. package/dist/module/wallet/Slp.d.ts.map +0 -1
  95. package/dist/module/wallet/Slp.js +0 -770
  96. package/dist/module/wallet/Slp.js.map +0 -1
  97. package/dist/module/webhook/WebhookSlp.d.ts +0 -15
  98. package/dist/module/webhook/WebhookSlp.d.ts.map +0 -1
  99. package/dist/module/webhook/WebhookSlp.js +0 -100
  100. package/dist/module/webhook/WebhookSlp.js.map +0 -1
  101. package/src/slp/GsppProvider.test.ts +0 -40
  102. package/src/slp/GsppProvider.ts +0 -329
  103. package/src/slp/SlpDbProvider.test.ts +0 -27
  104. package/src/slp/SlpDbProvider.ts +0 -291
  105. package/src/slp/SlpDbTemplates.ts +0 -400
  106. package/src/slp/SlpLibAuth.test.ts +0 -162
  107. package/src/slp/SlpLibAuth.ts +0 -532
  108. package/src/slp/SlpProvider.ts +0 -100
  109. package/src/slp/index.ts +0 -6
  110. package/src/slp/interface.ts +0 -128
  111. package/src/util/bchaddr.test.ts +0 -27
  112. package/src/util/bchaddr.ts +0 -27
  113. package/src/wallet/Slp.test.headless.js +0 -120
  114. package/src/wallet/Slp.test.ts +0 -1000
  115. package/src/wallet/Slp.ts +0 -1014
  116. package/src/webhook/WebhookSlp.test.ts +0 -288
  117. package/src/webhook/WebhookSlp.ts +0 -123
@@ -1,291 +0,0 @@
1
- import { Network, TxI } from "../interface.js";
2
- import {
3
- SlpDbResponse,
4
- SlpDbTx,
5
- SlpTokenBalance,
6
- SlpTokenInfo,
7
- SlpTxI,
8
- SlpUtxoI,
9
- } from "./interface.js";
10
- import {
11
- SlpAllUtxosTemplate,
12
- SlpAddressTransactionHistoryTemplate,
13
- SlpWaitForTransactionTemplate,
14
- SlpBatonUtxosTemplate,
15
- SlpSpendableUtxosTemplate,
16
- SlpTokenInfoTemplate,
17
- SlpAllTokenBalancesTemplate,
18
- SlpTokenBalanceTemplate,
19
- SlpAllOutpointsTemplate,
20
- } from "./SlpDbTemplates.js";
21
- import BigNumber from "bignumber.js";
22
- import {
23
- SlpCancelWatchFn,
24
- SlpProvider,
25
- SlpWatchBalanceCallback,
26
- SlpWatchTransactionCallback,
27
- _convertBalanceBigNumbers,
28
- _convertSlpTokenInfo,
29
- _convertUtxoBigNumbers,
30
- _emptyTokenBalance,
31
- } from "./SlpProvider.js";
32
- import axios from "axios";
33
- import { btoa } from "../util/base64.js";
34
-
35
- //import EventSource from "../../polyfill/eventsource.js";
36
- import EventSource from "../util/eventsource.js";
37
-
38
- import {
39
- getRuntimePlatform,
40
- RuntimePlatform,
41
- } from "../util/getRuntimePlatform.js";
42
-
43
- export class SlpDbProvider implements SlpProvider {
44
- public static defaultServers = {
45
- mainnet: {
46
- dataSource: "https://slpdb.fountainhead.cash",
47
- eventSource: "https://slpsocket.fountainhead.cash",
48
- },
49
- testnet: {
50
- dataSource: "https://slpdb-testnet.fountainhead.cash",
51
- eventSource: "https://slpsocket-testnet.fountainhead.cash",
52
- },
53
- regtest: {
54
- dataSource: "http://localhost:12300",
55
- eventSource: "http://localhost:12301",
56
- },
57
- };
58
-
59
- public servers = { ...SlpDbProvider.defaultServers };
60
-
61
- public caching: boolean = false;
62
-
63
- constructor(public network: Network = Network.MAINNET) {
64
- if (getRuntimePlatform() === RuntimePlatform.node) {
65
- if (process.env.SLPDB_MAINNET_DATA)
66
- this.servers.mainnet.dataSource = process.env.SLPDB_MAINNET_DATA;
67
- if (process.env.SLPDB_MAINNET_EVENTS)
68
- this.servers.mainnet.eventSource = process.env.SLPDB_MAINNET_EVENTS;
69
- if (process.env.SLPDB_TESTNET_DATA)
70
- this.servers.testnet.dataSource = process.env.SLPDB_TESTNET_DATA;
71
- if (process.env.SLPDB_TESTNET_EVENTS)
72
- this.servers.testnet.eventSource = process.env.SLPDB_TESTNET_EVENTS;
73
- if (process.env.SLPDB_REGTEST_DATA)
74
- this.servers.regtest.dataSource = process.env.SLPDB_REGTEST_DATA;
75
- if (process.env.SLPDB_REGTEST_EVENTS)
76
- this.servers.regtest.eventSource = process.env.SLPDB_REGTEST_EVENTS;
77
- }
78
- }
79
-
80
- // all oupoints, including mint batons
81
- async SlpOutpoints(slpaddr: string): Promise<String[]> {
82
- return (await this.SlpDbQuery(SlpAllOutpointsTemplate(slpaddr)))
83
- .g as String[];
84
- }
85
-
86
- // all utxos, including mint batons
87
- async SlpUtxos(slpaddr: string): Promise<SlpUtxoI[]> {
88
- return _convertUtxoBigNumbers(
89
- (await this.SlpDbQuery(SlpAllUtxosTemplate(slpaddr))).g as SlpUtxoI[]
90
- );
91
- }
92
-
93
- // look up the token information
94
- async SlpTokenInfo(tokenId: string): Promise<SlpTokenInfo | undefined> {
95
- const infos = (await this.SlpDbQuery(SlpTokenInfoTemplate(tokenId)))
96
- .t as SlpTokenInfo[];
97
- return _convertSlpTokenInfo(infos[0]);
98
- }
99
-
100
- // safe-spendable token utxos, without baton
101
- async SlpSpendableUtxos(
102
- slpaddr: string,
103
- tokenId?: string
104
- ): Promise<SlpUtxoI[]> {
105
- return _convertUtxoBigNumbers(
106
- (await this.SlpDbQuery(SlpSpendableUtxosTemplate(slpaddr, tokenId)))
107
- .g as SlpUtxoI[]
108
- );
109
- }
110
-
111
- // token mint baton utxos
112
- async SlpBatonUtxos(slpaddr: string, tokenId?: string): Promise<SlpUtxoI[]> {
113
- return _convertUtxoBigNumbers(
114
- (await this.SlpDbQuery(SlpBatonUtxosTemplate(slpaddr, tokenId)))
115
- .g as SlpUtxoI[]
116
- );
117
- }
118
-
119
- // get all token balances
120
- async SlpAllTokenBalances(slpaddr: string): Promise<SlpTokenBalance[]> {
121
- return _convertBalanceBigNumbers(
122
- (await this.SlpDbQuery(SlpAllTokenBalancesTemplate(slpaddr)))
123
- .g as SlpTokenBalance[]
124
- );
125
- }
126
-
127
- // get specific token balance
128
- async SlpTokenBalance(
129
- slpaddr: string,
130
- tokenId: string
131
- ): Promise<SlpTokenBalance> {
132
- const balances = _convertBalanceBigNumbers(
133
- (await this.SlpDbQuery(SlpTokenBalanceTemplate(slpaddr, tokenId)))
134
- .g as SlpTokenBalance[]
135
- );
136
-
137
- return balances[0] || _emptyTokenBalance(tokenId);
138
- }
139
-
140
- // get all slp transactions of this address
141
- async SlpAddressTransactionHistory(
142
- slpaddr: string,
143
- tokenId?: string,
144
- limit: number = 100,
145
- skip: number = 0
146
- ): Promise<SlpTxI[]> {
147
- const response = await this.SlpDbQuery(
148
- SlpAddressTransactionHistoryTemplate(slpaddr, tokenId, limit, skip)
149
- );
150
- return response.c.concat(response.u) as SlpTxI[];
151
- }
152
-
153
- // waits for next slp transaction to appear in mempool, code execution is halted
154
- async SlpWaitForTransaction(
155
- slpaddr: string,
156
- tokenId?: string
157
- ): Promise<SlpTxI> {
158
- return new Promise(async (resolve) => {
159
- const cancelFn = this.SlpWatchTransactions(
160
- async (tx: SlpTxI) => {
161
- await cancelFn();
162
- resolve(tx);
163
- },
164
- slpaddr,
165
- tokenId
166
- );
167
- });
168
- }
169
-
170
- // waits for a certain slp token balance to be available in this wallet, code execution is halted
171
- async SlpWaitForBalance(
172
- value: BigNumber.Value,
173
- slpaddr: string,
174
- tokenId: string
175
- ): Promise<SlpTokenBalance> {
176
- return new Promise((resolve) => {
177
- const cancelFn = this.SlpWatchBalance(
178
- async (balance: SlpTokenBalance) => {
179
- if (balance.value.isGreaterThanOrEqualTo(new BigNumber(value))) {
180
- await cancelFn();
181
- resolve(balance);
182
- }
183
- },
184
- slpaddr,
185
- tokenId
186
- );
187
- });
188
- }
189
-
190
- // set's up a callback to be executed each time the token balance of the wallet is changed
191
- SlpWatchBalance(
192
- callback: SlpWatchBalanceCallback,
193
- slpaddr: string,
194
- tokenId: string
195
- ): SlpCancelWatchFn {
196
- return this.SlpWatchTransactions(
197
- async () => {
198
- const balance = await this.SlpTokenBalance(slpaddr, tokenId);
199
- callback(balance);
200
- },
201
- slpaddr,
202
- tokenId
203
- );
204
- }
205
-
206
- // sets up a callback to be executed each time a new transaction associated with this wallet's address is entering the mempool
207
- SlpWatchTransactions(
208
- callback: SlpWatchTransactionCallback,
209
- slpaddr: string,
210
- tokenId?: string
211
- ): SlpCancelWatchFn {
212
- const eventSource: EventSource = this.SlpSocketEventSource(
213
- SlpWaitForTransactionTemplate(slpaddr, tokenId)
214
- );
215
- const cancelFn: SlpCancelWatchFn = async () => {
216
- eventSource.close();
217
- };
218
-
219
- eventSource.addEventListener(
220
- "message",
221
- async (txEvent: MessageEvent) => {
222
- const data = JSON.parse(txEvent.data);
223
- if (data.data && data.data.length) {
224
- const tx: SlpTxI = {
225
- tx_hash: data.data[0].tx.h,
226
- height: 0,
227
- details: data.data[0] as SlpDbTx,
228
- };
229
- callback(tx);
230
- }
231
- },
232
- false
233
- );
234
-
235
- return cancelFn;
236
- }
237
-
238
- public SlpDbQuery(queryObject: any): Promise<SlpDbResponse> {
239
- if (this.caching) {
240
- axiosInstance.defaults.headers = {};
241
- } else {
242
- axiosInstance.defaults.headers = noCacheHeaders;
243
- }
244
-
245
- return new Promise((resolve, reject) => {
246
- const url = `${this.servers[this.network].dataSource}/q/${B64QueryString(
247
- queryObject
248
- )}`;
249
- fetch_retry(url).then((response: any) => {
250
- if (response.hasOwnProperty("error")) {
251
- reject(new Error(response["error"]));
252
- }
253
- resolve(response.data as SlpDbResponse);
254
- });
255
- });
256
- }
257
-
258
- public SlpSocketEventSource(queryObject: any): EventSource {
259
- const url = `${this.servers[this.network].eventSource}/s/${B64QueryString(
260
- queryObject
261
- )}`;
262
- return new EventSource(url);
263
- }
264
- }
265
-
266
- const noCacheHeaders = {
267
- "Cache-Control": "no-cache",
268
- Pragma: "no-cache",
269
- Expires: "0",
270
- };
271
-
272
- const axiosInstance = axios.create({
273
- headers: noCacheHeaders,
274
- });
275
-
276
- const fetch_retry = (url, options = {}, n = 5) =>
277
- axiosInstance.get(url, options).catch(function (error) {
278
- if (n === 0) {
279
- throw error;
280
- }
281
- return new Promise((resolve) => {
282
- setTimeout(() => resolve(fetch_retry(url, options, n - 1)), 1000);
283
- });
284
- });
285
-
286
- const B64QueryString = function (queryObject): string {
287
- if (!queryObject || !Object.keys(queryObject).length) {
288
- throw new Error("Empty SLPDB query");
289
- }
290
- return btoa(JSON.stringify(queryObject));
291
- };
@@ -1,400 +0,0 @@
1
- // Slp Utxos for bch operation, to prevent accident burning of tokens and baton
2
- // prettier-ignore
3
- export const SlpAllUtxosTemplate = (slpaddr: string) => ({
4
- "v": 3,
5
- "q": {
6
- "db": ["g"],
7
- "aggregate": [
8
- {
9
- "$match": {
10
- "graphTxn.outputs.address": slpaddr,
11
- "graphTxn.outputs.status": {
12
- "$in": [
13
- "UNSPENT",
14
- "BATON_UNSPENT"
15
- ]
16
- }
17
- }
18
- },
19
- {
20
- "$unwind": "$graphTxn.outputs"
21
- },
22
- {
23
- "$match": {
24
- "graphTxn.outputs.address": slpaddr,
25
- "graphTxn.outputs.status": {
26
- "$in": [
27
- "UNSPENT",
28
- "BATON_UNSPENT"
29
- ]
30
- }
31
- }
32
- }
33
- ],
34
- "limit": 1e6,
35
- },
36
- "r": {
37
- "f": "[ .[] | { txid: .graphTxn.txid, vout: .graphTxn.outputs.vout, satoshis: .graphTxn.outputs.bchSatoshis, value: .graphTxn.outputs.slpAmount, decimals: .graphTxn.details.decimals, ticker: .graphTxn.details.symbol, tokenId: .graphTxn.details.tokenIdHex, type: .graphTxn.details.versionType, parentTokenId: .tokenDetails.nftGroupIdHex } ]"
38
- }
39
- });
40
-
41
- // Slp outpoints for bch operation, to prevent accident burning of tokens and baton
42
- // prettier-ignore
43
- export const SlpAllOutpointsTemplate = (slpaddr: string) => ({
44
- "v": 3,
45
- "q": {
46
- "db": ["g"],
47
- "aggregate": [
48
- {
49
- "$match": {
50
- "graphTxn.outputs.address": slpaddr,
51
- "graphTxn.outputs.status": {
52
- "$in": [
53
- "UNSPENT",
54
- "BATON_UNSPENT"
55
- ]
56
- }
57
- }
58
- },
59
- {
60
- "$unwind": "$graphTxn.outputs"
61
- },
62
- {
63
- "$match": {
64
- "graphTxn.outputs.address": slpaddr,
65
- "graphTxn.outputs.status": {
66
- "$in": [
67
- "UNSPENT",
68
- "BATON_UNSPENT"
69
- ]
70
- }
71
- }
72
- },
73
- {
74
- "$project": {
75
- "graphTxn": 1
76
- }
77
- }
78
- ],
79
- "limit": 1e6,
80
- },
81
- "r": {
82
- "f": "[ .[] | [.graphTxn.txid, .graphTxn.outputs.vout | tostring] | join(\":\") ]"
83
- }
84
- });
85
-
86
- // Slp Utxos for spending
87
- // prettier-ignore
88
- export const SlpSpendableUtxosTemplate = (slpaddr: string, tokenId?: string) => {
89
- let q = {
90
- "v": 3,
91
- "q": {
92
- "db": ["g"],
93
- "aggregate": [
94
- {
95
- "$match": {
96
- "graphTxn.outputs.address": slpaddr,
97
- "graphTxn.outputs.status": "UNSPENT"
98
- }
99
- },
100
- {
101
- "$unwind": "$graphTxn.outputs"
102
- },
103
- {
104
- "$match": {
105
- "graphTxn.outputs.address": slpaddr,
106
- "graphTxn.outputs.status": "UNSPENT"
107
- }
108
- },
109
- {
110
- "$lookup": {
111
- "from": "tokens",
112
- "localField": "graphTxn.details.tokenIdHex",
113
- "foreignField": "tokenDetails.tokenIdHex",
114
- "as": "token"
115
- }
116
- }
117
- ]
118
- },
119
- "r": {
120
- "f": "[ .[] | { txid: .graphTxn.txid, vout: .graphTxn.outputs.vout, satoshis: .graphTxn.outputs.bchSatoshis, value: .graphTxn.outputs.slpAmount, decimals: .token[0].tokenDetails.decimals, ticker: .token[0].tokenDetails.symbol, tokenId: .graphTxn.details.tokenIdHex, type: .graphTxn.details.versionType, parentTokenId: .token[0].nftParentId } ]"
121
- }
122
- };
123
-
124
- if (tokenId) {
125
- (q["q"]["aggregate"] as any [])[0]["$match"]["graphTxn.details.tokenIdHex"] = tokenId;
126
- }
127
-
128
- // console.log(JSON.stringify(q, null, 2));
129
-
130
- return q;
131
- }
132
-
133
- // prettier-ignore
134
- export const SlpAllTokenBalancesTemplate = (slpaddr: string) => {
135
- let q = {
136
- "v": 3,
137
- "q": {
138
- "db": ["g"],
139
- "aggregate": [
140
- {
141
- "$match": {
142
- "graphTxn.outputs.status": "UNSPENT",
143
- "graphTxn.outputs.address": slpaddr
144
- }
145
- },
146
- {
147
- "$unwind": "$graphTxn.outputs"
148
- },
149
- {
150
- "$match": {
151
- "graphTxn.outputs.status": "UNSPENT",
152
- "graphTxn.outputs.address": slpaddr
153
- }
154
- },
155
- {
156
- "$group": {
157
- "_id": "$tokenDetails.tokenIdHex",
158
- "slpAmount": {
159
- "$sum": "$graphTxn.outputs.slpAmount"
160
- }
161
- }
162
- },
163
- {
164
- "$sort": {
165
- "slpAmount": -1
166
- }
167
- },
168
- {
169
- "$lookup": {
170
- "from": "tokens",
171
- "localField": "_id",
172
- "foreignField": "tokenDetails.tokenIdHex",
173
- "as": "token"
174
- }
175
- },
176
- {
177
- "$match": {
178
- "slpAmount": {
179
- "$gt": 0
180
- }
181
- }
182
- }
183
- ],
184
- "sort": {
185
- "slpAmount": -1
186
- }
187
- },
188
- "r": {
189
- "f": "[ .[] | { value: .slpAmount, ticker: .token[0].tokenDetails.symbol, name: .token[0].tokenDetails.name, tokenId: ._id, type: .token[0].tokenDetails.versionType, parentTokenId: .token[0].nftParentId } ]"
190
- }
191
- }
192
-
193
- return q;
194
- }
195
-
196
- // prettier-ignore
197
- export const SlpTokenBalanceTemplate = (slpaddr: string, tokenId: string) => {
198
- let q = {
199
- "v": 3,
200
- "q": {
201
- "db": ["g"],
202
- "aggregate": [
203
- {
204
- "$match": {
205
- "tokenDetails.tokenIdHex": tokenId
206
- }
207
- },
208
- {
209
- "$match": {
210
- "graphTxn.outputs.status": "UNSPENT",
211
- "graphTxn.outputs.address": slpaddr
212
- }
213
- },
214
- {
215
- "$unwind": "$graphTxn.outputs"
216
- },
217
- {
218
- "$match": {
219
- "graphTxn.outputs.status": "UNSPENT",
220
- "graphTxn.outputs.address": slpaddr
221
- }
222
- },
223
- {
224
- "$group": {
225
- "_id": "$tokenDetails.tokenIdHex",
226
- "slpAmount": {
227
- "$sum": "$graphTxn.outputs.slpAmount"
228
- }
229
- }
230
- },
231
- {
232
- "$sort": {
233
- "slpAmount": -1
234
- }
235
- },
236
- {
237
- "$lookup": {
238
- "from": "tokens",
239
- "localField": "_id",
240
- "foreignField": "tokenDetails.tokenIdHex",
241
- "as": "token"
242
- }
243
- }
244
- ],
245
- "sort": {
246
- "slpAmount": -1
247
- }
248
- },
249
- "r": {
250
- "f": "[ .[] | { value: .slpAmount, ticker: .token[0].tokenDetails.symbol, name: .token[0].tokenDetails.name, tokenId: ._id, type: .token[0].tokenDetails.versionType, parentTokenId: .token[0].nftParentId } ]"
251
- }
252
- }
253
-
254
- // console.log(JSON.stringify(q, null, 2));
255
-
256
- return q;
257
- }
258
-
259
- // prettier-ignore
260
- export const SlpAddressTransactionHistoryTemplate = (address: string, tokenId?: string, limit: number = 100, skip: number = 0) => {
261
- const q = {
262
- "v": 3,
263
- "q": {
264
- "db": ["c", "u"],
265
- "find": {
266
- "$query": {
267
- "$or": [
268
- {"in.e.a": address},
269
- {"out.e.a": address},
270
- ],
271
- "$and": [
272
- {"slp.valid": true},
273
- ]
274
- },
275
- },
276
- "sort": {"blk.i": 1},
277
- "limit": limit,
278
- "skip": skip,
279
- },
280
- "r": {
281
- "f": "[ .[] | { tx_hash: .tx.h, height: .blk.i, details: . } ]"
282
- }
283
- };
284
-
285
- if (tokenId) {
286
- (q["q"]["find"]["$query"]["$and"] as any[]).push({"slp.detail.tokenIdHex": tokenId});
287
- }
288
-
289
- // console.log(JSON.stringify(q, null, 2));
290
-
291
- return q;
292
- };
293
-
294
- // prettier-ignore
295
- export const SlpWaitForTransactionTemplate = (slpaddr?: string, tokenId?: string) => {
296
- let q = {
297
- "v": 3,
298
- "q": {
299
- "db": ["c", "u"],
300
- "find": {
301
- }
302
- }
303
- }
304
-
305
- if (slpaddr) {
306
- q["q"]["find"]["$or"] = [
307
- {
308
- "in.e.a": slpaddr
309
- },
310
- {
311
- "out.e.a": slpaddr
312
- }
313
- ]
314
- }
315
-
316
- if (tokenId) {
317
- q["q"]["find"]["slp.detail.tokenIdHex"] = tokenId;
318
- }
319
-
320
- return q;
321
- };
322
-
323
- // Lookup SLP token information
324
- // prettier-ignore
325
- export const SlpTokenInfoTemplate = (tokenId?: string, limit: number = 100, skip: number = 0) => {
326
- let q = {
327
- "v": 3,
328
- "q": {
329
- "db": ["t"],
330
- "find": {},
331
- "sort": {
332
- "tokenStats.approx_txns_since_genesis": -1
333
- },
334
- "limit": limit,
335
- "skip": skip,
336
- },
337
- "r": {
338
- "f": "[ .[] | { ticker: .tokenDetails.symbol, name: .tokenDetails.name, tokenId: .tokenDetails.tokenIdHex, decimals: .tokenDetails.decimals, documentUrl: .tokenDetails.documentUri, documentHash: .tokenDetails.documentSha256Hex, initialAmount: .tokenDetails.genesisOrMintQuantity, type: .tokenDetails.versionType, parentTokenId: .nftParentId } ]"
339
- }
340
- }
341
-
342
- if (tokenId) {
343
- q["q"]["find"]["tokenDetails.tokenIdHex"] = tokenId;
344
- }
345
-
346
- // console.log(q);
347
-
348
- return q;
349
- }
350
-
351
- // Slp Utxos for bch operation, to prevent accident burning of tokens and baton
352
- // prettier-ignore
353
- export const SlpBatonUtxosTemplate = (slpaddr: string, tokenId?: string) => {
354
- let q = {
355
- "v": 3,
356
- "q": {
357
- "db": ["g"],
358
- "aggregate": [
359
- {
360
- "$match": {
361
- "graphTxn.outputs.address": slpaddr,
362
- "graphTxn.outputs.status": "BATON_UNSPENT"
363
- }
364
- },
365
- {
366
- "$unwind": "$graphTxn.outputs"
367
- },
368
- {
369
- "$match": {
370
- "graphTxn.outputs.address": slpaddr,
371
- "graphTxn.outputs.status": "BATON_UNSPENT"
372
- }
373
- },
374
- {
375
- "$project": {
376
- "graphTxn": 1
377
- }
378
- },
379
- {
380
- "$lookup": {
381
- "from": "tokens",
382
- "localField": "graphTxn.details.tokenIdHex",
383
- "foreignField": "tokenDetails.tokenIdHex",
384
- "as": "token"
385
- }
386
- }
387
- ]
388
- },
389
- "r": {
390
- "f": "[ .[] | { txid: .graphTxn.txid, vout: .graphTxn.outputs.vout, satoshis: .graphTxn.outputs.bchSatoshis, value: .graphTxn.outputs.slpAmount, decimals: .token[0].tokenDetails.decimals, ticker: .token[0].tokenDetails.symbol, tokenId: .graphTxn.details.tokenIdHex, type: .graphTxn.details.versionType } ]"
391
- }
392
- }
393
-
394
- if (tokenId) {
395
- (q["q"]["aggregate"] as any [])[0]["$match"]["graphTxn.details.tokenIdHex"] = tokenId;
396
- }
397
-
398
- // console.log(JSON.stringify(q, null, 2));
399
- return q;
400
- }