@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/README.md +658 -589
- package/lib/axis-client.cjs +1 -1
- package/lib/axis-client.cjs.map +1 -1
- package/lib/footprint.cjs +1 -1
- package/lib/footprint.cjs.map +1 -1
- package/package.json +1 -1
- package/src/api-client.js +490 -423
- package/src/api-dates.js +1 -1
- package/src/axis-account.js +1009 -970
- package/src/axis-market.js +181 -197
- package/src/axis.js +409 -408
- package/src/contract-client.js +65 -63
- package/src/errors.js +131 -62
- package/src/footprint.d.ts +129 -102
- package/src/footprint.js +469 -419
- package/src/index.d.ts +1626 -1446
- package/src/index.js +717 -522
- package/src/order-id.js +47 -47
- package/src/stream-client.js +2 -2
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
|
+
}
|
package/src/stream-client.js
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
import {Emitter} from './emitter.js'
|
|
2
2
|
|
|
3
3
|
/**
|
|
4
|
-
* WebSocket client of the
|
|
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} = {}) {
|