@zofai/zo-sdk 0.2.29 → 0.2.31
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/abstract/BaseAPI.cjs +3 -0
- package/dist/abstract/BaseAPI.cjs.map +1 -1
- package/dist/abstract/BaseAPI.d.cts +3 -2
- package/dist/abstract/BaseAPI.d.cts.map +1 -1
- package/dist/abstract/BaseAPI.d.mts +3 -2
- package/dist/abstract/BaseAPI.d.mts.map +1 -1
- package/dist/abstract/BaseAPI.mjs +3 -0
- package/dist/abstract/BaseAPI.mjs.map +1 -1
- package/dist/abstract/BaseDataAPI.cjs +48 -2
- package/dist/abstract/BaseDataAPI.cjs.map +1 -1
- package/dist/abstract/BaseDataAPI.d.cts +15 -2
- package/dist/abstract/BaseDataAPI.d.cts.map +1 -1
- package/dist/abstract/BaseDataAPI.d.mts +15 -2
- package/dist/abstract/BaseDataAPI.d.mts.map +1 -1
- package/dist/abstract/BaseDataAPI.mjs +48 -2
- package/dist/abstract/BaseDataAPI.mjs.map +1 -1
- package/dist/consts/deployments-zlp-mainnet.json +1 -1
- package/dist/consts/deployments-zo-oracle-mainnet.json +18 -1
- package/dist/data.cjs +19 -7
- package/dist/data.cjs.map +1 -1
- package/dist/data.d.cts.map +1 -1
- package/dist/data.d.mts.map +1 -1
- package/dist/data.mjs +19 -7
- package/dist/data.mjs.map +1 -1
- package/dist/implementations/SLPDataAPI.cjs +26 -8
- package/dist/implementations/SLPDataAPI.cjs.map +1 -1
- package/dist/implementations/SLPDataAPI.d.cts.map +1 -1
- package/dist/implementations/SLPDataAPI.d.mts.map +1 -1
- package/dist/implementations/SLPDataAPI.mjs +26 -8
- package/dist/implementations/SLPDataAPI.mjs.map +1 -1
- package/dist/implementations/USDZDataAPI.cjs +26 -8
- package/dist/implementations/USDZDataAPI.cjs.map +1 -1
- package/dist/implementations/USDZDataAPI.d.cts.map +1 -1
- package/dist/implementations/USDZDataAPI.d.mts.map +1 -1
- package/dist/implementations/USDZDataAPI.mjs +26 -8
- package/dist/implementations/USDZDataAPI.mjs.map +1 -1
- package/dist/implementations/ZBTCVCDataAPI.cjs +19 -7
- package/dist/implementations/ZBTCVCDataAPI.cjs.map +1 -1
- package/dist/implementations/ZBTCVCDataAPI.d.cts.map +1 -1
- package/dist/implementations/ZBTCVCDataAPI.d.mts.map +1 -1
- package/dist/implementations/ZBTCVCDataAPI.mjs +19 -7
- package/dist/implementations/ZBTCVCDataAPI.mjs.map +1 -1
- package/dist/implementations/ZLPAPI.cjs +456 -123
- package/dist/implementations/ZLPAPI.cjs.map +1 -1
- package/dist/implementations/ZLPAPI.d.cts +46 -1
- package/dist/implementations/ZLPAPI.d.cts.map +1 -1
- package/dist/implementations/ZLPAPI.d.mts +46 -1
- package/dist/implementations/ZLPAPI.d.mts.map +1 -1
- package/dist/implementations/ZLPAPI.mjs +457 -124
- package/dist/implementations/ZLPAPI.mjs.map +1 -1
- package/dist/implementations/ZLPDataAPI.cjs +64 -8
- package/dist/implementations/ZLPDataAPI.cjs.map +1 -1
- package/dist/implementations/ZLPDataAPI.d.cts +16 -0
- package/dist/implementations/ZLPDataAPI.d.cts.map +1 -1
- package/dist/implementations/ZLPDataAPI.d.mts +16 -0
- package/dist/implementations/ZLPDataAPI.d.mts.map +1 -1
- package/dist/implementations/ZLPDataAPI.mjs +64 -8
- package/dist/implementations/ZLPDataAPI.mjs.map +1 -1
- package/dist/index.cjs +1 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -0
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +2 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/dist/interfaces/base.d.cts +14 -1
- package/dist/interfaces/base.d.cts.map +1 -1
- package/dist/interfaces/base.d.mts +14 -1
- package/dist/interfaces/base.d.mts.map +1 -1
- package/dist/interfaces/zlp.d.cts +19 -0
- package/dist/interfaces/zlp.d.cts.map +1 -1
- package/dist/interfaces/zlp.d.mts +19 -0
- package/dist/interfaces/zlp.d.mts.map +1 -1
- package/dist/traderPositions.cjs +130 -0
- package/dist/traderPositions.cjs.map +1 -0
- package/dist/traderPositions.d.cts +72 -0
- package/dist/traderPositions.d.cts.map +1 -0
- package/dist/traderPositions.d.mts +72 -0
- package/dist/traderPositions.d.mts.map +1 -0
- package/dist/traderPositions.mjs +124 -0
- package/dist/traderPositions.mjs.map +1 -0
- package/docs/api-reference.md +45 -0
- package/docs/architecture.md +21 -3
- package/docs/common-operations.md +56 -8
- package/docs/getting-started.md +8 -3
- package/docs/lp-specific-features.md +89 -12
- package/docs/swap-integration.md +52 -20
- package/package.json +1 -1
- package/src/abstract/BaseAPI.ts +6 -2
- package/src/abstract/BaseDataAPI.ts +67 -3
- package/src/consts/deployments-zlp-mainnet.json +1 -1
- package/src/consts/deployments-zo-oracle-mainnet.json +18 -1
- package/src/data.ts +19 -7
- package/src/implementations/SLPDataAPI.ts +34 -16
- package/src/implementations/USDZDataAPI.ts +32 -14
- package/src/implementations/ZBTCVCDataAPI.ts +23 -11
- package/src/implementations/ZLPAPI.ts +760 -151
- package/src/implementations/ZLPDataAPI.ts +79 -14
- package/src/index.ts +2 -0
- package/src/interfaces/base.ts +15 -1
- package/src/interfaces/zlp.ts +170 -0
- package/src/traderPositions.ts +218 -0
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
const DEFAULT_PAGE_LIMIT = 200;
|
|
2
|
+
/**
|
|
3
|
+
* Parse `PositionName<collateral, index, LONG|SHORT>` from a Move type string.
|
|
4
|
+
*/
|
|
5
|
+
export function parsePositionNameType(positionNameType) {
|
|
6
|
+
const marker = 'PositionName<';
|
|
7
|
+
const start = positionNameType.indexOf(marker);
|
|
8
|
+
if (start < 0) {
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
const inner = positionNameType.slice(start + marker.length).split('>')[0];
|
|
12
|
+
if (!inner) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const parts = inner.split(',').map(part => part.trim());
|
|
16
|
+
if (parts.length < 3) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
return {
|
|
20
|
+
symbol0: parts[0],
|
|
21
|
+
symbol1: parts[1],
|
|
22
|
+
long: parts[2].includes('LONG'),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
function tokenKeyToMoveType(tokenKey, consts) {
|
|
26
|
+
const key = tokenKey.toLowerCase();
|
|
27
|
+
if (key === 'sui') {
|
|
28
|
+
return '0x2::sui::SUI';
|
|
29
|
+
}
|
|
30
|
+
const coin = consts.coins[key];
|
|
31
|
+
return coin?.module ?? null;
|
|
32
|
+
}
|
|
33
|
+
/** Map an indexed trader position row to PositionCap lookup fields for on-chain hydration. */
|
|
34
|
+
export function traderPositionToCapInfo(record, consts) {
|
|
35
|
+
if (!record.positionId) {
|
|
36
|
+
return null;
|
|
37
|
+
}
|
|
38
|
+
const fromType = record.positionNameType
|
|
39
|
+
? parsePositionNameType(record.positionNameType)
|
|
40
|
+
: null;
|
|
41
|
+
if (fromType) {
|
|
42
|
+
return {
|
|
43
|
+
positionCapId: record.positionId,
|
|
44
|
+
symbol0: fromType.symbol0,
|
|
45
|
+
symbol1: fromType.symbol1,
|
|
46
|
+
long: fromType.long,
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
const collateral = record.collateralToken?.trim().toLowerCase();
|
|
50
|
+
const index = record.indexToken?.trim().toLowerCase();
|
|
51
|
+
if (!collateral || !index) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
const symbol0 = tokenKeyToMoveType(collateral, consts);
|
|
55
|
+
const symbol1 = tokenKeyToMoveType(index, consts);
|
|
56
|
+
if (!symbol0 || !symbol1) {
|
|
57
|
+
return null;
|
|
58
|
+
}
|
|
59
|
+
return {
|
|
60
|
+
positionCapId: record.positionId,
|
|
61
|
+
symbol0,
|
|
62
|
+
symbol1,
|
|
63
|
+
long: (record.direction?.toUpperCase() ?? 'LONG') === 'LONG',
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export async function fetchTraderPositionsPage(apiEndpoint, query) {
|
|
67
|
+
if (!apiEndpoint) {
|
|
68
|
+
throw new Error('apiEndpoint is required to fetch trader positions');
|
|
69
|
+
}
|
|
70
|
+
const params = new URLSearchParams({
|
|
71
|
+
owner: query.owner.trim().toLowerCase(),
|
|
72
|
+
});
|
|
73
|
+
if (query.status) {
|
|
74
|
+
params.set('status', query.status);
|
|
75
|
+
}
|
|
76
|
+
if (query.indexToken) {
|
|
77
|
+
params.set('indexToken', query.indexToken.trim().toLowerCase());
|
|
78
|
+
}
|
|
79
|
+
if (query.collateralToken) {
|
|
80
|
+
params.set('collateralToken', query.collateralToken.trim().toLowerCase());
|
|
81
|
+
}
|
|
82
|
+
if (query.limit != null) {
|
|
83
|
+
params.set('limit', String(query.limit));
|
|
84
|
+
}
|
|
85
|
+
if (query.offset != null) {
|
|
86
|
+
params.set('offset', String(query.offset));
|
|
87
|
+
}
|
|
88
|
+
const url = `${apiEndpoint.replace(/\/$/, '')}/trader-positions?${params}`;
|
|
89
|
+
const res = await fetch(url, {
|
|
90
|
+
method: 'GET',
|
|
91
|
+
headers: { 'Content-Type': 'application/json' },
|
|
92
|
+
});
|
|
93
|
+
const body = await res.json();
|
|
94
|
+
if (!res.ok || !body.success || !body.data) {
|
|
95
|
+
throw new Error(body.error || body.message || `Failed to fetch trader positions (${res.status})`);
|
|
96
|
+
}
|
|
97
|
+
return body.data;
|
|
98
|
+
}
|
|
99
|
+
/** Fetch all pages matching the query (respects optional total `limit`). */
|
|
100
|
+
export async function fetchAllTraderPositions(apiEndpoint, query, maxRows) {
|
|
101
|
+
const rows = [];
|
|
102
|
+
let offset = 0;
|
|
103
|
+
let total = Number.POSITIVE_INFINITY;
|
|
104
|
+
while (offset < total) {
|
|
105
|
+
const remaining = maxRows != null ? maxRows - rows.length : DEFAULT_PAGE_LIMIT;
|
|
106
|
+
if (remaining <= 0) {
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
const pageLimit = Math.min(DEFAULT_PAGE_LIMIT, remaining);
|
|
110
|
+
const { positions, pagination } = await fetchTraderPositionsPage(apiEndpoint, {
|
|
111
|
+
...query,
|
|
112
|
+
limit: pageLimit,
|
|
113
|
+
offset,
|
|
114
|
+
});
|
|
115
|
+
rows.push(...positions);
|
|
116
|
+
total = pagination.total;
|
|
117
|
+
offset += positions.length;
|
|
118
|
+
if (positions.length === 0 || offset >= total) {
|
|
119
|
+
break;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
return maxRows != null ? rows.slice(0, maxRows) : rows;
|
|
123
|
+
}
|
|
124
|
+
//# sourceMappingURL=traderPositions.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"traderPositions.mjs","sourceRoot":"","sources":["../src/traderPositions.ts"],"names":[],"mappings":"AA6DA,MAAM,kBAAkB,GAAG,GAAG,CAAA;AAE9B;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,gBAAwB;IAK5D,MAAM,MAAM,GAAG,eAAe,CAAA;IAC9B,MAAM,KAAK,GAAG,gBAAgB,CAAC,OAAO,CAAC,MAAM,CAAC,CAAA;IAC9C,IAAI,KAAK,GAAG,CAAC,EAAE,CAAC;QACd,OAAO,IAAI,CAAA;IACb,CAAC;IACD,MAAM,KAAK,GAAG,gBAAgB,CAAC,KAAK,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAA;IACzE,IAAI,CAAC,KAAK,EAAE,CAAC;QACX,OAAO,IAAI,CAAA;IACb,CAAC;IACD,MAAM,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,EAAE,CAAC,CAAA;IACvD,IAAI,KAAK,CAAC,MAAM,GAAG,CAAC,EA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AAG,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,kBAAkB,CAAA;QAC9E,IAAI,SAAS,IAAI,CAAC,EAAE,CAAC;YACnB,MAAK;QACP,CAAC;QAED,MAAM,SAAS,GAAG,IAAI,CAAC,GAAG,CAAC,kBAAkB,EAAE,SAAS,CAAC,CAAA;QACzD,MAAM,EAAE,SAAS,EAAE,UAAU,EAAE,GAAG,MAAM,wBAAwB,CAAC,WAAW,EAAE;YAC5E,GAAG,KAAK;YACR,KAAK,EAAE,SAAS;YAChB,MAAM;SACP,CAAC,CAAA;QAEF,IAAI,CAAC,IAAI,CAAC,GAAG,SAAS,CAAC,CAAA;QACvB,KAAK,GAAG,UAAU,CAAC,KAAK,CAAA;QACxB,MAAM,IAAI,SAAS,CAAC,MAAM,CAAA;QAE1B,IAAI,SAAS,CAAC,MAAM,KAAK,CAAC,IAAI,MAAM,IAAI,KAAK,EAAE,CAAC;YAC9C,MAAK;QACP,CAAC;IACH,CAAC;IAED,OAAO,OAAO,IAAI,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,EAAE,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,CAAA;AACxD,CAAC"}
|
package/docs/api-reference.md
CHANGED
|
@@ -22,10 +22,55 @@
|
|
|
22
22
|
- **LP-specific**: `IZLPAPI`, `ISLPAPI`, `IUSDZAPI` for typed usage
|
|
23
23
|
- **Data types**: `IBaseMarketValuationInfo`, `IBaseVaultInfo`, `IBaseSymbolInfo`, `IBasePositionInfo`, `IBaseOrderInfo`, etc.
|
|
24
24
|
|
|
25
|
+
## Pyth Pro helpers (all APIs)
|
|
26
|
+
|
|
27
|
+
Available on ZLP / SLP / USDZ API (and DataAPI via the shared oracle base):
|
|
28
|
+
|
|
29
|
+
| Method | Purpose |
|
|
30
|
+
|--------|---------|
|
|
31
|
+
| `fetchPythProUpdateBytesForTokens(tokens, options?)` | Fetch signed Pyth Pro update bytes for the given token symbols |
|
|
32
|
+
| `getLatestPythProPricesForTokens(tokens, options?)` | Read latest Pyth Pro prices (off-chain; used by valuation) |
|
|
33
|
+
| `initV3OracleTxb(tokens, tx?, options?)` | PTB: verify update + return `OracleInputs` |
|
|
34
|
+
|
|
35
|
+
Default Pyth Pro channel is `fixed_rate@1000ms` (from zo-oracle config).
|
|
36
|
+
|
|
37
|
+
## Recommended transaction methods
|
|
38
|
+
|
|
39
|
+
### Liquidity (auto Pyth Pro valuation)
|
|
40
|
+
|
|
41
|
+
| Method | Notes |
|
|
42
|
+
|--------|-------|
|
|
43
|
+
| `deposit` / `depositPtb` | Fetches valuation oracle internally |
|
|
44
|
+
| `withdraw` / `withdrawPtb` / `withdrawLpCoinObject` | Same |
|
|
45
|
+
|
|
46
|
+
### Swap (pass `pythProUpdateBytes`)
|
|
47
|
+
|
|
48
|
+
| LP | Method | PTB |
|
|
49
|
+
|----|--------|-----|
|
|
50
|
+
| ZLP / USDZ | `swapV3` | `swapV3Ptb` |
|
|
51
|
+
| SLP | `swapV4` | `swapV4Ptb` |
|
|
52
|
+
|
|
53
|
+
### Trading (pass `pythProUpdateBytes`)
|
|
54
|
+
|
|
55
|
+
| Action | Methods |
|
|
56
|
+
|--------|---------|
|
|
57
|
+
| Open | `openPositionV3`, `openPositionWithCoinV3`, `openPositionWithSCardV3`, `openPositionWithCoinAndSCardV3` |
|
|
58
|
+
| Decrease | `decreasePositionV3`, `decreasePositionWithSCardV3` |
|
|
59
|
+
| Redeem | ZLP / USDZ: `redeemFromPositionV2` · SLP: `redeemFromPositionV3` |
|
|
60
|
+
|
|
61
|
+
### Valuation (DataAPI)
|
|
62
|
+
|
|
63
|
+
| LP | Methods |
|
|
64
|
+
|----|---------|
|
|
65
|
+
| ZLP / USDZ | `valuate` (legacy) · `valuateV2` / `valuateVaultsV2` / `valuateSymbolsV2` (Pyth Pro PTB) |
|
|
66
|
+
| SLP | `valuate` (legacy) · `valuateV3` / `valuateVaultsV3` / `valuateSymbolsV3` (Pyth Pro PTB) |
|
|
67
|
+
| All | `valuateMarket()` – off-chain market valuation (SLP uses Pyth Pro + LST RR × SUI) |
|
|
68
|
+
|
|
25
69
|
## Source code
|
|
26
70
|
|
|
27
71
|
- **Interfaces**: `src/interfaces/` – TypeScript interface definitions
|
|
28
72
|
- **Implementations**: `src/implementations/` – Concrete API implementations
|
|
73
|
+
- **Oracle Pro**: `src/oraclePro.ts`, `src/pythProClient.ts`, `src/storkClient.ts`
|
|
29
74
|
- **Abstract classes**: `src/abstract/` – Shared base functionality
|
|
30
75
|
- **Factory**: `src/factory/SDKFactory.ts` – SDK instance creation
|
|
31
76
|
|
package/docs/architecture.md
CHANGED
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
The SDK supports multiple LP tokens through a unified interface:
|
|
4
4
|
|
|
5
|
-
- **ZLP (ZO Liquidity Provider)**: Original ZO Protocol LP token
|
|
6
|
-
- **SLP (Sudo Liquidity Provider)**: Sudo SDK-based LP token
|
|
7
|
-
- **USDZ (USD Stablecoin)**: Stablecoin LP token
|
|
5
|
+
- **ZLP (ZO Liquidity Provider)**: Original ZO Protocol LP token (`zo-core`)
|
|
6
|
+
- **SLP (Sudo Liquidity Provider)**: Sudo SDK-based LP token (`sudo-core`)
|
|
7
|
+
- **USDZ (USD Stablecoin)**: Stablecoin LP token (`zo-core` / USDZ package)
|
|
8
8
|
|
|
9
9
|
## Key Components
|
|
10
10
|
|
|
@@ -12,3 +12,21 @@ The SDK supports multiple LP tokens through a unified interface:
|
|
|
12
12
|
2. **Abstract Base Classes**: Shared logic with delegation to DataAPI (`BaseAPI`, `BaseDataAPI`)
|
|
13
13
|
3. **Concrete Implementations**: LP-specific functionality (`ZLPAPI`, `SLPAPI`, `USDZAPI`)
|
|
14
14
|
4. **Factory Pattern**: Type-safe instance creation via `SDK` (e.g. `SDK.createZLPAPI()`)
|
|
15
|
+
5. **Oracle Pro**: Shared Pyth Pro + Stork helpers (`oraclePro.ts`, `pythProClient.ts`, `storkClient.ts`, `deployments-zo-oracle-mainnet.json`)
|
|
16
|
+
|
|
17
|
+
## Oracle upgrade (Pyth Pro + Stork)
|
|
18
|
+
|
|
19
|
+
Newer market entrypoints no longer take per-token legacy Pyth feeder objects on the hot path. Instead:
|
|
20
|
+
|
|
21
|
+
1. Client fetches **Pyth Pro update bytes** (`fetchPythProUpdateBytesForTokens`)
|
|
22
|
+
2. PTB verifies them via `pyth_lazer::parse_and_verify_le_ecdsa_update_v2`
|
|
23
|
+
3. Calls pass **`PythProUpdate` + `StorkState` + `OracleRegistry`** (and clock where required)
|
|
24
|
+
|
|
25
|
+
| Surface | ZLP / USDZ | SLP |
|
|
26
|
+
|---------|------------|-----|
|
|
27
|
+
| Valuation PTB | `valuate_*_v2` | `valuate_*_v3` |
|
|
28
|
+
| Swap | `swap_v3` | `swap_v4` |
|
|
29
|
+
| Open / decrease | `*_v3` | `*_v3` |
|
|
30
|
+
| Redeem | `redeem_from_position_v2` | `redeem_from_position_v3` |
|
|
31
|
+
|
|
32
|
+
Deposit and withdraw still use the market `deposit` / `withdraw` entrypoints; the SDK builds valuation with the Pyth Pro path under the hood.
|
|
@@ -2,6 +2,27 @@
|
|
|
2
2
|
|
|
3
3
|
These operations work across all LP tokens (ZLP, SLP, USDZ).
|
|
4
4
|
|
|
5
|
+
## Oracle model (Pyth Pro + Stork)
|
|
6
|
+
|
|
7
|
+
Newer market methods use **Pyth Pro** update bytes (verified on-chain) plus **Stork** where enabled, instead of per-token legacy Pyth feeder objects.
|
|
8
|
+
|
|
9
|
+
| Operation | How oracle updates are supplied |
|
|
10
|
+
|-----------|----------------------------------|
|
|
11
|
+
| **Deposit / withdraw** | Fetched automatically inside the SDK (`initValuationOracle`) |
|
|
12
|
+
| **Swap / open / decrease / redeem (v3+)** | Pass `pythProUpdateBytes` from `api.fetchPythProUpdateBytesForTokens(...)` |
|
|
13
|
+
|
|
14
|
+
```typescript
|
|
15
|
+
// Tokens needed for a trade (collateral + index)
|
|
16
|
+
const pythProUpdateBytes = await zlpAPI.fetchPythProUpdateBytesForTokens(['nusdc', 'btc'])
|
|
17
|
+
|
|
18
|
+
// Tokens needed for a vault swap (all vaults are valued)
|
|
19
|
+
const swapUpdateBytes = await zlpAPI.fetchPythProUpdateBytesForTokens(
|
|
20
|
+
Object.keys(zlpAPI.consts.zoCore.vaults) // or sudoCore.vaults for SLP
|
|
21
|
+
)
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
Legacy `openPositionV2` / `swap` / `swapV2Ptb` paths still exist, but new integrations should use the Pyth Pro methods below.
|
|
25
|
+
|
|
5
26
|
## Data / read operations
|
|
6
27
|
|
|
7
28
|
Use the API or DataAPI; the methods are the same.
|
|
@@ -14,11 +35,15 @@ const positionCaps = await zlpAPI.getPositionCapInfoList(ownerAddress)
|
|
|
14
35
|
const positions = await zlpAPI.getPositionInfoList(positionCaps, ownerAddress)
|
|
15
36
|
```
|
|
16
37
|
|
|
38
|
+
`valuateMarket()` on SLP (and similar valuation helpers) resolves vault/symbol USD prices from Pyth Pro, including LST vaults as RR × SUI.
|
|
39
|
+
|
|
17
40
|
## Deposit
|
|
18
41
|
|
|
42
|
+
Deposit builds valuation on-chain with Pyth Pro automatically — no update bytes argument.
|
|
43
|
+
|
|
19
44
|
```typescript
|
|
20
45
|
const depositTx = await zlpAPI.deposit(
|
|
21
|
-
'
|
|
46
|
+
'nusdc', // coin type
|
|
22
47
|
['coinObjectId'], // coin object IDs
|
|
23
48
|
1000000, // amount
|
|
24
49
|
0, // minimum amount out (optional)
|
|
@@ -31,22 +56,45 @@ const usdzDepositTx = await usdzAPI.deposit('usdc', ['coinObjectId'], 1000000)
|
|
|
31
56
|
|
|
32
57
|
## Withdraw
|
|
33
58
|
|
|
59
|
+
Same as deposit: valuation oracle is initialized inside the SDK.
|
|
60
|
+
|
|
34
61
|
```typescript
|
|
35
62
|
const withdrawTx = await zlpAPI.withdraw(
|
|
36
|
-
'
|
|
63
|
+
'nusdc',
|
|
37
64
|
['lpCoinObjectId'],
|
|
38
65
|
1000000,
|
|
39
66
|
0
|
|
40
67
|
)
|
|
41
68
|
```
|
|
42
69
|
|
|
43
|
-
## Swap
|
|
70
|
+
## Swap (Pyth Pro)
|
|
71
|
+
|
|
72
|
+
Prefer the Pyth Pro swap methods:
|
|
73
|
+
|
|
74
|
+
| LP | Method | PTB (returns coin) |
|
|
75
|
+
|----|--------|--------------------|
|
|
76
|
+
| **ZLP / USDZ** | `swapV3` | `swapV3Ptb` |
|
|
77
|
+
| **SLP** | `swapV4` | `swapV4Ptb` |
|
|
44
78
|
|
|
45
79
|
```typescript
|
|
46
|
-
const
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
80
|
+
const vaultTokens = Object.keys(zlpAPI.consts.zoCore.vaults)
|
|
81
|
+
const pythProUpdateBytes = await zlpAPI.fetchPythProUpdateBytesForTokens(vaultTokens)
|
|
82
|
+
|
|
83
|
+
const swapTx = await zlpAPI.swapV3(
|
|
84
|
+
'nusdc', // from token
|
|
85
|
+
'sui', // to token
|
|
86
|
+
BigInt(1000000), // amount
|
|
87
|
+
['coinObjectId'], // coin objects
|
|
88
|
+
pythProUpdateBytes,
|
|
89
|
+
0 // minAmountOut (optional)
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
// SLP uses swapV4 with the same argument shape
|
|
93
|
+
const slpSwapTx = await slpAPI.swapV4(
|
|
94
|
+
'usdc',
|
|
95
|
+
'sui',
|
|
96
|
+
BigInt(1000000),
|
|
97
|
+
['coinObjectId'],
|
|
98
|
+
await slpAPI.fetchPythProUpdateBytesForTokens(Object.keys(slpAPI.consts.sudoCore.vaults)),
|
|
51
99
|
)
|
|
52
100
|
```
|
package/docs/getting-started.md
CHANGED
|
@@ -46,8 +46,11 @@ const zlpAPI = SDK.createZLPAPI(network, provider, apiEndpoint, connectionURL)
|
|
|
46
46
|
const marketData = await zlpAPI.valuateMarket()
|
|
47
47
|
const marketDataAlt = await zlpAPI.dataAPI.valuateMarket()
|
|
48
48
|
|
|
49
|
-
//
|
|
50
|
-
const depositTx = await zlpAPI.deposit('
|
|
49
|
+
// Deposit / withdraw fetch Pyth Pro valuation updates internally
|
|
50
|
+
const depositTx = await zlpAPI.deposit('nusdc', ['coinId'], 1000000)
|
|
51
|
+
|
|
52
|
+
// Trading / swap (v3+) need update bytes first
|
|
53
|
+
const pythProUpdateBytes = await zlpAPI.fetchPythProUpdateBytesForTokens(['nusdc', 'btc'])
|
|
51
54
|
```
|
|
52
55
|
|
|
53
56
|
**DataAPI** instances are for read-only usage when you don't need to build transactions:
|
|
@@ -55,6 +58,8 @@ const depositTx = await zlpAPI.deposit('usdc', ['coinId'], 1000000)
|
|
|
55
58
|
```typescript
|
|
56
59
|
const zlpDataAPI = SDK.createZLPDataAPI(network, provider, apiEndpoint, connectionURL)
|
|
57
60
|
const marketData = await zlpDataAPI.valuateMarket()
|
|
58
|
-
const vaultInfo = await zlpDataAPI.getVaultInfo('
|
|
61
|
+
const vaultInfo = await zlpDataAPI.getVaultInfo('nusdc')
|
|
59
62
|
// zlpDataAPI.deposit(...) does not exist
|
|
60
63
|
```
|
|
64
|
+
|
|
65
|
+
For Pyth Pro trading and swap examples, see [Common Operations](common-operations.md), [Trading Examples](lp-specific-features.md), and [Swap Integration](swap-integration.md).
|
|
@@ -14,15 +14,27 @@ When you open or decrease a position, you choose how the order is executed:
|
|
|
14
14
|
- **Limit**: best when you want a specific price; order stays pending until the price condition is met.
|
|
15
15
|
- **IOC limit**: “fill at my price or not at all” in one block.
|
|
16
16
|
|
|
17
|
+
## Pyth Pro trading methods
|
|
18
|
+
|
|
19
|
+
Use **`openPositionV3`** / **`decreasePositionV3`** (and SCard / coin variants) for ZLP, SLP, and USDZ. These take `pythProUpdateBytes` from `fetchPythProUpdateBytesForTokens`.
|
|
20
|
+
|
|
21
|
+
| Action | Method |
|
|
22
|
+
|--------|--------|
|
|
23
|
+
| Open | `openPositionV3`, `openPositionWithCoinV3`, `openPositionWithSCardV3`, `openPositionWithCoinAndSCardV3` |
|
|
24
|
+
| Decrease | `decreasePositionV3`, `decreasePositionWithSCardV3` |
|
|
25
|
+
| Redeem collateral | ZLP / USDZ: `redeemFromPositionV2` · SLP: `redeemFromPositionV3` |
|
|
26
|
+
|
|
17
27
|
---
|
|
18
28
|
|
|
19
|
-
##
|
|
29
|
+
## Open position (market order)
|
|
20
30
|
|
|
21
31
|
Execute immediately with slippage protection:
|
|
22
32
|
|
|
23
33
|
```typescript
|
|
24
|
-
const
|
|
25
|
-
|
|
34
|
+
const pythProUpdateBytes = await zlpAPI.fetchPythProUpdateBytesForTokens(['nusdc', 'btc'])
|
|
35
|
+
|
|
36
|
+
const tx = await zlpAPI.openPositionV3(
|
|
37
|
+
'nusdc', // collateral token
|
|
26
38
|
'btc', // index token
|
|
27
39
|
BigInt(1000000), // size
|
|
28
40
|
BigInt(100000), // collateral amount
|
|
@@ -30,7 +42,8 @@ const tx = await zlpAPI.openPositionV2(
|
|
|
30
42
|
true, // long position
|
|
31
43
|
BigInt(50000), // reserve amount
|
|
32
44
|
30000, // index price (reference)
|
|
33
|
-
1.
|
|
45
|
+
1.0, // collateral price (reference)
|
|
46
|
+
pythProUpdateBytes, // Pyth Pro update bytes
|
|
34
47
|
false, // isLimitOrder: false = market order
|
|
35
48
|
false, // isIocOrder (ignored for market)
|
|
36
49
|
0.003, // pricesSlippage: 0.3% for market
|
|
@@ -41,15 +54,19 @@ const tx = await zlpAPI.openPositionV2(
|
|
|
41
54
|
)
|
|
42
55
|
```
|
|
43
56
|
|
|
57
|
+
The same `openPositionV3` signature works on `slpAPI` and `usdzAPI`.
|
|
58
|
+
|
|
44
59
|
---
|
|
45
60
|
|
|
46
|
-
##
|
|
61
|
+
## Open position (limit order)
|
|
47
62
|
|
|
48
63
|
Pending order at your limit price; use zero or small slippage:
|
|
49
64
|
|
|
50
65
|
```typescript
|
|
51
|
-
const
|
|
52
|
-
|
|
66
|
+
const pythProUpdateBytes = await zlpAPI.fetchPythProUpdateBytesForTokens(['nusdc', 'btc'])
|
|
67
|
+
|
|
68
|
+
const tx = await zlpAPI.openPositionV3(
|
|
69
|
+
'nusdc',
|
|
53
70
|
'btc',
|
|
54
71
|
BigInt(1000000),
|
|
55
72
|
BigInt(100000),
|
|
@@ -57,7 +74,8 @@ const tx = await zlpAPI.openPositionV2(
|
|
|
57
74
|
true, // long
|
|
58
75
|
BigInt(50000),
|
|
59
76
|
30000, // your limit price (index)
|
|
60
|
-
1.
|
|
77
|
+
1.0, // collateral price
|
|
78
|
+
pythProUpdateBytes,
|
|
61
79
|
true, // isLimitOrder: true = limit order
|
|
62
80
|
false, // isIocOrder: false = pending until price is met
|
|
63
81
|
0, // pricesSlippage: 0 for limit (exact price)
|
|
@@ -70,13 +88,15 @@ const tx = await zlpAPI.openPositionV2(
|
|
|
70
88
|
|
|
71
89
|
---
|
|
72
90
|
|
|
73
|
-
##
|
|
91
|
+
## Open position (IOC limit order)
|
|
74
92
|
|
|
75
93
|
Fill only if the limit can be met immediately; otherwise the order does not execute:
|
|
76
94
|
|
|
77
95
|
```typescript
|
|
78
|
-
const
|
|
79
|
-
|
|
96
|
+
const pythProUpdateBytes = await zlpAPI.fetchPythProUpdateBytesForTokens(['nusdc', 'btc'])
|
|
97
|
+
|
|
98
|
+
const tx = await zlpAPI.openPositionV3(
|
|
99
|
+
'nusdc',
|
|
80
100
|
'btc',
|
|
81
101
|
BigInt(1000000),
|
|
82
102
|
BigInt(100000),
|
|
@@ -84,7 +104,8 @@ const tx = await zlpAPI.openPositionV2(
|
|
|
84
104
|
true,
|
|
85
105
|
BigInt(50000),
|
|
86
106
|
30000,
|
|
87
|
-
1.
|
|
107
|
+
1.0,
|
|
108
|
+
pythProUpdateBytes,
|
|
88
109
|
true, // isLimitOrder: true
|
|
89
110
|
true, // isIocOrder: true = fill now or cancel
|
|
90
111
|
0,
|
|
@@ -94,3 +115,59 @@ const tx = await zlpAPI.openPositionV2(
|
|
|
94
115
|
'senderAddress'
|
|
95
116
|
)
|
|
96
117
|
```
|
|
118
|
+
|
|
119
|
+
---
|
|
120
|
+
|
|
121
|
+
## Decrease position
|
|
122
|
+
|
|
123
|
+
```typescript
|
|
124
|
+
const pythProUpdateBytes = await zlpAPI.fetchPythProUpdateBytesForTokens(['nusdc', 'btc'])
|
|
125
|
+
|
|
126
|
+
const tx = await zlpAPI.decreasePositionV3(
|
|
127
|
+
positionCapId,
|
|
128
|
+
'nusdc', // collateral token
|
|
129
|
+
'btc', // index token
|
|
130
|
+
BigInt(500000), // decrease amount
|
|
131
|
+
true, // long
|
|
132
|
+
30000, // index price
|
|
133
|
+
1.0, // collateral price
|
|
134
|
+
pythProUpdateBytes,
|
|
135
|
+
false, // isTriggerOrder
|
|
136
|
+
true, // isTakeProfitOrder
|
|
137
|
+
false, // isIocOrder
|
|
138
|
+
0.003, // pricesSlippage
|
|
139
|
+
0.5, // collateralSlippage
|
|
140
|
+
BigInt(500), // relayer fee
|
|
141
|
+
['coinObjectId'], // fee coin objects (or pass sender)
|
|
142
|
+
false, // sponsoredTx
|
|
143
|
+
'senderAddress'
|
|
144
|
+
)
|
|
145
|
+
```
|
|
146
|
+
|
|
147
|
+
---
|
|
148
|
+
|
|
149
|
+
## Redeem collateral from a position
|
|
150
|
+
|
|
151
|
+
```typescript
|
|
152
|
+
const pythProUpdateBytes = await zlpAPI.fetchPythProUpdateBytesForTokens(['nusdc', 'btc'])
|
|
153
|
+
|
|
154
|
+
// ZLP / USDZ
|
|
155
|
+
const redeemTx = await zlpAPI.redeemFromPositionV2(
|
|
156
|
+
positionCapId,
|
|
157
|
+
'nusdc',
|
|
158
|
+
'btc',
|
|
159
|
+
100000, // amount
|
|
160
|
+
true, // long
|
|
161
|
+
pythProUpdateBytes,
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
// SLP
|
|
165
|
+
const slpRedeemTx = await slpAPI.redeemFromPositionV3(
|
|
166
|
+
positionCapId,
|
|
167
|
+
'usdc',
|
|
168
|
+
'btc',
|
|
169
|
+
100000,
|
|
170
|
+
true,
|
|
171
|
+
pythProUpdateBytes,
|
|
172
|
+
)
|
|
173
|
+
```
|
package/docs/swap-integration.md
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
# Swap Integration
|
|
2
2
|
|
|
3
|
-
This guide covers how to integrate swaps using the ZO SDK. Swaps are supported on **ZLP**, **SLP**, and **USDZ
|
|
3
|
+
This guide covers how to integrate swaps using the ZO SDK. Swaps are supported on **ZLP**, **SLP**, and **USDZ**.
|
|
4
|
+
|
|
5
|
+
Prefer the **Pyth Pro** methods:
|
|
6
|
+
|
|
7
|
+
| LP | Swap (transfer output) | Swap PTB (return coin) |
|
|
8
|
+
|----|------------------------|-------------------------|
|
|
9
|
+
| **ZLP / USDZ** | `swapV3` | `swapV3Ptb` |
|
|
10
|
+
| **SLP** | `swapV4` | `swapV4Ptb` |
|
|
11
|
+
|
|
12
|
+
Fee estimation is available via `dataAPI.calculateSwapFeeBreakdown()`. Legacy `swap()` / `swapV2Ptb()` remain available but are not recommended for new integrations.
|
|
4
13
|
|
|
5
14
|
## Overview
|
|
6
15
|
|
|
7
16
|
A swap flow typically involves:
|
|
8
17
|
|
|
9
18
|
1. **Estimate fees** – Get the expected fee breakdown for the swap.
|
|
10
|
-
2. **
|
|
11
|
-
3. **
|
|
12
|
-
4. **
|
|
19
|
+
2. **Fetch Pyth Pro update bytes** – Required for vault valuation on the Pyth Pro path.
|
|
20
|
+
3. **Set slippage protection** – Compute `minAmountOut` based on expected output and slippage tolerance.
|
|
21
|
+
4. **Build transaction** – Call `swapV3` (ZLP/USDZ) or `swapV4` (SLP).
|
|
22
|
+
5. **Sign and send** – Sign with the wallet and submit to the network.
|
|
13
23
|
|
|
14
24
|
## Amounts and units
|
|
15
25
|
|
|
@@ -27,7 +37,7 @@ const fromAmount = BigInt(100 * 1e6) // 100_000_000 atomic units
|
|
|
27
37
|
Before building the swap transaction, you can estimate the fee breakdown to display to the user or compute a safe `minAmountOut`.
|
|
28
38
|
|
|
29
39
|
```typescript
|
|
30
|
-
import { SDK,
|
|
40
|
+
import { SDK, Network } from '@zofai/zo-sdk'
|
|
31
41
|
import { SuiClient } from '@mysten/sui/client'
|
|
32
42
|
|
|
33
43
|
const provider = new SuiClient({ url: 'https://fullnode.mainnet.sui.io' })
|
|
@@ -58,13 +68,14 @@ console.log(`Total fee: ${feeBreakdown.totalFeeValue.toFixed(4)} USD`)
|
|
|
58
68
|
console.log(`Fee rate: ${(feeBreakdown.totalFeeRate * 100).toFixed(2)}%`)
|
|
59
69
|
```
|
|
60
70
|
|
|
61
|
-
## Build and submit swap transaction
|
|
71
|
+
## Build and submit swap transaction (Pyth Pro)
|
|
62
72
|
|
|
63
|
-
|
|
73
|
+
You need:
|
|
64
74
|
|
|
65
75
|
- `fromToken`, `toToken` – token identifiers (e.g. `'usdc'`, `'sui'`).
|
|
66
76
|
- `fromAmount` – amount in atomic units (bigint).
|
|
67
77
|
- `fromCoinObjects` – owned coin object IDs for the source token.
|
|
78
|
+
- `pythProUpdateBytes` – from `fetchPythProUpdateBytesForTokens` over **all vault tokens** (swap values every vault).
|
|
68
79
|
- `minAmountOut` – optional; minimum output amount in atomic units (slippage protection).
|
|
69
80
|
|
|
70
81
|
```typescript
|
|
@@ -72,39 +83,48 @@ const slpAPI = SDK.createSLPAPI(network, provider, apiEndpoint, connectionURL)
|
|
|
72
83
|
|
|
73
84
|
const fromAmount = BigInt(100 * 1e6) // 100 USDC
|
|
74
85
|
const fromCoinObjects = ['0x...'] // user's USDC coin IDs
|
|
86
|
+
const minAmountOut = 0 // or compute from oracle + slippage
|
|
75
87
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
const minAmountOut = 0 // or compute from oracle + slippage
|
|
88
|
+
const vaultTokens = Object.keys(slpAPI.consts.sudoCore.vaults)
|
|
89
|
+
const pythProUpdateBytes = await slpAPI.fetchPythProUpdateBytesForTokens(vaultTokens)
|
|
79
90
|
|
|
80
|
-
const tx = await slpAPI.
|
|
91
|
+
const tx = await slpAPI.swapV4(
|
|
81
92
|
'usdc',
|
|
82
93
|
'sui',
|
|
83
94
|
fromAmount,
|
|
84
95
|
fromCoinObjects,
|
|
96
|
+
pythProUpdateBytes,
|
|
85
97
|
minAmountOut
|
|
86
98
|
)
|
|
87
99
|
|
|
100
|
+
// ZLP / USDZ: use swapV3 with the same argument shape
|
|
101
|
+
// const tx = await zlpAPI.swapV3('nusdc', 'sui', fromAmount, fromCoinObjects, pythProUpdateBytes, minAmountOut)
|
|
102
|
+
|
|
88
103
|
// Sign with wallet and execute
|
|
89
104
|
// const signed = await signAndExecuteTransaction({ transaction: tx })
|
|
90
105
|
```
|
|
91
106
|
|
|
92
|
-
## Swap and return coin
|
|
107
|
+
## Swap and return coin (PTB)
|
|
93
108
|
|
|
94
|
-
Use `
|
|
109
|
+
Use `swapV3Ptb` (ZLP/USDZ) or `swapV4Ptb` (SLP) when you need the output coin as a `TransactionObjectArgument`—for example, to pass into another move call or transfer it in the same transaction.
|
|
95
110
|
|
|
96
111
|
Pass an optional `tx` to compose into an existing transaction; otherwise a new transaction is created (you must sign and execute it via your wallet).
|
|
97
112
|
|
|
98
113
|
```typescript
|
|
114
|
+
import { Transaction } from '@mysten/sui/transactions'
|
|
115
|
+
|
|
99
116
|
const slpAPI = SDK.createSLPAPI(network, provider, apiEndpoint, connectionURL)
|
|
117
|
+
const vaultTokens = Object.keys(slpAPI.consts.sudoCore.vaults)
|
|
118
|
+
const pythProUpdateBytes = await slpAPI.fetchPythProUpdateBytesForTokens(vaultTokens)
|
|
100
119
|
|
|
101
|
-
// Option 1: Standalone swap (tx created
|
|
120
|
+
// Option 1: Standalone swap (tx created / passed in)
|
|
102
121
|
const tx = new Transaction()
|
|
103
|
-
const outputCoin = await slpAPI.
|
|
122
|
+
const outputCoin = await slpAPI.swapV4Ptb(
|
|
104
123
|
'usdc',
|
|
105
124
|
'sui',
|
|
106
125
|
BigInt(100 * 1e6),
|
|
107
126
|
fromCoinObjects,
|
|
127
|
+
pythProUpdateBytes,
|
|
108
128
|
minAmountOut,
|
|
109
129
|
tx
|
|
110
130
|
)
|
|
@@ -112,14 +132,22 @@ const outputCoin = await slpAPI.swapV2Ptb(
|
|
|
112
132
|
|
|
113
133
|
// Option 2: Compose with other move calls
|
|
114
134
|
const composedTx = new Transaction()
|
|
115
|
-
const swapCoin = await slpAPI.
|
|
135
|
+
const swapCoin = await slpAPI.swapV4Ptb(
|
|
136
|
+
'usdc',
|
|
137
|
+
'sui',
|
|
138
|
+
BigInt(100 * 1e6),
|
|
139
|
+
fromCoinObjects,
|
|
140
|
+
pythProUpdateBytes,
|
|
141
|
+
minAmountOut,
|
|
142
|
+
composedTx
|
|
143
|
+
)
|
|
116
144
|
composedTx.transferObjects([swapCoin], composedTx.pure.address(recipientAddress))
|
|
117
145
|
```
|
|
118
146
|
|
|
119
147
|
## Complete integration example
|
|
120
148
|
|
|
121
149
|
```typescript
|
|
122
|
-
import { SDK,
|
|
150
|
+
import { SDK, Network } from '@zofai/zo-sdk'
|
|
123
151
|
import { SuiClient } from '@mysten/sui/client'
|
|
124
152
|
|
|
125
153
|
async function swapWithFeeEstimate(
|
|
@@ -151,12 +179,16 @@ async function swapWithFeeEstimate(
|
|
|
151
179
|
const expectedOutAtomic = (netValueUsd * (10 ** toDecimals)) / toPrice
|
|
152
180
|
const minAmountOut = Math.floor(expectedOutAtomic * (1 - slippageBps / 10000))
|
|
153
181
|
|
|
154
|
-
// 3.
|
|
155
|
-
const
|
|
182
|
+
// 3. Fetch Pyth Pro update for all vaults, then build transaction
|
|
183
|
+
const vaultTokens = Object.keys(api.consts.sudoCore.vaults)
|
|
184
|
+
const pythProUpdateBytes = await api.fetchPythProUpdateBytesForTokens(vaultTokens)
|
|
185
|
+
|
|
186
|
+
const tx = await api.swapV4(
|
|
156
187
|
fromToken,
|
|
157
188
|
toToken,
|
|
158
189
|
BigInt(fromAmountAtomic),
|
|
159
190
|
fromCoinObjects,
|
|
191
|
+
pythProUpdateBytes,
|
|
160
192
|
minAmountOut
|
|
161
193
|
)
|
|
162
194
|
|
|
@@ -180,4 +212,4 @@ Use `calculateSwapFeeBreakdown` to get the USD value of each component and the t
|
|
|
180
212
|
|
|
181
213
|
## Token identifiers
|
|
182
214
|
|
|
183
|
-
Tokens are identified by their symbol in the deployment config, e.g. `'sui'`, `'usdc'`, `'usdt'`, `'eth'`, `'btc'`. Ensure the `fromToken` and `toToken` values match those in the LP’s supported tokens.
|
|
215
|
+
Tokens are identified by their symbol in the deployment config, e.g. `'sui'`, `'usdc'`, `'nusdc'`, `'usdt'`, `'eth'`, `'btc'`. Ensure the `fromToken` and `toToken` values match those in the LP’s supported tokens.
|