@axis-markets/client 0.8.3 → 0.9.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axis-markets/client",
3
- "version": "0.8.3",
3
+ "version": "0.9.1",
4
4
  "description": "AXIS contract and API client",
5
5
  "author": "team@stellar.expert",
6
6
  "license": "MIT",
package/src/allowance.js CHANGED
@@ -1,47 +1,78 @@
1
- import {PRICE_SCALE} from './asset-pair.js'
2
-
3
- /**
4
- * Default allowance lifetime granted with a trade, in ledgers: ~180 days at 5s per ledger (the maximum entry TTL)
5
- */
6
- export const APPROVAL_TTL_LEDGERS = 3_110_400
7
-
8
- /**
9
- * An allowance expiring within this number of ledgers is renewed with the next trade (~1 day)
10
- */
11
- export const APPROVAL_RENEW_LEDGERS = 17_280
12
-
13
- /**
14
- * Decide whether a call needs an in-call approval and adjust it. The approval amount is absolute and one allowance backs
15
- * every order selling the token across all markets, so it has to cover this call plus every order selling the same asset
16
- * @param {ApprovalPlanParams} params
17
- * @return {{amount: bigint, liveUntil: number}|undefined} - Approval to pass with the call, undefined when the current allowance already covers it
18
- */
19
- export function planApproval({required, committed, allowance, liveUntil, ledger, ttl = APPROVAL_TTL_LEDGERS}) {
20
- const target = required + committed
21
- const expiring = liveUntil > 0 && liveUntil < ledger + APPROVAL_RENEW_LEDGERS
22
- if (allowance >= target && !expiring)
23
- return undefined //TODO: reduce outstanding allowances if the backed amount is reduced (expired or cancelled)
24
- return {amount: target, liveUntil: ledger + ttl}
25
- }
26
-
27
- /**
28
- * Max amount of `selling` tokens a buy trade may cost: `ceil(amount × price / 10^18)`
29
- * @param {bigint} amount - Amount of `buying` tokens to acquire
30
- * @param {bigint} price - Limit price (`selling` per 1 `buying`, with AXIS standard price decimals)
31
- * @return {bigint}
32
- */
33
- export function maxQuoteSpend(amount, price) {
34
- if (amount <= 0n || price <= 0n)
35
- return 0n
36
- return (amount * price + PRICE_SCALE - 1n) / PRICE_SCALE
37
- }
38
-
39
- /**
40
- * @typedef {{}} ApprovalPlanParams
41
- * @property {bigint} required - Max amount of the selling asset the call may spend
42
- * @property {bigint} committed - Remaining amount of orders in the book selling the asset
43
- * @property {bigint} allowance - Current allowance (0 once expired)
44
- * @property {number} ledger - Current ledger sequence
45
- * @property {number} [liveUntil] - Ledger the current allowance lives until (0 or omitted when unknown)
46
- * @property {number} [ttl] - Lifetime of a new allowance in ledgers, {@link APPROVAL_TTL_LEDGERS} by default
47
- */
1
+ import {PRICE_SCALE} from './asset-pair.js'
2
+
3
+ /**
4
+ * Longest allowance lifetime the client grants, in ledgers: ~180 days at 5s per ledger, one hour (720 ledgers) short of
5
+ * the network maximum entry TTL (3,110,400 ledgers).
6
+ */
7
+ export const MAX_APPROVAL_TTL_LEDGERS = 3_109_680
8
+
9
+ /** Default allowance lifetime granted with a trade, in ledgers (~180 days) */
10
+ export const APPROVAL_TTL_LEDGERS = MAX_APPROVAL_TTL_LEDGERS
11
+
12
+ /** An allowance expiring within this number of ledgers is renewed with the next trade (~1 day) */
13
+ export const APPROVAL_RENEW_LEDGERS = 17_280
14
+
15
+ /**
16
+ * Decide whether a call needs an in-call approval and adjust it. The approval amount is absolute and one allowance backs
17
+ * every order selling the token across all markets, so it has to cover this call plus every order selling the same asset
18
+ * @param {ApprovalPlanParams} params
19
+ * @return {{amount: bigint, liveUntil: number}|undefined} - Approval to pass with the call, undefined when the current allowance already covers it
20
+ */
21
+ export function planApproval({required, committed, allowance, liveUntil, ledger, ttl = APPROVAL_TTL_LEDGERS}) {
22
+ const target = required + committed
23
+ const expiring = liveUntil > 0 && liveUntil < ledger + APPROVAL_RENEW_LEDGERS
24
+ if (allowance >= target && !expiring)
25
+ return undefined
26
+ return {amount: target, liveUntil: ledger + Math.min(ttl, MAX_APPROVAL_TTL_LEDGERS)}
27
+ }
28
+
29
+ /**
30
+ * Decide whether the allowance can be lowered once the open orders selling the token have been canceled or shrunk.
31
+ * @param {ReductionPlanParams} params
32
+ * @return {{amount: bigint, liveUntil: number}|undefined} - Approval to pass with the call, undefined when the allowance does not exceed the target
33
+ */
34
+ export function planReduction({target, allowance, liveUntil = 0, ledger, ttl = APPROVAL_TTL_LEDGERS}) {
35
+ if (liveUntil > 0 && liveUntil < ledger)
36
+ return undefined //an expired allowance reads as zero
37
+ if (allowance <= target)
38
+ return undefined
39
+ if (target <= 0n)
40
+ return {amount: 0n, liveUntil: 0} //the token checks no lifetime for a zero amount
41
+ //a nonzero approval must outlive the ledger it applies in: keep a lifetime that does, renew a short one
42
+ if (liveUntil > ledger + APPROVAL_RENEW_LEDGERS)
43
+ return {amount: target, liveUntil}
44
+ return {amount: target, liveUntil: ledger + Math.min(ttl, MAX_APPROVAL_TTL_LEDGERS)}
45
+ }
46
+
47
+ /**
48
+ * Max amount of `selling` tokens a buy trade may cost: `ceil(amount × price / 10^18)`
49
+ * @param {bigint} amount - Amount of `buying` tokens to acquire
50
+ * @param {bigint} price - Limit price (`selling` per 1 `buying`, with AXIS standard price decimals)
51
+ * @return {bigint}
52
+ */
53
+ export function maxQuoteSpend(amount, price) {
54
+ if (amount <= 0n || price <= 0n)
55
+ return 0n
56
+ return (amount * price + PRICE_SCALE - 1n) / PRICE_SCALE
57
+ }
58
+
59
+ /**
60
+ * @typedef {{}} ApprovalPlanParams
61
+ * @property {bigint} required - Max amount of the selling asset the call may spend
62
+ * @property {bigint} committed - Remaining amount of orders in the book selling the asset
63
+ * @property {bigint} allowance - Current allowance (0 once expired)
64
+ * @property {number} ledger - Current ledger sequence
65
+ * @property {number} [liveUntil] - Ledger the current allowance lives until (0 or omitted when unknown)
66
+ * @property {number} [ttl] - Lifetime of a new allowance in ledgers, {@link APPROVAL_TTL_LEDGERS} by default, at most
67
+ * {@link MAX_APPROVAL_TTL_LEDGERS}
68
+ */
69
+
70
+ /**
71
+ * @typedef {{}} ReductionPlanParams
72
+ * @property {bigint} target - Amount the open orders selling the asset still need after the call
73
+ * @property {bigint} allowance - Current allowance
74
+ * @property {number} ledger - Current ledger sequence
75
+ * @property {number} [liveUntil] - Ledger the current allowance lives until (0 or omitted when unknown)
76
+ * @property {number} [ttl] - Lifetime of a renewed allowance in ledgers, {@link APPROVAL_TTL_LEDGERS} by default, at
77
+ * most {@link MAX_APPROVAL_TTL_LEDGERS}
78
+ */
package/src/api-client.js CHANGED
@@ -248,9 +248,11 @@ export class AxisApiClient {
248
248
  * @typedef {{}} QuotePathStep
249
249
  * @property {string} selling - Selling asset contract id at this hop
250
250
  * @property {string} buying - Buying asset contract id at this hop
251
- * @property {string[]} orders - Maker order IDs matched at this hop
252
- * @property {string} [worstPrice] - Highest price among the matched orders (maker `buying` per `selling`, 18 decimals):
253
- * a trade limit at this price crosses every listed order
251
+ * @property {string[]} orders - Maker order IDs to pass to the contract: the orders the quote fills, followed on a
252
+ * direct route by spare orders the contract reaches only when it passes over one of them (at most 19 in all)
253
+ * @property {string} [worstPrice] - Highest price among the orders the quote fills (maker `buying` per `selling`, 18
254
+ * decimals): a trade limit at this price crosses them all. Spares are priced at or below it, except in a `maxPrice`
255
+ * crossing, where they reach up to `maxPrice`
254
256
  */
255
257
 
256
258
  /**
@@ -325,9 +327,22 @@ export class AxisApiClient {
325
327
  /**
326
328
  * @typedef {{}} ContractInfo - Contract state tracked by the indexer
327
329
  * @property {string} address - AXIS contract address
330
+ * @property {'mainnet'|'testnet'} [network] - Stellar network of the contract
328
331
  * @property {boolean} frozen - Whether trading is blocked by the safety admin
329
332
  * @property {ContractInfoConfig} [config] - Contract configuration
330
333
  * @property {ContractInfoMarket[]} markets - Markets opened by `subsidize`
334
+ * @property {ContractInfoAsset[]} [assets] - Metadata of the market tokens
335
+ */
336
+
337
+ /**
338
+ * @typedef {{}} ContractInfoAsset - Market token metadata (only `contract` until the AXIS API resolves it)
339
+ * @property {string} contract - Token contract address
340
+ * @property {string} [code] - Asset code (token symbol)
341
+ * @property {string} [issuer] - Issuer account of a classic asset, `native` for XLM, absent for a SEP-41 token
342
+ * @property {number} [decimals] - Token decimals
343
+ * @property {string} [name] - Asset name
344
+ * @property {string} [domain] - Home domain of the issuer
345
+ * @property {string} [icon] - Icon URL
331
346
  */
332
347
 
333
348
  /**