@drift-labs/sdk 0.1.33 → 0.1.34-master.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/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +1 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +17 -4
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +1 -0
- package/lib/accounts/pollingUserAccountSubscriber.js +17 -3
- package/lib/admin.d.ts +1 -0
- package/lib/admin.js +11 -0
- package/lib/constants/numericConstants.d.ts +2 -0
- package/lib/constants/numericConstants.js +3 -1
- package/lib/idl/clearing_house.json +48 -6
- package/lib/math/amm.d.ts +7 -2
- package/lib/math/amm.js +45 -6
- package/lib/math/funding.js +3 -1
- package/lib/math/market.d.ts +15 -0
- package/lib/math/market.js +29 -2
- package/lib/math/trade.d.ts +3 -1
- package/lib/math/trade.js +18 -6
- package/lib/orders.js +26 -7
- package/lib/types.d.ts +3 -1
- package/package.json +1 -1
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +19 -4
- package/src/accounts/pollingUserAccountSubscriber.ts +20 -3
- package/src/admin.ts +17 -0
- package/src/constants/numericConstants.ts +2 -0
- package/src/idl/clearing_house.json +48 -6
- package/src/math/amm.ts +71 -6
- package/src/math/funding.ts +11 -8
- package/src/math/market.ts +47 -1
- package/src/math/trade.ts +24 -13
- package/src/orders.ts +39 -9
- package/src/types.ts +3 -1
- package/src/assert/assert.js +0 -10
- package/src/assert/assert.js.map +0 -1
- package/src/math/conversion.js +0 -16
- package/src/math/conversion.js.map +0 -1
- package/src/math/funding.js +0 -223
- package/src/math/funding.js.map +0 -1
- package/src/math/insuranceFund.js +0 -23
- package/src/math/insuranceFund.js.map +0 -1
- package/src/math/position.js +0 -121
- package/src/math/position.js.map +0 -1
- package/src/math/utils.js +0 -27
- package/src/math/utils.js.map +0 -1
- package/src/oracles/switchboardClient.js +0 -60
- package/src/oracles/switchboardClient.js.map +0 -1
- package/src/token/index.js +0 -39
- package/src/token/index.js.map +0 -1
- package/src/tx/defaultTxSender.js +0 -13
- package/src/tx/defaultTxSender.js.map +0 -1
- package/src/tx/types.js +0 -3
- package/src/tx/types.js.map +0 -1
- package/src/tx/utils.js +0 -9
- package/src/tx/utils.js.map +0 -1
- package/src/util/computeUnits.js +0 -17
- package/src/util/computeUnits.js.map +0 -1
- package/src/util/tps.js +0 -17
- package/src/util/tps.js.map +0 -1
|
@@ -53,10 +53,15 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
53
53
|
|
|
54
54
|
await this.addToAccountLoader();
|
|
55
55
|
await this.fetchIfUnloaded();
|
|
56
|
-
this.eventEmitter.emit('update');
|
|
57
56
|
|
|
58
|
-
|
|
59
|
-
|
|
57
|
+
const subscriptionSucceeded = this.didSubscriptionSucceed();
|
|
58
|
+
|
|
59
|
+
if (subscriptionSucceeded) {
|
|
60
|
+
this.eventEmitter.emit('update');
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
this.isSubscribed = subscriptionSucceeded;
|
|
64
|
+
return subscriptionSucceeded;
|
|
60
65
|
}
|
|
61
66
|
|
|
62
67
|
async addToAccountLoader(userPublicKeys?: UserPublicKeys): Promise<void> {
|
|
@@ -168,6 +173,18 @@ export class PollingUserAccountSubscriber implements UserAccountSubscriber {
|
|
|
168
173
|
}
|
|
169
174
|
}
|
|
170
175
|
|
|
176
|
+
didSubscriptionSucceed(): boolean {
|
|
177
|
+
let success = true;
|
|
178
|
+
for (const [_, accountToPoll] of this.accountsToPoll) {
|
|
179
|
+
// userOrders may not exist
|
|
180
|
+
if (accountToPoll.key !== 'userOrders' && !this[accountToPoll.key]) {
|
|
181
|
+
success = false;
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
return success;
|
|
186
|
+
}
|
|
187
|
+
|
|
171
188
|
async unsubscribe(): Promise<void> {
|
|
172
189
|
if (!this.isSubscribed) {
|
|
173
190
|
return;
|
package/src/admin.ts
CHANGED
|
@@ -488,6 +488,23 @@ export class Admin extends ClearingHouse {
|
|
|
488
488
|
);
|
|
489
489
|
}
|
|
490
490
|
|
|
491
|
+
public async updateMarketBaseSpread(
|
|
492
|
+
marketIndex: BN,
|
|
493
|
+
baseSpread: number
|
|
494
|
+
): Promise<TransactionSignature> {
|
|
495
|
+
return await this.program.rpc.updateMarketBaseSpread(
|
|
496
|
+
marketIndex,
|
|
497
|
+
baseSpread,
|
|
498
|
+
{
|
|
499
|
+
accounts: {
|
|
500
|
+
admin: this.wallet.publicKey,
|
|
501
|
+
state: await this.getStatePublicKey(),
|
|
502
|
+
markets: this.getStateAccount().markets,
|
|
503
|
+
},
|
|
504
|
+
}
|
|
505
|
+
);
|
|
506
|
+
}
|
|
507
|
+
|
|
491
508
|
public async updatePartialLiquidationClosePercentage(
|
|
492
509
|
numerator: BN,
|
|
493
510
|
denominator: BN
|
|
@@ -6,6 +6,7 @@ export const TWO = new BN(2);
|
|
|
6
6
|
export const TEN = new BN(10);
|
|
7
7
|
export const TEN_THOUSAND = new BN(10000);
|
|
8
8
|
export const BN_MAX = new BN(Number.MAX_SAFE_INTEGER);
|
|
9
|
+
export const TEN_MILLION = TEN_THOUSAND.mul(TEN_THOUSAND);
|
|
9
10
|
|
|
10
11
|
export const MAX_LEVERAGE = new BN(5);
|
|
11
12
|
|
|
@@ -23,3 +24,4 @@ export const PRICE_TO_QUOTE_PRECISION =
|
|
|
23
24
|
export const AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO =
|
|
24
25
|
AMM_RESERVE_PRECISION.mul(PEG_PRECISION).div(QUOTE_PRECISION); // 10^10
|
|
25
26
|
export const MARGIN_PRECISION = TEN_THOUSAND;
|
|
27
|
+
export const BID_ASK_SPREAD_PRECISION = new BN(1000000);
|
|
@@ -1905,6 +1905,36 @@
|
|
|
1905
1905
|
}
|
|
1906
1906
|
]
|
|
1907
1907
|
},
|
|
1908
|
+
{
|
|
1909
|
+
"name": "updateMarketBaseSpread",
|
|
1910
|
+
"accounts": [
|
|
1911
|
+
{
|
|
1912
|
+
"name": "admin",
|
|
1913
|
+
"isMut": false,
|
|
1914
|
+
"isSigner": true
|
|
1915
|
+
},
|
|
1916
|
+
{
|
|
1917
|
+
"name": "state",
|
|
1918
|
+
"isMut": false,
|
|
1919
|
+
"isSigner": false
|
|
1920
|
+
},
|
|
1921
|
+
{
|
|
1922
|
+
"name": "markets",
|
|
1923
|
+
"isMut": true,
|
|
1924
|
+
"isSigner": false
|
|
1925
|
+
}
|
|
1926
|
+
],
|
|
1927
|
+
"args": [
|
|
1928
|
+
{
|
|
1929
|
+
"name": "marketIndex",
|
|
1930
|
+
"type": "u64"
|
|
1931
|
+
},
|
|
1932
|
+
{
|
|
1933
|
+
"name": "baseSpread",
|
|
1934
|
+
"type": "u16"
|
|
1935
|
+
}
|
|
1936
|
+
]
|
|
1937
|
+
},
|
|
1908
1938
|
{
|
|
1909
1939
|
"name": "updateMarketMinimumBaseAssetTradeSize",
|
|
1910
1940
|
"accounts": [
|
|
@@ -2508,7 +2538,11 @@
|
|
|
2508
2538
|
},
|
|
2509
2539
|
{
|
|
2510
2540
|
"name": "totalFeePaid",
|
|
2511
|
-
"type": "
|
|
2541
|
+
"type": "u64"
|
|
2542
|
+
},
|
|
2543
|
+
{
|
|
2544
|
+
"name": "totalFeeRebate",
|
|
2545
|
+
"type": "u64"
|
|
2512
2546
|
},
|
|
2513
2547
|
{
|
|
2514
2548
|
"name": "totalTokenDiscount",
|
|
@@ -3265,9 +3299,17 @@
|
|
|
3265
3299
|
"name": "minimumBaseAssetTradeSize",
|
|
3266
3300
|
"type": "u128"
|
|
3267
3301
|
},
|
|
3302
|
+
{
|
|
3303
|
+
"name": "baseSpread",
|
|
3304
|
+
"type": "u16"
|
|
3305
|
+
},
|
|
3306
|
+
{
|
|
3307
|
+
"name": "padding0",
|
|
3308
|
+
"type": "u16"
|
|
3309
|
+
},
|
|
3268
3310
|
{
|
|
3269
3311
|
"name": "padding1",
|
|
3270
|
-
"type": "
|
|
3312
|
+
"type": "u32"
|
|
3271
3313
|
},
|
|
3272
3314
|
{
|
|
3273
3315
|
"name": "padding2",
|
|
@@ -3331,7 +3373,7 @@
|
|
|
3331
3373
|
},
|
|
3332
3374
|
{
|
|
3333
3375
|
"name": "fee",
|
|
3334
|
-
"type": "
|
|
3376
|
+
"type": "i128"
|
|
3335
3377
|
},
|
|
3336
3378
|
{
|
|
3337
3379
|
"name": "fillerReward",
|
|
@@ -3582,10 +3624,10 @@
|
|
|
3582
3624
|
},
|
|
3583
3625
|
{
|
|
3584
3626
|
"name": "fee",
|
|
3585
|
-
"type": "
|
|
3627
|
+
"type": "i128"
|
|
3586
3628
|
},
|
|
3587
3629
|
{
|
|
3588
|
-
"name": "
|
|
3630
|
+
"name": "quoteAssetAmountSurplus",
|
|
3589
3631
|
"type": "u128"
|
|
3590
3632
|
},
|
|
3591
3633
|
{
|
|
@@ -3734,7 +3776,7 @@
|
|
|
3734
3776
|
},
|
|
3735
3777
|
{
|
|
3736
3778
|
"name": "fee",
|
|
3737
|
-
"type": "
|
|
3779
|
+
"type": "i128"
|
|
3738
3780
|
},
|
|
3739
3781
|
{
|
|
3740
3782
|
"name": "direction",
|
package/src/math/amm.ts
CHANGED
|
@@ -7,6 +7,8 @@ import {
|
|
|
7
7
|
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
8
8
|
QUOTE_PRECISION,
|
|
9
9
|
AMM_RESERVE_PRECISION,
|
|
10
|
+
BID_ASK_SPREAD_PRECISION,
|
|
11
|
+
ONE,
|
|
10
12
|
} from '../constants/numericConstants';
|
|
11
13
|
import { calculateBaseAssetValue } from './position';
|
|
12
14
|
import {
|
|
@@ -60,7 +62,10 @@ export type AssetType = 'quote' | 'base';
|
|
|
60
62
|
* @returns quoteAssetReserve and baseAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
|
|
61
63
|
*/
|
|
62
64
|
export function calculateAmmReservesAfterSwap(
|
|
63
|
-
amm:
|
|
65
|
+
amm: Pick<
|
|
66
|
+
AMM,
|
|
67
|
+
'pegMultiplier' | 'quoteAssetReserve' | 'sqrtK' | 'baseAssetReserve'
|
|
68
|
+
>,
|
|
64
69
|
inputAssetType: AssetType,
|
|
65
70
|
swapAmount: BN,
|
|
66
71
|
swapDirection: SwapDirection
|
|
@@ -93,6 +98,38 @@ export function calculateAmmReservesAfterSwap(
|
|
|
93
98
|
return [newQuoteAssetReserve, newBaseAssetReserve];
|
|
94
99
|
}
|
|
95
100
|
|
|
101
|
+
export function calculateSpreadReserves(
|
|
102
|
+
amm: AMM,
|
|
103
|
+
direction: PositionDirection
|
|
104
|
+
): {
|
|
105
|
+
baseAssetReserve: BN;
|
|
106
|
+
quoteAssetReserve: BN;
|
|
107
|
+
} {
|
|
108
|
+
if (amm.baseSpread === 0) {
|
|
109
|
+
return {
|
|
110
|
+
baseAssetReserve: amm.baseAssetReserve,
|
|
111
|
+
quoteAssetReserve: amm.quoteAssetReserve,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const quoteAsserReserveDelta = amm.quoteAssetReserve.div(
|
|
116
|
+
BID_ASK_SPREAD_PRECISION.div(new BN(amm.baseSpread / 4))
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
let quoteAssetReserve;
|
|
120
|
+
if (isVariant(direction, 'long')) {
|
|
121
|
+
quoteAssetReserve = amm.quoteAssetReserve.add(quoteAsserReserveDelta);
|
|
122
|
+
} else {
|
|
123
|
+
quoteAssetReserve = amm.quoteAssetReserve.sub(quoteAsserReserveDelta);
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const baseAssetReserve = amm.sqrtK.mul(amm.sqrtK).div(quoteAssetReserve);
|
|
127
|
+
return {
|
|
128
|
+
baseAssetReserve,
|
|
129
|
+
quoteAssetReserve,
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
96
133
|
/**
|
|
97
134
|
* Helper function calculating constant product curve output. Agnostic to whether input asset is quote or base
|
|
98
135
|
*
|
|
@@ -288,7 +325,9 @@ export function calculateTerminalPrice(market: Market) {
|
|
|
288
325
|
|
|
289
326
|
export function calculateMaxBaseAssetAmountToTrade(
|
|
290
327
|
amm: AMM,
|
|
291
|
-
limit_price: BN
|
|
328
|
+
limit_price: BN,
|
|
329
|
+
direction: PositionDirection,
|
|
330
|
+
useSpread: boolean
|
|
292
331
|
): [BN, PositionDirection] {
|
|
293
332
|
const invariant = amm.sqrtK.mul(amm.sqrtK);
|
|
294
333
|
|
|
@@ -300,14 +339,24 @@ export function calculateMaxBaseAssetAmountToTrade(
|
|
|
300
339
|
|
|
301
340
|
const newBaseAssetReserve = squareRootBN(newBaseAssetReserveSquared);
|
|
302
341
|
|
|
303
|
-
|
|
342
|
+
let baseAssetReserveBefore;
|
|
343
|
+
if (useSpread) {
|
|
344
|
+
baseAssetReserveBefore = calculateSpreadReserves(
|
|
345
|
+
amm,
|
|
346
|
+
direction
|
|
347
|
+
).baseAssetReserve;
|
|
348
|
+
} else {
|
|
349
|
+
baseAssetReserveBefore = amm.baseAssetReserve;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (newBaseAssetReserve.gt(baseAssetReserveBefore)) {
|
|
304
353
|
return [
|
|
305
|
-
newBaseAssetReserve.sub(
|
|
354
|
+
newBaseAssetReserve.sub(baseAssetReserveBefore),
|
|
306
355
|
PositionDirection.SHORT,
|
|
307
356
|
];
|
|
308
|
-
} else if (newBaseAssetReserve.lt(
|
|
357
|
+
} else if (newBaseAssetReserve.lt(baseAssetReserveBefore)) {
|
|
309
358
|
return [
|
|
310
|
-
|
|
359
|
+
baseAssetReserveBefore.sub(newBaseAssetReserve),
|
|
311
360
|
PositionDirection.LONG,
|
|
312
361
|
];
|
|
313
362
|
} else {
|
|
@@ -398,3 +447,19 @@ export function calculateBudgetedPeg(market: Market, cost: BN): BN {
|
|
|
398
447
|
|
|
399
448
|
return newPeg;
|
|
400
449
|
}
|
|
450
|
+
|
|
451
|
+
export function calculateQuoteAssetAmountSwapped(
|
|
452
|
+
quoteAssetReserves: BN,
|
|
453
|
+
pegMultiplier: BN,
|
|
454
|
+
swapDirection: SwapDirection
|
|
455
|
+
): BN {
|
|
456
|
+
let quoteAssetAmount = quoteAssetReserves
|
|
457
|
+
.mul(pegMultiplier)
|
|
458
|
+
.div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
459
|
+
|
|
460
|
+
if (isVariant(swapDirection, 'remove')) {
|
|
461
|
+
quoteAssetAmount = quoteAssetAmount.add(ONE);
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
return quoteAssetAmount;
|
|
465
|
+
}
|
package/src/math/funding.ts
CHANGED
|
@@ -43,13 +43,9 @@ export async function calculateAllEstimatedFundingRate(
|
|
|
43
43
|
const lastMarkPriceTwapTs = market.amm.lastMarkPriceTwapTs;
|
|
44
44
|
|
|
45
45
|
const timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
|
|
46
|
-
const markTwapTimeSinceLastUpdate =
|
|
47
|
-
BN.max(
|
|
46
|
+
const markTwapTimeSinceLastUpdate = BN.max(
|
|
48
47
|
secondsInHour,
|
|
49
|
-
BN.max(
|
|
50
|
-
ZERO,
|
|
51
|
-
secondsInHour.sub(timeSinceLastMarkChange)
|
|
52
|
-
)
|
|
48
|
+
BN.max(ZERO, secondsInHour.sub(timeSinceLastMarkChange))
|
|
53
49
|
);
|
|
54
50
|
const baseAssetPriceWithMantissa = calculateMarkPrice(market);
|
|
55
51
|
|
|
@@ -63,7 +59,10 @@ export async function calculateAllEstimatedFundingRate(
|
|
|
63
59
|
const lastOracleTwapWithMantissa = market.amm.lastOraclePriceTwap;
|
|
64
60
|
const lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
|
|
65
61
|
|
|
66
|
-
const oracleInvalidDuration = BN.max(
|
|
62
|
+
const oracleInvalidDuration = BN.max(
|
|
63
|
+
ZERO,
|
|
64
|
+
lastMarkPriceTwapTs.sub(lastOraclePriceTwapTs)
|
|
65
|
+
);
|
|
67
66
|
|
|
68
67
|
const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
|
|
69
68
|
const oracleTwapTimeSinceLastUpdate = BN.max(
|
|
@@ -87,7 +86,11 @@ export async function calculateAllEstimatedFundingRate(
|
|
|
87
86
|
.mul(lastOracleTwapWithMantissa)
|
|
88
87
|
.add(timeSinceLastMarkChange.mul(oraclePrice))
|
|
89
88
|
.add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
|
|
90
|
-
.div(
|
|
89
|
+
.div(
|
|
90
|
+
timeSinceLastMarkChange
|
|
91
|
+
.add(oracleTwapTimeSinceLastUpdate)
|
|
92
|
+
.add(oracleInvalidDuration)
|
|
93
|
+
);
|
|
91
94
|
}
|
|
92
95
|
|
|
93
96
|
const twapSpread = lastMarkTwapWithMantissa.sub(lastOracleTwapWithMantissa);
|
package/src/math/market.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { Market, PositionDirection } from '../types';
|
|
|
3
3
|
import {
|
|
4
4
|
calculateAmmReservesAfterSwap,
|
|
5
5
|
calculatePrice,
|
|
6
|
+
calculateSpreadReserves,
|
|
6
7
|
getSwapDirection,
|
|
7
8
|
} from './amm';
|
|
8
9
|
import { OraclePriceData } from '../oracles/types';
|
|
@@ -21,6 +22,44 @@ export function calculateMarkPrice(market: Market): BN {
|
|
|
21
22
|
);
|
|
22
23
|
}
|
|
23
24
|
|
|
25
|
+
/**
|
|
26
|
+
* Calculates market bid price
|
|
27
|
+
*
|
|
28
|
+
* @param market
|
|
29
|
+
* @return bidPrice : Precision MARK_PRICE_PRECISION
|
|
30
|
+
*/
|
|
31
|
+
export function calculateBidPrice(market: Market): BN {
|
|
32
|
+
const { baseAssetReserve, quoteAssetReserve } = calculateSpreadReserves(
|
|
33
|
+
market.amm,
|
|
34
|
+
PositionDirection.SHORT
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
return calculatePrice(
|
|
38
|
+
baseAssetReserve,
|
|
39
|
+
quoteAssetReserve,
|
|
40
|
+
market.amm.pegMultiplier
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Calculates market ask price
|
|
46
|
+
*
|
|
47
|
+
* @param market
|
|
48
|
+
* @return bidPrice : Precision MARK_PRICE_PRECISION
|
|
49
|
+
*/
|
|
50
|
+
export function calculateAskPrice(market: Market): BN {
|
|
51
|
+
const { baseAssetReserve, quoteAssetReserve } = calculateSpreadReserves(
|
|
52
|
+
market.amm,
|
|
53
|
+
PositionDirection.LONG
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
return calculatePrice(
|
|
57
|
+
baseAssetReserve,
|
|
58
|
+
quoteAssetReserve,
|
|
59
|
+
market.amm.pegMultiplier
|
|
60
|
+
);
|
|
61
|
+
}
|
|
62
|
+
|
|
24
63
|
export function calculateNewMarketAfterTrade(
|
|
25
64
|
baseAssetAmount: BN,
|
|
26
65
|
direction: PositionDirection,
|
|
@@ -48,5 +87,12 @@ export function calculateMarkOracleSpread(
|
|
|
48
87
|
oraclePriceData: OraclePriceData
|
|
49
88
|
): BN {
|
|
50
89
|
const markPrice = calculateMarkPrice(market);
|
|
51
|
-
return markPrice
|
|
90
|
+
return calculateOracleSpread(markPrice, oraclePriceData);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function calculateOracleSpread(
|
|
94
|
+
price: BN,
|
|
95
|
+
oraclePriceData: OraclePriceData
|
|
96
|
+
): BN {
|
|
97
|
+
return price.sub(oraclePriceData.price);
|
|
52
98
|
}
|
package/src/math/trade.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { Market, PositionDirection } from '../types';
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
3
|
import { assert } from '../assert/assert';
|
|
4
4
|
import {
|
|
@@ -6,7 +6,6 @@ import {
|
|
|
6
6
|
PEG_PRECISION,
|
|
7
7
|
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
8
8
|
ZERO,
|
|
9
|
-
ONE,
|
|
10
9
|
} from '../constants/numericConstants';
|
|
11
10
|
import { calculateMarkPrice } from './market';
|
|
12
11
|
import {
|
|
@@ -14,6 +13,7 @@ import {
|
|
|
14
13
|
calculatePrice,
|
|
15
14
|
getSwapDirection,
|
|
16
15
|
AssetType,
|
|
16
|
+
calculateSpreadReserves,
|
|
17
17
|
} from './amm';
|
|
18
18
|
import { squareRootBN } from './utils';
|
|
19
19
|
|
|
@@ -101,6 +101,8 @@ export function calculateTradeSlippage(
|
|
|
101
101
|
* @param direction
|
|
102
102
|
* @param amount
|
|
103
103
|
* @param market
|
|
104
|
+
* @param inputAssetType
|
|
105
|
+
* @param useSpread
|
|
104
106
|
* @return
|
|
105
107
|
* | 'acquiredBase' => positive/negative change in user's base : BN TODO-PRECISION
|
|
106
108
|
* | 'acquiredQuote' => positive/negative change in user's quote : BN TODO-PRECISION
|
|
@@ -109,27 +111,36 @@ export function calculateTradeAcquiredAmounts(
|
|
|
109
111
|
direction: PositionDirection,
|
|
110
112
|
amount: BN,
|
|
111
113
|
market: Market,
|
|
112
|
-
inputAssetType: AssetType = 'quote'
|
|
114
|
+
inputAssetType: AssetType = 'quote',
|
|
115
|
+
useSpread = true
|
|
113
116
|
): [BN, BN] {
|
|
114
117
|
if (amount.eq(ZERO)) {
|
|
115
118
|
return [ZERO, ZERO];
|
|
116
119
|
}
|
|
117
120
|
|
|
118
121
|
const swapDirection = getSwapDirection(inputAssetType, direction);
|
|
119
|
-
|
|
120
|
-
|
|
122
|
+
|
|
123
|
+
let amm: Parameters<typeof calculateAmmReservesAfterSwap>[0];
|
|
124
|
+
if (useSpread && market.amm.baseSpread > 0) {
|
|
125
|
+
const { baseAssetReserve, quoteAssetReserve } = calculateSpreadReserves(
|
|
121
126
|
market.amm,
|
|
122
|
-
|
|
123
|
-
amount,
|
|
124
|
-
swapDirection
|
|
127
|
+
direction
|
|
125
128
|
);
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
129
|
+
amm = {
|
|
130
|
+
baseAssetReserve,
|
|
131
|
+
quoteAssetReserve,
|
|
132
|
+
sqrtK: market.amm.sqrtK,
|
|
133
|
+
pegMultiplier: market.amm.pegMultiplier,
|
|
134
|
+
};
|
|
135
|
+
} else {
|
|
136
|
+
amm = market.amm;
|
|
131
137
|
}
|
|
132
138
|
|
|
139
|
+
const [newQuoteAssetReserve, newBaseAssetReserve] =
|
|
140
|
+
calculateAmmReservesAfterSwap(amm, inputAssetType, amount, swapDirection);
|
|
141
|
+
|
|
142
|
+
const acquiredBase = amm.baseAssetReserve.sub(newBaseAssetReserve);
|
|
143
|
+
const acquiredQuote = amm.quoteAssetReserve.sub(newQuoteAssetReserve);
|
|
133
144
|
return [acquiredBase, acquiredQuote];
|
|
134
145
|
}
|
|
135
146
|
|
package/src/orders.ts
CHANGED
|
@@ -11,6 +11,7 @@ import {
|
|
|
11
11
|
BN,
|
|
12
12
|
calculateAmmReservesAfterSwap,
|
|
13
13
|
calculateBaseAssetValue,
|
|
14
|
+
calculateSpreadReserves,
|
|
14
15
|
ClearingHouseUser,
|
|
15
16
|
isOrderRiskIncreasingInSameDirection,
|
|
16
17
|
TEN_THOUSAND,
|
|
@@ -211,7 +212,9 @@ export function calculateAmountToTradeForLimit(
|
|
|
211
212
|
|
|
212
213
|
const [maxAmountToTrade, direction] = calculateMaxBaseAssetAmountToTrade(
|
|
213
214
|
market.amm,
|
|
214
|
-
limitPrice
|
|
215
|
+
limitPrice,
|
|
216
|
+
order.direction,
|
|
217
|
+
!order.postOnly
|
|
215
218
|
);
|
|
216
219
|
|
|
217
220
|
// Check that directions are the same
|
|
@@ -295,24 +298,51 @@ export function calculateBaseAssetAmountUserCanExecute(
|
|
|
295
298
|
return ZERO;
|
|
296
299
|
}
|
|
297
300
|
|
|
298
|
-
const
|
|
301
|
+
const swapDirection = isVariant(order.direction, 'long')
|
|
302
|
+
? SwapDirection.ADD
|
|
303
|
+
: SwapDirection.REMOVE;
|
|
304
|
+
|
|
305
|
+
const useSpread = !order.postOnly;
|
|
306
|
+
let amm: Parameters<typeof calculateAmmReservesAfterSwap>[0];
|
|
307
|
+
if (useSpread) {
|
|
308
|
+
const { baseAssetReserve, quoteAssetReserve } = calculateSpreadReserves(
|
|
309
|
+
market.amm,
|
|
310
|
+
order.direction
|
|
311
|
+
);
|
|
312
|
+
amm = {
|
|
313
|
+
baseAssetReserve,
|
|
314
|
+
quoteAssetReserve,
|
|
315
|
+
sqrtK: market.amm.sqrtK,
|
|
316
|
+
pegMultiplier: market.amm.pegMultiplier,
|
|
317
|
+
};
|
|
318
|
+
} else {
|
|
319
|
+
amm = market.amm;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const baseAssetReservesBefore = amm.baseAssetReserve;
|
|
299
323
|
const [_, baseAssetReservesAfter] = calculateAmmReservesAfterSwap(
|
|
300
|
-
|
|
324
|
+
amm,
|
|
301
325
|
'quote',
|
|
302
326
|
quoteAssetAmount,
|
|
303
|
-
|
|
304
|
-
? SwapDirection.ADD
|
|
305
|
-
: SwapDirection.REMOVE
|
|
327
|
+
swapDirection
|
|
306
328
|
);
|
|
307
329
|
|
|
308
|
-
let baseAssetAmount = baseAssetReservesBefore
|
|
330
|
+
let baseAssetAmount = baseAssetReservesBefore
|
|
331
|
+
.sub(baseAssetReservesAfter)
|
|
332
|
+
.abs();
|
|
309
333
|
if (order.reduceOnly) {
|
|
310
334
|
const position =
|
|
311
335
|
user.getUserPosition(order.marketIndex) ||
|
|
312
336
|
user.getEmptyPosition(order.marketIndex);
|
|
313
|
-
if (
|
|
337
|
+
if (
|
|
338
|
+
isVariant(order.direction, 'long') &&
|
|
339
|
+
position.baseAssetAmount.gte(ZERO)
|
|
340
|
+
) {
|
|
314
341
|
baseAssetAmount = ZERO;
|
|
315
|
-
} else if (
|
|
342
|
+
} else if (
|
|
343
|
+
isVariant(order.direction, 'short') &&
|
|
344
|
+
position.baseAssetAmount.lte(ZERO)
|
|
345
|
+
) {
|
|
316
346
|
baseAssetAmount = ZERO;
|
|
317
347
|
} else {
|
|
318
348
|
BN.min(baseAssetAmount, position.baseAssetAmount.abs());
|
package/src/types.ts
CHANGED
|
@@ -154,7 +154,7 @@ export type TradeRecord = {
|
|
|
154
154
|
markPriceBefore: BN;
|
|
155
155
|
markPriceAfter: BN;
|
|
156
156
|
fee: BN;
|
|
157
|
-
|
|
157
|
+
quoteAssetAmountSurplus: BN;
|
|
158
158
|
refereeDiscount: BN;
|
|
159
159
|
tokenDiscount: BN;
|
|
160
160
|
marketIndex: BN;
|
|
@@ -309,6 +309,7 @@ export type AMM = {
|
|
|
309
309
|
minimumQuoteAssetTradeSize: BN;
|
|
310
310
|
minimumBaseAssetTradeSize: BN;
|
|
311
311
|
lastOraclePrice: BN;
|
|
312
|
+
baseSpread: number;
|
|
312
313
|
};
|
|
313
314
|
|
|
314
315
|
// # User Account Types
|
|
@@ -331,6 +332,7 @@ export type UserAccount = {
|
|
|
331
332
|
cumulativeDeposits: BN;
|
|
332
333
|
positions: PublicKey;
|
|
333
334
|
totalFeePaid: BN;
|
|
335
|
+
totalFeeRebate: BN;
|
|
334
336
|
totalTokenDiscount: BN;
|
|
335
337
|
totalReferralReward: BN;
|
|
336
338
|
totalRefereeDiscount: BN;
|
package/src/assert/assert.js
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.assert = void 0;
|
|
4
|
-
function assert(condition, error) {
|
|
5
|
-
if (!condition) {
|
|
6
|
-
throw new Error(error || 'Unspecified AssertionError');
|
|
7
|
-
}
|
|
8
|
-
}
|
|
9
|
-
exports.assert = assert;
|
|
10
|
-
//# sourceMappingURL=assert.js.map
|
package/src/assert/assert.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"assert.js","sourceRoot":"","sources":["assert.ts"],"names":[],"mappings":";;;AAAA,SAAgB,MAAM,CAAC,SAAkB,EAAE,KAAc;IACxD,IAAI,CAAC,SAAS,EAAE;QACf,MAAM,IAAI,KAAK,CAAC,KAAK,IAAI,4BAA4B,CAAC,CAAC;KACvD;AACF,CAAC;AAJD,wBAIC"}
|
package/src/math/conversion.js
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.convertBaseAssetAmountToNumber = exports.convertToNumber = void 0;
|
|
4
|
-
const numericConstants_1 = require("../constants/numericConstants");
|
|
5
|
-
const convertToNumber = (bigNumber, precision = numericConstants_1.MARK_PRICE_PRECISION) => {
|
|
6
|
-
if (!bigNumber)
|
|
7
|
-
return 0;
|
|
8
|
-
return (bigNumber.div(precision).toNumber() +
|
|
9
|
-
bigNumber.mod(precision).toNumber() / precision.toNumber());
|
|
10
|
-
};
|
|
11
|
-
exports.convertToNumber = convertToNumber;
|
|
12
|
-
const convertBaseAssetAmountToNumber = (baseAssetAmount) => {
|
|
13
|
-
return (0, exports.convertToNumber)(baseAssetAmount, numericConstants_1.MARK_PRICE_PRECISION.mul(numericConstants_1.PEG_PRECISION));
|
|
14
|
-
};
|
|
15
|
-
exports.convertBaseAssetAmountToNumber = convertBaseAssetAmountToNumber;
|
|
16
|
-
//# sourceMappingURL=conversion.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"conversion.js","sourceRoot":"","sources":["conversion.ts"],"names":[],"mappings":";;;AACA,oEAGuC;AAEhC,MAAM,eAAe,GAAG,CAC9B,SAAa,EACb,YAAgB,uCAAoB,EACnC,EAAE;IACH,IAAI,CAAC,SAAS;QAAE,OAAO,CAAC,CAAC;IACzB,OAAO,CACN,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE;QACnC,SAAS,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC,QAAQ,EAAE,GAAG,SAAS,CAAC,QAAQ,EAAE,CAC1D,CAAC;AACH,CAAC,CAAC;AATW,QAAA,eAAe,mBAS1B;AAEK,MAAM,8BAA8B,GAAG,CAAC,eAAmB,EAAE,EAAE;IACrE,OAAO,IAAA,uBAAe,EACrB,eAAe,EACf,uCAAoB,CAAC,GAAG,CAAC,gCAAa,CAAC,CACvC,CAAC;AACH,CAAC,CAAC;AALW,QAAA,8BAA8B,kCAKzC"}
|