@axis-markets/client 0.8.2 → 0.8.3
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 +660 -658
- package/lib/axis-client.cjs +1 -1
- package/lib/axis-client.cjs.map +1 -1
- package/package.json +1 -1
- package/src/allowance.js +47 -46
- package/src/axis-account.js +1022 -1015
- package/src/axis.js +470 -465
- package/src/index.d.ts +1637 -1632
package/package.json
CHANGED
package/src/allowance.js
CHANGED
|
@@ -1,46 +1,47 @@
|
|
|
1
|
-
import {PRICE_SCALE} from './asset-pair.js'
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
*
|
|
5
|
-
*/
|
|
6
|
-
export const APPROVAL_TTL_LEDGERS =
|
|
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}) {
|
|
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 +
|
|
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
|
-
|
|
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
|
+
*/
|