ychapi 0.903.0 → 0.912.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.
- package/dist/api.js +823 -0
- package/dist/ychapi.min.js +1 -0
- package/package.json +5 -2
package/dist/api.js
ADDED
|
@@ -0,0 +1,823 @@
|
|
|
1
|
+
|
|
2
|
+
/*!
|
|
3
|
+
* Walrus-compatible exchange API js layer
|
|
4
|
+
*
|
|
5
|
+
* Copyright Lynxline LLC, yshurik, 2019-2025,
|
|
6
|
+
* Common Clause license https://commonsclause.com/
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
/*!
|
|
10
|
+
* STARTUP API
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/*!
|
|
14
|
+
* Start the system.
|
|
15
|
+
* Initialize the data and start the sync.
|
|
16
|
+
* If there is valid non-expired jwt token in cookies,
|
|
17
|
+
* then user data is loaded from the server and login is not required
|
|
18
|
+
* (still login key is not initialized and user needs to be asked for the password on first api call)
|
|
19
|
+
*
|
|
20
|
+
* @param {object} callbacks
|
|
21
|
+
* @param {function} callbacks.on_sync_connecting() - show "syncing...", hide all visible data
|
|
22
|
+
* @param {function} callbacks.on_sync_connection_lost() - to indicate error from sync connection lost
|
|
23
|
+
* @param {function} callbacks.on_sync_connection_error(err) - report error from call_init() data initialization
|
|
24
|
+
* @param {function} callbacks.on_sync_data_init - data init is complete, can now fill gui with datas (get calls)
|
|
25
|
+
* @param {function} callbacks.on_sync_ready - data init and sync connection established - switch to "ready" state
|
|
26
|
+
* @param {function} callbacks.on_sync_message - handle sync messages
|
|
27
|
+
* @param {function} callbacks.on_sync_data_update - notify about data updates
|
|
28
|
+
* available data:
|
|
29
|
+
* - login()
|
|
30
|
+
* - logout()
|
|
31
|
+
* - market(market)
|
|
32
|
+
* - coininfo(coininfo)
|
|
33
|
+
* - buys(market_name, coina, coinb, buys), buys - see details in get_buys()
|
|
34
|
+
* - sells(market_name, coina, coinb, sells), sells - see details in get_sells()
|
|
35
|
+
* - trades(market_name, coina, coinb, trades), trades - see details in get_trades()
|
|
36
|
+
* - balance(coin, balance)
|
|
37
|
+
* - txouts(coin, txouts)
|
|
38
|
+
* - user_buys(market_name, coina, coinb, buys), buys - see details in get_buys()
|
|
39
|
+
* - user_sells(market_name, coina, coinb, sells), sells - see details in get_sells()
|
|
40
|
+
* - user_trades(market_name, coina, coinb, trades), trades - see details in get_trades()
|
|
41
|
+
* @returns null
|
|
42
|
+
*/
|
|
43
|
+
ychapi.start = function(base_uri, callbacks) {
|
|
44
|
+
return ychapi._start(base_uri, callbacks);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
/*!
|
|
48
|
+
* Request the server for initial data.
|
|
49
|
+
* It is called automatically on start().
|
|
50
|
+
* Use only for cases like logout or force data reload.
|
|
51
|
+
* @returns {Promise}
|
|
52
|
+
* - resolved (no data)
|
|
53
|
+
* - rejected with the error from the server
|
|
54
|
+
*/
|
|
55
|
+
ychapi.call_init = async function() {
|
|
56
|
+
return ychapi._call_init();
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
/*!
|
|
60
|
+
* Process the login and initialize data same as start()/call_init().
|
|
61
|
+
* @param {string} login
|
|
62
|
+
* @param {string} pass
|
|
63
|
+
* @param {string} otp_code (2FA)
|
|
64
|
+
* @returns {Promise}
|
|
65
|
+
* - resolved (no data)
|
|
66
|
+
* - rejected with the error from the server
|
|
67
|
+
* @security
|
|
68
|
+
* - pass is not sent to the server
|
|
69
|
+
* - login/pass converted to uprvk/upubk and kept in memory (login key)
|
|
70
|
+
*/
|
|
71
|
+
ychapi.call_login = async function(login, pass, otp_code) {
|
|
72
|
+
return ychapi._call_login(login, pass, otp_code);
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
/*!
|
|
76
|
+
* Logout: clear the user data and remove jwt from cookies.
|
|
77
|
+
* @returns null
|
|
78
|
+
*/
|
|
79
|
+
ychapi.call_logout = async function() {
|
|
80
|
+
return ychapi._call_logout();
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
/*!
|
|
84
|
+
* GET DATA API
|
|
85
|
+
*/
|
|
86
|
+
|
|
87
|
+
/*!
|
|
88
|
+
* Get the server-sorted-ordered list of coin names
|
|
89
|
+
* @returns {string[]}
|
|
90
|
+
*/
|
|
91
|
+
ychapi.get_coin_names = function() {
|
|
92
|
+
return ychapi._get_coin_names();
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
/*!
|
|
96
|
+
* Get the coininfo by coin name
|
|
97
|
+
* @param {string} coin
|
|
98
|
+
* @returns {object}
|
|
99
|
+
*/
|
|
100
|
+
ychapi.get_coin_info = function(coin) {
|
|
101
|
+
return ychapi._get_coin_info(coin);
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
/*!
|
|
105
|
+
* Get the server-sorted-ordered list of market groups names
|
|
106
|
+
* @returns {string[]}
|
|
107
|
+
*/
|
|
108
|
+
ychapi.get_markets_groups_names = function() {
|
|
109
|
+
return ychapi._get_markets_groups_names();
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/*!
|
|
113
|
+
* Get the market group by group name
|
|
114
|
+
* @param {string} group_name
|
|
115
|
+
* @returns {object}
|
|
116
|
+
*/
|
|
117
|
+
ychapi.get_market_group = function(group_name) {
|
|
118
|
+
return ychapi._get_market_group(group_name);
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/*!
|
|
122
|
+
* Get the market group by coinb (grouped by coinb)
|
|
123
|
+
* @param {string} coinb
|
|
124
|
+
* @returns {object}
|
|
125
|
+
*/
|
|
126
|
+
ychapi.get_market_group_by_coinb = function(coinb) {
|
|
127
|
+
return ychapi._get_market_group_by_coinb(coinb);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
/*!
|
|
131
|
+
* Get the list of markets names in a group
|
|
132
|
+
* @param {string} group_name
|
|
133
|
+
* @returns {string[]}
|
|
134
|
+
*/
|
|
135
|
+
ychapi.get_markets_names_in_group = function(group_name) {
|
|
136
|
+
return ychapi._get_markets_names_in_group(group_name);
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
/*!
|
|
140
|
+
* Get the server-sorted-ordered list of markets names
|
|
141
|
+
* @returns {string[]}
|
|
142
|
+
*/
|
|
143
|
+
ychapi.get_markets_names = function() {
|
|
144
|
+
return ychapi._get_markets_names();
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
/*!
|
|
148
|
+
* Get the market info by market name
|
|
149
|
+
* @param {string} market_name
|
|
150
|
+
* @returns {object}
|
|
151
|
+
*/
|
|
152
|
+
ychapi.get_market_info = function(market_name) {
|
|
153
|
+
return ychapi._get_market_info(market_name);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
/*!
|
|
157
|
+
* Get the coin balance by coin name
|
|
158
|
+
* @param {string} coin
|
|
159
|
+
* @returns {Balance object}
|
|
160
|
+
* {
|
|
161
|
+
* coin: {string} - coin name
|
|
162
|
+
* sum: {BigInt} - total amount
|
|
163
|
+
* free: {BigInt} - available amount for operations
|
|
164
|
+
* debit: {BigInt} - debit (others owe to the user)
|
|
165
|
+
* credit: {BigInt} - credit (the user owes to others)
|
|
166
|
+
* txouts: {BigInt} - txouts sum
|
|
167
|
+
* offtrade: {BigInt} - allocated txouts, (can not trade)
|
|
168
|
+
* orders: {BigInt} - orders sum
|
|
169
|
+
* ordersindebit: {BigInt} - orders in debit sum
|
|
170
|
+
* ordersintxouts: {BigInt} - orders in txouts sum
|
|
171
|
+
* deposits: {BigInt} - deposits in processing sum
|
|
172
|
+
* withdraws: {BigInt} - withdrawals in processing sum
|
|
173
|
+
* clearance: {BigInt} - processing now sum (a change on the blockchain)
|
|
174
|
+
* cycle: {number} - cycle number for peg-based coins (peg_t1)
|
|
175
|
+
* }
|
|
176
|
+
*/
|
|
177
|
+
ychapi.get_coin_balance = function(coin) {
|
|
178
|
+
return ychapi._get_coin_balance(coin);
|
|
179
|
+
};
|
|
180
|
+
|
|
181
|
+
/*!
|
|
182
|
+
* Get the coin usd rate by coin name
|
|
183
|
+
* @param {string} coin
|
|
184
|
+
* @returns {number} one coin value estimate in usd
|
|
185
|
+
*/
|
|
186
|
+
ychapi.get_coin_usd_rate = function(coin) {
|
|
187
|
+
return ychapi._get_coin_usd_rate(coin);
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
/*!
|
|
191
|
+
* Get deposits by coin name
|
|
192
|
+
* @param {string} coin
|
|
193
|
+
* @returns {object[]}
|
|
194
|
+
*/
|
|
195
|
+
ychapi.get_coin_deposits = function(coin) {
|
|
196
|
+
return ychapi._get_coin_deposits(coin);
|
|
197
|
+
};
|
|
198
|
+
|
|
199
|
+
/*!
|
|
200
|
+
* Get withdrawals by coin name
|
|
201
|
+
* @param {string} coin
|
|
202
|
+
* @returns {object[]}
|
|
203
|
+
*/
|
|
204
|
+
ychapi.get_coin_withdrawals = function(coin) {
|
|
205
|
+
return ychapi._get_coin_withdrawals(coin);
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
/*!
|
|
209
|
+
* Get txouts by coin name
|
|
210
|
+
* @param {string} coin
|
|
211
|
+
* @returns {object[]}
|
|
212
|
+
*/
|
|
213
|
+
ychapi.get_coin_txouts = function(coin) {
|
|
214
|
+
return ychapi._get_coin_txouts(coin);
|
|
215
|
+
};
|
|
216
|
+
|
|
217
|
+
/*!
|
|
218
|
+
* REGISTRATION API
|
|
219
|
+
*/
|
|
220
|
+
|
|
221
|
+
/*!
|
|
222
|
+
* Register a new user
|
|
223
|
+
* @param {string} login - the login
|
|
224
|
+
* @param {string} pass - the password
|
|
225
|
+
* @param {string} invitation_code - the invitation code
|
|
226
|
+
* @returns {Promise}
|
|
227
|
+
* - resolved (no data)
|
|
228
|
+
* - rejected with the error from the server
|
|
229
|
+
* @security
|
|
230
|
+
* - pass is not sent to the server
|
|
231
|
+
* - login/pass are not kept in memory
|
|
232
|
+
*/
|
|
233
|
+
ychapi.call_register = async function(login, pass, invitation_code) {
|
|
234
|
+
return ychapi._call_register(login, pass, invitation_code);
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
/*!
|
|
238
|
+
* Get the email from the redeem code
|
|
239
|
+
* @param {string} redeem_code - the redeem code
|
|
240
|
+
* @returns {Promise}
|
|
241
|
+
* - resolved with the email {string} from the server,
|
|
242
|
+
* - rejected with the error from the server
|
|
243
|
+
*/
|
|
244
|
+
ychapi.call_get_email_from_redeem = async function(redeem_code) {
|
|
245
|
+
return ychapi._call_get_email_from_redeem(redeem_code);
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
/*!
|
|
249
|
+
* Get the login from the registration code
|
|
250
|
+
* @param {string} code - the confirmation code from email
|
|
251
|
+
* @returns {Promise}
|
|
252
|
+
* - resolved with the login {string} from the server,
|
|
253
|
+
* - rejected with the error from the server
|
|
254
|
+
*/
|
|
255
|
+
ychapi.call_get_registration_login = async function(code) {
|
|
256
|
+
return ychapi._call_get_registration_login(code);
|
|
257
|
+
};
|
|
258
|
+
|
|
259
|
+
/*!
|
|
260
|
+
* Confirm the registration login and get the 2FA setup data
|
|
261
|
+
* @param {string} login - the login
|
|
262
|
+
* @param {string} pass - the password
|
|
263
|
+
* @param {string} email_code - the confirmation code from email
|
|
264
|
+
* @returns {Promise}
|
|
265
|
+
* - resolved with the data {qr, url, mob} from the server,
|
|
266
|
+
* - rejected with the error from the server
|
|
267
|
+
* @security
|
|
268
|
+
* - pass is not sent to the server
|
|
269
|
+
* - login/pass are not kept in memory
|
|
270
|
+
*/
|
|
271
|
+
ychapi.call_confirm_registration_login = async function(login, pass, email_code) {
|
|
272
|
+
return ychapi._call_confirm_registration_login(login, pass, email_code);
|
|
273
|
+
};
|
|
274
|
+
|
|
275
|
+
/*!
|
|
276
|
+
* Confirm the registration 2FA
|
|
277
|
+
* @param {string} login - the login
|
|
278
|
+
* @param {string} pass - the password
|
|
279
|
+
* @param {string} email_code - the confirmation code from email
|
|
280
|
+
* @param {string} otp_code - the OTP code
|
|
281
|
+
* @returns {Promise}
|
|
282
|
+
* - resolved (no data)
|
|
283
|
+
* - rejected with the error from the server
|
|
284
|
+
* @security
|
|
285
|
+
* - pass is not sent to the server
|
|
286
|
+
* - login/pass are not kept in memory
|
|
287
|
+
*/
|
|
288
|
+
ychapi.call_confirm_registration_2fa = async function(login, pass, email_code, otp_code) {
|
|
289
|
+
return ychapi._call_confirm_registration_2fa(login, pass, email_code, otp_code);
|
|
290
|
+
};
|
|
291
|
+
|
|
292
|
+
/*!
|
|
293
|
+
* KEYS API
|
|
294
|
+
*/
|
|
295
|
+
|
|
296
|
+
/*!
|
|
297
|
+
* Get the keys from the user login/password
|
|
298
|
+
* @param {string} login
|
|
299
|
+
* @param {string} password
|
|
300
|
+
* @returns [uprvk, upubk]
|
|
301
|
+
*/
|
|
302
|
+
ychapi.get_user_login_keys_v1 = function(login, password) {
|
|
303
|
+
return ychapi._get_user_login_keys_v1(login, password);
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
/*!
|
|
307
|
+
* Get the user login public key
|
|
308
|
+
* @returns {string}
|
|
309
|
+
*/
|
|
310
|
+
ychapi.get_user_login_pubk = function() {
|
|
311
|
+
return ychapi._get_user_login_pubk();
|
|
312
|
+
};
|
|
313
|
+
|
|
314
|
+
/*!
|
|
315
|
+
* Check if the user login private key is set
|
|
316
|
+
* @returns {boolean}
|
|
317
|
+
*/
|
|
318
|
+
ychapi.has_user_login_prvk = function() {
|
|
319
|
+
return ychapi._has_user_login_prvk();
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
/*!
|
|
323
|
+
* Get the user signing public key
|
|
324
|
+
* @returns {string}
|
|
325
|
+
*/
|
|
326
|
+
ychapi.get_user_signing_pubk = function() {
|
|
327
|
+
return ychapi._get_user_signing_pubk();
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
/*!
|
|
331
|
+
* Check if the user signing private key is set
|
|
332
|
+
* @returns {boolean}
|
|
333
|
+
*/
|
|
334
|
+
ychapi.has_user_signing_prvk = function() {
|
|
335
|
+
return ychapi._has_user_signing_prvk();
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
/*!
|
|
339
|
+
* Set the user login keys
|
|
340
|
+
* @param {string} pubk
|
|
341
|
+
* @param {string} prvk
|
|
342
|
+
*/
|
|
343
|
+
ychapi.set_user_login_keys = function(pubk, prvk) {
|
|
344
|
+
return ychapi._set_user_login_keys(pubk, prvk);
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
/*!
|
|
348
|
+
* DEPOSITS API
|
|
349
|
+
*/
|
|
350
|
+
|
|
351
|
+
/*!
|
|
352
|
+
* Check if the user has a holding address for a coin
|
|
353
|
+
* @param {string} coin
|
|
354
|
+
* @returns {boolean}
|
|
355
|
+
*/
|
|
356
|
+
ychapi.has_coin_holding_address = function(coin) {
|
|
357
|
+
return ychapi._has_coin_holding_address(coin);
|
|
358
|
+
};
|
|
359
|
+
|
|
360
|
+
/*!
|
|
361
|
+
* Get the holding address for a coin
|
|
362
|
+
* @param {string} coin
|
|
363
|
+
* @returns {string}
|
|
364
|
+
*/
|
|
365
|
+
ychapi.get_coin_holding_address = function(coin) {
|
|
366
|
+
return ychapi._get_coin_holding_address(coin);
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
/*!
|
|
370
|
+
* Get the user current locktime for a coin
|
|
371
|
+
* @param {string} coin
|
|
372
|
+
* @returns {number} time_t
|
|
373
|
+
*/
|
|
374
|
+
ychapi.get_coin_user_locktime = function(coin) {
|
|
375
|
+
return ychapi._get_coin_user_locktime(coin);
|
|
376
|
+
};
|
|
377
|
+
|
|
378
|
+
/*!
|
|
379
|
+
* Get the server current locktime for a coin
|
|
380
|
+
* @param {string} coin
|
|
381
|
+
* @returns {number} time_t
|
|
382
|
+
*/
|
|
383
|
+
ychapi.get_coin_server_locktime = function(coin) {
|
|
384
|
+
return ychapi._get_coin_server_locktime(coin);
|
|
385
|
+
};
|
|
386
|
+
|
|
387
|
+
/*!
|
|
388
|
+
* TODO: may obsolete
|
|
389
|
+
*
|
|
390
|
+
* @deprecated
|
|
391
|
+
* ALERT: API below is expected to be obsolete and planned to be removed in next versions
|
|
392
|
+
*
|
|
393
|
+
*/
|
|
394
|
+
ychapi.get_coin_allowed_deposit_senders = function(coin) {
|
|
395
|
+
return ychapi._get_coin_allowed_deposit_senders(coin);
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
/*!
|
|
399
|
+
* Register the deposit evm address
|
|
400
|
+
* @param {string} coin
|
|
401
|
+
* @param {string} address
|
|
402
|
+
* @returns {Promise}
|
|
403
|
+
* - resolved (no data),
|
|
404
|
+
* - rejected with the error from the server
|
|
405
|
+
*
|
|
406
|
+
* @deprecated
|
|
407
|
+
* ALERT: API below is expected to be obsolete and planned to be removed in next versions
|
|
408
|
+
*
|
|
409
|
+
*/
|
|
410
|
+
ychapi.call_register_deposit_evm_address = function(coin, address) {
|
|
411
|
+
return ychapi._call_register_deposit_evm_address(coin, address);
|
|
412
|
+
};
|
|
413
|
+
|
|
414
|
+
/*!
|
|
415
|
+
* Unregister the deposit evm address
|
|
416
|
+
* @param {string} coin
|
|
417
|
+
* @param {string} address
|
|
418
|
+
* @returns {Promise}
|
|
419
|
+
* - resolved (no data),
|
|
420
|
+
* - rejected with the error from the server
|
|
421
|
+
*
|
|
422
|
+
* @deprecated
|
|
423
|
+
* ALERT: API below is expected to be obsolete and planned to be removed in next versions
|
|
424
|
+
*
|
|
425
|
+
*/
|
|
426
|
+
ychapi.call_unregister_deposit_evm_address = function(coin, address) {
|
|
427
|
+
return ychapi._call_unregister_deposit_evm_address(coin, address);
|
|
428
|
+
};
|
|
429
|
+
|
|
430
|
+
/*!
|
|
431
|
+
* TRADING API
|
|
432
|
+
*/
|
|
433
|
+
|
|
434
|
+
/*!
|
|
435
|
+
* Get the user trading discount
|
|
436
|
+
* @returns {number}
|
|
437
|
+
*/
|
|
438
|
+
ychapi.get_user_trading_discount = function() {
|
|
439
|
+
return ychapi._get_user_trading_discount();
|
|
440
|
+
};
|
|
441
|
+
|
|
442
|
+
/*!
|
|
443
|
+
* Get the buy fee with respect to the market and user discount
|
|
444
|
+
* @param {string} market_name - the market name
|
|
445
|
+
* @param {string} coina - the coin to buy
|
|
446
|
+
* @param {string} coinb - the coin to spend (base)
|
|
447
|
+
* @returns {BigInt}
|
|
448
|
+
*/
|
|
449
|
+
ychapi.get_buy_fee = function(market_name, coina, coinb) {
|
|
450
|
+
return ychapi._get_buy_fee(market_name, coina, coinb);
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
/*!
|
|
454
|
+
* Get the sell fee with respect to the market and user discount
|
|
455
|
+
* @param {string} market_name - the market name
|
|
456
|
+
* @param {string} coina - the coin to sell
|
|
457
|
+
* @param {string} coinb - the coin to acquire (base)
|
|
458
|
+
* @returns {BigInt}
|
|
459
|
+
*/
|
|
460
|
+
ychapi.get_sell_fee = function(market_name, coina, coinb) {
|
|
461
|
+
return ychapi._get_sell_fee(market_name, coina, coinb);
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
/*!
|
|
465
|
+
* Get the user buys
|
|
466
|
+
* @param {string} market_name - the market name
|
|
467
|
+
* @returns {object[]}
|
|
468
|
+
*/
|
|
469
|
+
ychapi.get_user_buys = function(market_name) {
|
|
470
|
+
return ychapi._get_user_buys(market_name);
|
|
471
|
+
};
|
|
472
|
+
|
|
473
|
+
/*!
|
|
474
|
+
* Get the user sells
|
|
475
|
+
* @param {string} market_name - the market name
|
|
476
|
+
* @returns {object[]}
|
|
477
|
+
*/
|
|
478
|
+
ychapi.get_user_sells = function(market_name) {
|
|
479
|
+
return ychapi._get_user_sells(market_name);
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
/*!
|
|
483
|
+
* Get the user trades
|
|
484
|
+
* @param {string} market_name - the market name
|
|
485
|
+
* @returns {object[]}
|
|
486
|
+
*/
|
|
487
|
+
ychapi.get_user_trades = function(market_name) {
|
|
488
|
+
return ychapi._get_user_trades(market_name);
|
|
489
|
+
};
|
|
490
|
+
|
|
491
|
+
/*!
|
|
492
|
+
* Get the market buys
|
|
493
|
+
* @param {string} market_name - the market name
|
|
494
|
+
* @returns {object[]}
|
|
495
|
+
*/
|
|
496
|
+
ychapi.get_market_buys = function(market_name) {
|
|
497
|
+
return ychapi._get_market_buys(market_name);
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
/*!
|
|
501
|
+
* Get the market sells
|
|
502
|
+
* @param {string} market_name - the market name
|
|
503
|
+
* @returns {object[]}
|
|
504
|
+
*/
|
|
505
|
+
ychapi.get_market_sells = function(market_name) {
|
|
506
|
+
return ychapi._get_market_sells(market_name);
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
/*!
|
|
510
|
+
* Get the market trades
|
|
511
|
+
* @param {string} market_name - the market name
|
|
512
|
+
* @returns {object[]}
|
|
513
|
+
*/
|
|
514
|
+
ychapi.get_market_trades = function(market_name) {
|
|
515
|
+
return ychapi._get_market_trades(market_name);
|
|
516
|
+
};
|
|
517
|
+
|
|
518
|
+
/*!
|
|
519
|
+
* Buy a coin
|
|
520
|
+
* @param {string} market_name - the market name
|
|
521
|
+
* @param {string} coina - the coin to buy
|
|
522
|
+
* @param {string} coinb - the coin to spend (base)
|
|
523
|
+
* @param {string} price - the price of the coin
|
|
524
|
+
* @param {BigInt} amounta - the amount of the coin to buy
|
|
525
|
+
* @param {BigInt} amountb - the amount of the coin to spend
|
|
526
|
+
* @param {BigInt} feeb - the fee (coinb) to buy
|
|
527
|
+
* @returns {Promise}
|
|
528
|
+
* - resolved (no data),
|
|
529
|
+
* - rejected with the error from the preparing/server
|
|
530
|
+
*/
|
|
531
|
+
ychapi.call_buy = async function(market_name, coina, coinb, price, amounta, amountb, feeb) {
|
|
532
|
+
return ychapi._call_buy(market_name, coina, coinb, price, amounta, amountb, feeb);
|
|
533
|
+
};
|
|
534
|
+
|
|
535
|
+
/*!
|
|
536
|
+
* Sell a coin
|
|
537
|
+
* @param {string} market_name - the market name
|
|
538
|
+
* @param {string} coina - the coin to sell
|
|
539
|
+
* @param {string} coinb - the coin to acquire (base)
|
|
540
|
+
* @param {string} price - the price of the coin
|
|
541
|
+
* @param {BigInt} amounta - the amount of the coin to sell
|
|
542
|
+
* @param {BigInt} amountb - the amount of the coin to acquire
|
|
543
|
+
* @param {BigInt} feea - the fee (coina) to sell
|
|
544
|
+
* @returns {Promise}
|
|
545
|
+
* - resolved (no data),
|
|
546
|
+
* - rejected with the error from the preparing/server
|
|
547
|
+
*/
|
|
548
|
+
ychapi.call_sell = async function(market_name, coina, coinb, price, amounta, amountb, feea) {
|
|
549
|
+
return ychapi._call_sell(market_name, coina, coinb, price, amounta, amountb, feea);
|
|
550
|
+
};
|
|
551
|
+
|
|
552
|
+
/*!
|
|
553
|
+
* Cancel a buy order
|
|
554
|
+
* @param {string} coina - the coin to buy
|
|
555
|
+
* @param {string} coinb - the coin to spend (base)
|
|
556
|
+
* @param {int} index - the index of the order
|
|
557
|
+
* @returns {Promise}
|
|
558
|
+
* - resolved (no data),
|
|
559
|
+
* - rejected with the error from the server
|
|
560
|
+
*/
|
|
561
|
+
ychapi.call_buy_order_cancel = function(coina, coinb, index) {
|
|
562
|
+
return ychapi._call_buy_order_cancel(coina, coinb, index);
|
|
563
|
+
};
|
|
564
|
+
|
|
565
|
+
/*!
|
|
566
|
+
* Cancel a sell order
|
|
567
|
+
* @param {string} coina - the coin to sell
|
|
568
|
+
* @param {string} coinb - the coin to acquire (base)
|
|
569
|
+
* @param {int} index - the index of the order
|
|
570
|
+
* @returns {Promise}
|
|
571
|
+
* - resolved (no data),
|
|
572
|
+
* - rejected with the error from the server
|
|
573
|
+
*/
|
|
574
|
+
ychapi.call_sell_order_cancel = function(coina, coinb, index) {
|
|
575
|
+
return ychapi._call_sell_order_cancel(coina, coinb, index);
|
|
576
|
+
};
|
|
577
|
+
|
|
578
|
+
/*!
|
|
579
|
+
* Get the chart data
|
|
580
|
+
* @param {string} market_name
|
|
581
|
+
* @param {string} period (60, 300, 900, 3600, 21600, 86400)
|
|
582
|
+
* @returns {Promise}
|
|
583
|
+
* - resolved with the chart data,
|
|
584
|
+
* - rejected with the error from the server
|
|
585
|
+
*/
|
|
586
|
+
ychapi.call_get_chart_data = function(market_name, period) {
|
|
587
|
+
return ychapi._call_get_chart_data(market_name, period);
|
|
588
|
+
};
|
|
589
|
+
|
|
590
|
+
/*!
|
|
591
|
+
* WITHDRAW/TRANSFER API
|
|
592
|
+
*/
|
|
593
|
+
|
|
594
|
+
/*!
|
|
595
|
+
* Validate an address
|
|
596
|
+
* @param {string} coin
|
|
597
|
+
* @param {string} address
|
|
598
|
+
* @returns error string or null if address is valid
|
|
599
|
+
*/
|
|
600
|
+
ychapi.validate_address = function(coin, address) {
|
|
601
|
+
return ychapi._validate_address(coin, address);
|
|
602
|
+
};
|
|
603
|
+
|
|
604
|
+
/*!
|
|
605
|
+
* Get the withdraw rawtx
|
|
606
|
+
* @param {object} withdraw_plan
|
|
607
|
+
* @returns {string}
|
|
608
|
+
*/
|
|
609
|
+
ychapi.get_withdraw_rawtx = function(withdraw_plan) {
|
|
610
|
+
return ychapi._get_withdraw_rawtx(withdraw_plan);
|
|
611
|
+
};
|
|
612
|
+
|
|
613
|
+
/*!
|
|
614
|
+
* Send an amount of a coin to a receiver - another user (internal transfer)
|
|
615
|
+
* @param {string} coin - the coin to send
|
|
616
|
+
* @param {string} receiver_login - the login of the receiver
|
|
617
|
+
* @param {string} amount - the amount to send
|
|
618
|
+
* @param {string} fee - the fee to send
|
|
619
|
+
* @returns {Promise}
|
|
620
|
+
* - resolved (no data),
|
|
621
|
+
* - rejected with the error from the preparing/server
|
|
622
|
+
*/
|
|
623
|
+
ychapi.call_send = async function(coin, receiver_login, amount, fee) {
|
|
624
|
+
return ychapi._call_send(coin, receiver_login, amount, fee);
|
|
625
|
+
};
|
|
626
|
+
|
|
627
|
+
/*!
|
|
628
|
+
* Create the withdraw plan
|
|
629
|
+
* @param {string} coin
|
|
630
|
+
* @param {string} scope ("available", "allocated")
|
|
631
|
+
*/
|
|
632
|
+
ychapi.create_withdraw = function(coin, scope) {
|
|
633
|
+
return ychapi._create_withdraw(coin, scope);
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
/*!
|
|
637
|
+
* Update the withdraw plan for inputs or fees change
|
|
638
|
+
* @param {object} withdraw_plan
|
|
639
|
+
*/
|
|
640
|
+
ychapi.update_withdraw = function(withdraw_plan) {
|
|
641
|
+
return ychapi._update_withdraw(withdraw_plan);
|
|
642
|
+
};
|
|
643
|
+
|
|
644
|
+
/*!
|
|
645
|
+
* Set the withdraw amount
|
|
646
|
+
* @param {object} withdraw_plan
|
|
647
|
+
* @param {BigInt} amount_bn
|
|
648
|
+
* @param {string} mode ("manual", "reduce")
|
|
649
|
+
*/
|
|
650
|
+
ychapi.set_withdraw_amount = function(withdraw_plan, amount_bn, mode) {
|
|
651
|
+
return ychapi._set_withdraw_amount(withdraw_plan, amount_bn, mode);
|
|
652
|
+
};
|
|
653
|
+
|
|
654
|
+
/*!
|
|
655
|
+
* Check if the withdraw has an inputs stage
|
|
656
|
+
* @param {object} withdraw_task
|
|
657
|
+
* @returns {boolean}
|
|
658
|
+
*/
|
|
659
|
+
ychapi.has_withdraw_inputs_stage = function(withdraw_plan) {
|
|
660
|
+
return ychapi._has_withdraw_inputs_stage(withdraw_plan);
|
|
661
|
+
};
|
|
662
|
+
|
|
663
|
+
/*!
|
|
664
|
+
* Prepare the withdraw for the inputs to finalize the transaction to sign.
|
|
665
|
+
* Coin types: txout_t1
|
|
666
|
+
* @param {object} withdraw_plan
|
|
667
|
+
* @returns {Promise}
|
|
668
|
+
* - resolved with the txouts,
|
|
669
|
+
* - rejected with the error from the server
|
|
670
|
+
*/
|
|
671
|
+
ychapi.call_withdraw_inputs_stage = function(withdraw_plan) {
|
|
672
|
+
return ychapi._call_withdraw_inputs_stage(withdraw_plan);
|
|
673
|
+
};
|
|
674
|
+
|
|
675
|
+
/*!
|
|
676
|
+
* Set the withdraw inputs for the debit
|
|
677
|
+
* @param {object} withdraw_plan
|
|
678
|
+
* @param {object[]} txouts
|
|
679
|
+
* @returns {Promise}
|
|
680
|
+
* - resolved (no data),
|
|
681
|
+
* - rejected with the error from the server
|
|
682
|
+
*/
|
|
683
|
+
ychapi.set_withdraw_inputs_for_debit = function(withdraw_plan, txouts) {
|
|
684
|
+
return ychapi._set_withdraw_inputs_for_debit(withdraw_plan, txouts);
|
|
685
|
+
};
|
|
686
|
+
|
|
687
|
+
/*!
|
|
688
|
+
* Make the withdraw
|
|
689
|
+
* @param {string} coin
|
|
690
|
+
* @param {string} addr_send
|
|
691
|
+
* @param {string} sendfee
|
|
692
|
+
* @param {object} op1
|
|
693
|
+
* @param {object} op2
|
|
694
|
+
* @returns {Promise}
|
|
695
|
+
* - resolved (no data),
|
|
696
|
+
* - rejected with the error from the server
|
|
697
|
+
*/
|
|
698
|
+
ychapi.call_withdraw = function(coin, addr_send, sendfee, op1, op2) {
|
|
699
|
+
return ychapi._call_withdraw(coin, addr_send, sendfee, op1, op2);
|
|
700
|
+
};
|
|
701
|
+
|
|
702
|
+
/*!
|
|
703
|
+
* Report the txid of the withdraw (erc20_t1, evm_t1) after the withdraw is broadcasted via web3 api
|
|
704
|
+
* @param {string} coin
|
|
705
|
+
* @param {string} gidx
|
|
706
|
+
* @param {string} txid
|
|
707
|
+
* @returns {Promise}
|
|
708
|
+
* - resolved (no data),
|
|
709
|
+
* - rejected with the error from the server
|
|
710
|
+
*/
|
|
711
|
+
ychapi.call_withdraw_report_txid = function(coin, gidx, txid) {
|
|
712
|
+
return ychapi._call_withdraw_report_txid(coin, gidx, txid);
|
|
713
|
+
};
|
|
714
|
+
|
|
715
|
+
/*!
|
|
716
|
+
* Cancel the withdraw (erc20_t1, evm_t1)
|
|
717
|
+
* @param {string} coin
|
|
718
|
+
* @param {string} gidx
|
|
719
|
+
* @returns {Promise}
|
|
720
|
+
* - resolved (no data),
|
|
721
|
+
* - rejected with the error from the server
|
|
722
|
+
*/
|
|
723
|
+
ychapi.call_withdraw_cancel = function(coin, gidx) {
|
|
724
|
+
return ychapi._call_withdraw_cancel(coin, gidx);
|
|
725
|
+
};
|
|
726
|
+
|
|
727
|
+
/*!
|
|
728
|
+
* UTILS API
|
|
729
|
+
*/
|
|
730
|
+
|
|
731
|
+
/*!
|
|
732
|
+
* Get the page size
|
|
733
|
+
* @returns {number}
|
|
734
|
+
*/
|
|
735
|
+
ychapi.get_page_size = function() {
|
|
736
|
+
return ychapi._get_page_size();
|
|
737
|
+
};
|
|
738
|
+
|
|
739
|
+
/*!
|
|
740
|
+
* Get the evm vault7u abi
|
|
741
|
+
* @returns {string}
|
|
742
|
+
*/
|
|
743
|
+
ychapi.get_evm_vault7u_abi = function() {
|
|
744
|
+
return ychapi._get_evm_vault7u_abi();
|
|
745
|
+
};
|
|
746
|
+
|
|
747
|
+
/*!
|
|
748
|
+
* Sign the evm vault7u user1
|
|
749
|
+
* @param {string} user
|
|
750
|
+
* @param {string} statehex
|
|
751
|
+
* @param {string} amount
|
|
752
|
+
* @param {string} addr
|
|
753
|
+
* @param {string} tw
|
|
754
|
+
* @returns {string}
|
|
755
|
+
*/
|
|
756
|
+
ychapi.sign_evm_vault7u_user1 = function(user, statehex, amount, addr, tw) {
|
|
757
|
+
return ychapi._sign_evm_vault7u_user1(user, statehex, amount, addr, tw);
|
|
758
|
+
};
|
|
759
|
+
|
|
760
|
+
/*!
|
|
761
|
+
* Get the user account id
|
|
762
|
+
* @returns {string}
|
|
763
|
+
*/
|
|
764
|
+
ychapi.get_user_account_id = function() {
|
|
765
|
+
return ychapi._get_user_account_id();
|
|
766
|
+
};
|
|
767
|
+
|
|
768
|
+
/*!
|
|
769
|
+
* Get the user account login
|
|
770
|
+
* @returns {string}
|
|
771
|
+
*/
|
|
772
|
+
ychapi.get_user_account_login = function() {
|
|
773
|
+
return ychapi._get_user_account_login();
|
|
774
|
+
};
|
|
775
|
+
|
|
776
|
+
/*!
|
|
777
|
+
* Get the user account twofa enabled
|
|
778
|
+
* @returns {boolean}
|
|
779
|
+
*/
|
|
780
|
+
ychapi.get_user_account_twofa = function() {
|
|
781
|
+
return ychapi._get_user_account_twofa();
|
|
782
|
+
};
|
|
783
|
+
|
|
784
|
+
/*!
|
|
785
|
+
* Get the user rewards
|
|
786
|
+
* @param {string} coin
|
|
787
|
+
* @returns {BigInt}
|
|
788
|
+
*/
|
|
789
|
+
ychapi.get_user_rewards = function(coin) {
|
|
790
|
+
return ychapi._get_user_rewards(coin);
|
|
791
|
+
};
|
|
792
|
+
|
|
793
|
+
/*!
|
|
794
|
+
* Get the user referral invitation code
|
|
795
|
+
* @returns {string}
|
|
796
|
+
*/
|
|
797
|
+
ychapi.get_user_referral_invitation_code = function() {
|
|
798
|
+
return ychapi._get_user_referral_invitation_code();
|
|
799
|
+
};
|
|
800
|
+
|
|
801
|
+
/*!
|
|
802
|
+
* Get the user referral invitation title
|
|
803
|
+
* @returns {string}
|
|
804
|
+
*/
|
|
805
|
+
ychapi.get_user_referral_invitation_title = function() {
|
|
806
|
+
return ychapi._get_user_referral_invitation_title();
|
|
807
|
+
};
|
|
808
|
+
|
|
809
|
+
/*!
|
|
810
|
+
* Check if the user is logged in
|
|
811
|
+
* @returns {boolean}
|
|
812
|
+
*/
|
|
813
|
+
ychapi.is_logged_in = function() {
|
|
814
|
+
return ychapi._is_logged_in();
|
|
815
|
+
};
|
|
816
|
+
|
|
817
|
+
/*!
|
|
818
|
+
* Check if the user has a profile
|
|
819
|
+
* @returns {boolean}
|
|
820
|
+
*/
|
|
821
|
+
ychapi.has_profile = function() {
|
|
822
|
+
return ychapi._has_profile();
|
|
823
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var ychapi={};window.ychapi=ychapi,ychapi._data={buys:{},sells:{},trades:{},markets:{}},ychapi._nnum="",ychapi._sigi_num=30,ychapi._pagesize=15,ychapi._pubkey1="",ychapi._prvkey1="",ychapi._pubkey2="",ychapi._pubkey3="",ychapi._addresses={},ychapi._locktimes1={},ychapi._locktimes2={},ychapi._asset_extra_info={},ychapi._ready_init_data=!1,console.log("ychapi init1"),ychapi._start=null,ychapi._call_init=null,ychapi._call_login=null,ychapi._call_logout=null,ychapi._get_coin_names=null,ychapi._get_coin_info=null,ychapi._get_markets_groups_names=null,ychapi._get_market_group=null,ychapi._get_market_group_by_coinb=null,ychapi._get_markets_names_in_group=null,ychapi._get_markets_names=null,ychapi._get_market_info=null,ychapi._get_coin_balance=null,ychapi._get_coin_usd_rate=null,ychapi._get_coin_deposits=null,ychapi._get_coin_withdrawals=null,ychapi._get_coin_txouts=null,ychapi._call_register=null,ychapi._call_get_email_from_redeem=null,ychapi._call_get_registration_login=null,ychapi._call_confirm_registration_login=null,ychapi._call_confirm_registration_2fa=null,ychapi._get_user_login_keys_v1=null,ychapi._get_user_login_pubk=null,ychapi._has_user_login_prvk=null,ychapi._get_user_signing_pubk=null,ychapi._has_user_signing_prvk=null,ychapi._set_user_login_keys=null,ychapi._has_coin_holding_address=null,ychapi._get_coin_holding_address=null,ychapi._get_coin_user_locktime=null,ychapi._get_coin_server_locktime=null,ychapi._get_coin_allowed_deposit_senders=null,ychapi._call_register_deposit_evm_address=null,ychapi._call_unregister_deposit_evm_address=null,ychapi._get_user_trading_discount=null,ychapi._get_buy_fee=null,ychapi._get_sell_fee=null,ychapi._get_user_buys=null,ychapi._get_user_sells=null,ychapi._get_user_trades=null,ychapi._get_market_buys=null,ychapi._get_market_sells=null,ychapi._get_market_trades=null,ychapi._call_buy=null,ychapi._call_sell=null,ychapi._call_buy_order_cancel=null,ychapi._call_sell_order_cancel=null,ychapi._call_get_chart_data=null,ychapi._validate_address=null,ychapi._get_withdraw_rawtx=null,ychapi._call_send=null,ychapi._create_withdraw=null,ychapi._update_withdraw=null,ychapi._set_withdraw_amount=null,ychapi._has_withdraw_inputs_stage=null,ychapi._call_withdraw_inputs_stage=null,ychapi._set_withdraw_inputs_for_debit=null,ychapi._call_withdraw=null,ychapi._call_withdraw_report_txid=null,ychapi._call_withdraw_cancel=null,ychapi._get_page_size=null,ychapi._get_evm_vault7u_abi=null,ychapi._sign_evm_vault7u_user1=null,ychapi._get_user_account_id=null,ychapi._get_user_account_login=null,ychapi._get_user_account_twofa=null,ychapi._get_user_rewards=null,ychapi._get_user_referral_invitation_code=null,ychapi._get_user_referral_invitation_title=null,ychapi._is_logged_in=null,ychapi._has_profile=null,ychapi._apicall=async function(e,t){if(null!=t&&null!=t||(t=0),"digest"in e){ychapi._init_coinjs();let t=ychapi._get_user_login_prvk();"uprvk"in e&&(t=e.uprvk);const n=ychapi._str2bytes(e.digest+ychapi._nnum),i=Crypto.SHA256(n,{asBytes:!0}),a=Crypto.SHA256(i,{asBytes:!0}),c=coinjs.privkey2wif(t),o=coinjs.signHash(c,a);e.data.usig=o}let n="PUT";return"calltype"in e&&(n=e.calltype),new Promise(((i,a)=>{$.ajax({type:n,url:ychapi._base_uri+e.uri,data:JSON.stringify(e.data,((e,t)=>"bigint"==typeof t?t.toString()+"n":t)),dataType:"text",success:async function(c){const o=JSON.parse(c,jsonBNparse);"GETRAW"==n||o.ok?i(o):0==o.ok&&"nnum"in o&&""!=o.nnum&&t<1?(ychapi._nnum=o.nnum,await ychapi._apicall(e,t+1).then((function(e){i(e)})).catch((function(e){a(e)}))):a(o.error)},error:function(e,t,n){a(t+": "+n)}})}))},ychapi._start=function(e,t){ychapi._base_uri=e,ychapi._ready_init_data=!1,ychapi._callbacks=t,ychapi._timer_reauth=setTimeout((function(){}),ychapi._timer_reauth_duration),ychapi._start_ws(),ychapi._call_init()},ychapi._do_apicall_reauth=function(){""!=ychapi._get_jwt_uid()&&ychapi._apicall({calltype:"GET",uri:"/u/reauth",data:{}}).then((function(e){document.cookie="jwt="+e.access+";SameSite=Strict",ychapi._do_ws_init()})).catch((function(e){ychapi._call_logout()}))},ychapi._get_user_login_pubk=function(){return ychapi._pubkey1},ychapi._has_user_login_prvk=function(){return null!=ychapi._prvkey1&&""!=ychapi._prvkey1},ychapi._get_user_login_prvk=function(){return ychapi._prvkey1},ychapi._get_user_signing_pubk=function(){return ychapi._pubkey1},ychapi._has_user_signing_prvk=function(){return null!=ychapi._prvkey1&&""!=ychapi._prvkey1},ychapi._get_user_signing_prvk=function(){return ychapi._prvkey1},ychapi._get_server_signing_pubk=function(){return ychapi._pubkey2},ychapi._get_credit_signing_pubk=function(){return ychapi._pubkey3},ychapi._set_user_login_keys=function(e,t){ychapi._pubkey1=e,ychapi._prvkey1=t},ychapi._get_coin_user_locktime=function(e){return null==ychapi._data.profile?0:e in ychapi._locktimes1?ychapi._locktimes1[e]:0},ychapi._get_coin_server_locktime=function(e){return null==ychapi._data.profile?0:e in ychapi._locktimes2?ychapi._locktimes2[e]:0},ychapi._get_coin_allowed_deposit_senders=function(e){return null!=ychapi._data.profile&&(e in ychapi._asset_extra_info&&"senders"in ychapi._asset_extra_info[e]?ychapi._asset_extra_info[e].senders:[])},ychapi._get_user_login_keys_v1=function(e,t){e=ychcore.cleanup_email(e);let n=Crypto.SHA256(Crypto.SHA256(e)+Crypto.SHA256(t));return[n,ychapi._priv_to_pub(n,!0)]},ychapi._priv_to_pub=function(e,t){let n=BigInteger.fromByteArrayUnsigned(Crypto.util.hexToBytes(e)),i=EllipticCurve.getSECCurveByName("secp256k1").getG().multiply(n),a=i.getX().toBigInteger(),c=i.getY().toBigInteger(),o=EllipticCurve.integerToBytes(a,32);if(o=o.concat(EllipticCurve.integerToBytes(c,32)),o.unshift(4),1==t){let e=EllipticCurve.integerToBytes(a,32);return c.isEven()?e.unshift(2):e.unshift(3),Crypto.util.bytesToHex(e)}return Crypto.util.bytesToHex(o)},ychapi._init_coinjs=function(e){coinjs.pub=[111],coinjs.priv=[239],coinjs.multisig=[196],coinjs.base32pref="",coinjs.compressed=!1,coinjs.txversion=1,coinjs.shf=0,coinjs.maxrbf=0,coinjs.txExtraTimeField=0;const t=ychapi._get_coin_info(e);if(null==t)return;if("peg_t1"!=t.type&&"txout_t1"!=t.type)return;const n=t.cfg;if(""==n.pub)return;const i=Crypto.util.hexToBytes(n.pub),a=Crypto.util.hexToBytes(n.prv),c=Crypto.util.hexToBytes(n.msig);coinjs.pub=i,coinjs.priv=a,coinjs.multisig=c,coinjs.base32pref=n.pref,coinjs.compressed=n.comp,coinjs.txversion=n.txver,coinjs.txExtraTimeField=n.txtime,coinjs.shf=n.shf,coinjs.maxrbf=n.maxrbf},ychapi._get_txouts_selection=function(e,t){let n=[],i=0n,a=0n,c=0n,o=0n;if(e.forEach((function(e,t){o+=e.free})),o<t)return console.log("ych_select_txouts, free",o," is less than amount",t),[n,-1,-1,-1];for(;i<t;){for(let o=0;o<e.length;o++){let r=e[o];if(o+1==e.length){n.push(r),r.selected=r.free,i+=r.free,a+=r.orders,c+=r.filled,e=e.slice(0,o);break}if(i+r.free>=t){n.push(r),r.selected=r.free,i+=r.free,a+=r.orders,c+=r.filled,e.splice(o,1);break}if(i>=t)break}if(i<t&&0==e.length)return console.log("ych_select_txouts, no fit",i,t),[n,-1,-1,-1]}return i>t&&n.length>0&&(txout=n[0],txout.selected-=i-t),[n,i,a,c]},ychapi._sign_txout_t1=function(e){if(""==e.txid)return[null,"Txid is empty"];if(e.nout<0)return[null,"Nout is not valid"];const t=e.coin,n=ychapi._get_coin_holding_address(t);if(""==n)return[null,"Not change address"];const i=ychapi._get_coin_info(t);if(null==i)return[null,"No coininfo"];const a=i.fee.minamount;if("txout_t1"!=i.type)return[null,"Not supported"];if(""==e.addr.rdsc)return[null,"Redeem script is not valid"];let c=e.free-e.selected,o=e.amount-c;o<a&&(o=a),o>e.amount&&(o=e.amount),c=e.amount-o;let r=e.amount-e.filled-c;r<a&&(r=a),r>e.amount-e.filled&&(r=e.amount-e.filled),c=e.amount-e.filled-r;let s={};if(s.txid=e.txid,s.nout=e.nout,s.amnt=e.amount,s.fill=e.filled,s.usea=e.selected,c>=a){const t=ychapi._sign_txout_t1_multipos(e,n,c);return 0==t.length?[null,"Fail to sign change"]:(s.sigv=c,s.siga=n,s.sigs=t,s.sigf="",[s,null])}const u=ychapi._sign_txout_t1_full(e);return 0==u.length?[null,"Fail to sign txout"]:(s.sigv=0n,s.siga="",s.sigs=[],s.sigf=u,[s,null])},ychapi._sign_txout_t1_hash=function(e,t,n){const i=e.coin;if(""==e.txid)return[null,"Txid is empty"];if(e.nout<0)return[null,"Nout is not valid"];const a=ychapi._get_coin_info(i);if(null==a)return[null,"No coininfo "+i];if("txout_t1"==a.type){if(""==e.addr.rdsc)return[null,"Redeem script is not valid"];ychapi._init_coinjs(i);const a=1;let c=coinjs.transaction();const o=coinjs.privkey2wif(ychapi._get_user_signing_prvk()),r=c.transactionSig(t,o,a,Crypto.util.hexToBytes(n)),s="",u=0n;return[{txid:e.txid,nout:e.nout,amnt:e.amount,fill:e.filled,usea:e.selected,sigf:r,sign:s,sigv:u},null]}return[null,"Not supported coin "+i]},ychapi._sign_txout_t1_full=function(e){const t=e.coin;if(""==e.txid)return console.log("Txid is empty"),[];if(e.nout<0)return console.log("Nout is not valid"),[];const n=ychapi._get_coin_info(t);if(null==n)return console.log("No coininfo",t),[];if("txout_t1"==n.type){if(""==e.addr.rdsc)return console.log("Redeem script is not valid"),[];ychapi._init_coinjs(t);let n=null;coinjs.maxrbf>0&&(n=coinjs.maxrbf-1);const i=130;let a=coinjs.transaction();a.addinput(e.txid,e.nout,Crypto.util.hexToBytes(e.addr.rdsc),n,e.amount);let c=a.transactionHash(0,i);if(2==e.addr.vers){c=a.transactionHashSegWitV0(0,e.addr.rdsc,i,Number(e.amount)).hash}const o=coinjs.privkey2wif(ychapi._get_user_signing_prvk());return a.transactionSig(0,o,i,Crypto.util.hexToBytes(c))}return console.log("Not supported",t),[]},ychapi._sign_txout_t1_multipos=function(e,t,n){const i=e.coin;if(""==e.txid)return console.log("Txid is empty"),[];if(e.nout<0)return console.log("Nout is not valid"),[];if(""==t)return console.log("Change address is empty"),[];if("bigint"!=typeof n)return console.log("Change need bigint",change),[];const a=ychapi._get_coin_info(i);if(null==a)return console.log("No coininfo",i),[];if("txout_t1"==a.type){if(""==e.addr.rdsc)return console.log("Redeem script is not valid"),[];ychapi._init_coinjs(i);let a=null;coinjs.maxrbf>0&&(a=coinjs.maxrbf-1);let c=coinjs.privkey2wif(ychapi._get_user_signing_prvk()),o=[];const r="e".repeat(64);for(let i=0;i<ychapi._sigi_num;i++){const s=131;let u=coinjs.transaction();for(let e=0;e<i;e++){const e=0,n=[];u.addinput(r,e,Crypto.util.hexToBytes(n),null,1n),u.addoutput(t,1n)}u.addinput(e.txid,e.nout,Crypto.util.hexToBytes(e.addr.rdsc),a,e.amount),u.addoutput(t,n);let l=u.transactionHash(i,s);if(2==e.addr.vers){l=u.transactionHashSegWitV0(i,e.addr.rdsc,s,Number(e.amount)).hash}const _=u.transactionSig(i,c,s,Crypto.util.hexToBytes(l));o.push(_)}return o}return console.log("Not supported",i),[]},ychapi._evm_vault7u_abi=[{inputs:[{internalType:"address",name:"tss_addr",type:"address"},{internalType:"address",name:"weth_addr",type:"address"},{internalType:"address[]",name:"add_coins",type:"address[]"}],stateMutability:"nonpayable",type:"constructor"},{anonymous:!1,inputs:[{indexed:!0,internalType:"address",name:"coin",type:"address"},{indexed:!0,internalType:"address",name:"sender",type:"address"},{indexed:!0,internalType:"address",name:"user",type:"address"},{indexed:!1,internalType:"uint256",name:"amount",type:"uint256"},{indexed:!1,internalType:"bytes32",name:"state",type:"bytes32"},{indexed:!1,internalType:"uint256",name:"irev",type:"uint256"},{indexed:!1,internalType:"uint256",name:"orev",type:"uint256"}],name:"In",type:"event"},{anonymous:!1,inputs:[{indexed:!0,internalType:"address",name:"coin",type:"address"},{indexed:!0,internalType:"address",name:"user",type:"address"},{indexed:!1,internalType:"bytes32",name:"state1",type:"bytes32"},{indexed:!1,internalType:"bytes32",name:"state2",type:"bytes32"},{indexed:!1,internalType:"uint256",name:"irev",type:"uint256"},{indexed:!1,internalType:"uint256",name:"orev",type:"uint256"},{indexed:!1,internalType:"uint256",name:"balance",type:"uint256"},{indexed:!1,internalType:"uint256",name:"sigr",type:"uint256"},{indexed:!1,internalType:"uint256",name:"sigv",type:"uint256"}],name:"Op",type:"event"},{anonymous:!1,inputs:[{indexed:!0,internalType:"address",name:"sender",type:"address"},{indexed:!0,internalType:"address",name:"user",type:"address"},{indexed:!1,internalType:"bool",name:"reg",type:"bool"}],name:"Reg",type:"event"},{inputs:[{internalType:"address",name:"",type:"address"},{internalType:"address",name:"",type:"address"}],name:"balances",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"coin",type:"address"},{components:[{internalType:"address",name:"user",type:"address"},{internalType:"uint256",name:"usea",type:"uint256"},{internalType:"bytes",name:"sig1",type:"bytes"},{internalType:"uint256",name:"sigr",type:"uint256"},{internalType:"uint256",name:"sigv",type:"uint256"}],internalType:"struct Vault7.Use[]",name:"uses",type:"tuple[]"},{components:[{internalType:"address",name:"user",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"}],internalType:"struct Vault7.Fill[]",name:"fills",type:"tuple[]"},{internalType:"bytes",name:"sig2",type:"bytes"},{internalType:"uint256",name:"tw",type:"uint256"}],name:"clearance1",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"",type:"address"}],name:"coins",outputs:[{internalType:"bool",name:"",type:"bool"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"coin",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"address",name:"user",type:"address"},{internalType:"bytes",name:"sig1",type:"bytes"}],name:"deposit1",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"",type:"address"},{internalType:"address",name:"",type:"address"}],name:"irevs",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"addr",type:"address"},{internalType:"uint256",name:"min_amount",type:"uint256"},{internalType:"bool",name:"on",type:"bool"},{internalType:"bytes",name:"sig2",type:"bytes"},{internalType:"uint256",name:"tw",type:"uint256"}],name:"listCoin",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[],name:"locktime1",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"",type:"address"}],name:"locktime2",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"",type:"address"}],name:"minamount",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"",type:"address"},{internalType:"address",name:"",type:"address"}],name:"orevs",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"user",type:"address"},{internalType:"bytes",name:"sig1",type:"bytes"},{internalType:"uint256",name:"tw",type:"uint256"}],name:"register1",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"sender",type:"address"},{internalType:"address",name:"user",type:"address"},{internalType:"bool",name:"on",type:"bool"},{internalType:"bytes",name:"sig2",type:"bytes"},{internalType:"uint256",name:"tw",type:"uint256"}],name:"register2",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"bytes32",name:"",type:"bytes32"}],name:"sigr",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"bytes32",name:"",type:"bytes32"},{internalType:"uint256",name:"",type:"uint256"}],name:"sigv",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"",type:"address"},{internalType:"address",name:"",type:"address"}],name:"states",outputs:[{internalType:"bytes32",name:"",type:"bytes32"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"",type:"address"}],name:"totals",outputs:[{internalType:"uint256",name:"",type:"uint256"}],stateMutability:"view",type:"function"},{inputs:[],name:"unregister1",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"sender",type:"address"},{internalType:"address",name:"user",type:"address"},{internalType:"bytes",name:"sig1",type:"bytes"}],name:"unregister2",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"",type:"address"}],name:"users",outputs:[{internalType:"address",name:"",type:"address"}],stateMutability:"view",type:"function"},{inputs:[],name:"ustate",outputs:[{internalType:"bytes32",name:"",type:"bytes32"}],stateMutability:"view",type:"function"},{inputs:[{internalType:"address",name:"coin",type:"address"},{internalType:"address",name:"user",type:"address"},{internalType:"address",name:"dest",type:"address"},{components:[{internalType:"address",name:"user",type:"address"},{internalType:"uint256",name:"usea",type:"uint256"},{internalType:"bytes",name:"sig1",type:"bytes"},{internalType:"uint256",name:"sigr",type:"uint256"},{internalType:"uint256",name:"sigv",type:"uint256"}],internalType:"struct Vault7.Use[]",name:"uses",type:"tuple[]"},{internalType:"bytes",name:"sig2",type:"bytes"},{internalType:"uint256",name:"tw",type:"uint256"}],name:"withdraw1",outputs:[],stateMutability:"nonpayable",type:"function"},{inputs:[{internalType:"address",name:"coin",type:"address"},{internalType:"address",name:"dest",type:"address"},{internalType:"uint256",name:"amount",type:"uint256"},{internalType:"bytes",name:"sig2",type:"bytes"}],name:"withdraw3",outputs:[],stateMutability:"nonpayable",type:"function"},{stateMutability:"payable",type:"receive"}],ychapi._get_evm_vault7u_abi=function(){return ychapi._evm_vault7u_abi},ychapi._get_evm_vault7u_hash1=function(e,t,n,i,a){const c=_ethers.utils.arrayify(t),o=_ethers.utils.arrayify(e),r=_ethers.utils.arrayify(i),s=_ethers.utils.hexZeroPad(_ethers.utils.hexValue(n),32),u=_ethers.utils.arrayify(s),l=_ethers.utils.hexZeroPad(_ethers.utils.hexValue(a),32),_=_ethers.utils.arrayify(l),p=_ethers.utils.concat([o,c,u,r,_]),d=_ethers.utils.keccak256(p),y=_ethers.utils.arrayify(d),h=_ethers.utils.toUtf8Bytes("Ethereum Signed Message:\n32"),f=_ethers.utils.concat([h,y]);return _ethers.utils.keccak256(f)},ychapi._sign_evm_vault7u_user1=function(e,t,n,i,a){const c=ychapi._get_evm_vault7u_hash1(e,t,n,i,a),o=new _ethers.utils.SigningKey("0x"+ychapi._get_user_signing_prvk()).signDigest(c);return _ethers.utils.joinSignature(o)},ychapi._get_evm_vault7u_next_state1=function(e,t,n,i){const a=_ethers.utils.arrayify(e),c=_ethers.utils.arrayify(t),o=_ethers.utils.hexZeroPad(_ethers.utils.hexValue(n),32),r=_ethers.utils.arrayify(o),s=_ethers.utils.hexZeroPad(_ethers.utils.hexValue(i),32),u=_ethers.utils.arrayify(s),l=_ethers.utils.concat([a,c,r,u]);return _ethers.utils.keccak256(l)},ychapi._call_register=async function(e,t,n){return new Promise(((i,a)=>{const[c,o]=ychapi._get_user_login_keys_v1(e,t),r=n+e+o;ychapi._apicall({uri:"/u/register",uprvk:c,digest:r,data:{invt:n,user:e,upub:o}}).then((function(){i()})).catch((function(e){a(e)}))}))},ychapi._call_get_email_from_redeem=async function(e){return new Promise(((t,n)=>{ychapi._apicall({uri:"/u/redeem",data:{code:e}}).then((function(e){t(e.email)})).catch((function(e){n(e)}))}))},ychapi._call_get_registration_login=async function(e){return new Promise(((t,n)=>{ychapi._apicall({uri:"/u/confirmg",data:{code:e}}).then((function(e){t(e.login)})).catch((function(e){n(e)}))}))},ychapi._call_confirm_registration_login=async function(e,t,n){return new Promise(((i,a)=>{const[c,o]=ychapi._get_user_login_keys_v1(e,t),r=n+o;ychapi._apicall({uri:"/u/confirme",uprvk:c,digest:r,data:{code:n}}).then((function(e){i(e)})).catch((function(e){a(e)}))}))},ychapi._call_confirm_registration_2fa=async function(e,t,n,i){return new Promise(((a,c)=>{const[o,r]=ychapi._get_user_login_keys_v1(e,t),s=n+i+r;ychapi._apicall({uri:"/u/confirma",uprvk:o,digest:s,data:{code:n,auth:i}}).then((function(e){a(e)})).catch((function(e){c(e)}))}))},ychapi._ready_data=!1,ychapi._call_init=async function(){return new Promise(((e,t)=>{ychapi._apicall({calltype:"GET",uri:"/u/init",data:{}}).then((function(t){ychapi._on_data_call_init(t),ychapi._ready_init_data=!0,e(t)})).catch((function(e){ychapi._ready_init_data=!1,console.log("apicall err:",e),console.log("apicall err, logout"),ychapi._callbacks.on_sync_connection_error(e),t(e)}))}))},ychapi._call_login=async function(e,t,n){return new Promise(((i,a)=>{const[c,o]=ychapi._get_user_login_keys_v1(e,t),r=e+o+n;ychapi._apicall({uri:"/u/init",uprvk:c,digest:r,data:{user:e,upub:o,code:n}}).then((function(e){if(!e.access)return void a("notoken");if("unconfirmed"==e.access)return void a("unconfirmed");document.cookie="jwt="+e.access+";SameSite=Strict",window.location.hash="";""!=ychapi._get_jwt_uid()?(ychapi._set_user_login_keys(o,c),ychapi._on_data_call_init(e),i()):a("notoken")})).catch((function(e){a(e)}))}))},ychapi._call_logout=function(){document.cookie="jwt=; expires=Thu, 01 Jan 1970 00:00:01 GMT;SameSite=Strict",ychapi._set_user_login_keys("",""),ychapi._data.profile=void 0,ychapi._callbacks.on_sync_data_update("logout",{}),ychapi._do_ws_init()},ychapi._on_data_call_init=function(e){ychapi._data=e,ychapi._nnum=e.nnum;for(const e in ychapi._data.coininfos)ychapi._callbacks.on_sync_data_update("coininfo",ychapi._data.coininfos[e]);if(null!=e.profile){ychapi._pubkey1=e.profile.pubk1,ychapi._pubkey2=e.profile.pubk2,ychapi._pubkey3=e.profile.pubk3;e.profile.assets.forEach((function(e){const t=e.coin;ychapi._addresses[t]=e.address,ychapi._locktimes1[t]=e.locktime1,ychapi._locktimes2[t]=e.locktime2,null!=e.extra&&(ychapi._asset_extra_info[t]=e.extra)})),ychapi._callbacks.on_sync_data_update("login",{})}else ychapi._call_logout();ychapi._ready_data=!0,ychapi._callbacks.on_sync_data_init(),ychapi._do_ws_init()},ychapi._get_coin_names=function(){return ychapi._data.coins},ychapi._get_coin_info=function(e){return e in ychapi._data.coininfos?ychapi._data.coininfos[e]:null},ychapi._get_coin_balance=function(e){return null==ychapi._data.profile?null:e in ychapi._data.profile.balances?ychapi._data.profile.balances[e]:null},ychapi._get_coin_usd_rate=function(e){const t=ychapi._get_coin_info(e);return null==t?0:t.ext.priceext},ychapi._get_coin_deposits=function(e){return null==ychapi._data.profile?[]:e in ychapi._data.profile.deposits?ychapi._data.profile.deposits[e]:[]},ychapi._get_coin_withdrawals=function(e){return null==ychapi._data.profile?[]:e in ychapi._data.profile.withdraws?ychapi._data.profile.withdraws[e]:[]},ychapi._get_coin_txouts=function(e){return null==ychapi._data.profile?[]:e in ychapi._data.profile.txouts?ychapi._data.profile.txouts[e]:[]},ychapi._get_markets_groups_names=function(){let e=[];return ychapi._data.groups.forEach((function(t,n){e.push(t.name)})),e},ychapi._get_market_group=function(e){for(let t in ychapi._data.groups){const n=ychapi._data.groups[t];if(n.name==e)return n}return null},ychapi._get_market_group_by_coinb=function(e){for(let t in ychapi._data.groups){const n=ychapi._data.groups[t];if(n.coinb==e)return n}return null},ychapi._get_markets_names_in_group=function(e){const t=ychapi._get_market_group(e);if(null==t)return[];let n=[];for(let e in ychapi._data.markets)n.push(ychapi._data.markets[e]);n.sort((function(e,t){return Number(e.index-t.index)}));let i=[];return n.forEach((function(e,n){e.coinb==t.coinb&&i.push(e.name)})),i},ychapi._get_markets_names=function(){let e=[];for(let t in ychapi._data.markets)e.push(ychapi._data.markets[t]);e.sort((function(e,t){return Number(e.index-t.index)}));let t=[];return e.forEach((function(e,n){t.push(e.name)})),t},ychapi._get_market_info=function(e){return e in ychapi._data.markets?ychapi._data.markets[e]:null},ychapi._get_user_account_id=function(){return null==ychapi._data.profile?"":ychapi._data.profile.uidx},ychapi._get_user_account_login=function(){return null==ychapi._data.profile?"":ychapi._data.profile.user},ychapi._get_user_account_twofa=function(){return null!=ychapi._data.profile&&ychapi._data.profile.twofa},ychapi._get_user_trading_discount=function(){return null==ychapi._data.profile?0:ychapi._data.profile.discount},ychapi._get_user_rewards=function(e){return null==ychapi._data.profile?0n:e in ychapi._data.profile.rewards?ychapi._data.profile.rewards[e]:0n},ychapi._get_user_referral_invitation_code=function(){return null==ychapi._data.profile?"":ychapi._data.profile.invt},ychapi._get_user_referral_invitation_title=function(){return null==ychapi._data.profile?"":ychapi._data.profile.title},ychapi._get_user_trades=function(e){return null==ychapi._data.profile||null==ychapi._data.profile.trades?[]:e in ychapi._data.profile.trades?ychapi._data.profile.trades[e]:[]},ychapi._get_user_buys=function(e){return null==ychapi._data.profile||null==ychapi._data.profile.buys?[]:e in ychapi._data.profile.buys?ychapi._data.profile.buys[e]:[]},ychapi._get_user_sells=function(e){return null==ychapi._data.profile||null==ychapi._data.profile.sells?[]:e in ychapi._data.profile.sells?ychapi._data.profile.sells[e]:[]},ychapi._get_market_trades=function(e){return null==ychapi._data.trades?[]:e in ychapi._data.trades?ychapi._data.trades[e]:[]},ychapi._get_market_buys=function(e){return null==ychapi._data.buys?[]:e in ychapi._data.buys?ychapi._data.buys[e]:[]},ychapi._get_market_sells=function(e){return null==ychapi._data.sells?[]:e in ychapi._data.sells?ychapi._data.sells[e]:[]},ychapi._get_buy_fee=function(e,t,n){const i=ychapi._get_user_trading_discount(),a=ychapi._get_coin_info(n).fee.buyfee*(100-i)/100;if(null==ychapi._data.markets)return a;if(!e in ychapi._data.markets)return a;const c=ychapi._data.markets[e];return c.fee.over?c.fee.buyfee:a},ychapi._get_sell_fee=function(e,t,n){const i=ychapi._get_user_trading_discount(),a=ychapi._get_coin_info(t).fee.sellfee*(100-i)/100;if(null==ychapi._data.markets)return a;if(!e in ychapi._data.markets)return a;const c=ychapi._data.markets[e];return c.fee.over?c.fee.sellfee:a},ychapi._is_logged_in=function(){const e=ychapi._get_cookie("jwt").split(".");if(3==e.length){const t=e[1].replace("-","+").replace("_","/");if("uid"in JSON.parse(window.atob(t)))return!0}return!1},ychapi._has_profile=function(){return null!=ychapi._data.profile&&null!=ychapi._data.profile},ychapi._call_send=function(e,t,n,i){return new Promise(((a,c)=>{if(!ychapi._has_user_login_prvk())return void c("no login key to sign");if(""==ychapi._get_coin_holding_address(e))return void c("Not available the change address");const o=ychapi._get_coin_info(e),r=o.fee.sendfee;if(BigInt(Math.floor(.5+Number(n)*r))!=i)return void c("Fee calculation error");const s=n+i;let u=0n,l=s;const _=ychapi._get_coin_balance(e);if(s>_.free)return void c("Not enough coins");if(_.debit>0){let e=_.debit-_.ordersindebit;if(e<0)return void c("Error debt usage is negative");e>=s?(u=s,l=0n):e>0&&(u=e,l=s-e)}let p=[];if(l>0){console.log("total_via_txouts",l);let t=JSON.parse(JSON.stringify(ychapi._get_coin_txouts(e),((e,t)=>"bigint"==typeof t?t.toString()+"n":t)),jsonBNparse);ychapi._init_coinjs(e);const n=t.filter((e=>350==e.state||400==e.state||800==e.state));if(t=t.filter((e=>400==e.state)),t.sort((function(e,t){return Number(e.free-t.free)})),"txout_t1"==o.type){const[e,n,i,a]=ychapi._get_txouts_selection(t,l);if(n<0n||n<l)return void c("send: not enough available coins in txouts to use "+l);console.log("selected txouts:",e,n,i,a);for(let t=0;t<e.length;t++){const n=e[t],[i,a]=ychapi._sign_txout_t1(n);if(null!=a)return void c(a);p.push(i)}}if("evm_t1"==o.type||"erc20_t1"==o.type){const t=o.cfg.unit,i=l;for(let a=0;a<n.length;a++){const c=n[a],o="0x"+c.txid,r=c.orders+c.filled+i,s=r*t,u=[ychapi._sign_evm_vault7u_user1(ychapi._get_coin_holding_address(e),o,s,ychcore.evm_zeroaddr,c.nout+1)];let l={};l.txid=c.txid,l.nout=c.nout,l.amnt=c.amount,l.fill=c.filled,l.usea=i,l.sigf="",l.sigv=r,l.sigs=u,p.push(l)}}}let d="";p.forEach((function(e,t){d+=e.txid+":"+e.nout,d+=e.fill.toString(),d+=e.usea.toString()}));const y=e+t+n.toString()+i.toString()+u.toString()+d+ychapi._get_user_login_pubk();ychapi._apicall({uri:"/u/send",digest:y,data:{coin:e,addr:t,amount:n,fee:i,usedebit:u,usetxouts:p}}).then((function(){a()})).catch((function(e){c(e)}))}))},ychapi._call_buy=function(e,t,n,i,a,c,o){return new Promise(((r,s)=>{if(!ychapi._has_user_login_prvk())return void s("no login key to sign");if(""==ychapi._get_coin_holding_address(n))return void s("not available the change address");const u=ychapi._get_coin_info(n),l=ychapi._get_buy_fee(e,t,n);if(a*i/BigInt(1e8)!=c)return void s("amountb calculation error");if(BigInt(Math.floor(.5+Number(c)*l))!=o)return void s("feeb calculation error");const _=c+o,p=ychapi._get_coin_balance(n);let d=0n,y=_;if(p.debit>0){let e=p.debit-p.ordersindebit;if(e<0)return void s("error debt usage is negative");e>=_?(d=_,y=0):e>0&&(d=e,y=_-e)}let h=[];if(y>0){let e=JSON.parse(JSON.stringify(ychapi._get_coin_txouts(n),((e,t)=>"bigint"==typeof t?t.toString()+"n":t)),jsonBNparse);ychapi._init_coinjs(n);const t=e.filter((e=>350==e.state||400==e.state||800==e.state));if(e=e.filter((e=>400==e.state)),e.sort((function(e,t){return Number(e.free-t.free)})),"txout_t1"==u.type){const[t,n]=ychapi._get_txouts_selection(e,y);if(n<0||n<y)return void s("not enough available coins in txouts to trade "+y);for(let e=0;e<t.length;e++){const n=t[e],[i,a]=ychapi._sign_txout_t1(n);if(null!=a)return void s(a);h.push(i)}}if("evm_t1"==u.type||"erc20_t1"==u.type){const e=u.cfg.unit,i=y;for(let a=0;a<t.length;a++){const c=t[a],o="0x"+c.txid,r=c.orders+c.filled+i,s=r*e,u=[ychapi._sign_evm_vault7u_user1(ychapi._get_coin_holding_address(n),o,s,ychcore.evm_zeroaddr,c.nout+1)],l={txid:c.txid,nout:c.nout,amnt:c.amount,fill:c.filled,usea:i,sigf:"",sigv:r,sigs:u};h.push(l)}}}let f="";h.forEach((function(e){f+=e.txid+":"+e.nout,f+=e.fill.toString(),f+=e.usea.toString()}));const g=t+n+i.toString()+a.toString()+c.toString()+"0"+o.toString()+d.toString()+f+ychapi._get_user_login_pubk();ychapi._apicall({uri:"/u/buy",digest:g,data:{coina:t,coinb:n,price:i,amounta:a,amountb:c,feea:0n,feeb:o,usedebit:d,usetxouts:h}}).then((function(){r()})).catch((function(e){s(e)}))}))},ychapi._call_buy_order_cancel=function(e,t,n){return new Promise(((i,a)=>{const c=e+t+n.toString()+ychapi._get_user_login_pubk();ychapi._apicall({uri:"/u/nobuy",digest:c,data:{coina:e,coinb:t,index:n}}).then((function(){i()})).catch((function(e){a(e)}))}))},ychapi._call_sell=function(e,t,n,i,a,c,o){return new Promise(((r,s)=>{if(!ychapi._has_user_login_prvk())return void s("no login key to sign");if(""==ychapi._get_coin_holding_address(t))return void s("not available the change address");const u=ychapi._get_coin_info(t),l=ychapi._get_sell_fee(e,t,n);if(a*i/BigInt(1e8)!=c)return void s("amountb calculation error");if(BigInt(Math.floor(.5+Number(a)*l))!=o)return void s("feea calculation error");const _=a+o,p=ychapi._get_coin_balance(t);let d=0n,y=_;if(p.debit>0){let e=p.debit-p.ordersindebit;if(e<0)return void s("error debt usage is negative");e>=_?(d=_,y=0n):e>0&&(d=e,y=_-e)}let h=[];if(y>0){let e=JSON.parse(JSON.stringify(ychapi._get_coin_txouts(t),((e,t)=>"bigint"==typeof t?t.toString()+"n":t)),jsonBNparse);ychapi._init_coinjs(t);const n=e.filter((e=>350==e.state||400==e.state||800==e.state));if(e=e.filter((e=>400==e.state)),e.sort((function(e,t){return Number(e.free-t.free)})),"txout_t1"==u.type){const[t,n,i,a]=ychapi._get_txouts_selection(e,y);if(n<0||n<y){const e="not enough available coins in txouts to trade "+y;return page.show_error_sell(e),void s(e)}for(let e=0;e<t.length;e++){const n=t[e],[i,a]=ychapi._sign_txout_t1(n);if(null!=a)return page.show_error_sell(a),void s(a);h.push(i)}}if("evm_t1"==u.type||"erc20_t1"==u.type){const e=u.cfg.unit,i=y;for(let a=0;a<n.length;a++){const c=n[a],o="0x"+c.txid,r=c.orders+c.filled+i,s=r*e,u=[ychapi._sign_evm_vault7u_user1(ychapi._get_coin_holding_address(t),o,s,ychcore.evm_zeroaddr,c.nout+1)],l={txid:c.txid,nout:c.nout,amnt:c.amount,fill:c.filled,usea:i,sigf:"",sigv:r,sigs:u};h.push(l)}}}let f="";h.forEach((function(e,t){f+=e.txid+":"+e.nout,f+=e.fill.toString(),f+=e.usea.toString()}));const g=t+n+i.toString()+a.toString()+c.toString()+o.toString()+"0"+d.toString()+f+ychapi._get_user_login_pubk();ychapi._apicall({uri:"/u/sell",digest:g,data:{coina:t,coinb:n,price:i,amounta:a,amountb:c,feea:o,feeb:0n,usedebit:d,usetxouts:h}}).then((function(){r()})).catch((function(e){s(e)}))}))},ychapi._call_sell_order_cancel=function(e,t,n){return new Promise(((i,a)=>{const c=e+t+n.toString()+ychapi._get_user_login_pubk();ychapi._apicall({uri:"/u/nosell",digest:c,data:{coina:e,coinb:t,index:n}}).then((function(){i()})).catch((function(e){a(e)}))}))},ychapi._call_get_chart_data=async function(e,t){return new Promise(((n,i)=>{ychapi._apicall({calltype:"GETRAW",uri:"/u/chart/"+e+"/"+t,data:{}}).then((function(e){n(e)})).catch((function(e){i(e)}))}))},ychapi._validate_address=function(e,t){if(""==t)return"Address is empty";const n=ychapi._get_coin_info(e);if("txout_t1"==n.type){ychapi._init_coinjs(e);try{let e=coinjs.addressDecode(t);if(0==e)return"Address is not recognized(1)";if("other"==e.type)return"Address is not recognized(2)"}catch(e){return"Address is not recognized(3)"}}if("peg_t1"==n.type){ychapi._init_coinjs(part.coin);try{let e=coinjs.addressDecode(t);if(0==e)return"Address is not recognized(1)";if("bech32"==e.type)return"Address type bech32 is not supported";if("other"==e.type)return"Address is not recognized(2)"}catch(e){return"Address is not recognized(3)"}}return"evm_t1"!=n.type&&"erc20_t1"!=n.type||ychcore.is_valid_evm_address(t)?null:"Address is not recognized"},ychapi._has_coin_holding_address=function(e){return null!=ychapi._data.profile&&(e in ychapi._addresses&&""!=ychapi._addresses[e])},ychapi._get_coin_holding_address=function(e){return null==ychapi._data.profile?"":e in ychapi._addresses?ychapi._addresses[e]:""},ychapi._get_coin_credit_address=function(e){if("txout_t1"!=ychapi._get_coin_info(e).type)return"";if(null==ychapi._data.profile)return"";const t=ychapi._get_credit_signing_pubk();if(null==t)return"";const n=ychapi._get_server_signing_pubk();if(null==n)return"";return coinjs.pubkeys2MultisigAddressWithBackup(t,n,0,0).address},ychapi._call_register_deposit_evm_address=function(e,t,n){return new Promise(((i,a)=>{const c="register1",o=e+c+ychapi._get_coin_holding_address(e)+t.toString()+n+ychapi._get_user_login_pubk();ychapi._apicall({uri:"/u/address",digest:o,data:{coin:e,call:c,user:ychapi._get_coin_holding_address(e),blck:t,sender:n}}).then((function(e){i()})).catch((function(e){a(e)}))}))},ychapi._call_unregister_deposit_evm_address=function(e,t,n){return new Promise(((i,a)=>{const c="unregister2",o=part.coin+c+ychapi._get_coin_holding_address(part.coin)+t.toString()+account_addr+ychapi._get_user_login_pubk();ychapi._apicall({uri:"/u/address",digest:o,data:{coin:e,call:c,user:ychapi._get_coin_holding_address(e),blck:t,sender:n}}).then((function(e){i()})).catch((function(e){a(e)}))}))},ychapi._create_withdraw=function(e,t){let n={};n.coin=e,n.scope=t,n.sendfee=0n,n.datatag=0n,n.mode="reduce",n.last_error=null;return"txout_t1"==ychapi._get_coin_info(e).type&&(n.datatag=1n),n.op1=ychapi._create_withdraw_op(),n.op2=ychapi._create_withdraw_op(),n.op3=ychapi._create_withdraw_op(),n.get_available_amount=function(){let e=0n;return n.op1.txouts.forEach((function(t){e+=t.free})),e},n.get_sendfee=function(){return n.sendfee},n.get_output_datatag_amount=function(){return 0n==n.op2.amount_out?0n:n.datatag},n.set_txouts_user=function(e){e.txouts_user=JSON.parse(JSON.stringify(ychapi._get_coin_txouts(n.coin),((e,t)=>"bigint"==typeof t?t.toString()+"n":t)),jsonBNparse),e.txouts_user=e.txouts_user.filter((e=>400==e.state&&"allocated"!=n.scope||500==e.state&&"allocated"==n.scope)),e.txouts_user.sort((function(e,t){return Number(t.free-e.free)})),e.txouts=e.txouts_user},n.set_txouts_debit=function(e){if(e.txouts_debit=[],"allocated"==n.scope)return;const t=ychapi._get_coin_balance(n.coin);if(null==t)return;if(t.debit<=0)return;const i=ychapi._get_coin_info(n.coin);if("peg_t1"!=i.type&&"evm_t1"!=i.type&&"txout_t1"!=i.type&&"erc20_t1"!=i.type)return;0n!=t.debit-t.ordersindebit&&(e.txouts_debit=[ychapi._create_virtual_debit_txout(n.coin)],e.txouts=e.txouts.concat(e.txouts_debit))},n.select_txouts2=function(){if("reduce"==n.mode){let e=0n;n.op2.txouts.forEach((function(t){e+=t.free})),e<n.op2.amount_inp&&(n.op2.amount_inp=e)}let e=n.op2.select2(n.op2.amount_inp);return null!=e?e:("reduce"==n.mode?n.op2.reduce(n.get_output_datatag_amount()):n.op2.change-=n.get_output_datatag_amount(),null)},n.stabilize_op=function(e){n.trace("stabilize_op",e);let t=0n;const i=ychapi._get_coin_info(n.coin);"txout_t1"==i.type&&(t=i.fee.minamount);let a=e.stabilize(i,t);return n.trace("stabilize_op end",e),a},n.trace=function(e,t){},n},ychapi._update_withdraw=function(e){return e.set_txouts_user(e.op1),e.set_txouts_debit(e.op1),e.op1.make_id2idx(),e.set_txouts_user(e.op2),e.set_txouts_debit(e.op2),e.op2.make_id2idx(),err=e.last_error=ychapi._compute_withdraw_datas(e),err},ychapi._set_withdraw_amount=function(e,t,n){e.op1.amount_inp=t,e.op2.amount_inp=t;const i=Number(e.op1.amount_inp)/1e8,a=ychapi._get_coin_info(e.coin);return null==a?"Coin is not supported":(e.sendfee=BigInt(Math.floor(.5+i*a.fee.withfee*1e8)),"allocated"==e.scope&&(e.sendfee=0n),e.mode=n,"manual"==e.mode&&(e.op2.reduce_mode=!1,e.op3.reduce_mode=!0),"reduce"==e.mode&&(e.op2.reduce_mode=!0,e.op3.reduce_mode=!0),err=e.last_error=ychapi._compute_withdraw_datas(e),err)},ychapi._set_withdraw_inputs_for_debit=function(e,t){e.set_txouts_user(e.op3),e.op3.txouts_debit=t,e.op3.txouts=e.op3.txouts.concat(e.op3.txouts_debit),e.op3.make_id2idx();let n=e.op3.select3(e.op2.amount_out);return null!=n?n:(e.op3.change>0n?e.op3.change-=e.get_output_datatag_amount():e.op3.reduce(e.get_output_datatag_amount()),n=e.stabilize_op(e.op3),null!=n?n:null)},ychapi._compute_withdraw_datas=function(e){let t=e.last_error=e.op1.select1(e.get_sendfee(),e.op2);if(null!=t)return t;if(t=e.last_error=e.select_txouts2(),null!=t)return t;if(t=e.last_error=e.stabilize_op(e.op2),null!=t)return t;if(t=e.last_error=ychapi._validate_address(e.coin,ychapi._get_coin_holding_address(e.coin)),null!=t)return t;const n=ychapi._get_coin_info(e.coin);if(null==n)return"Coin is not supported";if("txout_t1"==n.type){const n=ychapi._get_coin_credit_address(e.coin);if(""==n)return"Credit address is not available";if(t=e.last_error=ychapi._validate_address(e.coin,n),null!=t)return t}return t=e.last_error=ychapi._validate_withdraw_inputs_outputs(e),null!=t?t:null},ychapi._build_txout_t1_tx=function(e,t,n){if("txout_t1"!=ychapi._get_coin_info(e).type)return null;ychapi._init_coinjs(e);let i=null;coinjs.maxrbf>0&&(i=coinjs.maxrbf-1);let a=coinjs.transaction();for(let e=0;e<t.selected.length;e++){let n=t.selected[e],c=coinjs.pubkeys2MultisigAddressWithBackup(n.addr.pub1,n.addr.pub2,n.addr.lck1,n.addr.lck2),o=i;2==n.hold&&(o=n.nseq),a.addinput(n.txid,n.nout,Crypto.util.hexToBytes(c.redeemScript),o,n.amount)}t.amount_out>0n&&a.addoutput(n,t.amount_out),t.change>0n&&a.addoutput(ychapi._get_coin_holding_address(e),t.change),t.credit>0n&&a.addoutput(ychapi._get_coin_credit_address(e),t.credit);let c=[];t.selected.forEach((function(e){let t=Crypto.util.hexToBytes(e.txid).reverse();for(let e=0;e<t.length;e++)c.push(t[e]);let n=coinjs.numToBytes(e.nout);for(let e=0;e<n.length;e++)c.push(n[e])})),c.push("L".charCodeAt(0));for(let e=0;e<n.length;e++)c.push(n.charCodeAt(e));let o=coinjs.numToBytes(Number(t.amount_out));for(let e=0;e<o.length;e++)c.push(o[e]);var r=Crypto.SHA256(Crypto.SHA256(c,{asBytes:!0}),{asBytes:!0});let s="XCH:0:"+Crypto.util.bytesToHex(r.reverse());return a.adddata(Crypto.util.bytesToHex(ychapi._str2bytes(s)),1),a},ychapi._get_withdraw_rawtx=function(e){const t=ychapi._build_txout_t1_tx(e.coin,e.op2,e.addr_send);return null==t?null:t.serialize()},ychapi._validate_withdraw_inputs_outputs=function(e){let t=0n;e.op2.selected.forEach((function(e){t+=e.amount}));let n=e.op2.amount_out+e.op2.change+e.op2.credit+e.get_output_datatag_amount()+e.op2.netfee;return t!=n?"Can not configure inputs(6), change the quantity":null},ychapi._create_virtual_debit_txout=function(e){let t=0n,n=0n;const i=ychapi._get_coin_balance(e);null!=i&&(t=i.debit,n=i.ordersindebit);let a={gidx:1,cidx:1,coin:e,hold:2,state:500,addr:{vers:1,host:"",addr:ychapi._get_coin_holding_address(e),pub1:ychapi._get_credit_signing_pubk(),pub2:ychapi._get_server_signing_pubk(),lck1:0,lck2:0,pksc:"",rdsc:"00"},txid:ychcore.zerotxid,nout:0,free:t-n,orders:0n,amount:t-n,filled:0n,selected:0n};return"peg_t1"==ychapi._get_coin_info(e).type&&(a.amount=t,a.orders=0n,a.filled=n),a},ychapi._has_withdraw_inputs_stage=function(e){return ychapi._has_withdraw_op2_inputs_stage(e.coin,e.op2)},ychapi._has_withdraw_op2_inputs_stage=function(e,t){if("txout_t1"!=ychapi._get_coin_info(e).type)return!1;for(let e=0;e<t.selected.length;e++){if(t.selected[e].txid==ychcore.zerotxid)return!0}return!1},ychapi._prepare_withdraw_call_payload=function(e,t,n,i,a){const c=ychapi._get_coin_info(e);ychapi._init_coinjs(e);const o=Math.floor((new Date).getTime()/1e3)+c.cfg.tw,r=BigInt(o);let s=[],u=[],l="",_="";if("txout_t1"==c.type){for(let e=0;e<i.selected.length;e++){const t=i.selected[e];l+=t.txid+":"+t.nout,l+=t.filled.toString(),l+=t.selected.toString();const[n,a]=ychapi._sign_txout_t1(t);if(null!=a)return[null,null,a];s.push(n)}const n=ychapi._build_txout_t1_tx(e,a,t);console.log("op2 tx:",n);for(let e=0;e<a.selected.length;e++){const t=a.selected[e];_+=t.txid+":"+t.nout,_+=t.filled.toString(),_+=t.selected.toString();const i=1;let c=n.transactionHash(e,i);if(2==t.addr.vers){c=n.transactionHashSegWitV0(e,t.addr.rdsc,i,Number(t.amount)).hash}const[o,r]=ychapi._sign_txout_t1_hash(t,e,c);if(null!=r)return[null,null,r];u.push(o)}}if("peg_t1"==c.type)for(let e=0;e<a.selected.length;e++){const t=a.selected[e];_+=t.txid+":"+t.nout,_+=t.filled.toString(),_+=t.selected.toString();const n="",i="",c=0n,o={txid:t.txid,nout:t.nout,amnt:t.amount,fill:t.filled,usea:t.selected,sigf:n,sign:i,sigv:c};u.push(o)}if("evm_t1"==c.type||"erc20_t1"==c.type){const n=c.cfg.unit;for(let t=0;t<i.selected.length;t++){const a=i.selected[t];l+=a.txid+":"+a.nout,l+=a.filled.toString(),l+=a.selected.toString();const c="0x"+a.txid,o=a.orders+a.filled+a.selected,r=o*n,u=[ychapi._sign_evm_vault7u_user1(ychapi._get_coin_holding_address(e),c,r,ychcore.evm_zeroaddr,a.nout+1)],_={txid:a.txid,nout:a.nout,amnt:a.amount,fill:a.filled,usea:a.selected,sigf:"",sigv:o,sigs:u};s.push(_)}let p=0,d=0;if(a.selected.forEach((function(e,t){e.txid==ychcore.zerotxid?d++:p++})),p>1){return[null,null,"More than one state: unfinished chain operations, try later"]}if(d>1){return[null,null,"More than one debit, error"]}a.selected.forEach((function(i,a){_+=i.txid+":"+i.nout,_+=i.filled.toString(),_+=i.selected.toString();let c="",s="",l=0n;if(i.txid==ychcore.zerotxid);else{l=i.selected;const a=i.selected*n,u="0x"+i.txid;c=ychapi._sign_evm_vault7u_user1(ychapi._get_coin_holding_address(e),u,a,t,o);const _=(0,ychapi._get_evm_vault7u_next_state1)(u,t,a,r),p=(i.orders+i.filled)*n;s=ychapi._sign_evm_vault7u_user1(ychapi._get_coin_holding_address(e),_,p,ychcore.evm_zeroaddr,i.nout+1)}const p={txid:i.txid,nout:i.nout,amnt:i.amount,fill:i.filled,usea:i.selected,sigf:c,sign:s,sigv:l,sigs:[]};u.push(p)}))}const p=ychapi._get_coin_holding_address(e),d=ychapi._get_coin_credit_address(e);return[e+t+d+p+a.amount_out.toString()+a.debit.toString()+a.credit.toString()+a.change.toString()+a.netfee.toString()+n.toString()+l+_+ychapi._get_user_login_pubk(),{coin:e,addr_send:t,addr_credit:d,addr_change:p,value_send:a.amount_out,value_debit:a.debit,value_credit:a.credit,value_change:a.change,value_netfee:a.netfee,value_sendfee:n,usetxouts1:s,usetxouts2:u,twin:r},null]},ychapi._call_withdraw_inputs_stage=function(e){return new Promise(((t,n)=>{let i=!1;for(let t=0;t<e.op2.selected.length;t++){if(e.op2.selected[t].txid==ychcore.zerotxid){i=!0;break}}if(!i)throw new Error("No debit");if("txout_t1"!=ychapi._get_coin_info(e.coin).type)throw new Error("Invalid coin type");const[a,c,o]=ychapi._prepare_withdraw_call_payload(e.coin,e.addr_send,e.get_sendfee(),e.op1,e.op2);if(null!=o)throw o;ychapi._apicall({uri:"/u/withdraw",digest:a,data:c}).then((function(e){if(!("step"in e))throw new Error("Unexpected response without step");if("txouts"!=e.step)throw new Error("Unexpected response without txouts step: "+e.step);t(e.txouts)})).catch((function(e){n(e)}))}))},ychapi._call_withdraw=function(e,t,n,i,a){return new Promise(((c,o)=>{if(ychapi._has_withdraw_op2_inputs_stage(e,a))return void o("Debit is not filled");const[r,s,u]=ychapi._prepare_withdraw_call_payload(e,t,n,i,a);null==u?ychapi._apicall({uri:"/u/withdraw",digest:r,data:s}).then((function(e){c(e)})).catch((function(e){o(e)})):o(u)}))},ychapi._call_withdraw_report_txid=function(e,t,n){return new Promise(((i,a)=>{ychapi._apicall({uri:"/u/withdraw_evm_txid",data:{coin:e,gidx:t,txid:n}}).then((function(e){i()})).catch((function(e){a(e)}))}))},ychapi._call_withdraw_cancel=function(e,t){return new Promise(((n,i)=>{const a=withdraw.coin+withdraw.gidx+ychapi._get_user_login_pubk();ychapi._apicall({uri:"/u/nowithdraw",digest:a,data:{coin:e,gidx:t}}).then((function(e){n()})).catch((function(e){i(e)}))}))},ychapi._create_withdraw_op=function(){let e={reduce_mode:!1,amount_inp:0n,amount_out:0n,txouts:[],txouts_user:[],txouts_debit:[],txout_id_to_idx:{},selected:[],unselected:[],netfee:0n,debit:0n,change:0n,credit:0n,num_inps:0,num_outs:0,rawtx:"",select1:function(t,n){if(e.selected=[],e.txouts.forEach((function(e){e.selected=0n})),1==e.txouts_debit.length){let n=e.txouts_debit[0];if(n.free>0n){let i=n.free;i>t&&(i=t),n.selected=i,e.selected.push(n),t-=i}}if(t>0n){let n=[...e.txouts_user];n.sort((function(e,t){return Number(e.free-t.free)}));{let e=0n;if(n.forEach((function(t,n){e+=t.free})),e<t)return console.log("txouts_free < amount",e,t),"Not enough available for withdraw"}let i=0n;for(;i<t;)for(let a=0;a<n.length;a++){let c=n[a];if(a+1==n.length){e.selected.push(c);let o=c.free;i+o>t&&(o=t-i),c.selected=o,i+=c.free,n=n.slice(0,a);break}if(i+c.free>=t){e.selected.push(c);let o=c.free;i+o>t&&(o=t-i),c.selected=o,i+=c.free,n.splice(a,1);break}if(i>=t)break}}return e.make_unselected(),e.use2credit(n),null},make_id2idx:function(){e.txout_id_to_idx={},e.txouts.forEach((function(t,n){const i=t.txid+":"+t.nout;e.txout_id_to_idx[i]=n}))},make_unselected:function(){let t={};e.selected.forEach((function(n){let i=n.txid+":"+n.nout;t[i]=!0,e.credit+=n.filled;const a=n.amount-n.selected-n.filled;e.change+=a})),e.unselected=[],e.txouts.forEach((function(n){n.txid+":"+n.nout in t||e.unselected.push(n)}))},use2credit:function(t){return e.selected.forEach((function(e,n){const i=e.txid+":"+e.nout;if(!(i in t.txout_id_to_idx))return;const a=t.txout_id_to_idx[i];let c=t.txouts[a];c.txid==ychcore.zerotxid?(c.free=e.free-e.selected,c.amount=e.amount-e.selected):(c.free=e.free-e.selected,c.filled=e.filled+e.selected)})),null},select2:function(t){e.selected=[],e.amount_inp=t,e.amount_out=t,e.debit=0n,e.credit=0n,e.change=0n,e.netfee=0n,e.txouts.forEach((function(e){e.selected=0n}));let n=[...e.txouts_user];n.sort((function(e,t){return Number(e.free-t.free)}));let i=0n,a=t,c=0n;n.forEach((function(e){c+=e.free})),a>c&&(i=a-c,a=c);let o=!1;if(i>0n){let t=i;if(1==e.txouts_debit.length){let n=e.txouts_debit[0];if(n.free>0n){let i=n.free;i>t&&(i=t),n.selected=i,e.selected.push(n),t-=i,o=!0}}if(t>0n)return"Not enough available for withdraw"}let r=a,s=0n;for(;s<r;){for(let t=0;t<n.length;t++){let i=n[t];if(t+1==n.length){e.selected.push(i);let a=i.free;s+a>r&&(a=r-s),i.selected=a,s+=i.free,n=n.slice(0,t);break}if(s+i.free>=r){e.selected.push(i);let a=i.free;s+a>r&&(a=r-s),i.selected=a,s+=i.free,n.splice(t,1);break}if(s>=r)break}if(0==n.length)break}return s<r?"Not enough available for withdraw":(e.make_unselected(),null)},select3:function(t){e.selected=[],e.amount_inp=t,e.amount_out=t,e.debit=0n,e.credit=0n,e.change=0n,e.netfee=0n,e.txouts.forEach((function(e){e.selected=0n}));let n=[...e.txouts_user];n.sort((function(e,t){return Number(e.free-t.free)}));let i=0n,a=t,c=0n;n.forEach((function(e){c+=e.free})),a>c&&(i=a-c,a=c);let o=a,r=0n;for(;r<o;){for(let t=0;t<n.length;t++){let i=n[t];if(t+1==n.length){e.selected.push(i);let a=i.free;r+a>o&&(a=o-r),i.selected=a,r+=i.free,n=n.slice(0,t);break}if(r+i.free>=o){e.selected.push(i);let a=i.free;r+a>o&&(a=o-r),i.selected=a,r+=i.free,n.splice(t,1);break}if(r>=o)break}if(0==n.length)break}if(r<o)return"Not enough available for withdraw";for(n=[...e.txouts_debit],n.sort((function(e,t){return Number(e.free-t.free)})),o=i,r=0n;r<o;){for(let t=0;t<n.length;t++){let i=n[t];if(t+1==n.length){e.selected.push(i);let a=i.free;r+a>o&&(a=o-r),i.selected=a,r+=i.free,n=n.slice(0,t);break}if(r+i.free>=o){e.selected.push(i);let a=i.free;r+a>o&&(a=o-r),i.selected=a,r+=i.free,n.splice(t,1);break}if(r>=o)break}if(0==n.length)break}return r<o?"Not enough available for withdraw":(e.make_unselected(),null)},add_more:function(t){let n=[],i=null;e.unselected.forEach((function(e){e.txid!=ychcore.zerotxid?n.push(e):i=e}));let a=[],c=0n;for(n.sort((function(e,t){return Number(e.amount-e.filled-(t.amount-t.filled))}));c<t;){for(let i=0;i<n.length;i++){let o=n[i],r=o.amount-o.filled;if(i+1==n.length){e.selected.push(o),a.push(o),o.selected=0n,c+=r,n=n.slice(0,i);break}if(c+r>=t){e.selected.push(o),a.push(o),o.selected=0n,c+=r,n.splice(i,1);break}if(c>=t)break}if(0==n.length)break}if(c<t&&null!=i){let t=i,n=t.amount-t.filled;e.selected.push(i),a.push(i),i.selected=0n,c+=n}if(c<t)return"Not enough available to select";let o={};return e.selected.forEach((function(e){let t=e.txid+":"+e.nout;o[t]=!0})),a.forEach((function(t){e.credit+=t.filled;const n=t.amount-t.selected-t.filled;e.change+=n})),e.unselected=[],e.txouts.forEach((function(t){t.txid+":"+t.nout in o||e.unselected.push(t)})),null},reduce:function(t){e.amount_out-=t;let n=e.selected;n.sort((function(e,t){return Number(e.selected-t.selected)}));for(let e=0;e<n.length;e++){let i=n[e],a=t;if(a>i.selected&&(a=i.selected),i.selected-=a,0n==(t-=a))break}},stabilize_changes:function(t){if(0n==t)return!0;if((0n==e.credit||e.credit>=t)&&(0n==e.change||e.change>=t))return!0;const n=e.change+e.credit;if(e.credit>0n&&t<=n&&n<t+t)return e.debit=e.change,e.credit=n,e.change=0n,!0;if(e.credit>0n&&e.credit<t&&n>=t+t){let n=t-e.credit;return e.debit+=n,e.credit+=n,e.change-=n,!0}return e.change>0n&&e.change<t&&n>=t+t&&(e.debit+=e.change,e.credit+=e.change,e.change=0n,!0)},stabilize_change:function(t,n){if(e.stabilize_changes(t))return null;let i=t-(e.change+e.credit),a=0n;if(e.unselected.forEach((function(e){a+=e.amount-e.filled})),i<=a){let n=e.add_more(i);if(null!=n)return n;if(e.stabilize_changes(t))return null}else if(n&&e.amount_out>i){e.reduce(i),e.change+=i;if(e.stabilize_changes(t))return null}return"Can not configure inputs(1), change the quantity"},compute_netfee:function(t){if(0n!=e.amount_inp)if("peg_t1"!=t.type)if("txout_t1"!=t.type)e.netfee=0n;else{const n=t.fee.txbytefee;e.num_inps=e.selected.length,e.num_outs=1,e.change>0n&&e.num_outs++,e.credit>0n&&e.num_outs++,e.num_outs++;let i=386*e.num_inps+34*(e.num_outs-1)+80;t.fee.txbyteround>1&&(i=Math.ceil(i/t.fee.txbyteround)*t.fee.txbyteround),e.netfee=BigInt(Math.floor(i*n*1e8+.5));const a=t.fee.mempoolminfee,c=BigInt(Math.floor(1e8*a+.5));e.netfee<c&&(e.netfee=c)}else e.netfee=10000000n;else e.netfee=0n},stabilize_netfee:function(t,n){let i=0n;"txout_t1"==t.type&&(i=t.fee.minamount);let a=0n,c=0n,o=0n;for(let r=0;r<10;r++){if(e.change+=a,e.credit+=c,e.debit+=o,a=0n,c=0n,o=0n,null!=e.stabilize_change(i,n))break;e.compute_netfee(t);let r=e.netfee,s=r;if(e.debit>0n&&e.credit>0n){let t=e.credit;if(t>e.debit&&(t=e.debit),t>s&&(t=s),e.credit-t>0n&&e.credit-t<i&&(t=e.credit-i),s-=t,c+=t,e.credit-=t,o+=t,e.debit-=t,0n==s)return null}if(e.change>0n&&e.change>=s){if(a+=s,e.change-=s,s=0n,null!=e.stabilize_change(i,n))break;if(e.compute_netfee(t),r!=e.netfee)continue;return null}e.change>0n&&e.change<s&&(a+=e.change,s-=e.change,e.change=0n);let u=0n;if(e.unselected.forEach((function(e,t){u+=e.amount-e.filled})),s<=u){if(e.add_more(s),a+=s,e.change-=s,s=0n,!e.stabilize_changes(i))break;if(e.compute_netfee(t),r!=e.netfee)continue;return null}if(n&&e.amount_out>s){if(e.reduce(s),s=0n,!e.stabilize_changes(i))break;if(e.compute_netfee(t),r!=e.netfee)continue;return null}}return"Can not configure inputs(3), change the quantity"},stabilize:function(t,n){let i=e.stabilize_change(n,e.reduce_mode);return null!=i?i:(i=e.stabilize_netfee(t,e.reduce_mode),null!=i?i:null)}};return e},ychapi._ready_wsdata=!1,ychapi._timer_reauth_duration=3e5,ychapi._ws_callbacks={},ychapi._start_ws=function(){ychapi._ready_wsdata=!1;let e="ws:";"https:"==window.location.protocol&&(e="wss:");const t=e+"//"+window.location.hostname+":"+window.location.port+"/ws";console.log("Connecting to ws: "+t),ychapi._websocket=new ReconnectingWebSocket(t),ychapi._websocket.onopen=ychapi._on_ws_onopen,ychapi._websocket.onclose=ychapi._on_ws_onclose,ychapi._websocket.onmessage=ychapi._on_ws_onmessage},ychapi._on_ws_onclose=function(e){ychapi._ready_wsdata=!1,ychapi._callbacks.on_sync_connection_lost()},ychapi._on_ws_onopen=function(e){ychapi._ready_wsdata=!0,ychapi._do_ws_init()},ychapi._do_ws_init=function(){if(ychapi._websocket.readyState!=WebSocket.OPEN)return;const e={type:"init",jwt:ychapi._get_cookie("jwt")};ychapi._websocket.send(JSON.stringify(e)),ychapi._ready_wsdata=!0,ychapi._callbacks.on_sync_connecting(),ychapi._ready_wsdata&&ychapi._ready_data&&ychapi._callbacks.on_sync_ready(),clearTimeout(ychapi._timer_reauth),ychapi._timer_reauth=setTimeout(ychapi._do_apicall_reauth,ychapi._timer_reauth_duration)},ychapi._on_ws_onmessage=function(e){if("message"==e.type){let t={};e.data.split("\n").forEach((function(e,n){try{t=JSON.parse(e,jsonBNparse)}catch(e){return}ychapi._on_ws_message(t)}))}else console.log("Received ws event, not a message: ",e)},ychapi._on_ws_message=function(e){"type"in e?(e.type in ychapi._ws_callbacks?ychapi._ws_callbacks[e.type](e):console.log("Received unknown ws event, message: ",e),ychapi._callbacks.on_sync_message(e)):console.log("Received unknown ws event, message: ",e)},ychapi._ws_callbacks.nnum=function(e){const t=e.objects[0];ychapi._nnum=t},ychapi._ws_callbacks.coininfo=function(e){const t=e.objects[0];null==ychapi._data.coininfos&&(ychapi._data.coininfos={}),ychapi._data.coininfos[t.coin]=t,ychapi._callbacks.on_sync_data_update("coininfo",t)},ychapi._ws_callbacks.market=function(e){const t=e.objects[0];ychapi._data.markets[t.name]=t,ychapi._callbacks.on_sync_data_update("market",t)},ychapi._ws_callbacks.markettradesupdate=function(e){const t=e.objects[0],n=t.coina+"-"+t.coinb;ychapi._data.trades[n]=t.page,ychapi._callbacks.on_sync_data_update("trades",n,t.coina,t.coinb,t.page)},ychapi._ws_callbacks.usertradesupdate=function(e){if(null==ychapi._data.profile)return;if(null==ychapi._data.profile)return;const t=e.objects[0],n=t.coina+"-"+t.coinb;ychapi._data.profile.trades[n]=t.page,ychapi._callbacks.on_sync_data_update("user_trades",n,t.coina,t.coinb,t.page)},ychapi._ws_callbacks.depositaddress=function(e){if(null==ychapi._data.profile)return;if(null==ychapi._data.profile)return;const t=e.objects[0],n=t.coin;if(ychapi._addresses[n]=t.addr,ychapi._locktimes1[n]=t.lck1,ychapi._locktimes2[n]=t.lck2,n in ychapi._data.profile.balances){const e=ychapi._data.profile.balances[n];ychapi._callbacks.on_sync_data_update("balance",e)}},ychapi._ws_callbacks.addressesupdate1=function(e){const t=e.objects[0],n=t.coin,i=t.list;n in ychapi._asset_extra_info||(ychapi._asset_extra_info[n]={},ychapi._asset_extra_info[n].senders=[]),ychapi._asset_extra_info[n].senders=i},ychapi._ws_callbacks.adddeposit=function(e){if(null==ychapi._data.profile)return;if(null==ychapi._data.profile)return;const t=e.objects[1],n=t.coin;let i=ychapi._data.profile.deposits[n],a=!0;i.forEach((function(e,n){e.index>=t.index&&(a=!1)})),a&&(i.unshift(t),i=i.slice(0,ychapi._get_page_size()),ychapi._data.profile.deposits[n]=i)},ychapi._ws_callbacks.regdeposit=function(e){if(null==ychapi._data.profile)return;if(null==ychapi._data.profile)return;const t=e.objects[1],n=t.coin;let i=ychapi._data.profile.deposits[n];i.forEach((function(e,n){t.index==e.index&&(i[n]=t,e=t)}))},ychapi._ws_callbacks.addwithdraw=function(e){if(null==ychapi._data.profile)return;if(null==ychapi._data.profile)return;const t=e.objects[1],n=t.coin;let i=ychapi._data.profile.withdraws[n],a=!0;i.forEach((function(e,n){e.uidx>=t.uidx&&(a=!1)})),a&&(i.unshift(t),i=i.slice(0,ychapi._get_page_size()),ychapi._data.profile.withdraws[t.coin]=i)},ychapi._ws_callbacks.regwithdraw=function(e){if(null==ychapi._data.profile)return;if(null==ychapi._data.profile)return;const t=e.objects[1],n=t.coin;let i=ychapi._data.profile.withdraws[n];i.forEach((function(e,n){t.uidx==e.uidx&&(i[n]=t,e=t)}))},ychapi._ws_callbacks.balances=function(e){e.objects[0].forEach((function(e){ychapi._data.profile.balances[e.coin]=e,ychapi._callbacks.on_sync_data_update("balance",e)}))},ychapi._ws_callbacks.accountchange=function(e){},ychapi._ws_callbacks.newcandle=function(e){},ychapi._ws_callbacks.updatecandle=function(e){},ychapi._ws_callbacks.txoutsupdate=function(e){if(null==ychapi._data.profile)return;if(null==ychapi._data.profile)return;const t=e.objects[0],n=e.objects[1];ychapi._data.profile.txouts[t]=n,ychapi._callbacks.on_sync_data_update("txouts",t,n)},ychapi._ws_callbacks.buysupdate=function(e){const t=e.objects[0],n=t.coina+"-"+t.coinb;ychapi._data.buys[n]=t.buyspage,ychapi._callbacks.on_sync_data_update("buys",n,t.coina,t.coinb,t.buyspage)},ychapi._ws_callbacks.sellsupdate=function(e){const t=e.objects[0],n=t.coina+"-"+t.coinb;ychapi._data.sells[n]=t.sellspage,ychapi._callbacks.on_sync_data_update("sells",n,t.coina,t.coinb,t.sellspage)},ychapi._ws_callbacks.userbuysupdate=function(e){if(null==ychapi._data.profile)return;if(null==ychapi._data.profile)return;const t=e.objects[0],n=t.coina+"-"+t.coinb;ychapi._data.profile.buys[n]=t.buyspage,ychapi._callbacks.on_sync_data_update("user_buys",n,t.coina,t.coinb,t.buyspage)},ychapi._ws_callbacks.usersellsupdate=function(e){if(null==ychapi._data.profile)return;if(null==ychapi._data.profile)return;const t=e.objects[0],n=t.coina+"-"+t.coinb;ychapi._data.profile.sells[n]=t.sellspage,ychapi._callbacks.on_sync_data_update("user_sells",n,t.coina,t.coinb,t.sellspage)},ychapi._get_cookie=function(e){const t=e+"=",n=decodeURIComponent(document.cookie).split(";");for(let e=0;e<n.length;e++){let i=n[e];for(;" "==i.charAt(0);)i=i.substring(1);if(0==i.indexOf(t))return i.substring(t.length,i.length)}return""},ychapi._get_jwt_uid=function(){const e=ychapi._get_cookie("jwt").split(".");if(3==e.length){const t=e[1].replace("-","+").replace("_","/");return JSON.parse(window.atob(t)).uid}return""},ychapi._str2bytes=function(e){let t=[];for(let n=0;n<e.length;n++){let i=e.charCodeAt(n);i<128?t.push(i):i<2048?t.push(192|i>>6,128|63&i):i<55296||i>=57344?t.push(224|i>>12,128|i>>6&63,128|63&i):(n++,i=65536+((1023&i)<<10|1023&e.charCodeAt(n)),t.push(240|i>>18,128|i>>12&63,128|i>>6&63,128|63&i))}return t},ychapi._get_page_size=function(){return ychapi._pagesize};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ychapi",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.912.0",
|
|
4
4
|
"description": "Walrus-compatible exchange js api layer",
|
|
5
5
|
"main": "api.js",
|
|
6
6
|
"scripts": {
|
|
@@ -18,5 +18,8 @@
|
|
|
18
18
|
"bugs": {
|
|
19
19
|
"url": "https://github.com/hyperwalrus/ychapi/issues"
|
|
20
20
|
},
|
|
21
|
-
"homepage": "https://github.com/hyperwalrus/ychapi#readme"
|
|
21
|
+
"homepage": "https://github.com/hyperwalrus/ychapi#readme",
|
|
22
|
+
"files": [
|
|
23
|
+
"dist/"
|
|
24
|
+
]
|
|
22
25
|
}
|