@axis-markets/client 0.8.3 → 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/README.md +684 -660
- package/lib/axis-client.cjs +1 -1
- package/lib/axis-client.cjs.map +1 -1
- package/package.json +1 -1
- package/src/allowance.js +78 -47
- package/src/axis-account.js +439 -85
- package/src/errors.js +150 -21
- package/src/index.d.ts +92 -8
- package/src/index.js +175 -21
package/package.json
CHANGED
package/src/allowance.js
CHANGED
|
@@ -1,47 +1,78 @@
|
|
|
1
|
-
import {PRICE_SCALE} from './asset-pair.js'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
*
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
*
|
|
31
|
-
* @
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
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
|
+
*/
|