@cetusprotocol/margin-sdk 0.0.0-experimental-20260302132606
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 +0 -0
- package/dist/index.d.ts +1212 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/events_filter_to_actions.md +427 -0
- package/package.json +44 -0
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
# Margin Trading 事件解析与 Action 区分规则
|
|
2
|
+
|
|
3
|
+
## 背景
|
|
4
|
+
|
|
5
|
+
合约中移除 `MarginTradingContext` 后,原有记录在 context 中的数据(action, amount, cointype)需要通过事件组合来推断。
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
# Part 1: 三字段提取规则(快速参考)
|
|
10
|
+
|
|
11
|
+
## 返回字段
|
|
12
|
+
|
|
13
|
+
```typescript
|
|
14
|
+
interface ActionResult {
|
|
15
|
+
action: string // 操作类型
|
|
16
|
+
amount: string // 数量
|
|
17
|
+
coinType: string // 币种
|
|
18
|
+
}
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
## 快速参考表
|
|
22
|
+
|
|
23
|
+
| Action | Amount 来源 | CoinType 来源 | 符号 |
|
|
24
|
+
|--------|------------|---------------|:----:|
|
|
25
|
+
| `open_position` | `DepositEvent.deposit_amount` | `DepositEvent.coin_type` | + |
|
|
26
|
+
| `close_position` | `WithdrawEvent.withdraw_amount` | `WithdrawEvent.coin_type` | - |
|
|
27
|
+
| `increase_size` | `DepositEvent.deposit_amount` | `DepositEvent.coin_type` | + |
|
|
28
|
+
| `decrease_size` | `WithdrawEvent.withdraw_amount` | `WithdrawEvent.coin_type` | - |
|
|
29
|
+
| `top_up_collateral` | `DepositEvent.deposit_amount` | `DepositEvent.coin_type` | + |
|
|
30
|
+
| `withdraw_collateral` | `WithdrawEvent.withdraw_amount` | `WithdrawEvent.coin_type` | - |
|
|
31
|
+
| `liquidation` | `LiquidateEvent.withdraw_amount` | `LiquidateEvent.withdraw_type` | - |
|
|
32
|
+
| `claim_reward` | `ClaimRewardEvent.reward_amount` | `ClaimRewardEvent.reward_coin_type` | + |
|
|
33
|
+
|
|
34
|
+
## Action 判断条件
|
|
35
|
+
|
|
36
|
+
| Action | 判断条件 |
|
|
37
|
+
|--------|---------|
|
|
38
|
+
| `open_position` | 存在 `OpenPositionEvent` |
|
|
39
|
+
| `close_position` | 存在 `ClosePositionEvent` |
|
|
40
|
+
| `liquidation` | 存在 `LiquidateEvent` |
|
|
41
|
+
| `claim_reward` | 仅有 `ClaimRewardEvent` |
|
|
42
|
+
| `increase_size` | `DepositEvent` + `BorrowEvent` |
|
|
43
|
+
| `top_up_collateral` | `DepositEvent` only (无 `BorrowEvent`) |
|
|
44
|
+
| `decrease_size` | `WithdrawEvent` + `RepayEvent` |
|
|
45
|
+
| `withdraw_collateral` | `WithdrawEvent` only (无 `RepayEvent`) |
|
|
46
|
+
|
|
47
|
+
## 完整提取逻辑
|
|
48
|
+
|
|
49
|
+
```typescript
|
|
50
|
+
function getActionType(events): string {
|
|
51
|
+
// 第一优先级:唯一标识事件
|
|
52
|
+
if (events.openPositionEvent) return 'open_position'
|
|
53
|
+
if (events.closePositionEvent) return 'close_position'
|
|
54
|
+
if (events.liquidateEvent) return 'liquidation'
|
|
55
|
+
if (events.claimRewardEvent && !hasOtherEvents) return 'claim_reward'
|
|
56
|
+
|
|
57
|
+
// 第二优先级:事件组合判断
|
|
58
|
+
const hasDeposit = events.depositEvents.length > 0
|
|
59
|
+
const hasWithdraw = events.withdrawEvents.length > 0
|
|
60
|
+
const hasBorrow = events.borrowEvents.length > 0
|
|
61
|
+
const hasRepay = events.repayEvents.length > 0
|
|
62
|
+
|
|
63
|
+
if (hasDeposit && hasBorrow) return 'increase_size'
|
|
64
|
+
if (hasDeposit && !hasBorrow) return 'top_up_collateral'
|
|
65
|
+
if (hasWithdraw && hasRepay) return 'decrease_size'
|
|
66
|
+
if (hasWithdraw && !hasRepay) return 'withdraw_collateral'
|
|
67
|
+
|
|
68
|
+
return 'unknown'
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function extractActionResult(events, position): ActionResult {
|
|
72
|
+
const action = getActionType(events)
|
|
73
|
+
const { is_long, base_token, quote_token } = position
|
|
74
|
+
|
|
75
|
+
// 根据仓位方向确定抵押资产类型
|
|
76
|
+
const collateralCoinType = is_long ? base_token : quote_token
|
|
77
|
+
|
|
78
|
+
switch (action) {
|
|
79
|
+
case 'open_position':
|
|
80
|
+
case 'increase_size':
|
|
81
|
+
case 'top_up_collateral': {
|
|
82
|
+
// 筛选抵押资产的 DepositEvent
|
|
83
|
+
const depositEvent = events.depositEvents.find(e => e.coin_type === collateralCoinType)
|
|
84
|
+
return {
|
|
85
|
+
action,
|
|
86
|
+
amount: depositEvent.deposit_amount,
|
|
87
|
+
coinType: depositEvent.coin_type
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
case 'close_position':
|
|
92
|
+
case 'decrease_size':
|
|
93
|
+
case 'withdraw_collateral': {
|
|
94
|
+
// 筛选抵押资产的 WithdrawEvent(不依赖事件顺序)
|
|
95
|
+
const withdrawEvent = events.withdrawEvents.find(e => e.coin_type === collateralCoinType)
|
|
96
|
+
return {
|
|
97
|
+
action,
|
|
98
|
+
amount: withdrawEvent.withdraw_amount,
|
|
99
|
+
coinType: withdrawEvent.coin_type
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
case 'liquidation':
|
|
104
|
+
return {
|
|
105
|
+
action,
|
|
106
|
+
amount: events.liquidateEvent.withdraw_amount,
|
|
107
|
+
coinType: events.liquidateEvent.withdraw_type
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
case 'claim_reward':
|
|
111
|
+
return {
|
|
112
|
+
action,
|
|
113
|
+
amount: events.claimRewardEvent.reward_amount,
|
|
114
|
+
coinType: events.claimRewardEvent.reward_coin_type
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
default:
|
|
118
|
+
return { action: 'unknown', amount: '0', coinType: '' }
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
## 说明
|
|
124
|
+
|
|
125
|
+
- **CoinType 无需额外筛选**:事件中的 `coin_type` 已是正确的质押资产类型
|
|
126
|
+
- Long 仓位 → `coin_type` = base_token
|
|
127
|
+
- Short 仓位 → `coin_type` = quote_token
|
|
128
|
+
- **Amount 直接读取**:从对应事件字段直接获取,无需计算
|
|
129
|
+
- **仅关注 SuiLend 层**:忽略 Swap 层的资产转换,只展示实际的质押资产变化
|
|
130
|
+
|
|
131
|
+
---
|
|
132
|
+
|
|
133
|
+
# Part 2: 详细分析
|
|
134
|
+
|
|
135
|
+
## 1. 合约事件清单
|
|
136
|
+
|
|
137
|
+
### 1.1 Position 事件 (position.move:35-93)
|
|
138
|
+
|
|
139
|
+
| 事件 | 字段 | 发射位置 |
|
|
140
|
+
|------|------|----------|
|
|
141
|
+
| `OpenPositionEvent` | position_id, market_id, obligation_id, leverage, is_long, collateral_coin_type, loan_coin_type, owner, timestamp | router.move:71-77 |
|
|
142
|
+
| `ClosePositionEvent` | position_id, market_id, owner, timestamp | router.move:107 |
|
|
143
|
+
| `DepositEvent` | position_id, owner, coin_type, deposit_amount, timestamp | router.move:366 |
|
|
144
|
+
| `WithdrawEvent` | position_id, owner, coin_type, withdraw_amount, timestamp | router.move:314 |
|
|
145
|
+
| `BorrowEvent` | position_id, owner, coin_type, borrow_amount, timestamp | router.move:188 |
|
|
146
|
+
| `RepayEvent` | position_id, owner, repay_coin_type, repay_amount, timestamp | router.move:222 |
|
|
147
|
+
| `ClaimRewardEvent` | position_id, owner, reward_coin_type, reward_amount, timestamp | router.move:396 |
|
|
148
|
+
|
|
149
|
+
### 1.2 外部 Swap 事件
|
|
150
|
+
|
|
151
|
+
```
|
|
152
|
+
Package: 0x33ec64e9bb369bf045ddc198c81adbf2acab424da37465d95296ee02045d2b17
|
|
153
|
+
Module: router
|
|
154
|
+
Event: ConfirmSwapEvent
|
|
155
|
+
|
|
156
|
+
字段:
|
|
157
|
+
- amount_in: string
|
|
158
|
+
- amount_out: string
|
|
159
|
+
- from: { name: string } // 输入币种
|
|
160
|
+
- target: { name: string } // 输出币种
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## 2. Action 列表(共 10 种)
|
|
166
|
+
|
|
167
|
+
```move
|
|
168
|
+
action_bytes == b"open_position" ||
|
|
169
|
+
action_bytes == b"close_position" ||
|
|
170
|
+
action_bytes == b"increase_size" ||
|
|
171
|
+
action_bytes == b"decrease_size" ||
|
|
172
|
+
action_bytes == b"increase_leverage" ||
|
|
173
|
+
action_bytes == b"decrease_leverage" ||
|
|
174
|
+
action_bytes == b"top_up_collateral" ||
|
|
175
|
+
action_bytes == b"withdraw_collateral" ||
|
|
176
|
+
action_bytes == b"repay_debt" ||
|
|
177
|
+
action_bytes == b"claim_reward"
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
## 3. 事件特征矩阵
|
|
183
|
+
|
|
184
|
+
| Action | Open | Close | Deposit | Withdraw | Borrow | Repay | Claim | Liquidate | Swap |
|
|
185
|
+
|--------|:----:|:-----:|:-------:|:--------:|:------:|:-----:|:-----:|:---------:|:----:|
|
|
186
|
+
| open_position | **必须** | - | **必须** | - | **必须** | - | - | - | 可选 |
|
|
187
|
+
| close_position | - | **必须** | - | **必须**(1+) | - | **必须** | - | - | 可选 |
|
|
188
|
+
| increase_size | - | - | **必须** | - | **必须** | - | - | - | 可选 |
|
|
189
|
+
| decrease_size | - | - | - | **必须**(1+) | - | **必须** | - | - | 可选 |
|
|
190
|
+
| increase_leverage | - | - | **必须** | - | **必须** | - | - | - | **必须** |
|
|
191
|
+
| decrease_leverage | - | - | - | **必须** | - | **必须** | - | - | **必须** |
|
|
192
|
+
| top_up_collateral | - | - | **必须** | - | - | - | - | - | 可选 |
|
|
193
|
+
| withdraw_collateral | - | - | - | **必须** | - | - | - | - | - |
|
|
194
|
+
| repay_debt | - | - | - | - | - | **必须** | - | - | 可选 |
|
|
195
|
+
| claim_reward | - | - | - | - | - | - | **必须** | - | - |
|
|
196
|
+
| liquidation | - | - | - | - | - | - | - | **必须** | - |
|
|
197
|
+
|
|
198
|
+
> **注意**: "(1+)" 表示可能有多个该事件
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
## 4. 难点场景分析
|
|
203
|
+
|
|
204
|
+
### 4.1 increase_size vs increase_leverage
|
|
205
|
+
|
|
206
|
+
**事件组合相同**: `DepositEvent + BorrowEvent + ConfirmSwapEvent`
|
|
207
|
+
|
|
208
|
+
**区分方法**:
|
|
209
|
+
```
|
|
210
|
+
user_input = DepositEvent.deposit_amount - ConfirmSwapEvent.amount_out
|
|
211
|
+
|
|
212
|
+
IF user_input > 0:
|
|
213
|
+
→ increase_size (用户投入了本金)
|
|
214
|
+
ELSE:
|
|
215
|
+
→ increase_leverage (用户没有投入本金,deposit = swap_out)
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
### 4.2 decrease_size vs decrease_leverage
|
|
219
|
+
|
|
220
|
+
**事件组合**: `WithdrawEvent + RepayEvent`
|
|
221
|
+
|
|
222
|
+
#### 4.2.1 两种操作的 SDK 流程对比
|
|
223
|
+
|
|
224
|
+
| 步骤 | decrease_size | decrease_leverage |
|
|
225
|
+
|------|---------------|-------------------|
|
|
226
|
+
| 1 | Flash loan 借**债务资产** | Flash loan 借**抵押资产** X |
|
|
227
|
+
| 2 | Repay 还债 | Swap X → 债务资产 |
|
|
228
|
+
| 3 | Withdraw 抵押资产 Y | Repay 还债 |
|
|
229
|
+
| 4 | Swap 部分 Y → 债务资产 (还 flash loan) | Withdraw X (还 flash loan) |
|
|
230
|
+
| 5 | 剩余 Y 返还用户 | - |
|
|
231
|
+
|
|
232
|
+
#### 4.2.2 关键特征
|
|
233
|
+
|
|
234
|
+
| 特征 | decrease_size | decrease_leverage |
|
|
235
|
+
|------|---------------|-------------------|
|
|
236
|
+
| ConfirmSwapEvent | **可选** | **必须存在** |
|
|
237
|
+
| Swap 方向 | collateral → debt | collateral → debt |
|
|
238
|
+
| swap_in vs withdraw | swap_in **<<** withdraw | swap_in **≈** withdraw |
|
|
239
|
+
| Flash Loan 借入币种 | 债务资产 | 抵押资产 |
|
|
240
|
+
|
|
241
|
+
**重要**: 两者的 Swap 方向**都是** collateral → debt,**不能**通过 swap.from 币种来区分!
|
|
242
|
+
|
|
243
|
+
#### 4.2.3 精确区分方法
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
function distinguishDecreaseAction(
|
|
247
|
+
events: TransactionEvents,
|
|
248
|
+
position: { is_long: boolean, base_token: string, quote_token: string }
|
|
249
|
+
): { action: string, amount: string, coinType: string } {
|
|
250
|
+
|
|
251
|
+
const { is_long, base_token, quote_token } = position
|
|
252
|
+
const collateralCoinType = is_long ? base_token : quote_token
|
|
253
|
+
|
|
254
|
+
// 根据 coin_type 筛选抵押资产的 WithdrawEvent
|
|
255
|
+
const withdrawEvent = events.withdrawEvents.find(e => e.coin_type === collateralCoinType)
|
|
256
|
+
const hasSwap = events.confirmSwapEvents.length > 0
|
|
257
|
+
|
|
258
|
+
// ========== 步骤 1: 检查是否有 Swap 事件 ==========
|
|
259
|
+
if (!hasSwap) {
|
|
260
|
+
// 无 swap 事件 → decrease_size
|
|
261
|
+
// 原因: decrease_leverage 必须有 swap (将抵押资产换成债务资产还债)
|
|
262
|
+
return {
|
|
263
|
+
action: 'decrease_size',
|
|
264
|
+
amount: withdrawEvent.withdraw_amount,
|
|
265
|
+
coinType: withdrawEvent.coin_type // 抵押资产
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
// ========== 步骤 2: 有 Swap,通过比例判断 ==========
|
|
270
|
+
const swapEvent = events.confirmSwapEvents[0]
|
|
271
|
+
const swapIn = BigInt(swapEvent.amount_in)
|
|
272
|
+
const withdrawAmount = BigInt(withdrawEvent.withdraw_amount)
|
|
273
|
+
|
|
274
|
+
// 计算 swap_in / withdraw_amount 比例
|
|
275
|
+
const ratio = Number(swapIn * 100n / withdrawAmount) / 100
|
|
276
|
+
|
|
277
|
+
if (ratio >= 0.95 && ratio <= 1.05) {
|
|
278
|
+
// swap_in ≈ withdraw (95%-105%)
|
|
279
|
+
// → decrease_leverage
|
|
280
|
+
// 原因: flash loan 借抵押资产 X,全部 X 用于 swap,withdraw X 用于还 flash loan
|
|
281
|
+
return {
|
|
282
|
+
action: 'decrease_leverage',
|
|
283
|
+
amount: '0', // 固定值
|
|
284
|
+
coinType: withdrawEvent.coin_type // 抵押资产
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
// swap_in << withdraw (比例 < 95%)
|
|
289
|
+
// → decrease_size
|
|
290
|
+
// 原因: 只有部分 withdraw 被 swap 用于还 flash loan,大部分返还用户
|
|
291
|
+
return {
|
|
292
|
+
action: 'decrease_size',
|
|
293
|
+
amount: withdrawEvent.withdraw_amount,
|
|
294
|
+
coinType: withdrawEvent.coin_type // 抵押资产
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
```
|
|
298
|
+
|
|
299
|
+
#### 4.2.4 判断逻辑总结
|
|
300
|
+
|
|
301
|
+
```
|
|
302
|
+
IF 无 ConfirmSwapEvent:
|
|
303
|
+
→ decrease_size
|
|
304
|
+
→ amount = withdraw_amount
|
|
305
|
+
→ coinType = withdraw.coin_type
|
|
306
|
+
|
|
307
|
+
ELSE IF swap_in / withdraw_amount ∈ [0.95, 1.05]:
|
|
308
|
+
→ decrease_leverage
|
|
309
|
+
→ amount = '0' (固定值)
|
|
310
|
+
→ coinType = withdraw.coin_type
|
|
311
|
+
|
|
312
|
+
ELSE (swap_in / withdraw_amount < 0.95):
|
|
313
|
+
→ decrease_size
|
|
314
|
+
→ amount = withdraw_amount
|
|
315
|
+
→ coinType = withdraw.coin_type
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
#### 4.2.5 为什么比例判断有效?
|
|
319
|
+
|
|
320
|
+
**decrease_leverage 的数量关系**:
|
|
321
|
+
```
|
|
322
|
+
flash_loan_amount = X (抵押资产)
|
|
323
|
+
swap_in = X (全部 flash loan 用于 swap)
|
|
324
|
+
withdraw_amount = X (用于还 flash loan)
|
|
325
|
+
|
|
326
|
+
→ swap_in ≈ withdraw_amount ≈ X
|
|
327
|
+
→ 比例 ≈ 1.0
|
|
328
|
+
```
|
|
329
|
+
|
|
330
|
+
**decrease_size 的数量关系**:
|
|
331
|
+
```
|
|
332
|
+
flash_loan_amount = F (债务资产)
|
|
333
|
+
withdraw_amount = Y (抵押资产,返还用户 + 还 flash loan)
|
|
334
|
+
swap_in = 部分 Y (只用于还 flash loan 的部分)
|
|
335
|
+
|
|
336
|
+
→ swap_in << withdraw_amount
|
|
337
|
+
→ 比例 << 1.0 (通常 10%-50%)
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
#### 4.2.6 注意事项
|
|
341
|
+
|
|
342
|
+
1. **两者 Swap 方向相同**: 都是 collateral → debt,不能用 `swap.from` 币种判断
|
|
343
|
+
2. **CoinType 统一返回抵押资产**: 即 `withdraw.coin_type`,不要返回 `swap.target`
|
|
344
|
+
3. **Amount 区别**:
|
|
345
|
+
- decrease_size: 返回 `withdraw_amount`
|
|
346
|
+
- decrease_leverage: 返回 `'0'` (原 context 设计如此)
|
|
347
|
+
|
|
348
|
+
---
|
|
349
|
+
|
|
350
|
+
## 5. 边界情况
|
|
351
|
+
|
|
352
|
+
### 5.1 多个 WithdrawEvent
|
|
353
|
+
|
|
354
|
+
`close_position` 和 `decrease_size` 可能产生多个 `WithdrawEvent`:
|
|
355
|
+
- 主要抵押资产的 WithdrawEvent
|
|
356
|
+
- 奖励资产的 WithdrawEvent
|
|
357
|
+
|
|
358
|
+
**处理规则**: 不能依赖事件顺序,需要根据 `coin_type` 筛选:
|
|
359
|
+
```typescript
|
|
360
|
+
// 根据仓位方向确定抵押资产类型
|
|
361
|
+
const collateralCoinType = is_long ? base_token : quote_token
|
|
362
|
+
|
|
363
|
+
// 筛选出抵押资产的 WithdrawEvent
|
|
364
|
+
const mainWithdrawEvent = withdrawEvents.find(e => e.coin_type === collateralCoinType)
|
|
365
|
+
```
|
|
366
|
+
|
|
367
|
+
### 5.2 decrease_size 自动转 close_position
|
|
368
|
+
|
|
369
|
+
当用户请求提取的比例 >= 100% 时,SDK 自动调用 `close_position`,会产生 `ClosePositionEvent`
|
|
370
|
+
|
|
371
|
+
**处理规则**:
|
|
372
|
+
- **Action Type**: 以 `ClosePositionEvent` 存在为准,判定为 `close_position`
|
|
373
|
+
- **WithdrawEvent**: 根据 `coin_type` 筛选,不依赖事件顺序
|
|
374
|
+
|
|
375
|
+
```typescript
|
|
376
|
+
// Action 判断:ClosePositionEvent 存在即为 close_position
|
|
377
|
+
if (events.closePositionEvent) {
|
|
378
|
+
action = 'close_position'
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// Amount/CoinType 提取:根据 coin_type 筛选 WithdrawEvent
|
|
382
|
+
const collateralCoinType = is_long ? base_token : quote_token
|
|
383
|
+
const withdrawEvent = events.withdrawEvents.find(e => e.coin_type === collateralCoinType)
|
|
384
|
+
```
|
|
385
|
+
|
|
386
|
+
### 5.3 increase_leverage / decrease_leverage 的 amount 为 0
|
|
387
|
+
|
|
388
|
+
这两个 Action 原始 context 中的 amount 就是 `'0'`
|
|
389
|
+
|
|
390
|
+
---
|
|
391
|
+
|
|
392
|
+
## 6. 事件顺序规则
|
|
393
|
+
|
|
394
|
+
| Action | 典型事件顺序 |
|
|
395
|
+
|--------|-------------|
|
|
396
|
+
| open_position | `[ConfirmSwapEvent?] → OpenPositionEvent → DepositEvent → BorrowEvent` |
|
|
397
|
+
| close_position | `RepayEvent → WithdrawEvent(多个) → [ConfirmSwapEvent?] → ClosePositionEvent` |
|
|
398
|
+
| increase_size | `[ConfirmSwapEvent?] → DepositEvent → BorrowEvent` |
|
|
399
|
+
| decrease_size | `RepayEvent → WithdrawEvent(多个) → [ConfirmSwapEvent?]` |
|
|
400
|
+
| increase_leverage | `ConfirmSwapEvent → DepositEvent → BorrowEvent` |
|
|
401
|
+
| decrease_leverage | `ConfirmSwapEvent → RepayEvent → WithdrawEvent` |
|
|
402
|
+
| top_up_collateral | `[ConfirmSwapEvent?] → DepositEvent` |
|
|
403
|
+
| repay_debt | `[ConfirmSwapEvent?] → RepayEvent` |
|
|
404
|
+
| claim_reward | `ClaimRewardEvent` |
|
|
405
|
+
| withdraw_collateral | `WithdrawEvent` |
|
|
406
|
+
|
|
407
|
+
---
|
|
408
|
+
|
|
409
|
+
## 8. 总结
|
|
410
|
+
|
|
411
|
+
### 确定性规则(6 种 Action)
|
|
412
|
+
- `open_position`: OpenPositionEvent 存在
|
|
413
|
+
- `close_position`: ClosePositionEvent 存在
|
|
414
|
+
- `claim_reward`: 仅 ClaimRewardEvent
|
|
415
|
+
- `withdraw_collateral`: 仅 WithdrawEvent
|
|
416
|
+
- `top_up_collateral`: 仅 DepositEvent(无 BorrowEvent)
|
|
417
|
+
- `repay_debt`: 仅 RepayEvent(无 WithdrawEvent)
|
|
418
|
+
|
|
419
|
+
### 需要辅助判断的规则(4 种 Action)
|
|
420
|
+
- `increase_size` vs `increase_leverage`: 检查是否有用户本金输入
|
|
421
|
+
- `decrease_size` vs `decrease_leverage`: 检查 Flash Loan 借入币种
|
|
422
|
+
|
|
423
|
+
### 核心注意点
|
|
424
|
+
1. **事件中的 amount 是处理后的数量**,可能与用户原始输入不同
|
|
425
|
+
2. **increase_leverage / decrease_leverage 的 amount 原本就是 0**
|
|
426
|
+
3. **可能有多个 WithdrawEvent**,第一个是主操作
|
|
427
|
+
4. **decrease_size 可能自动转为 close_position**
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cetusprotocol/margin-sdk",
|
|
3
|
+
"version": "0.0.0-experimental-20260302132606",
|
|
4
|
+
"description": "SDK fon leverage",
|
|
5
|
+
"typings": "dist/index.d.ts",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"types": "./dist/index.d.ts",
|
|
9
|
+
"exports": {
|
|
10
|
+
".": {
|
|
11
|
+
"import": "./dist/index.js",
|
|
12
|
+
"types": "./dist/index.d.ts"
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"scripts": {
|
|
16
|
+
"test": "vitest run --include 'packages/margin-trading/**'",
|
|
17
|
+
"lint": "eslint src/*.ts src/**/*.ts",
|
|
18
|
+
"lint:fix": "eslint src/*.ts src/**/*.ts --fix",
|
|
19
|
+
"build": "pnpm run build:tsup",
|
|
20
|
+
"build:tsup": "npm run build:clean && npm run _build:node",
|
|
21
|
+
"build:clean": "rm -rf dist",
|
|
22
|
+
"_build:node": "tsup --format esm --dts",
|
|
23
|
+
"build:doc": "npx typedoc",
|
|
24
|
+
"publish_batch:test": "npm publish --tag experimental --access public",
|
|
25
|
+
"publish:test": "node ../../scripts/version.js leverage && npm publish --tag experimental --access public"
|
|
26
|
+
},
|
|
27
|
+
"repository": {
|
|
28
|
+
"type": "git",
|
|
29
|
+
"url": ""
|
|
30
|
+
},
|
|
31
|
+
"keywords": [],
|
|
32
|
+
"author": "test",
|
|
33
|
+
"license": "Apache-2.0",
|
|
34
|
+
"dependencies": {
|
|
35
|
+
"@pythnetwork/pyth-sui-js": "2.2.0",
|
|
36
|
+
"@cetusprotocol/aggregator-sdk": "1.4.3",
|
|
37
|
+
"@suilend/sdk": "1.1.75",
|
|
38
|
+
"@suilend/sui-fe": "0.3.29",
|
|
39
|
+
"@sentry/nextjs": "^10.5.0",
|
|
40
|
+
"@mysten/sui": "1.43.1",
|
|
41
|
+
"@cetusprotocol/common-sdk": "1.3.2",
|
|
42
|
+
"@cetusprotocol/sui-clmm-sdk": "1.4.0"
|
|
43
|
+
}
|
|
44
|
+
}
|