@xoxno/sdk-js 1.0.217 → 1.0.219
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/dist/cjs/sdk/stellar/__tests__/auth-policy.test.d.ts +1 -0
- package/dist/cjs/sdk/stellar/__tests__/math/account.test.d.ts +10 -0
- package/dist/cjs/sdk/stellar/__tests__/math/parity.test.d.ts +14 -0
- package/dist/cjs/sdk/stellar/__tests__/math/scaled-rates.test.d.ts +6 -0
- package/dist/cjs/sdk/stellar/__tests__/route-verify.test.d.ts +1 -0
- package/dist/cjs/sdk/stellar/auth-policy.d.ts +106 -0
- package/dist/cjs/sdk/stellar/contracts.d.ts +6 -0
- package/dist/cjs/sdk/stellar/index.d.ts +3 -0
- package/dist/cjs/sdk/stellar/math/account.d.ts +183 -0
- package/dist/cjs/sdk/stellar/math/index.d.ts +12 -0
- package/dist/cjs/sdk/stellar/math/limits.d.ts +80 -0
- package/dist/cjs/sdk/stellar/math/projection.d.ts +53 -0
- package/dist/cjs/sdk/stellar/math/rates.d.ts +34 -0
- package/dist/cjs/sdk/stellar/math/scaled.d.ts +99 -0
- package/dist/cjs/sdk/stellar/route-verify.d.ts +100 -0
- package/dist/cjs/sdk/stellar/scval-encode.d.ts +10 -0
- package/dist/cjs/sdk/stellar/swap.d.ts +26 -6
- package/dist/index.cjs +1 -1
- package/dist/index.esm.js +1 -1
- package/dist/sdk/stellar/__tests__/auth-policy.test.d.ts +1 -0
- package/dist/sdk/stellar/__tests__/math/account.test.d.ts +10 -0
- package/dist/sdk/stellar/__tests__/math/parity.test.d.ts +14 -0
- package/dist/sdk/stellar/__tests__/math/scaled-rates.test.d.ts +6 -0
- package/dist/sdk/stellar/__tests__/route-verify.test.d.ts +1 -0
- package/dist/sdk/stellar/auth-policy.d.ts +106 -0
- package/dist/sdk/stellar/contracts.d.ts +6 -0
- package/dist/sdk/stellar/index.cjs +1 -1
- package/dist/sdk/stellar/index.d.ts +3 -0
- package/dist/sdk/stellar/index.esm.js +1 -1
- package/dist/sdk/stellar/math/account.d.ts +183 -0
- package/dist/sdk/stellar/math/index.d.ts +12 -0
- package/dist/sdk/stellar/math/limits.d.ts +80 -0
- package/dist/sdk/stellar/math/projection.d.ts +53 -0
- package/dist/sdk/stellar/math/rates.d.ts +34 -0
- package/dist/sdk/stellar/math/scaled.d.ts +99 -0
- package/dist/sdk/stellar/route-verify.d.ts +100 -0
- package/dist/sdk/stellar/scval-encode.d.ts +10 -0
- package/dist/sdk/stellar/swap.d.ts +26 -6
- package/package.json +1 -1
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract-exact fixed-point primitives for the Stellar lending math module.
|
|
3
|
+
*
|
|
4
|
+
* Mirrors `rs-lending-xlm/common/src/math/fp_core.rs` (`mul_div_*`,
|
|
5
|
+
* `rescale_*`, `div_by_int_half_up`), `common/src/math/fp.rs` (`Ray`, `Wad`)
|
|
6
|
+
* and `common/src/rates/scaling.rs` (`unscale_supply_floor`,
|
|
7
|
+
* `unscale_borrow_ceil`). All arithmetic is native `BigInt` on the raw decimal
|
|
8
|
+
* strings the API serves; nothing here touches floating point except the
|
|
9
|
+
* `*Short` display converters at the bottom.
|
|
10
|
+
*
|
|
11
|
+
* Every helper assumes the contract's non-negative domain (`Ray`, `Wad` and
|
|
12
|
+
* position balances are never negative on-chain); {@link toBigInt} enforces
|
|
13
|
+
* that at the string boundary.
|
|
14
|
+
*/
|
|
15
|
+
/** 10^27 — shares, indexes, rates and utilization. */
|
|
16
|
+
export declare const RAY: bigint;
|
|
17
|
+
/** 10^18 — USD values, prices per whole token, health factor. */
|
|
18
|
+
export declare const WAD: bigint;
|
|
19
|
+
/** 10,000 — risk ratios and fees. */
|
|
20
|
+
export declare const BPS = 10000n;
|
|
21
|
+
export declare const RAY_DECIMALS = 27;
|
|
22
|
+
export declare const WAD_DECIMALS = 18;
|
|
23
|
+
/** Parse a raw integer string (or bigint/number) into a non-negative bigint. */
|
|
24
|
+
export declare function toBigInt(value: string | number | bigint, label?: string): bigint;
|
|
25
|
+
/** `fp_core::mul_div_floor` — floor(x * y / d) for non-negative operands. */
|
|
26
|
+
export declare function mulDivFloor(x: bigint, y: bigint, d: bigint): bigint;
|
|
27
|
+
/** `fp_core::mul_div_ceil` — ceil(x * y / d) for non-negative operands. */
|
|
28
|
+
export declare function mulDivCeil(x: bigint, y: bigint, d: bigint): bigint;
|
|
29
|
+
/** `fp_core::mul_div_half_up` — floor((x * y + d/2) / d) for non-negative operands. */
|
|
30
|
+
export declare function mulDivHalfUp(x: bigint, y: bigint, d: bigint): bigint;
|
|
31
|
+
/** `fp_core::div_by_int_half_up` — a / b rounded half up (non-negative a, b > 0). */
|
|
32
|
+
export declare function divByIntHalfUp(a: bigint, b: bigint): bigint;
|
|
33
|
+
/** `fp_core::rescale_floor` — move `a` between decimal scales, truncating when dropping digits. */
|
|
34
|
+
export declare function rescaleFloor(a: bigint, fromDecimals: number, toDecimals: number): bigint;
|
|
35
|
+
/** `fp_core::rescale_ceil` — like {@link rescaleFloor} but rounds a dropped remainder up. */
|
|
36
|
+
export declare function rescaleCeil(a: bigint, fromDecimals: number, toDecimals: number): bigint;
|
|
37
|
+
/** `fp_core::rescale_half_up` — like {@link rescaleFloor} but rounds a dropped remainder half up. */
|
|
38
|
+
export declare function rescaleHalfUp(a: bigint, fromDecimals: number, toDecimals: number): bigint;
|
|
39
|
+
export type Rounding = 'floor' | 'ceil' | 'halfUp';
|
|
40
|
+
/**
|
|
41
|
+
* Scaled shares → token base units with one rounding direction at both steps.
|
|
42
|
+
* `scaling.rs`: `unscale_supply_floor` = `mul_floor` + `to_asset_floor`,
|
|
43
|
+
* `unscale_borrow_ceil` = `mul_ceil` + `to_asset_ceil`, `unscale_supply` /
|
|
44
|
+
* `unscale_borrow` (displayed balance) = `mul` (half-up) + `to_asset` (half-up).
|
|
45
|
+
*/
|
|
46
|
+
export declare function unscale(scaledRay: bigint, indexRay: bigint, decimals: number, rounding: Rounding): bigint;
|
|
47
|
+
/**
|
|
48
|
+
* Current supplied amount in token base units — `scaling.rs::unscale_supply_floor`
|
|
49
|
+
* (`mul_floor` then `to_asset_floor`). This is what the pool pays on a full
|
|
50
|
+
* withdrawal, so the SDK never reports more collateral than the contract releases.
|
|
51
|
+
*/
|
|
52
|
+
export declare function unscaleSupplyFloor(scaledRay: string | bigint, indexRay: string | bigint, decimals: number): bigint;
|
|
53
|
+
/**
|
|
54
|
+
* Current borrowed amount in token base units — `scaling.rs::unscale_borrow_ceil`
|
|
55
|
+
* (`mul_ceil` then `to_asset_ceil`). A repayment of at least this amount burns
|
|
56
|
+
* every debt share (`resolve_repay`), so a max repay always covers the debt.
|
|
57
|
+
*
|
|
58
|
+
* Note: the xoxno-ui mirror (`stellar-scaled-math.ts`) rounds the share product
|
|
59
|
+
* half-up before the ceil rescale; the contract ceils both steps. This module
|
|
60
|
+
* follows the contract.
|
|
61
|
+
*/
|
|
62
|
+
export declare function unscaleBorrowCeil(scaledRay: string | bigint, indexRay: string | bigint, decimals: number): bigint;
|
|
63
|
+
/**
|
|
64
|
+
* Displayed balance in token base units — `scaling.rs::unscale_supply` /
|
|
65
|
+
* `unscale_borrow` (half-up at both steps). `resolve_withdrawal` treats a
|
|
66
|
+
* request at or above this value as a full withdrawal.
|
|
67
|
+
*/
|
|
68
|
+
export declare function unscaleHalfUp(scaledRay: string | bigint, indexRay: string | bigint, decimals: number): bigint;
|
|
69
|
+
/** `Ray::from_asset` — token base units → RAY (exact for decimals ≤ 27). */
|
|
70
|
+
export declare function baseToRay(amountBase: bigint, decimals: number): bigint;
|
|
71
|
+
/**
|
|
72
|
+
* USD value (WAD) of a scaled position — `common/src/rates/value.rs`
|
|
73
|
+
* `position_value` (halfUp) / `position_value_floor` / `position_value_ceil`:
|
|
74
|
+
* `asset_ray = round(scaled * index / RAY)`, `asset_wad = round(asset_ray / 1e9)`,
|
|
75
|
+
* `usd_wad = round(asset_wad * price / WAD)` with one direction at all three steps.
|
|
76
|
+
*/
|
|
77
|
+
export declare function positionValueWad(scaledRay: bigint, indexRay: bigint, priceWad: bigint, rounding: Rounding): bigint;
|
|
78
|
+
/**
|
|
79
|
+
* USD value (WAD) of a token amount in base units, using the same last two
|
|
80
|
+
* steps as {@link positionValueWad}. Used by projections, where the hypothetical
|
|
81
|
+
* amount is a token quantity rather than shares.
|
|
82
|
+
*/
|
|
83
|
+
export declare function baseValueWad(amountBase: bigint, decimals: number, priceWad: bigint, rounding: Rounding): bigint;
|
|
84
|
+
/** WAD → JS number (display only; ~1e-16 relative precision). */
|
|
85
|
+
export declare const wadToNumber: (wad: bigint) => number;
|
|
86
|
+
/** RAY → JS number (display only). */
|
|
87
|
+
export declare const rayToNumber: (ray: bigint) => number;
|
|
88
|
+
/** Token base units → human-readable number (display only). */
|
|
89
|
+
export declare const baseToShort: (amountBase: bigint, decimals: number) => number;
|
|
90
|
+
/**
|
|
91
|
+
* Decimal JS number (a `usdPrice` / `*Short` API field) → integer at `decimals`.
|
|
92
|
+
* Uses `toFixed` so no float arithmetic runs after the parse; precision is that
|
|
93
|
+
* of the input double (~15–16 significant digits). Domain: `0 < value < 1e21`
|
|
94
|
+
* (`toFixed` switches to exponential notation at 1e21); anything outside,
|
|
95
|
+
* non-finite included, maps to 0 rather than throwing.
|
|
96
|
+
*/
|
|
97
|
+
export declare function numberToScaled(value: number, decimals: number): bigint;
|
|
98
|
+
/** JS USD price → WAD, see {@link numberToScaled}. */
|
|
99
|
+
export declare const numberToWad: (usd: number) => bigint;
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Decoder and pre-signing verifier for the aggregator router's packed route
|
|
3
|
+
* (`routeXdr` / `swap_xdr`).
|
|
4
|
+
*
|
|
5
|
+
* The quote server is unauthenticated and returns the route as an opaque blob
|
|
6
|
+
* next to separate JSON display fields. The router enforces only the minimum
|
|
7
|
+
* that is ENCODED in the blob, so a caller must decode it and compare it with
|
|
8
|
+
* its own request before a wallet signs. The decoder mirrors
|
|
9
|
+
* `contracts/swap-aggregator/src/program.rs` (`Program::decode` + `validate`)
|
|
10
|
+
* and `execute::run` check for check; the encoder is `./scval-encode.ts`.
|
|
11
|
+
*/
|
|
12
|
+
import { type StellarNetwork } from './contracts.js';
|
|
13
|
+
import { type StellarStrategySwapInput, type StellarSwapVenue } from './scval-encode.js';
|
|
14
|
+
/**
|
|
15
|
+
* Cap on the serialized payload, checked before XDR parsing so a hostile
|
|
16
|
+
* response cannot force a proportional allocation. The largest legal payload
|
|
17
|
+
* (256 addresses, 126 amounts, 346 program bytes) is below 16 KiB.
|
|
18
|
+
*/
|
|
19
|
+
export declare const STELLAR_ROUTE_MAX_BYTES: number;
|
|
20
|
+
export type StellarRouteErrorCode = 'MALFORMED' | 'OVERSIZED' | 'UNSUPPORTED_VERSION' | 'UNKNOWN_OPCODE' | 'INVALID_STRUCTURE' | 'TOKEN_IN_MISMATCH' | 'TOKEN_OUT_MISMATCH' | 'MIN_OUT_TOO_LOW' | 'AMOUNT_IN_EXCEEDED' | 'EXPECTATION_MISSING';
|
|
21
|
+
/** Thrown when a route cannot be decoded or disagrees with the caller's request. */
|
|
22
|
+
export declare class StellarRouteVerificationError extends Error {
|
|
23
|
+
readonly code: StellarRouteErrorCode;
|
|
24
|
+
constructor(code: StellarRouteErrorCode, message: string);
|
|
25
|
+
}
|
|
26
|
+
export type DecodedStellarRouteMode = {
|
|
27
|
+
kind: 'all';
|
|
28
|
+
} | {
|
|
29
|
+
kind: 'prev';
|
|
30
|
+
} | {
|
|
31
|
+
kind: 'fixed';
|
|
32
|
+
amount: bigint;
|
|
33
|
+
} | {
|
|
34
|
+
kind: 'ppm';
|
|
35
|
+
ppm: number;
|
|
36
|
+
};
|
|
37
|
+
export type DecodedStellarRouteOp = {
|
|
38
|
+
kind: 'swap';
|
|
39
|
+
venue: StellarSwapVenue;
|
|
40
|
+
mode: DecodedStellarRouteMode;
|
|
41
|
+
pool: string;
|
|
42
|
+
tokenIn: string;
|
|
43
|
+
tokenOut: string;
|
|
44
|
+
} | {
|
|
45
|
+
kind: 'burn' | 'mint';
|
|
46
|
+
pool: string;
|
|
47
|
+
shareToken: string;
|
|
48
|
+
/** Burn: first index of the per-constituent floor run. Mint: index of the minimum shares. */
|
|
49
|
+
amountIndex: number;
|
|
50
|
+
};
|
|
51
|
+
export interface DecodedStellarRoute {
|
|
52
|
+
version: number;
|
|
53
|
+
tokenIn: string;
|
|
54
|
+
tokenOut: string;
|
|
55
|
+
/** The minimum output the router enforces on chain (`amounts[min_out]`). */
|
|
56
|
+
totalMinOut: bigint;
|
|
57
|
+
referralId: number;
|
|
58
|
+
assets: string[];
|
|
59
|
+
amounts: bigint[];
|
|
60
|
+
ops: DecodedStellarRouteOp[];
|
|
61
|
+
/** Split weights in parts per million. */
|
|
62
|
+
weights: number[];
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Any form the builders accept: base64 `routeXdr`, `0x` hex, raw bytes,
|
|
66
|
+
* `{ routeXdr }` / `{ swapXdr }` / `{ bytes }`, or a decoded payload object
|
|
67
|
+
* (encoded locally first, so the check covers exactly what would be signed).
|
|
68
|
+
*/
|
|
69
|
+
export type StellarRouteBytesInput = StellarStrategySwapInput;
|
|
70
|
+
/**
|
|
71
|
+
* Decode and structurally validate route bytes. Applies every check of the
|
|
72
|
+
* on-chain decoder, so a route that `Program::decode` rejects does not decode
|
|
73
|
+
* here. Venue behaviour (pools, liquidity, budget) is not checked.
|
|
74
|
+
* @throws StellarRouteVerificationError
|
|
75
|
+
*/
|
|
76
|
+
export declare function decodeStellarRouteBytes(route: StellarRouteBytesInput): DecodedStellarRoute;
|
|
77
|
+
/**
|
|
78
|
+
* Contract id of a quote-server token id: `C…` as is, `XLM` / `native` and
|
|
79
|
+
* `CODE:GISSUER` through the Stellar Asset Contract id of `network`.
|
|
80
|
+
* @throws StellarRouteVerificationError when the id needs a network and none is given.
|
|
81
|
+
*/
|
|
82
|
+
export declare function stellarTokenContractId(tokenId: string, network?: StellarNetwork): string;
|
|
83
|
+
/** What the CALLER asked for. Take these from request state, not from the response. */
|
|
84
|
+
export interface StellarRouteExpectation {
|
|
85
|
+
/** Input token contract id (`C…`). */
|
|
86
|
+
tokenIn: string;
|
|
87
|
+
/** Output token contract id (`C…`). */
|
|
88
|
+
tokenOut: string;
|
|
89
|
+
/** Lowest acceptable encoded `total_min_out`, base units. This is the value shown to the user. */
|
|
90
|
+
minOut: string | bigint;
|
|
91
|
+
/** Input amount, base units. When given, no fixed-size leg may spend more of `tokenIn`. */
|
|
92
|
+
amountIn?: string | bigint;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Decode `route` and require it to match `expected`. Call before signing any
|
|
96
|
+
* transaction that carries route bytes; never sign when this throws.
|
|
97
|
+
* @returns The decoded route, for display or further checks.
|
|
98
|
+
* @throws StellarRouteVerificationError
|
|
99
|
+
*/
|
|
100
|
+
export declare function verifyStellarRouteBytes(route: StellarRouteBytesInput, expected: StellarRouteExpectation): DecodedStellarRoute;
|
|
@@ -21,6 +21,16 @@ export declare const STELLAR_SWAP_VENUE_OPCODE: Record<StellarSwapVenue, number>
|
|
|
21
21
|
export declare const STELLAR_PROGRAM_VERSION = 1;
|
|
22
22
|
/** Split weights are parts per million. */
|
|
23
23
|
export declare const PPM_DENOMINATOR = 1000000;
|
|
24
|
+
export declare const OP_BURN = 5;
|
|
25
|
+
export declare const OP_MINT = 6;
|
|
26
|
+
export declare const MODE_ALL = 0;
|
|
27
|
+
export declare const MODE_PREV = 1;
|
|
28
|
+
export declare const MODE_FIXED_BASE = 2;
|
|
29
|
+
export declare const MODE_PPM_BASE = 128;
|
|
30
|
+
export declare const MAX_OPS = 48;
|
|
31
|
+
export declare const MAX_WEIGHTS = 32;
|
|
32
|
+
export declare const MAX_ASSETS = 256;
|
|
33
|
+
export declare const MAX_AMOUNTS: number;
|
|
24
34
|
export interface StellarStrategySwapHopInput {
|
|
25
35
|
/** Quote-side estimate; not carried on the wire. */
|
|
26
36
|
amountOut?: string;
|
|
@@ -11,8 +11,27 @@
|
|
|
11
11
|
* opaque route bytes to the controller, which forwards them to the aggregator.
|
|
12
12
|
*/
|
|
13
13
|
import type { StellarAggregatorQuoteResponseDto } from '@xoxno/types';
|
|
14
|
+
import { type StellarNetwork } from './contracts.js';
|
|
14
15
|
import type { BuiltStellarTx, StellarBuilderOptions } from './lending.js';
|
|
15
16
|
import { type StellarStrategyPayloadInput, type StellarStrategySwapHopInput, type StellarStrategySwapInput, type StellarStrategySwapPathInput } from './scval-encode.js';
|
|
17
|
+
/**
|
|
18
|
+
* The caller's own request, used to check a quote before it is signed. Every
|
|
19
|
+
* field that is absent falls back to the quote's JSON, which proves only that
|
|
20
|
+
* the response agrees with itself — pass request state whenever it is known.
|
|
21
|
+
*/
|
|
22
|
+
export interface StellarQuoteExpectation {
|
|
23
|
+
tokenIn?: string;
|
|
24
|
+
tokenOut?: string;
|
|
25
|
+
/** Lowest acceptable minimum output, base units. */
|
|
26
|
+
minOut?: string | bigint;
|
|
27
|
+
amountIn?: string | bigint;
|
|
28
|
+
}
|
|
29
|
+
export interface StellarQuoteMapOptions {
|
|
30
|
+
referralId?: number | string;
|
|
31
|
+
/** Needed only when a token id is `XLM` or `CODE:GISSUER` instead of `C…`. */
|
|
32
|
+
network?: StellarNetwork;
|
|
33
|
+
expected?: StellarQuoteExpectation;
|
|
34
|
+
}
|
|
16
35
|
export type StellarStrategyPayload = StellarStrategyPayloadInput;
|
|
17
36
|
export type StellarStrategySwap = StellarStrategySwapInput;
|
|
18
37
|
export type StellarStrategySwapHop = StellarStrategySwapHopInput;
|
|
@@ -55,15 +74,16 @@ export declare function buildStellarBatchSwapTx(opts: StellarBatchSwapBuilderOpt
|
|
|
55
74
|
* Translate a quote-server response into the decoded `StrategyPayload` shape.
|
|
56
75
|
* Prefer `mapQuoteResponseToStrategySwap` when the quote includes `routeXdr`.
|
|
57
76
|
*/
|
|
58
|
-
export declare function mapQuoteResponseToStrategyPayload(quote: StellarAggregatorQuoteResponseDto, opts?:
|
|
59
|
-
referralId?: number | string;
|
|
60
|
-
}): StellarStrategyPayloadInput;
|
|
77
|
+
export declare function mapQuoteResponseToStrategyPayload(quote: StellarAggregatorQuoteResponseDto, opts?: Pick<StellarQuoteMapOptions, 'referralId'>): StellarStrategyPayloadInput;
|
|
61
78
|
/**
|
|
62
79
|
* Return the executable quote route when the API provided it; otherwise build
|
|
63
80
|
* the decoded fallback payload from hop/path fields.
|
|
81
|
+
*
|
|
82
|
+
* Either form is decoded and checked before it is returned: it must spend and
|
|
83
|
+
* deliver the expected pair and enforce a minimum that is not below the
|
|
84
|
+
* quote's `amountOutMin` (what a UI displays) nor below `expected.minOut`.
|
|
85
|
+
* @throws StellarRouteVerificationError — never sign a quote that throws here.
|
|
64
86
|
*/
|
|
65
|
-
export declare function mapQuoteResponseToStrategySwap(quote: StellarAggregatorQuoteResponseDto, opts?:
|
|
66
|
-
referralId?: number | string;
|
|
67
|
-
}): StellarStrategySwapInput;
|
|
87
|
+
export declare function mapQuoteResponseToStrategySwap(quote: StellarAggregatorQuoteResponseDto, opts?: StellarQuoteMapOptions): StellarStrategySwapInput;
|
|
68
88
|
/** @deprecated Use `mapQuoteResponseToStrategyPayload`. */
|
|
69
89
|
export declare const mapQuoteResponseToAggregatorSwap: typeof mapQuoteResponseToStrategyPayload;
|