@icgio/icg-utils 1.9.43 → 1.9.45
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/README.md +24 -28
- package/index.js +1 -3
- package/lib/crypto-rates.js +85 -202
- package/lib/dex-rate.js +80 -14
- package/lib/utils.js +134 -20
- package/package.json +1 -1
- package/constants/coins-map-coingecko.js +0 -10146
- package/constants/coins-map-coinmarketcap.js +0 -7497
package/lib/utils.js
CHANGED
|
@@ -826,7 +826,103 @@ function number_with_commas(x) {
|
|
|
826
826
|
return parts.join('.')
|
|
827
827
|
}
|
|
828
828
|
|
|
829
|
-
|
|
829
|
+
// =============================================================================
|
|
830
|
+
// Exchange Key Utilities
|
|
831
|
+
//
|
|
832
|
+
// Canonical exchange *account* keys are: "<base>_<index>", e.g. "mexc_1".
|
|
833
|
+
// We still support reading legacy base-only keys ("mexc") for migration/compat,
|
|
834
|
+
// but new configs should use account keys everywhere.
|
|
835
|
+
// =============================================================================
|
|
836
|
+
|
|
837
|
+
const EXCHANGE_ACCOUNT_KEY_RE = /^([a-z0-9]+)_(\d+)$/
|
|
838
|
+
|
|
839
|
+
function normalize_exchange_key(exchange) {
|
|
840
|
+
return String(exchange || '').trim().toLowerCase()
|
|
841
|
+
}
|
|
842
|
+
|
|
843
|
+
function is_exchange_account_key(exchange) {
|
|
844
|
+
return EXCHANGE_ACCOUNT_KEY_RE.test(normalize_exchange_key(exchange))
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
function parse_exchange_account_key(exchange_account_key) {
|
|
848
|
+
const key = normalize_exchange_key(exchange_account_key)
|
|
849
|
+
const m = key.match(EXCHANGE_ACCOUNT_KEY_RE)
|
|
850
|
+
if (!m) {
|
|
851
|
+
throw new Error(`Invalid exchange account key "${exchange_account_key}" (expected "base_index", e.g. "mexc_1")`)
|
|
852
|
+
}
|
|
853
|
+
const base = m[1]
|
|
854
|
+
const index = Number(m[2])
|
|
855
|
+
if (!Number.isInteger(index) || index <= 0) {
|
|
856
|
+
throw new Error(`Invalid exchange account index in "${exchange_account_key}"`)
|
|
857
|
+
}
|
|
858
|
+
return { base, index }
|
|
859
|
+
}
|
|
860
|
+
|
|
861
|
+
function format_exchange_account_key(base, index) {
|
|
862
|
+
const b = normalize_exchange_key(base)
|
|
863
|
+
if (!b) throw new Error('format_exchange_account_key: base is required')
|
|
864
|
+
if (!Number.isInteger(index) || index <= 0) {
|
|
865
|
+
throw new Error(`format_exchange_account_key: index must be a positive integer (got ${index})`)
|
|
866
|
+
}
|
|
867
|
+
return `${b}_${index}`
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
function exchange_base_from_key(exchange_key) {
|
|
871
|
+
const key = normalize_exchange_key(exchange_key)
|
|
872
|
+
const m = key.match(EXCHANGE_ACCOUNT_KEY_RE)
|
|
873
|
+
if (m) return m[1]
|
|
874
|
+
// Legacy base-only key (or other legacy formats): treat whole key as base.
|
|
875
|
+
const underscore = key.indexOf('_')
|
|
876
|
+
return underscore === -1 ? key : key.slice(0, underscore)
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
function group_exchange_keys_by_base(exchange_keys) {
|
|
880
|
+
const grouped = {}
|
|
881
|
+
;(exchange_keys || []).forEach((k) => {
|
|
882
|
+
const key = normalize_exchange_key(k)
|
|
883
|
+
if (!key) return
|
|
884
|
+
const base = exchange_base_from_key(key)
|
|
885
|
+
if (!grouped[base]) grouped[base] = []
|
|
886
|
+
grouped[base].push(key)
|
|
887
|
+
})
|
|
888
|
+
// Stable deterministic ordering.
|
|
889
|
+
for (const base in grouped) {
|
|
890
|
+
grouped[base] = _.uniq(grouped[base]).sort()
|
|
891
|
+
}
|
|
892
|
+
return grouped
|
|
893
|
+
}
|
|
894
|
+
|
|
895
|
+
function pick_primary_key_for_base(keys_for_base, exchange_roles) {
|
|
896
|
+
const keys = (keys_for_base || []).map(normalize_exchange_key).filter(Boolean)
|
|
897
|
+
if (keys.length === 0) return null
|
|
898
|
+
|
|
899
|
+
if (exchange_roles && typeof exchange_roles === 'object') {
|
|
900
|
+
const primaries = keys.filter((k) => exchange_roles[normalize_exchange_key(k)] === 'primary')
|
|
901
|
+
if (primaries.length === 1) return primaries[0]
|
|
902
|
+
if (primaries.length > 1) return primaries.sort()[0]
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
// Legacy heuristic: base-only key (no underscore) is primary if present.
|
|
906
|
+
const base_only = keys.find((k) => k.indexOf('_') === -1)
|
|
907
|
+
if (base_only) return base_only
|
|
908
|
+
|
|
909
|
+
// Otherwise, prefer lowest account index if possible.
|
|
910
|
+
const parsed = []
|
|
911
|
+
for (const k of keys) {
|
|
912
|
+
if (is_exchange_account_key(k)) {
|
|
913
|
+
const { index } = parse_exchange_account_key(k)
|
|
914
|
+
parsed.push({ k, index })
|
|
915
|
+
}
|
|
916
|
+
}
|
|
917
|
+
if (parsed.length > 0) {
|
|
918
|
+
return _.sortBy(parsed, ['index', 'k'])[0].k
|
|
919
|
+
}
|
|
920
|
+
|
|
921
|
+
// Fallback: stable first.
|
|
922
|
+
return keys.sort()[0]
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
function exchange_cur_quote_cur_converter(exchange_cur_quote_cur, opts = {}) {
|
|
830
926
|
let exchange_pairs = {},
|
|
831
927
|
exchange_curs = {},
|
|
832
928
|
exchange_cur_pairs = {}
|
|
@@ -864,36 +960,47 @@ function exchange_cur_quote_cur_converter(exchange_cur_quote_cur) {
|
|
|
864
960
|
pair_exchanges[pair].push(exchange)
|
|
865
961
|
})
|
|
866
962
|
}
|
|
867
|
-
//
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
963
|
+
// Primary-only (aka "without affiliates") sets.
|
|
964
|
+
//
|
|
965
|
+
// Legacy behavior: keys without "_" are treated as primary.
|
|
966
|
+
// Canonical behavior: use exchange_roles to pick exactly 1 primary per base.
|
|
967
|
+
const exchange_roles = opts && opts.exchange_roles ? opts.exchange_roles : null
|
|
968
|
+
|
|
969
|
+
const exchange_keys = Object.keys(exchange_pairs)
|
|
970
|
+
const by_base = group_exchange_keys_by_base(exchange_keys)
|
|
971
|
+
|
|
972
|
+
const exchange_primary_by_base = {}
|
|
973
|
+
const primary_exchange_keys = []
|
|
974
|
+
for (const base in by_base) {
|
|
975
|
+
const primary = pick_primary_key_for_base(by_base[base], exchange_roles)
|
|
976
|
+
if (primary) {
|
|
977
|
+
exchange_primary_by_base[base] = primary
|
|
978
|
+
primary_exchange_keys.push(primary)
|
|
872
979
|
}
|
|
873
980
|
}
|
|
874
|
-
|
|
981
|
+
const primary_exchange_key_set = new Set(primary_exchange_keys)
|
|
982
|
+
|
|
983
|
+
let exchange_pairs_without_affiliates = {}
|
|
875
984
|
let exchange_curs_without_affiliates = {}
|
|
876
|
-
for (
|
|
877
|
-
if (
|
|
878
|
-
|
|
879
|
-
}
|
|
985
|
+
for (const exchange of primary_exchange_keys) {
|
|
986
|
+
if (exchange_pairs[exchange]) exchange_pairs_without_affiliates[exchange] = exchange_pairs[exchange]
|
|
987
|
+
if (exchange_curs[exchange]) exchange_curs_without_affiliates[exchange] = exchange_curs[exchange]
|
|
880
988
|
}
|
|
881
|
-
|
|
989
|
+
|
|
882
990
|
let pair_exchanges_without_affiliates = {}
|
|
883
991
|
for (let pair in pair_exchanges) {
|
|
884
|
-
pair_exchanges_without_affiliates[pair] = pair_exchanges[pair].filter((exchange) =>
|
|
992
|
+
pair_exchanges_without_affiliates[pair] = pair_exchanges[pair].filter((exchange) => primary_exchange_key_set.has(exchange))
|
|
885
993
|
if (pair_exchanges_without_affiliates[pair].length === 0) {
|
|
886
994
|
delete pair_exchanges_without_affiliates[pair]
|
|
887
995
|
}
|
|
888
996
|
}
|
|
889
997
|
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
}
|
|
998
|
+
// Map primary exchange key -> affiliate exchange keys (same base, excluding primary).
|
|
999
|
+
const exchange_affiliates = {}
|
|
1000
|
+
for (const base in by_base) {
|
|
1001
|
+
const primary = exchange_primary_by_base[base]
|
|
1002
|
+
if (!primary) continue
|
|
1003
|
+
exchange_affiliates[primary] = by_base[base].filter((k) => k !== primary)
|
|
897
1004
|
}
|
|
898
1005
|
|
|
899
1006
|
// Add exchange_pairs_without_2
|
|
@@ -907,6 +1014,7 @@ function exchange_cur_quote_cur_converter(exchange_cur_quote_cur) {
|
|
|
907
1014
|
pair_exchanges,
|
|
908
1015
|
pair_exchanges_without_affiliates,
|
|
909
1016
|
exchange_affiliates,
|
|
1017
|
+
exchange_primary_by_base,
|
|
910
1018
|
}
|
|
911
1019
|
}
|
|
912
1020
|
|
|
@@ -973,6 +1081,12 @@ module.exports = {
|
|
|
973
1081
|
reverse_pair,
|
|
974
1082
|
cur_dict_to_pair_list,
|
|
975
1083
|
number_with_commas,
|
|
1084
|
+
normalize_exchange_key,
|
|
1085
|
+
is_exchange_account_key,
|
|
1086
|
+
parse_exchange_account_key,
|
|
1087
|
+
format_exchange_account_key,
|
|
1088
|
+
exchange_base_from_key,
|
|
1089
|
+
group_exchange_keys_by_base,
|
|
976
1090
|
exchange_cur_quote_cur_converter,
|
|
977
1091
|
sort_keys,
|
|
978
1092
|
convert_deposit_withdrawal_chain,
|