@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/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/api-client.js +18 -3
- package/src/axis-account.js +439 -85
- package/src/errors.js +150 -21
- package/src/index.d.ts +116 -10
- 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
|
+
*/
|
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
|
|
252
|
-
*
|
|
253
|
-
*
|
|
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
|
/**
|