@axis-markets/client 0.5.0 → 0.8.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.
@@ -0,0 +1,17 @@
1
+ /**
2
+ * Parse an AXIS API timestamp (`YYYY-MM-DD HH:mm:ss` UTC)
3
+ * @param {string|number|undefined} value
4
+ * @return {number} - UNIX milliseconds, 0 when missing or invalid
5
+ */
6
+ export function parseApiDate(value) {
7
+ if (value === undefined || value === null || value === '')
8
+ return 0
9
+ if (typeof value === 'number')
10
+ return value
11
+ let text = String(value).trim().replace(' ', 'T')
12
+ if (!/[zZ]|[+-]\d\d:?\d\d$/.test(text)) {
13
+ text += 'Z'
14
+ }
15
+ const ms = Date.parse(text)
16
+ return Number.isFinite(ms) ? ms : 0
17
+ }
@@ -0,0 +1,104 @@
1
+ import {Address} from '@stellar/stellar-sdk'
2
+
3
+ /** Price scale of the contract (18 decimals) */
4
+ export const PRICE_SCALE = 10n ** 18n
5
+
6
+ /**
7
+ * Serialized `ScAddress` by address string
8
+ * @type {Map<string, Uint8Array>}
9
+ */
10
+ const keyCache = new Map()
11
+
12
+ /**
13
+ * @param {string} address
14
+ * @return {Uint8Array}
15
+ */
16
+ function getAddressKey(address) {
17
+ let key = keyCache.get(address)
18
+ if (!key) {
19
+ key = Address.fromString(address).toScAddress().toXDR()
20
+ if (keyCache.size >= 10_000) { //will never happen in real life
21
+ keyCache.clear()
22
+ }
23
+ keyCache.set(address, key)
24
+ }
25
+ return key
26
+ }
27
+
28
+ /**
29
+ * Compare two assets in the contract canonical order
30
+ * @param {string} a - Token contract address
31
+ * @param {string} b - Token contract address
32
+ * @return {number} - Negative if `a` first, positive if `b` first, 0 if equal
33
+ */
34
+ export function compareAssets(a, b) {
35
+ if (a === b)
36
+ return 0
37
+ const key1 = getAddressKey(a)
38
+ const key2 = getAddressKey(b)
39
+ const length = Math.min(key1.length, key2.length)
40
+ for (let i = 0; i < length; i++) {
41
+ if (key1[i] !== key2[i])
42
+ return key1[i] - key2[i]
43
+ }
44
+ return key1.length - key2.length
45
+ }
46
+
47
+ /**
48
+ * Reorder an asset pair the way the contract stores markets
49
+ * @param {string} x - Token contract address
50
+ * @param {string} y - Token contract address
51
+ * @return {[string, string]} - `[a, b]`: `a` is the base, `b` the quote of the canonical market symbol
52
+ */
53
+ export function canonicalPair(x, y) {
54
+ return compareAssets(x, y) <= 0 ? [x, y] : [y, x]
55
+ }
56
+
57
+ /**
58
+ * Canonical market key `a/b`
59
+ * @param {string} a - Token contract address
60
+ * @param {string} b - Token contract address
61
+ * @return {string}
62
+ */
63
+ export function pairKey(a, b) {
64
+ const [a1, a2] = canonicalPair(a, b)
65
+ return a1 + '/' + a2
66
+ }
67
+
68
+ /**
69
+ * Calculate inverted price, rounded down: `floor(10^36 / price)`
70
+ * @param {bigint} price - Price in the contract scale
71
+ * @return {bigint}
72
+ */
73
+ export function invertPrice(price) {
74
+ if (price <= 0n)
75
+ return 0n
76
+ return PRICE_SCALE * PRICE_SCALE / price
77
+ }
78
+
79
+ /**
80
+ * 24h ticker entry of the market seen in the inverted orientation (`b/a`)
81
+ * @param {TickerEntry} entry - Standard ticker entry
82
+ * @return {TickerEntry}
83
+ */
84
+ export function invertTicker(entry) {
85
+ const [a, b] = entry.symbol.split('/')
86
+ const inv = value => {
87
+ const n = Number(value)
88
+ return n > 0 ? String(1 / n) : '0'
89
+ }
90
+ const open = Number(entry.openPrice)
91
+ const last = Number(entry.lastPrice)
92
+ return {
93
+ ...entry,
94
+ symbol: b + '/' + a,
95
+ openPrice: inv(entry.openPrice),
96
+ highPrice: inv(entry.lowPrice),
97
+ lowPrice: inv(entry.highPrice),
98
+ lastPrice: inv(entry.lastPrice),
99
+ volume: entry.quoteVolume,
100
+ quoteVolume: entry.volume,
101
+ change: open > 0 && last > 0 ? Math.round((open / last - 1) * 100_000) / 1000 : 0,
102
+ avgPrice: inv(entry.avgPrice)
103
+ }
104
+ }