@signals-protocol/v1-sdk 1.0.0
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 +99 -0
- package/dist/clmsr-sdk.d.ts +110 -0
- package/dist/clmsr-sdk.js +980 -0
- package/dist/fees.d.ts +69 -0
- package/dist/fees.js +229 -0
- package/dist/index.d.ts +12 -0
- package/dist/index.js +65 -0
- package/dist/types.d.ts +161 -0
- package/dist/types.js +83 -0
- package/dist/utils/math.d.ts +185 -0
- package/dist/utils/math.js +427 -0
- package/dist/utils/prb-math.d.ts +6 -0
- package/dist/utils/prb-math.js +309 -0
- package/package.json +62 -0
package/README.md
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# Signals v1 SDK
|
|
2
|
+
|
|
3
|
+
Contract-aligned TypeScript SDK for Signals v1 CLMSR market math. Quote open and
|
|
4
|
+
close costs, compute inverse quantities, and map subgraph data into calculation
|
|
5
|
+
types.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @signals-protocol/v1-sdk
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Quick start
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import {
|
|
17
|
+
CLMSRSDK,
|
|
18
|
+
mapMarket,
|
|
19
|
+
mapDistribution,
|
|
20
|
+
toMicroUSDC,
|
|
21
|
+
} from "@signals-protocol/v1-sdk";
|
|
22
|
+
|
|
23
|
+
const sdk = new CLMSRSDK();
|
|
24
|
+
|
|
25
|
+
// Raw data from your subgraph (strings)
|
|
26
|
+
const rawMarket = {
|
|
27
|
+
liquidityParameter: "1000000000000000000",
|
|
28
|
+
minTick: -500,
|
|
29
|
+
maxTick: 500,
|
|
30
|
+
tickSpacing: 10,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
const rawDistribution = {
|
|
34
|
+
totalSum: "1000000000000000000",
|
|
35
|
+
binFactors: ["1000000000000000000", "1000000000000000000"],
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
const market = mapMarket(rawMarket);
|
|
39
|
+
const distribution = mapDistribution(rawDistribution);
|
|
40
|
+
|
|
41
|
+
const quantity = toMicroUSDC(10); // 10 USDC in 6 decimals
|
|
42
|
+
const quote = sdk.calculateOpenCost(-100, 100, quantity, distribution, market);
|
|
43
|
+
|
|
44
|
+
console.log(quote.cost.toString());
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
## Common tasks
|
|
48
|
+
|
|
49
|
+
### Quote sell proceeds
|
|
50
|
+
|
|
51
|
+
```ts
|
|
52
|
+
import { CLMSRSDK, toMicroUSDC } from "@signals-protocol/v1-sdk";
|
|
53
|
+
|
|
54
|
+
const sdk = new CLMSRSDK();
|
|
55
|
+
|
|
56
|
+
const position = {
|
|
57
|
+
lowerTick: -100,
|
|
58
|
+
upperTick: 100,
|
|
59
|
+
quantity: toMicroUSDC(10),
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const proceeds = sdk.calculateDecreaseProceeds(
|
|
63
|
+
position,
|
|
64
|
+
toMicroUSDC(5),
|
|
65
|
+
distribution,
|
|
66
|
+
market
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
console.log(proceeds.proceeds.toString());
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
### Inverse: budget to quantity
|
|
73
|
+
|
|
74
|
+
```ts
|
|
75
|
+
const result = sdk.calculateQuantityFromCost(
|
|
76
|
+
-100,
|
|
77
|
+
100,
|
|
78
|
+
toMicroUSDC(50),
|
|
79
|
+
distribution,
|
|
80
|
+
market,
|
|
81
|
+
true
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
console.log(result.quantity.toString());
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Exports
|
|
88
|
+
|
|
89
|
+
- `CLMSRSDK` / `SignalsSDK` for pricing and inverse functions
|
|
90
|
+
- `createCLMSRSDK`, `createSignalsSDK` convenience constructors
|
|
91
|
+
- `mapMarket`, `mapDistribution` for GraphQL-to-SDK conversions
|
|
92
|
+
- `MathUtils` for WAD/USDC helpers and math utilities
|
|
93
|
+
- Types in `types.ts` (e.g. `Market`, `MarketDistribution`, `Position`, `FeeInfo`)
|
|
94
|
+
|
|
95
|
+
## Notes
|
|
96
|
+
|
|
97
|
+
- Amounts are `big.js` values (USDC uses 6 decimals, WAD uses 18 decimals).
|
|
98
|
+
- Fee overlays follow the contract fee policy descriptor when provided.
|
|
99
|
+
- The SDK mirrors contract math and overflow guards for compatibility.
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { MarketDistribution, Market, Position, OpenCostResult, IncreaseCostResult, DecreaseProceedsResult, CloseProceedsResult, ClaimResult, QuantityFromCostResult, QuantityFromProceedsResult, PositionValueResult, USDCAmount, Quantity, Tick } from "./types";
|
|
2
|
+
export * from "./types";
|
|
3
|
+
export { toWAD, toMicroUSDC } from "./utils/math";
|
|
4
|
+
/**
|
|
5
|
+
* CLMSR SDK - 컨트랙트 뷰함수들과 역함수 제공
|
|
6
|
+
*/
|
|
7
|
+
export declare class CLMSRSDK {
|
|
8
|
+
/**
|
|
9
|
+
* calculateOpenCost - 새 포지션 열기 비용 계산
|
|
10
|
+
* @param lowerTick Lower tick bound (inclusive)
|
|
11
|
+
* @param upperTick Upper tick bound (exclusive)
|
|
12
|
+
* @param quantity 매수 수량
|
|
13
|
+
* @param distribution Current market distribution
|
|
14
|
+
* @param market Market parameters
|
|
15
|
+
*/
|
|
16
|
+
calculateOpenCost(lowerTick: Tick, upperTick: Tick, quantity: Quantity, distribution: MarketDistribution, market: Market): OpenCostResult;
|
|
17
|
+
/**
|
|
18
|
+
* calculateIncreaseCost - 기존 포지션 증가 비용 계산
|
|
19
|
+
*/
|
|
20
|
+
calculateIncreaseCost(position: Position, additionalQuantity: Quantity, distribution: MarketDistribution, market: Market): IncreaseCostResult;
|
|
21
|
+
/**
|
|
22
|
+
* Decrease position 비용 계산
|
|
23
|
+
*/
|
|
24
|
+
calculateDecreaseProceeds(position: Position, sellQuantity: Quantity, distribution: MarketDistribution, market: Market): DecreaseProceedsResult;
|
|
25
|
+
/**
|
|
26
|
+
* Close position 비용 계산
|
|
27
|
+
*/
|
|
28
|
+
calculateCloseProceeds(position: Position, distribution: MarketDistribution, market: Market): CloseProceedsResult;
|
|
29
|
+
/**
|
|
30
|
+
* Claim amount 계산
|
|
31
|
+
*/
|
|
32
|
+
calculateClaim(position: Position, settlementTick: Tick): ClaimResult;
|
|
33
|
+
/**
|
|
34
|
+
* 포지션의 현재 가치와 미실현 손익 계산
|
|
35
|
+
* @param position 포지션 정보
|
|
36
|
+
* @param totalCost 포지션의 총 비용 (OPEN + INCREASE 비용 합계)
|
|
37
|
+
* @param distribution Current market distribution
|
|
38
|
+
* @param market Market parameters
|
|
39
|
+
* @returns 포지션 현재 가치와 미실현 손익
|
|
40
|
+
*/
|
|
41
|
+
calculatePositionValue(position: Position, totalCost: USDCAmount, distribution: MarketDistribution, market: Market): PositionValueResult;
|
|
42
|
+
/**
|
|
43
|
+
* Sell position의 예상 수익 계산
|
|
44
|
+
* @param position 포지션 정보
|
|
45
|
+
* @param sellQuantity 매도할 수량
|
|
46
|
+
* @param distribution Current market distribution
|
|
47
|
+
* @param market Market parameters
|
|
48
|
+
* @returns 예상 수익
|
|
49
|
+
*/
|
|
50
|
+
calculateSellProceeds(position: Position, sellQuantity: Quantity, distribution: MarketDistribution, market: Market): DecreaseProceedsResult;
|
|
51
|
+
/**
|
|
52
|
+
* 주어진 총 지출(수수료 포함)으로 살 수 있는 수량 계산 (역산)
|
|
53
|
+
* @param lowerTick Lower tick bound (inclusive)
|
|
54
|
+
* @param upperTick Upper tick bound (exclusive)
|
|
55
|
+
* @param cost 총 지출 한도 (수수료 포함, 6 decimals)
|
|
56
|
+
* @param distribution Current market distribution
|
|
57
|
+
* @param market Market parameters
|
|
58
|
+
* @returns 구매 가능한 수량과 순수 베팅 비용
|
|
59
|
+
*/
|
|
60
|
+
calculateQuantityFromCost(lowerTick: Tick, upperTick: Tick, cost: USDCAmount, distribution: MarketDistribution, market: Market, includeFees?: boolean): QuantityFromCostResult;
|
|
61
|
+
private _calculateQuantityFromNetCost;
|
|
62
|
+
private _computeTotalSpendWithFees;
|
|
63
|
+
/**
|
|
64
|
+
* 주어진 목표 수익(수수료 반영)으로 필요한 매도 수량 역산
|
|
65
|
+
* @param position 보유 포지션 정보
|
|
66
|
+
* @param targetProceeds 수수료 제외 후 실제로 받고 싶은 금액 (6 decimals)
|
|
67
|
+
* @param distribution Current market distribution
|
|
68
|
+
* @param market Market parameters
|
|
69
|
+
* @returns 매도해야 할 수량과 검증된 실제 수익(수수료 제외 전 기준)
|
|
70
|
+
*/
|
|
71
|
+
calculateQuantityFromProceeds(position: Position, targetProceeds: USDCAmount, distribution: MarketDistribution, market: Market, includeFees?: boolean): QuantityFromProceedsResult;
|
|
72
|
+
/**
|
|
73
|
+
* 시장별 최대 수량 한계 검증 (컨트랙트와 동일한 제한)
|
|
74
|
+
* @param quantity 검증할 수량 (6 decimals)
|
|
75
|
+
* @param alpha 유동성 파라미터 α (18 decimals WAD)
|
|
76
|
+
* @throws Error if quantity exceeds market limit
|
|
77
|
+
*/
|
|
78
|
+
private _assertQuantityWithinLimit;
|
|
79
|
+
private _calculateTradeCostWad;
|
|
80
|
+
private _calculateSingleTradeCostWad;
|
|
81
|
+
private _calculateTradeCostChunked;
|
|
82
|
+
private _calculateSellProceedsWad;
|
|
83
|
+
private _calculateSingleSellProceedsWad;
|
|
84
|
+
private _calculateSellProceedsChunked;
|
|
85
|
+
private _computeSafeChunk;
|
|
86
|
+
/**
|
|
87
|
+
* 내부 헬퍼: 매도 수익 계산 (코드 중복 제거)
|
|
88
|
+
* @param lowerTick Lower tick bound (inclusive)
|
|
89
|
+
* @param upperTick Upper tick bound (exclusive)
|
|
90
|
+
* @param sellQuantity 매도할 수량
|
|
91
|
+
* @param positionQuantity 현재 포지션 수량 (검증용)
|
|
92
|
+
* @param distribution Current market distribution
|
|
93
|
+
* @param market Market parameters
|
|
94
|
+
* @returns 매도 수익
|
|
95
|
+
*/
|
|
96
|
+
private _calculateQuantityFromBaseProceeds;
|
|
97
|
+
private _calcSellProceeds;
|
|
98
|
+
private _computeNetProceedsAfterFees;
|
|
99
|
+
private computeFeeOverlay;
|
|
100
|
+
private validateTickRange;
|
|
101
|
+
private getAffectedSum;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Create CLMSR SDK instance
|
|
105
|
+
*/
|
|
106
|
+
export declare function createCLMSRSDK(): CLMSRSDK;
|
|
107
|
+
/**
|
|
108
|
+
* Create Signals SDK instance (v1 alias)
|
|
109
|
+
*/
|
|
110
|
+
export declare function createSignalsSDK(): CLMSRSDK;
|