@axis-markets/client 0.7.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.
@@ -1,197 +1,181 @@
1
- import {canonicalPair} from './asset-pair.js'
2
- import {parseApiDate} from './api-dates.js'
3
-
4
- /**
5
- * DEX Market
6
- */
7
- export class AxisMarket {
8
- /**
9
- * @param {Axis} axis
10
- * @param {{a: string, b: string, created?: string, refreshed?: string}} record - Market data
11
- */
12
- constructor(axis, record) {
13
- this.axis = axis
14
- const [a, b] = canonicalPair(record.a, record.b)
15
- this.a = a
16
- this.b = b
17
- this.key = a + '/' + b
18
- this.update(record)
19
- }
20
-
21
- /**
22
- * Base asset
23
- * @type {string}
24
- * @readonly
25
- */
26
- a
27
- /**
28
- * Quote asset
29
- * @type {string}
30
- * @readonly
31
- */
32
- b
33
- /**
34
- * Canonical market key `a/b`
35
- * @type {string}
36
- * @readonly
37
- */
38
- key
39
- /**
40
- * Creation timestamp (first oracle check) (UNIX milliseconds)
41
- * @type {number}
42
- */
43
- created = 0
44
- /**
45
- * Last oracle check (UNIX milliseconds)
46
- * @type {number}
47
- */
48
- refreshed = 0
49
-
50
- /**
51
- * Base asset
52
- * @return {string}
53
- */
54
- get base() {
55
- return this.a
56
- }
57
-
58
- /**
59
- * Quote asset
60
- * @return {string}
61
- */
62
- get quote() {
63
- return this.b
64
- }
65
-
66
- /**
67
- * Check whether the asset is one of the market assets
68
- * @param {string} asset
69
- * @return {boolean}
70
- */
71
- has(asset) {
72
- return asset === this.a || asset === this.b
73
- }
74
-
75
- /**
76
- * The other market asset
77
- * @param {string} asset - One of the market assets
78
- * @return {string}
79
- */
80
- counter(asset) {
81
- if (asset === this.a)
82
- return this.b
83
- if (asset === this.b)
84
- return this.a
85
- throw new Error(`Asset ${asset} does not belong to the market ${this.key}`)
86
- }
87
-
88
- /**
89
- * Apply the contract state change record
90
- * @param {{created?: string, refreshed?: string}} record
91
- * @internal
92
- */
93
- update(record) {
94
- if (record.created) {
95
- this.created = parseApiDate(record.created)
96
- }
97
- if (record.refreshed) {
98
- this.refreshed = parseApiDate(record.refreshed)
99
- }
100
- }
101
-
102
- /**
103
- * Re-check both market assets against the price oracle and cache oracle prices
104
- * @param {Signer} [signer] - Transaction source, the {@link Axis} signer by default
105
- * @return {Promise<Market|undefined>}
106
- */
107
- async requote(signer) {
108
- return this.axis.contractClient(signer).requote(this.a, this.b)
109
- }
110
-
111
- /**
112
- * Extend oracle price feeds access for the market
113
- * @param {{amount: bigint, sponsor?: string, signer?: Signer}} params - `amount` of fee tokens to burn
114
- * @return {Promise<bigint[]>} - New access expiration UNIX timestamps (in seconds) for each oracle-listed asset
115
- */
116
- async subsidize({amount, sponsor, signer}) {
117
- const client = this.axis.contractClient(signer)
118
- return client.subsidize({sponsor: sponsor ?? client.publicKey, selling: this.a, buying: this.b, amount})
119
- }
120
-
121
- /**
122
- * Orderbook depth data with given precision
123
- * @param {{base?: string, depth?: number, step?: string, limit?: number}} [params] - `base` is the base asset, `step` the price step (automatic by default)
124
- * @return {Promise<import('./api-client.js').OrderbookDepth>}
125
- */
126
- async getDepth({base = this.a, depth, step, limit} = {}) {
127
- return this.axis.api.getDepth({market: base + '/' + this.counter(base), depth, step, limit})
128
- }
129
-
130
- /**
131
- * Stream the orderbook depth
132
- * Returns a snapshot, then updates on book changes
133
- * @param {{base?: string, depth?: number, step?: string, limit?: number}} params
134
- * @param {function(OrderbookDepth): void} callback
135
- * @return {function(): void} - Unsubscribe function
136
- */
137
- subscribeDepth({base = this.a, depth, step, limit} = {}, callback) {
138
- const market = base + '/' + this.counter(base)
139
- return this.axis.stream.subscribe('depth', {market, depth, step, limit}, message => {
140
- if (message.type === 'depth') {
141
- callback(message.data)
142
- }
143
- })
144
- }
145
-
146
- /**
147
- * Stream the market trades
148
- * Returns a snapshot, then updates on trades
149
- * @param {function(ApiTrade[], boolean): void} callback - Trades and whether they are the snapshot
150
- * @return {function(): void} - Unsubscribe function
151
- */
152
- subscribeTrades(callback) {
153
- return this.axis.stream.subscribe('trades', {market: this.key}, message => {
154
- if (message.type === 'trades') {
155
- callback(message.trades, true)
156
- } else if (message.type === 'trade') {
157
- callback([message.trade], false)
158
- }
159
- })
160
- }
161
-
162
- /**
163
- * Stream the market candles
164
- * Returns a snapshot, then updates on trades
165
- * @param {{base?: string, resolution: number|string, limit?: number}} params - `base` is the base asset of the view, `resolution` in seconds or an alias (`5m`, `1h`, …), `limit` the candles in the snapshot (200 by default, at most 200)
166
- * @param {function(Candle[], boolean): void} callback - Candles
167
- * @return {function(): void} - Unsubscribe function
168
- */
169
- subscribeCandles({base = this.a, resolution, limit}, callback) {
170
- const market = base + '/' + this.counter(base)
171
- return this.axis.stream.subscribe('candles', {market, resolution, limit}, message => {
172
- if (message.type === 'candles') {
173
- callback(message.candles, true)
174
- } else if (message.type === 'candle') {
175
- callback([message.candle], false)
176
- }
177
- })
178
- }
179
-
180
- /**
181
- * Last 24h ticker entry of the market, available while {@link Axis#subscribeTicker} runs
182
- * @return {TickerEntry|undefined}
183
- */
184
- get ticker() {
185
- return this.axis.tickers.get(this.key)
186
- }
187
-
188
- toJSON() {
189
- return {a: this.a, b: this.b, key: this.key, created: this.created, refreshed: this.refreshed}
190
- }
191
- }
192
-
193
- /**
194
- * @typedef {{}} Signer - Account signing contract calls
195
- * @property {string} publicKey - Transaction source account
196
- * @property {SignTransactionCallback} signTransaction
197
- */
1
+ import {canonicalPair} from './asset-pair.js'
2
+ import {parseApiDate} from './api-dates.js'
3
+
4
+ /**
5
+ * DEX Market
6
+ */
7
+ export class AxisMarket {
8
+ /**
9
+ * @param {Axis} axis
10
+ * @param {{base: string, quote: string, created?: string, refreshed?: string}} record - Market data
11
+ */
12
+ constructor(axis, record) {
13
+ this.axis = axis
14
+ const [base, quote] = canonicalPair(record.base, record.quote)
15
+ this.base = base
16
+ this.quote = quote
17
+ this.key = base + '/' + quote
18
+ this.update(record)
19
+ }
20
+
21
+ /**
22
+ * Base asset, the first of the pair in the contract canonical order
23
+ * @type {string}
24
+ * @readonly
25
+ */
26
+ base
27
+ /**
28
+ * Quote asset, the second of the pair in the contract canonical order
29
+ * @type {string}
30
+ * @readonly
31
+ */
32
+ quote
33
+ /**
34
+ * Canonical market key `base/quote`
35
+ * @type {string}
36
+ * @readonly
37
+ */
38
+ key
39
+ /**
40
+ * Creation timestamp (first oracle check) (UNIX milliseconds)
41
+ * @type {number}
42
+ */
43
+ created = 0
44
+ /**
45
+ * Last oracle check (UNIX milliseconds)
46
+ * @type {number}
47
+ */
48
+ refreshed = 0
49
+
50
+ /**
51
+ * Check whether the asset is one of the market assets
52
+ * @param {string} asset
53
+ * @return {boolean}
54
+ */
55
+ has(asset) {
56
+ return asset === this.base || asset === this.quote
57
+ }
58
+
59
+ /**
60
+ * The other market asset
61
+ * @param {string} asset - One of the market assets
62
+ * @return {string}
63
+ */
64
+ counter(asset) {
65
+ if (asset === this.base)
66
+ return this.quote
67
+ if (asset === this.quote)
68
+ return this.base
69
+ throw new Error(`Asset ${asset} does not belong to the market ${this.key}`)
70
+ }
71
+
72
+ /**
73
+ * Apply the contract state change record
74
+ * @param {{created?: string, refreshed?: string}} record
75
+ * @internal
76
+ */
77
+ update(record) {
78
+ if (record.created) {
79
+ this.created = parseApiDate(record.created)
80
+ }
81
+ if (record.refreshed) {
82
+ this.refreshed = parseApiDate(record.refreshed)
83
+ }
84
+ }
85
+
86
+ /**
87
+ * Re-check both market assets against the price oracle and cache oracle prices
88
+ * @param {Signer} [signer] - Transaction source, the {@link Axis} signer by default
89
+ * @return {Promise<Market|undefined>}
90
+ */
91
+ async requote(signer) {
92
+ return this.axis.contractClient(signer).requote(this.base, this.quote)
93
+ }
94
+
95
+ /**
96
+ * Extend oracle price feeds access for the market
97
+ * @param {{amount: bigint, sponsor?: string, signer?: Signer}} params - `amount` of fee tokens to burn
98
+ * @return {Promise<bigint[]>} - New access expiration UNIX timestamps (in seconds) for each oracle-listed asset
99
+ */
100
+ async subsidize({amount, sponsor, signer}) {
101
+ const client = this.axis.contractClient(signer)
102
+ return client.subsidize({sponsor: sponsor ?? client.publicKey, selling: this.base, buying: this.quote, amount})
103
+ }
104
+
105
+ /**
106
+ * Orderbook depth data with given precision
107
+ * @param {{base?: string, depth?: number, step?: string, limit?: number}} [params] - `base` is the base asset, `step` the price step (automatic by default)
108
+ * @return {Promise<import('./api-client.js').OrderbookDepth>}
109
+ */
110
+ async getDepth({base = this.base, depth, step, limit} = {}) {
111
+ return this.axis.api.getDepth({market: base + '/' + this.counter(base), depth, step, limit})
112
+ }
113
+
114
+ /**
115
+ * Stream the orderbook depth
116
+ * Returns a snapshot, then updates on book changes
117
+ * @param {{base?: string, depth?: number, step?: string, limit?: number}} params
118
+ * @param {function(OrderbookDepth): void} callback
119
+ * @return {function(): void} - Unsubscribe function
120
+ */
121
+ subscribeDepth({base = this.base, depth, step, limit} = {}, callback) {
122
+ const market = base + '/' + this.counter(base)
123
+ return this.axis.stream.subscribe('depth', {market, depth, step, limit}, message => {
124
+ if (message.type === 'depth') {
125
+ callback(message.data)
126
+ }
127
+ })
128
+ }
129
+
130
+ /**
131
+ * Stream the market trades
132
+ * Returns a snapshot, then updates on trades
133
+ * @param {function(ApiTrade[], boolean): void} callback - Trades and whether they are the snapshot
134
+ * @return {function(): void} - Unsubscribe function
135
+ */
136
+ subscribeTrades(callback) {
137
+ return this.axis.stream.subscribe('trades', {market: this.key}, message => {
138
+ if (message.type === 'trades') {
139
+ callback(message.trades, true)
140
+ } else if (message.type === 'trade') {
141
+ callback([message.trade], false)
142
+ }
143
+ })
144
+ }
145
+
146
+ /**
147
+ * Stream the market candles
148
+ * Returns a snapshot, then updates on trades
149
+ * @param {{base?: string, resolution: number|string, limit?: number}} params - `base` is the base asset of the view, `resolution` in seconds or an alias (`5m`, `1h`, …), `limit` the candles in the snapshot (200 by default, at most 200)
150
+ * @param {function(Candle[], boolean): void} callback - Candles
151
+ * @return {function(): void} - Unsubscribe function
152
+ */
153
+ subscribeCandles({base = this.base, resolution, limit}, callback) {
154
+ const market = base + '/' + this.counter(base)
155
+ return this.axis.stream.subscribe('candles', {market, resolution, limit}, message => {
156
+ if (message.type === 'candles') {
157
+ callback(message.candles, true)
158
+ } else if (message.type === 'candle') {
159
+ callback([message.candle], false)
160
+ }
161
+ })
162
+ }
163
+
164
+ /**
165
+ * Last 24h ticker entry of the market, available while {@link Axis#subscribeTicker} runs
166
+ * @return {TickerEntry|undefined}
167
+ */
168
+ get ticker() {
169
+ return this.axis.tickers.get(this.key)
170
+ }
171
+
172
+ toJSON() {
173
+ return {base: this.base, quote: this.quote, key: this.key, created: this.created, refreshed: this.refreshed}
174
+ }
175
+ }
176
+
177
+ /**
178
+ * @typedef {{}} Signer - Account signing contract calls
179
+ * @property {string} publicKey - Transaction source account
180
+ * @property {SignTransactionCallback} signTransaction
181
+ */