@icgio/icg-utils 1.9.15 → 1.9.17

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.
@@ -392,6 +392,32 @@ let get_crypto_rates = (cb, pair_or_pairs_or_cur_curs, type, source = 'coingecko
392
392
  console.error(pair, error)
393
393
  }
394
394
  })
395
+ } else if (exchange === 'weex') {
396
+ let pair = cur.toUpperCase() + 'USDT'
397
+ let url = `https://contract-openapi.weex.com/api/spot/v1/market/depth?symbol=${pair}_spbl&type=step0`
398
+ limiter.process(needle.get, url, (err, res, body) => {
399
+ try {
400
+ const asks = body.data.asks
401
+ const bids = body.data.bids
402
+ let rate = (parseFloat(asks[0][0]) + parseFloat(bids[0][0])) / 2
403
+ result[cur] = {
404
+ USD: rate,
405
+ USDT: rate,
406
+ }
407
+ let result_length = _.keys(result).length
408
+ if (result_length === length) {
409
+ for (let stable of stablecoins) {
410
+ result[stable] = {
411
+ USD: 1,
412
+ USDT: 1,
413
+ }
414
+ }
415
+ cb(result)
416
+ }
417
+ } catch (error) {
418
+ console.error(pair, error)
419
+ }
420
+ })
395
421
  } else if (exchange === 'xt') {
396
422
  let pair = cur.toLowerCase() + '_usdt'
397
423
  let url = `https://sapi.xt.com/v4/public/depth?symbol=${pair}&limit=20`
package/lib/mysql.js ADDED
@@ -0,0 +1,84 @@
1
+ // Sample usage:
2
+ //
3
+ // const Mysql = require('./mysql')
4
+ // const db = new Mysql({
5
+ // host: 'localhost',
6
+ // user: 'root',
7
+ // password: 'password',
8
+ // database: 'testdb',
9
+ // })
10
+ //
11
+ // // Query example:
12
+ // db.query('SELECT * FROM users WHERE id = ?', [1], (res) => {
13
+ // if (res.success) {
14
+ // console.log('User:', res.body)
15
+ // } else {
16
+ // console.error('Query error:', res.body)
17
+ // }
18
+ // })
19
+ //
20
+ // // Insert example:
21
+ // const rows = [
22
+ // { name: 'Alice', age: 30 },
23
+ // { name: 'Bob', age: 25 },
24
+ // ]
25
+ // db.write_rows_with_check('users', rows, (res) => {
26
+ // if (res.success) {
27
+ // console.log('Insert result:', res.body)
28
+ // } else {
29
+ // console.error('Insert error:', res.body)
30
+ // }
31
+ // })
32
+ //
33
+
34
+ const _ = require('lodash')
35
+
36
+ const mysql = require('mysql2/promise')
37
+
38
+ function fields_check(fields) {
39
+ let result = true
40
+ for (let key in fields) {
41
+ if (_.isNaN(fields[key]) || _.isNull(fields[key]) || _.isUndefined(fields[key])) {
42
+ result = false
43
+ }
44
+ }
45
+ return result
46
+ }
47
+
48
+ class Mysql {
49
+ constructor(config) {
50
+ // config: { host, user, password, database, ... }
51
+ this.config = config
52
+ this.pool = mysql.createPool(config)
53
+ }
54
+
55
+ async query(query_string, params, cb) {
56
+ try {
57
+ const [rows, fields] = await this.pool.execute(query_string, params)
58
+ cb({ success: true, body: rows })
59
+ } catch (err) {
60
+ cb({ success: false, body: err })
61
+ }
62
+ }
63
+
64
+ async write_rows_with_check(table, rows, cb) {
65
+ // rows: array of objects, each object is a row
66
+ try {
67
+ const validRows = rows.filter(fields_check)
68
+ if (validRows.length === 0) {
69
+ return cb({ success: false, body: 'No valid rows to insert' })
70
+ }
71
+ const keys = Object.keys(validRows[0])
72
+ const placeholders = keys.map(() => '?').join(',')
73
+ const sql = `INSERT INTO \`${table}\` (${keys.map((k) => `\`${k}\``).join(',')}) VALUES (${placeholders})`
74
+ const values = validRows.map((row) => keys.map((k) => row[k]))
75
+ // Use bulk insert
76
+ const [result] = await this.pool.query(sql, values)
77
+ cb({ success: true, body: result })
78
+ } catch (err) {
79
+ cb({ success: false, body: err })
80
+ }
81
+ }
82
+ }
83
+
84
+ module.exports = Mysql
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@icgio/icg-utils",
3
- "version": "1.9.15",
3
+ "version": "1.9.17",
4
4
  "description": "Utils package.",
5
5
  "main": "index.js",
6
6
  "directories": {