@axis-markets/client 0.8.2 → 0.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@axis-markets/client",
3
- "version": "0.8.2",
3
+ "version": "0.9.0",
4
4
  "description": "AXIS contract and API client",
5
5
  "author": "team@stellar.expert",
6
6
  "license": "MIT",
package/src/allowance.js CHANGED
@@ -1,13 +1,15 @@
1
1
  import {PRICE_SCALE} from './asset-pair.js'
2
2
 
3
3
  /**
4
- * Allowance lifetime granted with a trade, in ledgers: ~30 days at 5s per ledger (the maximum entry TTL ~180 days)
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).
5
6
  */
6
- export const APPROVAL_TTL_LEDGERS = 518_400
7
+ export const MAX_APPROVAL_TTL_LEDGERS = 3_109_680
7
8
 
8
- /**
9
- * An allowance expiring within this number of ledgers is renewed with the next trade (~1 day)
10
- */
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) */
11
13
  export const APPROVAL_RENEW_LEDGERS = 17_280
12
14
 
13
15
  /**
@@ -16,12 +18,30 @@ export const APPROVAL_RENEW_LEDGERS = 17_280
16
18
  * @param {ApprovalPlanParams} params
17
19
  * @return {{amount: bigint, liveUntil: number}|undefined} - Approval to pass with the call, undefined when the current allowance already covers it
18
20
  */
19
- export function planApproval({required, committed, allowance, liveUntil, ledger}) {
21
+ export function planApproval({required, committed, allowance, liveUntil, ledger, ttl = APPROVAL_TTL_LEDGERS}) {
20
22
  const target = required + committed
21
23
  const expiring = liveUntil > 0 && liveUntil < ledger + APPROVAL_RENEW_LEDGERS
22
24
  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 + APPROVAL_TTL_LEDGERS}
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)}
25
45
  }
26
46
 
27
47
  /**
@@ -43,4 +63,16 @@ export function maxQuoteSpend(amount, price) {
43
63
  * @property {bigint} allowance - Current allowance (0 once expired)
44
64
  * @property {number} ledger - Current ledger sequence
45
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}
46
78
  */