@drift-labs/sdk 2.135.0-beta.0 → 2.135.0-beta.2
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/VERSION +1 -1
- package/bun.lock +3 -0
- package/lib/browser/dlob/DLOB.d.ts +67 -27
- package/lib/browser/dlob/DLOB.js +4 -4
- package/lib/browser/dlob/DLOBSubscriber.js +1 -1
- package/lib/browser/dlob/orderBookLevels.d.ts +3 -3
- package/lib/browser/dlob/orderBookLevels.js +5 -5
- package/lib/browser/driftClient.d.ts +2 -2
- package/lib/browser/driftClient.js +5 -14
- package/lib/browser/math/amm.d.ts +8 -12
- package/lib/browser/math/amm.js +20 -45
- package/lib/browser/math/funding.d.ts +5 -5
- package/lib/browser/math/funding.js +10 -10
- package/lib/browser/math/market.d.ts +6 -6
- package/lib/browser/math/market.js +12 -12
- package/lib/browser/math/orders.d.ts +5 -5
- package/lib/browser/math/orders.js +8 -8
- package/lib/browser/math/position.d.ts +2 -2
- package/lib/browser/math/position.js +3 -3
- package/lib/browser/math/trade.d.ts +5 -5
- package/lib/browser/math/trade.js +19 -19
- package/lib/browser/oracles/types.d.ts +2 -6
- package/lib/browser/oracles/utils.d.ts +2 -2
- package/lib/browser/oracles/utils.js +3 -5
- package/lib/browser/user.d.ts +1 -1
- package/lib/browser/user.js +10 -10
- package/lib/node/dlob/DLOB.d.ts +67 -27
- package/lib/node/dlob/DLOB.d.ts.map +1 -1
- package/lib/node/dlob/DLOB.js +4 -4
- package/lib/node/dlob/DLOBSubscriber.js +1 -1
- package/lib/node/dlob/orderBookLevels.d.ts +3 -3
- package/lib/node/dlob/orderBookLevels.d.ts.map +1 -1
- package/lib/node/dlob/orderBookLevels.js +5 -5
- package/lib/node/driftClient.d.ts +2 -2
- package/lib/node/driftClient.d.ts.map +1 -1
- package/lib/node/driftClient.js +5 -14
- package/lib/node/math/amm.d.ts +8 -12
- package/lib/node/math/amm.d.ts.map +1 -1
- package/lib/node/math/amm.js +20 -45
- package/lib/node/math/funding.d.ts +5 -5
- package/lib/node/math/funding.d.ts.map +1 -1
- package/lib/node/math/funding.js +10 -10
- package/lib/node/math/market.d.ts +6 -6
- package/lib/node/math/market.d.ts.map +1 -1
- package/lib/node/math/market.js +12 -12
- package/lib/node/math/orders.d.ts +5 -5
- package/lib/node/math/orders.d.ts.map +1 -1
- package/lib/node/math/orders.js +8 -8
- package/lib/node/math/position.d.ts +2 -2
- package/lib/node/math/position.d.ts.map +1 -1
- package/lib/node/math/position.js +3 -3
- package/lib/node/math/trade.d.ts +5 -5
- package/lib/node/math/trade.d.ts.map +1 -1
- package/lib/node/math/trade.js +19 -19
- package/lib/node/oracles/types.d.ts +2 -6
- package/lib/node/oracles/types.d.ts.map +1 -1
- package/lib/node/oracles/utils.d.ts +2 -2
- package/lib/node/oracles/utils.d.ts.map +1 -1
- package/lib/node/oracles/utils.js +3 -5
- package/lib/node/user.d.ts +1 -1
- package/lib/node/user.d.ts.map +1 -1
- package/lib/node/user.js +10 -10
- package/package.json +2 -1
- package/src/dlob/DLOB.ts +101 -61
- package/src/dlob/DLOBSubscriber.ts +1 -1
- package/src/dlob/orderBookLevels.ts +7 -7
- package/src/driftClient.ts +12 -18
- package/src/idl/drift.json +1 -1
- package/src/math/amm.ts +20 -74
- package/src/math/funding.ts +17 -5
- package/src/math/market.ts +13 -13
- package/src/math/orders.ts +10 -10
- package/src/math/position.ts +4 -4
- package/src/math/trade.ts +32 -20
- package/src/oracles/types.ts +5 -6
- package/src/oracles/utils.ts +5 -6
- package/src/user.ts +11 -11
package/VERSION
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
2.135.0-beta.
|
|
1
|
+
2.135.0-beta.2
|
package/bun.lock
CHANGED
|
@@ -29,6 +29,7 @@
|
|
|
29
29
|
"tweetnacl-util": "0.15.1",
|
|
30
30
|
"uuid": "8.3.2",
|
|
31
31
|
"yargs": "17.7.2",
|
|
32
|
+
"zod": "^4.0.17",
|
|
32
33
|
"zstddec": "0.1.0",
|
|
33
34
|
},
|
|
34
35
|
"devDependencies": {
|
|
@@ -1138,6 +1139,8 @@
|
|
|
1138
1139
|
|
|
1139
1140
|
"yocto-queue": ["yocto-queue@0.1.0", "", {}, "sha512-rVksvsnNCdJ/ohGc6xgPwyN8eheCxsiLM8mxuE/t/mOVqJewPuO1miLpTHQiRgTKCLexL4MeAFVagts7HmNZ2Q=="],
|
|
1140
1141
|
|
|
1142
|
+
"zod": ["zod@4.0.17", "", {}, "sha512-1PHjlYRevNxxdy2JZ8JcNAw7rX8V9P1AKkP+x/xZfxB0K5FYfuV+Ug6P/6NVSR2jHQ+FzDDoDHS04nYUsOIyLQ=="],
|
|
1143
|
+
|
|
1141
1144
|
"zstddec": ["zstddec@0.1.0", "", {}, "sha512-w2NTI8+3l3eeltKAdK8QpiLo/flRAr2p8AGeakfMZOXBxOg9HIu4LVDxBi81sYgVhFhdJjv1OrB5ssI8uFPoLg=="],
|
|
1142
1145
|
|
|
1143
1146
|
"@babel/types/@babel/helper-validator-identifier": ["@babel/helper-validator-identifier@7.27.1", "", {}, "sha512-D2hP9eA+Sqx1kBZgzxZh0y1trbuU+JoDkiEwqhQ36nodYqJwyEIhPSdMNd7lOm/4io72luTPWH20Yda0xOuUow=="],
|
|
@@ -4,7 +4,7 @@ import { BN } from '@coral-xyz/anchor';
|
|
|
4
4
|
import { DLOBNode, DLOBNodeType, TriggerOrderNode } from './DLOBNode';
|
|
5
5
|
import { DriftClient } from '../driftClient';
|
|
6
6
|
import { MarketType, MarketTypeStr, Order, PerpMarketAccount, PositionDirection, SpotMarketAccount, StateAccount } from '../types';
|
|
7
|
-
import { OraclePriceData } from '../oracles/types';
|
|
7
|
+
import { MMOraclePriceData, OraclePriceData } from '../oracles/types';
|
|
8
8
|
import { ProtectMakerParamsMap } from './types';
|
|
9
9
|
import { SlotSubscriber } from '../slot/SlotSubscriber';
|
|
10
10
|
import { UserMap } from '../userMap/userMap';
|
|
@@ -83,23 +83,45 @@ export declare class DLOB {
|
|
|
83
83
|
updateRestingLimitOrders(slot: number): void;
|
|
84
84
|
updateRestingLimitOrdersForMarketType(slot: number, marketTypeStr: MarketTypeStr): void;
|
|
85
85
|
getOrder(orderId: number, userAccount: PublicKey): Order | undefined;
|
|
86
|
-
findNodesToFill(marketIndex: number, fallbackBid: BN | undefined, fallbackAsk: BN | undefined, slot: number, ts: number, marketType:
|
|
86
|
+
findNodesToFill<T extends MarketType>(marketIndex: number, fallbackBid: BN | undefined, fallbackAsk: BN | undefined, slot: number, ts: number, marketType: T, oraclePriceData: T extends {
|
|
87
|
+
spot: unknown;
|
|
88
|
+
} ? OraclePriceData : MMOraclePriceData, stateAccount: StateAccount, marketAccount: T extends {
|
|
89
|
+
spot: unknown;
|
|
90
|
+
} ? SpotMarketAccount : PerpMarketAccount): NodeToFill[];
|
|
87
91
|
getMakerRebate(marketType: MarketType, stateAccount: StateAccount, marketAccount: PerpMarketAccount | SpotMarketAccount): {
|
|
88
92
|
makerRebateNumerator: number;
|
|
89
93
|
makerRebateDenominator: number;
|
|
90
94
|
};
|
|
91
95
|
mergeNodesToFill(restingLimitOrderNodesToFill: NodeToFill[], takingOrderNodesToFill: NodeToFill[]): NodeToFill[];
|
|
92
|
-
findRestingLimitOrderNodesToFill(marketIndex: number, slot: number, marketType:
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
+
findRestingLimitOrderNodesToFill<T extends MarketType>(marketIndex: number, slot: number, marketType: T, oraclePriceData: T extends {
|
|
97
|
+
spot: unknown;
|
|
98
|
+
} ? OraclePriceData : MMOraclePriceData, isAmmPaused: boolean, minAuctionDuration: number, makerRebateNumerator: number, makerRebateDenominator: number, fallbackAsk: BN | undefined, fallbackBid: BN | undefined): NodeToFill[];
|
|
99
|
+
findTakingNodesToFill<T extends MarketType>(marketIndex: number, slot: number, marketType: T, oraclePriceData: T extends {
|
|
100
|
+
spot: unknown;
|
|
101
|
+
} ? OraclePriceData : MMOraclePriceData, isAmmPaused: boolean, minAuctionDuration: number, fallbackAsk: BN | undefined, fallbackBid?: BN | undefined): NodeToFill[];
|
|
102
|
+
findTakingNodesCrossingMakerNodes<T extends MarketType>(marketIndex: number, slot: number, marketType: T, oraclePriceData: T extends {
|
|
103
|
+
spot: unknown;
|
|
104
|
+
} ? OraclePriceData : MMOraclePriceData, takerNodeGenerator: Generator<DLOBNode>, makerNodeGeneratorFn: (marketIndex: number, slot: number, marketType: MarketType, oraclePriceData: T extends {
|
|
105
|
+
spot: unknown;
|
|
106
|
+
} ? OraclePriceData : MMOraclePriceData) => Generator<DLOBNode>, doesCross: (takerPrice: BN | undefined, makerPrice: BN) => boolean): NodeToFill[];
|
|
107
|
+
findNodesCrossingFallbackLiquidity<T extends MarketType>(marketType: T, slot: number, oraclePriceData: T extends {
|
|
108
|
+
spot: unknown;
|
|
109
|
+
} ? OraclePriceData : MMOraclePriceData, nodeGenerator: Generator<DLOBNode>, doesCross: (nodePrice: BN | undefined) => boolean, minAuctionDuration: number): NodeToFill[];
|
|
96
110
|
findExpiredNodesToFill(marketIndex: number, ts: number, marketType: MarketType, slot?: BN): NodeToFill[];
|
|
97
|
-
getTakingBids(marketIndex: number, marketType:
|
|
98
|
-
|
|
111
|
+
getTakingBids<T extends MarketType>(marketIndex: number, marketType: T, slot: number, oraclePriceData: T extends {
|
|
112
|
+
spot: unknown;
|
|
113
|
+
} ? OraclePriceData : MMOraclePriceData, filterFcn?: DLOBFilterFcn): Generator<DLOBNode>;
|
|
114
|
+
getTakingAsks<T extends MarketType>(marketIndex: number, marketType: T, slot: number, oraclePriceData: T extends {
|
|
115
|
+
spot: unknown;
|
|
116
|
+
} ? OraclePriceData : MMOraclePriceData, filterFcn?: DLOBFilterFcn): Generator<DLOBNode>;
|
|
99
117
|
protected signedMsgGenerator(signedMsgOrderList: NodeList<'signedMsg'>, filter: (x: DLOBNode) => boolean): Generator<DLOBNode>;
|
|
100
118
|
protected getBestNode(generatorList: Array<Generator<DLOBNode>>, oraclePriceData: OraclePriceData, slot: number, compareFcn: (bestDLOBNode: DLOBNode, currentDLOBNode: DLOBNode, slot: number, oraclePriceData: OraclePriceData) => boolean, filterFcn?: DLOBFilterFcn): Generator<DLOBNode>;
|
|
101
|
-
getRestingLimitAsks(marketIndex: number, slot: number, marketType:
|
|
102
|
-
|
|
119
|
+
getRestingLimitAsks<T extends MarketType>(marketIndex: number, slot: number, marketType: T, oraclePriceData: T extends {
|
|
120
|
+
spot: unknown;
|
|
121
|
+
} ? OraclePriceData : MMOraclePriceData, filterFcn?: DLOBFilterFcn): Generator<DLOBNode>;
|
|
122
|
+
getRestingLimitBids<T extends MarketType>(marketIndex: number, slot: number, marketType: T, oraclePriceData: T extends {
|
|
123
|
+
spot: unknown;
|
|
124
|
+
} ? OraclePriceData : MMOraclePriceData, filterFcn?: DLOBFilterFcn): Generator<DLOBNode>;
|
|
103
125
|
/**
|
|
104
126
|
* This will look at both the taking and resting limit asks
|
|
105
127
|
* @param marketIndex
|
|
@@ -109,7 +131,9 @@ export declare class DLOB {
|
|
|
109
131
|
* @param oraclePriceData
|
|
110
132
|
* @param filterFcn
|
|
111
133
|
*/
|
|
112
|
-
getAsks(marketIndex: number, _fallbackAsk: BN | undefined, slot: number, marketType:
|
|
134
|
+
getAsks<T extends MarketType>(marketIndex: number, _fallbackAsk: BN | undefined, slot: number, marketType: T, oraclePriceData: T extends {
|
|
135
|
+
spot: unknown;
|
|
136
|
+
} ? OraclePriceData : MMOraclePriceData, filterFcn?: DLOBFilterFcn): Generator<DLOBNode>;
|
|
113
137
|
/**
|
|
114
138
|
* This will look at both the taking and resting limit bids
|
|
115
139
|
* @param marketIndex
|
|
@@ -119,14 +143,22 @@ export declare class DLOB {
|
|
|
119
143
|
* @param oraclePriceData
|
|
120
144
|
* @param filterFcn
|
|
121
145
|
*/
|
|
122
|
-
getBids(marketIndex: number, _fallbackBid: BN | undefined, slot: number, marketType:
|
|
123
|
-
|
|
146
|
+
getBids<T extends MarketType>(marketIndex: number, _fallbackBid: BN | undefined, slot: number, marketType: T, oraclePriceData: T extends {
|
|
147
|
+
spot: unknown;
|
|
148
|
+
} ? OraclePriceData : MMOraclePriceData, filterFcn?: DLOBFilterFcn): Generator<DLOBNode>;
|
|
149
|
+
findCrossingRestingLimitOrders<T extends MarketType>(marketIndex: number, slot: number, marketType: T, oraclePriceData: T extends {
|
|
150
|
+
spot: unknown;
|
|
151
|
+
} ? OraclePriceData : MMOraclePriceData): NodeToFill[];
|
|
124
152
|
determineMakerAndTaker(askNode: DLOBNode, bidNode: DLOBNode): {
|
|
125
153
|
takerNode: DLOBNode;
|
|
126
154
|
makerNode: DLOBNode;
|
|
127
155
|
} | undefined;
|
|
128
|
-
getBestAsk(marketIndex: number, slot: number, marketType:
|
|
129
|
-
|
|
156
|
+
getBestAsk<T extends MarketType>(marketIndex: number, slot: number, marketType: T, oraclePriceData: T extends {
|
|
157
|
+
spot: unknown;
|
|
158
|
+
} ? OraclePriceData : MMOraclePriceData): BN | undefined;
|
|
159
|
+
getBestBid<T extends MarketType>(marketIndex: number, slot: number, marketType: T, oraclePriceData: T extends {
|
|
160
|
+
spot: unknown;
|
|
161
|
+
} ? OraclePriceData : MMOraclePriceData): BN | undefined;
|
|
130
162
|
getStopLosses(marketIndex: number, marketType: MarketType, direction: PositionDirection): Generator<DLOBNode>;
|
|
131
163
|
getStopLossMarkets(marketIndex: number, marketType: MarketType, direction: PositionDirection): Generator<DLOBNode>;
|
|
132
164
|
getStopLossLimits(marketIndex: number, marketType: MarketType, direction: PositionDirection): Generator<DLOBNode>;
|
|
@@ -147,11 +179,13 @@ export declare class DLOB {
|
|
|
147
179
|
* @param depth how many levels of the order book to return
|
|
148
180
|
* @param fallbackL2Generators L2 generators for fallback liquidity e.g. vAMM {@link getVammL2Generator}, openbook {@link SerumSubscriber}
|
|
149
181
|
*/
|
|
150
|
-
getL2({ marketIndex, marketType, slot, oraclePriceData, depth, fallbackL2Generators, }: {
|
|
182
|
+
getL2<T extends MarketType>({ marketIndex, marketType, slot, oraclePriceData, depth, fallbackL2Generators, }: {
|
|
151
183
|
marketIndex: number;
|
|
152
|
-
marketType:
|
|
184
|
+
marketType: T;
|
|
153
185
|
slot: number;
|
|
154
|
-
oraclePriceData:
|
|
186
|
+
oraclePriceData: T extends {
|
|
187
|
+
spot: unknown;
|
|
188
|
+
} ? OraclePriceData : MMOraclePriceData;
|
|
155
189
|
depth: number;
|
|
156
190
|
fallbackL2Generators?: L2OrderBookGenerator[];
|
|
157
191
|
}): L2OrderBook;
|
|
@@ -163,11 +197,13 @@ export declare class DLOB {
|
|
|
163
197
|
* @param slot
|
|
164
198
|
* @param oraclePriceData
|
|
165
199
|
*/
|
|
166
|
-
getL3({ marketIndex, marketType, slot, oraclePriceData, }: {
|
|
200
|
+
getL3<T extends MarketType>({ marketIndex, marketType, slot, oraclePriceData, }: {
|
|
167
201
|
marketIndex: number;
|
|
168
|
-
marketType:
|
|
202
|
+
marketType: T;
|
|
169
203
|
slot: number;
|
|
170
|
-
oraclePriceData:
|
|
204
|
+
oraclePriceData: T extends {
|
|
205
|
+
spot: unknown;
|
|
206
|
+
} ? OraclePriceData : MMOraclePriceData;
|
|
171
207
|
}): L3OrderBook;
|
|
172
208
|
private estimateFillExactBaseAmountInForSide;
|
|
173
209
|
/**
|
|
@@ -180,20 +216,24 @@ export declare class DLOB {
|
|
|
180
216
|
* @param param.oraclePriceData the oracle price data
|
|
181
217
|
* @returns the estimated quote amount filled: QUOTE_PRECISION
|
|
182
218
|
*/
|
|
183
|
-
estimateFillWithExactBaseAmount({ marketIndex, marketType, baseAmount, orderDirection, slot, oraclePriceData, }: {
|
|
219
|
+
estimateFillWithExactBaseAmount<T extends MarketType>({ marketIndex, marketType, baseAmount, orderDirection, slot, oraclePriceData, }: {
|
|
184
220
|
marketIndex: number;
|
|
185
|
-
marketType:
|
|
221
|
+
marketType: T;
|
|
186
222
|
baseAmount: BN;
|
|
187
223
|
orderDirection: PositionDirection;
|
|
188
224
|
slot: number;
|
|
189
|
-
oraclePriceData:
|
|
225
|
+
oraclePriceData: T extends {
|
|
226
|
+
spot: unknown;
|
|
227
|
+
} ? OraclePriceData : MMOraclePriceData;
|
|
190
228
|
}): BN;
|
|
191
|
-
getBestMakers({ marketIndex, marketType, direction, slot, oraclePriceData, numMakers, }: {
|
|
229
|
+
getBestMakers<T extends MarketType>({ marketIndex, marketType, direction, slot, oraclePriceData, numMakers, }: {
|
|
192
230
|
marketIndex: number;
|
|
193
|
-
marketType:
|
|
231
|
+
marketType: T;
|
|
194
232
|
direction: PositionDirection;
|
|
195
233
|
slot: number;
|
|
196
|
-
oraclePriceData:
|
|
234
|
+
oraclePriceData: T extends {
|
|
235
|
+
spot: unknown;
|
|
236
|
+
} ? OraclePriceData : MMOraclePriceData;
|
|
197
237
|
numMakers: number;
|
|
198
238
|
}): PublicKey[];
|
|
199
239
|
}
|
package/lib/browser/dlob/DLOB.js
CHANGED
|
@@ -895,7 +895,7 @@ class DLOB {
|
|
|
895
895
|
printTop(driftClient, slotSubscriber, marketIndex, marketType) {
|
|
896
896
|
if ((0, types_1.isVariant)(marketType, 'perp')) {
|
|
897
897
|
const slot = slotSubscriber.getSlot();
|
|
898
|
-
const oraclePriceData = driftClient.
|
|
898
|
+
const oraclePriceData = driftClient.getMMOracleDataForPerpMarket(marketIndex);
|
|
899
899
|
const bestAsk = this.getBestAsk(marketIndex, slot, marketType, oraclePriceData);
|
|
900
900
|
const bestBid = this.getBestBid(marketIndex, slot, marketType, oraclePriceData);
|
|
901
901
|
const mid = bestAsk.add(bestBid).div(new anchor_1.BN(2));
|
|
@@ -915,9 +915,9 @@ class DLOB {
|
|
|
915
915
|
}
|
|
916
916
|
else if ((0, types_1.isVariant)(marketType, 'spot')) {
|
|
917
917
|
const slot = slotSubscriber.getSlot();
|
|
918
|
-
const oraclePriceData = driftClient.
|
|
919
|
-
const bestAsk = this.getBestAsk(marketIndex, slot,
|
|
920
|
-
const bestBid = this.getBestBid(marketIndex, slot,
|
|
918
|
+
const oraclePriceData = driftClient.getOracleDataForSpotMarket(marketIndex);
|
|
919
|
+
const bestAsk = this.getBestAsk(marketIndex, slot, types_1.MarketType.SPOT, oraclePriceData);
|
|
920
|
+
const bestBid = this.getBestBid(marketIndex, slot, types_1.MarketType.SPOT, oraclePriceData);
|
|
921
921
|
const mid = bestAsk.add(bestBid).div(new anchor_1.BN(2));
|
|
922
922
|
const bidSpread = ((0, conversion_1.convertToNumber)(bestBid, numericConstants_1.PRICE_PRECISION) /
|
|
923
923
|
(0, conversion_1.convertToNumber)(oraclePriceData.price, numericConstants_1.PRICE_PRECISION) -
|
|
@@ -83,7 +83,7 @@ class DLOBSubscriber {
|
|
|
83
83
|
fallbackL2Generators = [
|
|
84
84
|
(0, orderBookLevels_1.getVammL2Generator)({
|
|
85
85
|
marketAccount: this.driftClient.getPerpMarketAccount(marketIndex),
|
|
86
|
-
|
|
86
|
+
mmOraclePriceData: this.driftClient.getMMOracleDataForPerpMarket(marketIndex),
|
|
87
87
|
numOrders: numVammOrders !== null && numVammOrders !== void 0 ? numVammOrders : depth,
|
|
88
88
|
topOfBookQuoteAmounts: marketIndex < 3
|
|
89
89
|
? orderBookLevels_1.MAJORS_TOP_OF_BOOK_QUOTE_AMOUNTS
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
import { BN } from '@coral-xyz/anchor';
|
|
3
3
|
import { DLOBNode } from './DLOBNode';
|
|
4
4
|
import { PerpMarketAccount } from '../types';
|
|
5
|
-
import { OraclePriceData } from '../oracles/types';
|
|
5
|
+
import { MMOraclePriceData, OraclePriceData } from '../oracles/types';
|
|
6
6
|
import { PublicKey } from '@solana/web3.js';
|
|
7
7
|
type liquiditySource = 'serum' | 'vamm' | 'dlob' | 'phoenix' | 'openbook' | 'indicative';
|
|
8
8
|
export type L2Level = {
|
|
@@ -43,9 +43,9 @@ export declare const MAJORS_TOP_OF_BOOK_QUOTE_AMOUNTS: BN[];
|
|
|
43
43
|
export declare function getL2GeneratorFromDLOBNodes(dlobNodes: Generator<DLOBNode>, oraclePriceData: OraclePriceData, slot: number): Generator<L2Level>;
|
|
44
44
|
export declare function mergeL2LevelGenerators(l2LevelGenerators: Generator<L2Level>[], compare: (a: L2Level, b: L2Level) => boolean): Generator<L2Level>;
|
|
45
45
|
export declare function createL2Levels(generator: Generator<L2Level>, depth: number): L2Level[];
|
|
46
|
-
export declare function getVammL2Generator({ marketAccount,
|
|
46
|
+
export declare function getVammL2Generator({ marketAccount, mmOraclePriceData, numOrders, now, topOfBookQuoteAmounts, }: {
|
|
47
47
|
marketAccount: PerpMarketAccount;
|
|
48
|
-
|
|
48
|
+
mmOraclePriceData: MMOraclePriceData;
|
|
49
49
|
numOrders: number;
|
|
50
50
|
now?: BN;
|
|
51
51
|
topOfBookQuoteAmounts?: BN[];
|
|
@@ -101,8 +101,8 @@ function createL2Levels(generator, depth) {
|
|
|
101
101
|
return levels;
|
|
102
102
|
}
|
|
103
103
|
exports.createL2Levels = createL2Levels;
|
|
104
|
-
function getVammL2Generator({ marketAccount,
|
|
105
|
-
const updatedAmm = (0, amm_1.calculateUpdatedAMM)(marketAccount.amm,
|
|
104
|
+
function getVammL2Generator({ marketAccount, mmOraclePriceData, numOrders, now = new anchor_1.BN(Math.floor(Date.now() / 1000)), topOfBookQuoteAmounts = [], }) {
|
|
105
|
+
const updatedAmm = (0, amm_1.calculateUpdatedAMM)(marketAccount.amm, mmOraclePriceData);
|
|
106
106
|
const paused = (0, exchangeStatus_1.isOperationPaused)(marketAccount.pausedOperations, types_1.PerpOperation.AMM_FILL);
|
|
107
107
|
let [openBids, openAsks] = paused
|
|
108
108
|
? [numericConstants_1.ZERO, numericConstants_1.ZERO]
|
|
@@ -111,12 +111,12 @@ function getVammL2Generator({ marketAccount, oraclePriceData, numOrders, now = n
|
|
|
111
111
|
openBids = numericConstants_1.ZERO;
|
|
112
112
|
if (openAsks.abs().lt(marketAccount.amm.minOrderSize.muln(2)))
|
|
113
113
|
openAsks = numericConstants_1.ZERO;
|
|
114
|
-
const [bidReserves, askReserves] = (0, amm_1.calculateSpreadReserves)(updatedAmm,
|
|
114
|
+
const [bidReserves, askReserves] = (0, amm_1.calculateSpreadReserves)(updatedAmm, mmOraclePriceData, now, (0, types_1.isVariant)(marketAccount.contractType, 'prediction'));
|
|
115
115
|
const numBaseOrders = Math.max(1, numOrders - topOfBookQuoteAmounts.length);
|
|
116
116
|
const commonOpts = {
|
|
117
117
|
numOrders,
|
|
118
118
|
numBaseOrders,
|
|
119
|
-
|
|
119
|
+
mmOraclePriceData,
|
|
120
120
|
orderTickSize: marketAccount.amm.orderTickSize,
|
|
121
121
|
orderStepSize: marketAccount.amm.orderStepSize,
|
|
122
122
|
pegMultiplier: updatedAmm.pegMultiplier,
|
|
@@ -139,7 +139,7 @@ function getVammL2Generator({ marketAccount, oraclePriceData, numOrders, now = n
|
|
|
139
139
|
const raw = commonOpts.topOfBookQuoteAmounts[count]
|
|
140
140
|
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
141
141
|
.mul(numericConstants_1.PRICE_PRECISION)
|
|
142
|
-
.div(commonOpts.
|
|
142
|
+
.div(commonOpts.mmOraclePriceData.price);
|
|
143
143
|
baseSwap = (0, orders_1.standardizeBaseAssetAmount)(raw, commonOpts.orderStepSize);
|
|
144
144
|
const remaining = openLiquidity.abs().sub(topSize);
|
|
145
145
|
if (remaining.lt(baseSwap))
|
|
@@ -12,7 +12,7 @@ import { EventEmitter } from 'events';
|
|
|
12
12
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
13
13
|
import { DataAndSlot, DriftClientAccountEvents, DriftClientAccountSubscriber } from './accounts/types';
|
|
14
14
|
import { TxSender, TxSigAndSlot } from './tx/types';
|
|
15
|
-
import { OraclePriceData } from './oracles/types';
|
|
15
|
+
import { MMOraclePriceData, OraclePriceData } from './oracles/types';
|
|
16
16
|
import { DriftClientConfig } from './driftClientConfig';
|
|
17
17
|
import { User } from './user';
|
|
18
18
|
import { UserSubscriptionConfig } from './userConfig';
|
|
@@ -849,7 +849,7 @@ export declare class DriftClient {
|
|
|
849
849
|
getSettleFundingPaymentIx(userAccountPublicKey: PublicKey): Promise<TransactionInstruction>;
|
|
850
850
|
triggerEvent(eventName: keyof DriftClientAccountEvents, data?: any): void;
|
|
851
851
|
getOracleDataForPerpMarket(marketIndex: number): OraclePriceData;
|
|
852
|
-
getMMOracleDataForPerpMarket(marketIndex: number):
|
|
852
|
+
getMMOracleDataForPerpMarket(marketIndex: number): MMOraclePriceData;
|
|
853
853
|
getOracleDataForSpotMarket(marketIndex: number): OraclePriceData;
|
|
854
854
|
initializeInsuranceFundStake(marketIndex: number, txParams?: TxParams): Promise<TransactionSignature>;
|
|
855
855
|
getInitializeInsuranceFundStakeIx(marketIndex: number): Promise<TransactionInstruction>;
|
|
@@ -4437,17 +4437,13 @@ class DriftClient {
|
|
|
4437
4437
|
this.eventEmitter.emit(eventName, data);
|
|
4438
4438
|
}
|
|
4439
4439
|
getOracleDataForPerpMarket(marketIndex) {
|
|
4440
|
-
|
|
4441
|
-
const isMMOracleActive = !perpMarket.amm.mmOracleSlot.eq(numericConstants_1.ZERO);
|
|
4442
|
-
return {
|
|
4443
|
-
...this.accountSubscriber.getOraclePriceDataAndSlotForPerpMarket(marketIndex).data,
|
|
4444
|
-
isMMOracleActive,
|
|
4445
|
-
};
|
|
4440
|
+
return this.accountSubscriber.getOraclePriceDataAndSlotForPerpMarket(marketIndex).data;
|
|
4446
4441
|
}
|
|
4447
4442
|
getMMOracleDataForPerpMarket(marketIndex) {
|
|
4448
4443
|
const perpMarket = this.getPerpMarketAccount(marketIndex);
|
|
4449
4444
|
const oracleData = this.getOracleDataForPerpMarket(marketIndex);
|
|
4450
4445
|
const stateAccountAndSlot = this.accountSubscriber.getStateAccountAndSlot();
|
|
4446
|
+
const isMMOracleActive = !perpMarket.amm.mmOracleSlot.eq(numericConstants_1.ZERO);
|
|
4451
4447
|
const pctDiff = perpMarket.amm.mmOraclePrice
|
|
4452
4448
|
.sub(oracleData.price)
|
|
4453
4449
|
.abs()
|
|
@@ -4458,21 +4454,16 @@ class DriftClient {
|
|
|
4458
4454
|
perpMarket.amm.mmOracleSlot < oracleData.slot ||
|
|
4459
4455
|
pctDiff.gt(numericConstants_1.PERCENTAGE_PRECISION.divn(100)) // 1% threshold
|
|
4460
4456
|
) {
|
|
4461
|
-
return { ...oracleData,
|
|
4457
|
+
return { ...oracleData, isMMOracleActive };
|
|
4462
4458
|
}
|
|
4463
4459
|
else {
|
|
4464
|
-
const conf = (0, utils_3.getOracleConfidenceFromMMOracleData)(
|
|
4465
|
-
mmOraclePrice: perpMarket.amm.mmOraclePrice,
|
|
4466
|
-
mmOracleSlot: perpMarket.amm.mmOracleSlot,
|
|
4467
|
-
oraclePriceData: oracleData,
|
|
4468
|
-
});
|
|
4460
|
+
const conf = (0, utils_3.getOracleConfidenceFromMMOracleData)(perpMarket.amm.mmOraclePrice, oracleData);
|
|
4469
4461
|
return {
|
|
4470
4462
|
price: perpMarket.amm.mmOraclePrice,
|
|
4471
4463
|
slot: perpMarket.amm.mmOracleSlot,
|
|
4472
4464
|
confidence: conf,
|
|
4473
4465
|
hasSufficientNumberOfDataPoints: true,
|
|
4474
|
-
|
|
4475
|
-
isMMOracleActive: oracleData.isMMOracleActive,
|
|
4466
|
+
isMMOracleActive,
|
|
4476
4467
|
};
|
|
4477
4468
|
}
|
|
4478
4469
|
}
|
|
@@ -1,22 +1,18 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '@coral-xyz/anchor';
|
|
3
3
|
import { AMM, PositionDirection, SwapDirection, PerpMarketAccount } from '../types';
|
|
4
|
-
import { OraclePriceData } from '../oracles/types';
|
|
4
|
+
import { MMOraclePriceData, OraclePriceData } from '../oracles/types';
|
|
5
5
|
export declare function calculatePegFromTargetPrice(targetPrice: BN, baseAssetReserve: BN, quoteAssetReserve: BN): BN;
|
|
6
|
-
export declare function calculateOptimalPegAndBudget(amm: AMM,
|
|
7
|
-
export declare function calculateNewAmm(amm: AMM,
|
|
8
|
-
export declare function calculateUpdatedAMM(amm: AMM,
|
|
9
|
-
export declare function calculateUpdatedAMMSpreadReserves(amm: AMM, direction: PositionDirection,
|
|
6
|
+
export declare function calculateOptimalPegAndBudget(amm: AMM, mmOraclePriceData: MMOraclePriceData): [BN, BN, BN, boolean];
|
|
7
|
+
export declare function calculateNewAmm(amm: AMM, mmOraclePriceData: MMOraclePriceData): [BN, BN, BN, BN];
|
|
8
|
+
export declare function calculateUpdatedAMM(amm: AMM, mmOraclePriceData: MMOraclePriceData): AMM;
|
|
9
|
+
export declare function calculateUpdatedAMMSpreadReserves(amm: AMM, direction: PositionDirection, mmOraclePriceData: MMOraclePriceData, isPrediction?: boolean): {
|
|
10
10
|
baseAssetReserve: BN;
|
|
11
11
|
quoteAssetReserve: BN;
|
|
12
12
|
sqrtK: BN;
|
|
13
13
|
newPeg: BN;
|
|
14
14
|
};
|
|
15
|
-
export declare function
|
|
16
|
-
/**
|
|
17
|
-
* @deprecated Use calculateAMMBidAskPrice instead
|
|
18
|
-
*/
|
|
19
|
-
export declare function calculateBidAskPrice(amm: AMM, oraclePriceData: OraclePriceData, withUpdate?: boolean, isPrediction?: boolean): [BN, BN];
|
|
15
|
+
export declare function calculateBidAskPrice(amm: AMM, mmOraclePriceData: MMOraclePriceData, withUpdate?: boolean, isPrediction?: boolean): [BN, BN];
|
|
20
16
|
/**
|
|
21
17
|
* Calculates a price given an arbitrary base and quote amount (they must have the same precision)
|
|
22
18
|
*
|
|
@@ -69,7 +65,7 @@ export declare function calculateSpreadBN(baseSpread: number, lastOracleReserveP
|
|
|
69
65
|
};
|
|
70
66
|
export declare function calculateSpread(amm: AMM, oraclePriceData: OraclePriceData, now?: BN, reservePrice?: BN): [number, number];
|
|
71
67
|
export declare function getQuoteAssetReservePredictionMarketBounds(amm: AMM, direction: PositionDirection): [BN, BN];
|
|
72
|
-
export declare function calculateSpreadReserves(amm: AMM,
|
|
68
|
+
export declare function calculateSpreadReserves(amm: AMM, mmOraclePriceData: MMOraclePriceData, now?: BN, isPrediction?: boolean): {
|
|
73
69
|
baseAssetReserve: any;
|
|
74
70
|
quoteAssetReserve: any;
|
|
75
71
|
}[];
|
|
@@ -97,6 +93,6 @@ export declare function getSwapDirection(inputAssetType: AssetType, positionDire
|
|
|
97
93
|
* @returns cost : Precision PRICE_PRECISION
|
|
98
94
|
*/
|
|
99
95
|
export declare function calculateTerminalPrice(market: PerpMarketAccount): BN;
|
|
100
|
-
export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, limit_price: BN, direction: PositionDirection,
|
|
96
|
+
export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, limit_price: BN, direction: PositionDirection, mmOraclePriceData?: MMOraclePriceData, now?: BN, isPrediction?: boolean): [BN, PositionDirection];
|
|
101
97
|
export declare function calculateQuoteAssetAmountSwapped(quoteAssetReserves: BN, pegMultiplier: BN, swapDirection: SwapDirection): BN;
|
|
102
98
|
export declare function calculateMaxBaseAssetAmountFillable(amm: AMM, orderDirection: PositionDirection): BN;
|
package/lib/browser/math/amm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateMaxBaseAssetAmountFillable = exports.calculateQuoteAssetAmountSwapped = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateSpreadReserves = exports.getQuoteAssetReservePredictionMarketBounds = exports.calculateSpread = exports.calculateSpreadBN = exports.calculateVolSpreadBN = exports.calculateMaxSpread = exports.calculateEffectiveLeverage = exports.calculateReferencePriceOffset = exports.calculateInventoryScale = exports.calculateInventoryLiquidityRatio = exports.calculateMarketOpenBidAsk = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = exports.calculateBidAskPrice = exports.
|
|
3
|
+
exports.calculateMaxBaseAssetAmountFillable = exports.calculateQuoteAssetAmountSwapped = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateSpreadReserves = exports.getQuoteAssetReservePredictionMarketBounds = exports.calculateSpread = exports.calculateSpreadBN = exports.calculateVolSpreadBN = exports.calculateMaxSpread = exports.calculateEffectiveLeverage = exports.calculateReferencePriceOffset = exports.calculateInventoryScale = exports.calculateInventoryLiquidityRatio = exports.calculateMarketOpenBidAsk = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = exports.calculateBidAskPrice = exports.calculateUpdatedAMMSpreadReserves = exports.calculateUpdatedAMM = exports.calculateNewAmm = exports.calculateOptimalPegAndBudget = exports.calculatePegFromTargetPrice = void 0;
|
|
4
4
|
const anchor_1 = require("@coral-xyz/anchor");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const types_1 = require("../types");
|
|
@@ -17,9 +17,9 @@ function calculatePegFromTargetPrice(targetPrice, baseAssetReserve, quoteAssetRe
|
|
|
17
17
|
.div(numericConstants_1.PRICE_DIV_PEG), numericConstants_1.ONE);
|
|
18
18
|
}
|
|
19
19
|
exports.calculatePegFromTargetPrice = calculatePegFromTargetPrice;
|
|
20
|
-
function calculateOptimalPegAndBudget(amm,
|
|
20
|
+
function calculateOptimalPegAndBudget(amm, mmOraclePriceData) {
|
|
21
21
|
const reservePriceBefore = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
22
|
-
const targetPrice =
|
|
22
|
+
const targetPrice = mmOraclePriceData.price;
|
|
23
23
|
const newPeg = calculatePegFromTargetPrice(targetPrice, amm.baseAssetReserve, amm.quoteAssetReserve);
|
|
24
24
|
const prePegCost = (0, repeg_1.calculateRepegCost)(amm, newPeg);
|
|
25
25
|
const totalFeeLB = amm.totalExchangeFee.div(new anchor_1.BN(2));
|
|
@@ -54,10 +54,10 @@ function calculateOptimalPegAndBudget(amm, oraclePriceData) {
|
|
|
54
54
|
return [targetPrice, newPeg, budget, checkLowerBound];
|
|
55
55
|
}
|
|
56
56
|
exports.calculateOptimalPegAndBudget = calculateOptimalPegAndBudget;
|
|
57
|
-
function calculateNewAmm(amm,
|
|
57
|
+
function calculateNewAmm(amm, mmOraclePriceData) {
|
|
58
58
|
let pKNumer = new anchor_1.BN(1);
|
|
59
59
|
let pKDenom = new anchor_1.BN(1);
|
|
60
|
-
const [targetPrice, _newPeg, budget, _checkLowerBound] = calculateOptimalPegAndBudget(amm,
|
|
60
|
+
const [targetPrice, _newPeg, budget, _checkLowerBound] = calculateOptimalPegAndBudget(amm, mmOraclePriceData);
|
|
61
61
|
let prePegCost = (0, repeg_1.calculateRepegCost)(amm, _newPeg);
|
|
62
62
|
let newPeg = _newPeg;
|
|
63
63
|
if (prePegCost.gte(budget) && prePegCost.gt(numericConstants_1.ZERO)) {
|
|
@@ -81,12 +81,12 @@ function calculateNewAmm(amm, oraclePriceData) {
|
|
|
81
81
|
return [prePegCost, pKNumer, pKDenom, newPeg];
|
|
82
82
|
}
|
|
83
83
|
exports.calculateNewAmm = calculateNewAmm;
|
|
84
|
-
function calculateUpdatedAMM(amm,
|
|
85
|
-
if (amm.curveUpdateIntensity == 0 ||
|
|
84
|
+
function calculateUpdatedAMM(amm, mmOraclePriceData) {
|
|
85
|
+
if (amm.curveUpdateIntensity == 0 || mmOraclePriceData === undefined) {
|
|
86
86
|
return amm;
|
|
87
87
|
}
|
|
88
88
|
const newAmm = Object.assign({}, amm);
|
|
89
|
-
const [prepegCost, pKNumer, pKDenom, newPeg] = calculateNewAmm(amm,
|
|
89
|
+
const [prepegCost, pKNumer, pKDenom, newPeg] = calculateNewAmm(amm, mmOraclePriceData);
|
|
90
90
|
newAmm.baseAssetReserve = newAmm.baseAssetReserve.mul(pKNumer).div(pKDenom);
|
|
91
91
|
newAmm.sqrtK = newAmm.sqrtK.mul(pKNumer).div(pKDenom);
|
|
92
92
|
const invariant = newAmm.sqrtK.mul(newAmm.sqrtK);
|
|
@@ -104,13 +104,9 @@ function calculateUpdatedAMM(amm, oraclePriceData) {
|
|
|
104
104
|
return newAmm;
|
|
105
105
|
}
|
|
106
106
|
exports.calculateUpdatedAMM = calculateUpdatedAMM;
|
|
107
|
-
function calculateUpdatedAMMSpreadReserves(amm, direction,
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
console.log('Use driftClient method getMMOracleDataForPerpMarket for accurate updated AMM in calculateUpdatedAMMSpreadReserves');
|
|
111
|
-
}
|
|
112
|
-
const newAmm = calculateUpdatedAMM(amm, oraclePriceData);
|
|
113
|
-
const [shortReserves, longReserves] = calculateSpreadReserves(newAmm, oraclePriceData, undefined, isPrediction);
|
|
107
|
+
function calculateUpdatedAMMSpreadReserves(amm, direction, mmOraclePriceData, isPrediction = false) {
|
|
108
|
+
const newAmm = calculateUpdatedAMM(amm, mmOraclePriceData);
|
|
109
|
+
const [shortReserves, longReserves] = calculateSpreadReserves(newAmm, mmOraclePriceData, undefined, isPrediction);
|
|
114
110
|
const dirReserves = (0, types_1.isVariant)(direction, 'long')
|
|
115
111
|
? longReserves
|
|
116
112
|
: shortReserves;
|
|
@@ -123,36 +119,15 @@ function calculateUpdatedAMMSpreadReserves(amm, direction, oraclePriceData, isPr
|
|
|
123
119
|
return result;
|
|
124
120
|
}
|
|
125
121
|
exports.calculateUpdatedAMMSpreadReserves = calculateUpdatedAMMSpreadReserves;
|
|
126
|
-
function
|
|
127
|
-
if (!(oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.fetchedWithMMOracle) &&
|
|
128
|
-
(oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.isMMOracleActive)) {
|
|
129
|
-
console.log('Use driftClient method getMMOracleDataForPerpMarket for accurate MM pricing in calculateAMMBidAskPrice');
|
|
130
|
-
}
|
|
131
|
-
let newAmm;
|
|
132
|
-
if (withUpdate) {
|
|
133
|
-
newAmm = calculateUpdatedAMM(amm, oraclePriceData);
|
|
134
|
-
}
|
|
135
|
-
else {
|
|
136
|
-
newAmm = amm;
|
|
137
|
-
}
|
|
138
|
-
const [bidReserves, askReserves] = calculateSpreadReserves(newAmm, oraclePriceData, undefined, isPrediction);
|
|
139
|
-
const askPrice = calculatePrice(askReserves.baseAssetReserve, askReserves.quoteAssetReserve, newAmm.pegMultiplier);
|
|
140
|
-
const bidPrice = calculatePrice(bidReserves.baseAssetReserve, bidReserves.quoteAssetReserve, newAmm.pegMultiplier);
|
|
141
|
-
return [bidPrice, askPrice];
|
|
142
|
-
}
|
|
143
|
-
exports.calculateAMMBidAskPrice = calculateAMMBidAskPrice;
|
|
144
|
-
/**
|
|
145
|
-
* @deprecated Use calculateAMMBidAskPrice instead
|
|
146
|
-
*/
|
|
147
|
-
function calculateBidAskPrice(amm, oraclePriceData, withUpdate = true, isPrediction = false) {
|
|
122
|
+
function calculateBidAskPrice(amm, mmOraclePriceData, withUpdate = true, isPrediction = false) {
|
|
148
123
|
let newAmm;
|
|
149
124
|
if (withUpdate) {
|
|
150
|
-
newAmm = calculateUpdatedAMM(amm,
|
|
125
|
+
newAmm = calculateUpdatedAMM(amm, mmOraclePriceData);
|
|
151
126
|
}
|
|
152
127
|
else {
|
|
153
128
|
newAmm = amm;
|
|
154
129
|
}
|
|
155
|
-
const [bidReserves, askReserves] = calculateSpreadReserves(newAmm,
|
|
130
|
+
const [bidReserves, askReserves] = calculateSpreadReserves(newAmm, mmOraclePriceData, undefined, isPrediction);
|
|
156
131
|
const askPrice = calculatePrice(askReserves.baseAssetReserve, askReserves.quoteAssetReserve, newAmm.pegMultiplier);
|
|
157
132
|
const bidPrice = calculatePrice(bidReserves.baseAssetReserve, bidReserves.quoteAssetReserve, newAmm.pegMultiplier);
|
|
158
133
|
return [bidPrice, askPrice];
|
|
@@ -513,7 +488,7 @@ function getQuoteAssetReservePredictionMarketBounds(amm, direction) {
|
|
|
513
488
|
return [quoteAssetReserveLowerBound, quoteAssetReserveUpperBound];
|
|
514
489
|
}
|
|
515
490
|
exports.getQuoteAssetReservePredictionMarketBounds = getQuoteAssetReservePredictionMarketBounds;
|
|
516
|
-
function calculateSpreadReserves(amm,
|
|
491
|
+
function calculateSpreadReserves(amm, mmOraclePriceData, now, isPrediction = false) {
|
|
517
492
|
function calculateSpreadReserve(spread, direction, amm) {
|
|
518
493
|
if (spread === 0) {
|
|
519
494
|
return {
|
|
@@ -555,12 +530,12 @@ function calculateSpreadReserves(amm, oraclePriceData, now, isPrediction = false
|
|
|
555
530
|
const liquidityFractionSigned = liquidityFraction.mul((0, utils_1.sigNum)(amm.baseAssetAmountWithAmm.add(amm.baseAssetAmountWithUnsettledLp)));
|
|
556
531
|
referencePriceOffset = calculateReferencePriceOffset(reservePrice, amm.last24HAvgFundingRate, liquidityFractionSigned, amm.historicalOracleData.lastOraclePriceTwap5Min, amm.lastMarkPriceTwap5Min, amm.historicalOracleData.lastOraclePriceTwap, amm.lastMarkPriceTwap, maxOffset).toNumber();
|
|
557
532
|
}
|
|
558
|
-
let [longSpread, shortSpread] = calculateSpread(amm,
|
|
533
|
+
let [longSpread, shortSpread] = calculateSpread(amm, mmOraclePriceData, now, reservePrice);
|
|
559
534
|
const doReferencePricOffsetSmooth = Math.sign(referencePriceOffset) !== Math.sign(amm.referencePriceOffset) &&
|
|
560
535
|
amm.curveUpdateIntensity > 100;
|
|
561
536
|
if (doReferencePricOffsetSmooth) {
|
|
562
|
-
if (
|
|
563
|
-
const slotsPassed =
|
|
537
|
+
if (mmOraclePriceData.slot !== amm.lastUpdateSlot) {
|
|
538
|
+
const slotsPassed = mmOraclePriceData.slot.toNumber() - amm.lastUpdateSlot.toNumber();
|
|
564
539
|
const fullOffsetDelta = referencePriceOffset - amm.referencePriceOffset;
|
|
565
540
|
const raw = Math.trunc(Math.min(Math.abs(fullOffsetDelta), slotsPassed * 1000) / 10);
|
|
566
541
|
const maxAllowed = Math.abs(amm.referencePriceOffset) || Math.abs(referencePriceOffset);
|
|
@@ -638,7 +613,7 @@ function calculateTerminalPrice(market) {
|
|
|
638
613
|
return terminalPrice;
|
|
639
614
|
}
|
|
640
615
|
exports.calculateTerminalPrice = calculateTerminalPrice;
|
|
641
|
-
function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction,
|
|
616
|
+
function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, mmOraclePriceData, now, isPrediction = false) {
|
|
642
617
|
const invariant = amm.sqrtK.mul(amm.sqrtK);
|
|
643
618
|
const newBaseAssetReserveSquared = invariant
|
|
644
619
|
.mul(numericConstants_1.PRICE_PRECISION)
|
|
@@ -646,7 +621,7 @@ function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, oracleP
|
|
|
646
621
|
.div(limit_price)
|
|
647
622
|
.div(numericConstants_1.PEG_PRECISION);
|
|
648
623
|
const newBaseAssetReserve = (0, utils_1.squareRootBN)(newBaseAssetReserveSquared);
|
|
649
|
-
const [shortSpreadReserves, longSpreadReserves] = calculateSpreadReserves(amm,
|
|
624
|
+
const [shortSpreadReserves, longSpreadReserves] = calculateSpreadReserves(amm, mmOraclePriceData, now, isPrediction);
|
|
650
625
|
const baseAssetReserveBefore = (0, types_1.isVariant)(direction, 'long')
|
|
651
626
|
? longSpreadReserves.baseAssetReserve
|
|
652
627
|
: shortSpreadReserves.baseAssetReserve;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '@coral-xyz/anchor';
|
|
3
3
|
import { PerpMarketAccount } from '../types';
|
|
4
|
-
import { OraclePriceData } from '../oracles/types';
|
|
4
|
+
import { MMOraclePriceData, OraclePriceData } from '../oracles/types';
|
|
5
5
|
/**
|
|
6
6
|
*
|
|
7
7
|
* @param market
|
|
@@ -9,12 +9,12 @@ import { OraclePriceData } from '../oracles/types';
|
|
|
9
9
|
* @param periodAdjustment
|
|
10
10
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
11
11
|
*/
|
|
12
|
-
export declare function calculateAllEstimatedFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN, BN, BN, BN];
|
|
12
|
+
export declare function calculateAllEstimatedFundingRate(market: PerpMarketAccount, mmOraclePriceData?: MMOraclePriceData, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN, BN, BN, BN];
|
|
13
13
|
/**
|
|
14
14
|
* Calculate funding rates in human-readable form. Values will have some lost precision and shouldn't be used in strict accounting.
|
|
15
15
|
* @param period : 'hour' | 'year' :: Use 'hour' for the hourly payment as a percentage, 'year' for the payment as an estimated APR.
|
|
16
16
|
*/
|
|
17
|
-
export declare function calculateFormattedLiveFundingRate(market: PerpMarketAccount, oraclePriceData: OraclePriceData, period: 'hour' | 'year'): {
|
|
17
|
+
export declare function calculateFormattedLiveFundingRate(market: PerpMarketAccount, mmOraclePriceData: MMOraclePriceData, oraclePriceData: OraclePriceData, period: 'hour' | 'year'): {
|
|
18
18
|
longRate: number;
|
|
19
19
|
shortRate: number;
|
|
20
20
|
fundingRateUnit: string;
|
|
@@ -27,7 +27,7 @@ export declare function calculateFormattedLiveFundingRate(market: PerpMarketAcco
|
|
|
27
27
|
* @param periodAdjustment
|
|
28
28
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
29
29
|
*/
|
|
30
|
-
export declare function calculateLongShortFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN];
|
|
30
|
+
export declare function calculateLongShortFundingRate(market: PerpMarketAccount, mmOraclePriceData?: MMOraclePriceData, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN];
|
|
31
31
|
/**
|
|
32
32
|
*
|
|
33
33
|
* @param market
|
|
@@ -35,7 +35,7 @@ export declare function calculateLongShortFundingRate(market: PerpMarketAccount,
|
|
|
35
35
|
* @param periodAdjustment
|
|
36
36
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
37
37
|
*/
|
|
38
|
-
export declare function calculateLongShortFundingRateAndLiveTwaps(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN, BN, BN];
|
|
38
|
+
export declare function calculateLongShortFundingRateAndLiveTwaps(market: PerpMarketAccount, mmOraclePriceData?: MMOraclePriceData, oraclePriceData?: OraclePriceData, markPrice?: BN, now?: BN): [BN, BN, BN, BN];
|
|
39
39
|
/**
|
|
40
40
|
*
|
|
41
41
|
* @param market
|