@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.
package/src/order-id.js CHANGED
@@ -1,47 +1,47 @@
1
- import {xdr, Address, hash} from '@stellar/stellar-sdk'
2
-
3
- const U64_MAX = (1n << 64n) - 1n
4
-
5
- /**
6
- * Compute the id of the order `owner` creates with `nonce`.
7
- * Mirrors the contract: the first 16 bytes of `sha256(xdr(ScVec[owner: Address, nonce: u64]))`
8
- * interpreted as a big-endian `u128` (the full 128 bits, no mask).
9
- * @param {string} owner - Order owner address (account or contract)
10
- * @param {bigint|number|string} nonce - Client-chosen u64 nonce
11
- * @return {bigint} - u128 order id
12
- */
13
- export function orderId(owner, nonce) {
14
- nonce = BigInt(nonce)
15
- if (nonce < 0n || nonce > U64_MAX)
16
- throw new RangeError('Nonce must fit u64')
17
- const payload = xdr.ScVal.scvVec([
18
- Address.fromString(owner).toScVal(),
19
- xdr.ScVal.scvU64(nonce)
20
- ]).toXDR()
21
- const digest = hash(payload)
22
- let id = 0n
23
- for (let i = 0; i < 16; i++) {
24
- id = (id << 8n) | BigInt(digest[i])
25
- }
26
- return id
27
- }
28
-
29
- /**
30
- * Generate a u64 order nonce: wall clock (in milliseconds) shifted left by 22 bits plus 22 random bits.
31
- * The clock part fits 42 bits until the year 2109, so the nonce never overflows u64; the random part makes
32
- * collisions among the orders an account creates within the same millisecond negligible.
33
- * Internal to the client - consumers never supply or observe nonces, `buy`/`sell` generate one per call.
34
- * @return {bigint}
35
- * @internal
36
- */
37
- export function generateNonce() {
38
- return (BigInt(Date.now()) << 22n) | BigInt(randomBits22())
39
- }
40
-
41
- function randomBits22() {
42
- const crypto = globalThis.crypto
43
- if (crypto?.getRandomValues) {
44
- return crypto.getRandomValues(new Uint32Array(1))[0] & 0x3fffff
45
- }
46
- return Math.floor(Math.random() * 0x400000)
47
- }
1
+ import {xdr, Address, hash} from '@stellar/stellar-sdk'
2
+
3
+ const U64_MAX = (1n << 64n) - 1n
4
+
5
+ /**
6
+ * Compute the id of the order `owner` creates with `nonce`.
7
+ * Mirrors the contract: the first 16 bytes of `sha256(xdr(ScVec[owner: Address, nonce: u64]))`
8
+ * interpreted as a big-endian `u128` (the full 128 bits, no mask).
9
+ * @param {string} owner - Order owner address (account or contract)
10
+ * @param {bigint|number|string} nonce - Client-chosen u64 nonce
11
+ * @return {bigint} - u128 order id
12
+ */
13
+ export function orderId(owner, nonce) {
14
+ nonce = BigInt(nonce)
15
+ if (nonce < 0n || nonce > U64_MAX)
16
+ throw new RangeError('Nonce must fit u64')
17
+ const payload = xdr.ScVal.scvVec([
18
+ Address.fromString(owner).toScVal(),
19
+ xdr.ScVal.scvU64(nonce)
20
+ ]).toXDR()
21
+ const digest = hash(payload)
22
+ let id = 0n
23
+ for (let i = 0; i < 16; i++) {
24
+ id = (id << 8n) | BigInt(digest[i])
25
+ }
26
+ return id
27
+ }
28
+
29
+ /**
30
+ * Generate a u64 order nonce: wall clock (in milliseconds) shifted left by 22 bits plus 22 random bits.
31
+ * The clock part fits 42 bits until the year 2109, so the nonce never overflows u64; the random part makes
32
+ * collisions among the orders an account creates within the same millisecond negligible.
33
+ * Internal to the client - consumers never supply or observe nonces, `buy`/`sell` generate one per call.
34
+ * @return {bigint}
35
+ * @internal
36
+ */
37
+ export function generateNonce() {
38
+ return (BigInt(Date.now()) << 22n) | BigInt(randomBits22())
39
+ }
40
+
41
+ function randomBits22() {
42
+ const crypto = globalThis.crypto
43
+ if (crypto?.getRandomValues) {
44
+ return crypto.getRandomValues(new Uint32Array(1))[0] & 0x3fffff
45
+ }
46
+ return Math.floor(Math.random() * 0x400000)
47
+ }
@@ -1,13 +1,13 @@
1
1
  import {Emitter} from './emitter.js'
2
2
 
3
3
  /**
4
- * WebSocket client of the Aggregator push API
4
+ * WebSocket client of the AXIS API push channel
5
5
  *
6
6
  * Emits `open`, `close` and `error` (server errors not tied to a subscription).
7
7
  */
8
8
  export class AxisStreamClient extends Emitter {
9
9
  /**
10
- * @param {string} url - WebSocket endpoint, e.g. `wss://api.axis.markets/ws`
10
+ * @param {string} url - WebSocket endpoint, e.g. `wss://demo-api.axis.markets/ws` (testnet)
11
11
  * @param {StreamClientOptions} [options]
12
12
  */
13
13
  constructor(url, {WebSocket: WebSocketImpl, pingInterval = 20_000, minReconnectDelay = 1_000, maxReconnectDelay = 30_000} = {}) {