@drift-labs/sdk 2.18.0 → 2.19.1
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/bulkAccountLoader.d.ts +2 -1
- package/lib/accounts/bulkAccountLoader.js +3 -3
- package/lib/accounts/fetch.js +2 -2
- package/lib/accounts/pollingDriftClientAccountSubscriber.js +7 -7
- package/lib/accounts/pollingTokenAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserAccountSubscriber.js +2 -2
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +2 -2
- package/lib/accounts/types.d.ts +5 -4
- package/lib/accounts/webSocketAccountSubscriber.js +1 -1
- package/lib/accounts/webSocketDriftClientAccountSubscriber.js +3 -3
- package/lib/addresses/marketAddresses.js +1 -1
- package/lib/addresses/pda.js +5 -1
- package/lib/adminClient.js +61 -57
- package/lib/config.d.ts +2 -2
- package/lib/constants/perpMarkets.d.ts +1 -1
- package/lib/constants/perpMarkets.js +33 -3
- package/lib/constants/spotMarkets.d.ts +1 -1
- package/lib/dlob/DLOB.d.ts +4 -4
- package/lib/dlob/DLOB.js +89 -76
- package/lib/dlob/DLOBNode.d.ts +2 -2
- package/lib/dlob/DLOBNode.js +7 -7
- package/lib/dlob/DLOBOrders.d.ts +2 -2
- package/lib/dlob/NodeList.d.ts +2 -2
- package/lib/dlob/NodeList.js +4 -4
- package/lib/driftClient.d.ts +1 -1
- package/lib/driftClient.js +99 -95
- package/lib/driftClientConfig.d.ts +3 -3
- package/lib/events/eventSubscriber.js +2 -2
- package/lib/events/fetchLogs.d.ts +2 -2
- package/lib/events/pollingLogProvider.js +1 -1
- package/lib/events/types.d.ts +14 -14
- package/lib/examples/loadDlob.js +2 -2
- package/lib/examples/makeTradeExample.js +9 -9
- package/lib/factory/bigNum.js +13 -13
- package/lib/factory/oracleClient.js +6 -3
- package/lib/idl/drift.json +7 -4
- package/lib/index.js +5 -1
- package/lib/math/amm.d.ts +1 -1
- package/lib/math/amm.js +33 -38
- package/lib/math/auction.js +6 -6
- package/lib/math/exchangeStatus.js +2 -2
- package/lib/math/funding.js +2 -2
- package/lib/math/margin.js +4 -4
- package/lib/math/market.js +15 -15
- package/lib/math/oracles.js +1 -1
- package/lib/math/orders.js +24 -24
- package/lib/math/position.js +5 -5
- package/lib/math/repeg.js +1 -1
- package/lib/math/spotBalance.js +9 -9
- package/lib/math/spotMarket.js +3 -3
- package/lib/math/spotPosition.js +3 -3
- package/lib/math/trade.d.ts +1 -1
- package/lib/math/trade.js +43 -43
- package/lib/math/utils.js +1 -1
- package/lib/oracles/oracleClientCache.js +1 -1
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/types.d.ts +2 -2
- package/lib/serum/serumSubscriber.d.ts +4 -3
- package/lib/serum/serumSubscriber.js +40 -11
- package/lib/serum/types.d.ts +3 -1
- package/lib/slot/SlotSubscriber.d.ts +1 -1
- package/lib/tokenFaucet.js +5 -1
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +1 -1
- package/lib/tx/types.d.ts +1 -1
- package/lib/types.d.ts +49 -46
- package/lib/types.js +2 -1
- package/lib/user.js +63 -63
- package/lib/userConfig.d.ts +2 -2
- package/lib/userMap/userMap.js +1 -1
- package/lib/userMap/userStatsMap.js +3 -3
- package/lib/userStats.js +2 -2
- package/lib/userStatsConfig.d.ts +2 -2
- package/package.json +1 -1
- package/src/constants/perpMarkets.ts +33 -3
- package/src/dlob/DLOB.ts +17 -5
- package/src/dlob/NodeList.ts +2 -5
- package/src/factory/oracleClient.ts +5 -1
- package/src/idl/drift.json +7 -4
- package/src/math/amm.ts +22 -23
- package/src/math/trade.ts +1 -1
- package/src/serum/serumSubscriber.ts +62 -20
- package/src/serum/types.ts +9 -5
- package/src/types.ts +2 -1
- package/tests/amm/test.ts +177 -5
- package/tests/dlob/test.ts +2 -1
package/src/idl/drift.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "2.
|
|
2
|
+
"version": "2.19.1",
|
|
3
3
|
"name": "drift",
|
|
4
4
|
"instructions": [
|
|
5
5
|
{
|
|
@@ -6237,14 +6237,17 @@
|
|
|
6237
6237
|
{
|
|
6238
6238
|
"name": "Pyth"
|
|
6239
6239
|
},
|
|
6240
|
-
{
|
|
6241
|
-
"name": "Pyth1000"
|
|
6242
|
-
},
|
|
6243
6240
|
{
|
|
6244
6241
|
"name": "Switchboard"
|
|
6245
6242
|
},
|
|
6246
6243
|
{
|
|
6247
6244
|
"name": "QuoteAsset"
|
|
6245
|
+
},
|
|
6246
|
+
{
|
|
6247
|
+
"name": "Pyth1K"
|
|
6248
|
+
},
|
|
6249
|
+
{
|
|
6250
|
+
"name": "Pyth1M"
|
|
6248
6251
|
}
|
|
6249
6252
|
]
|
|
6250
6253
|
}
|
package/src/math/amm.ts
CHANGED
|
@@ -11,7 +11,6 @@ import {
|
|
|
11
11
|
MARGIN_PRECISION,
|
|
12
12
|
PRICE_DIV_PEG,
|
|
13
13
|
PERCENTAGE_PRECISION,
|
|
14
|
-
BASE_PRECISION,
|
|
15
14
|
DEFAULT_REVENUE_SINCE_LAST_FUNDING_SPREAD_RETREAT,
|
|
16
15
|
TWO,
|
|
17
16
|
} from '../constants/numericConstants';
|
|
@@ -362,10 +361,12 @@ export function calculateInventoryScale(
|
|
|
362
361
|
maxSpread: number
|
|
363
362
|
): number {
|
|
364
363
|
if (baseAssetAmountWithAmm.eq(ZERO)) {
|
|
365
|
-
return
|
|
364
|
+
return 1;
|
|
366
365
|
}
|
|
367
366
|
|
|
368
|
-
const
|
|
367
|
+
const MAX_BID_ASK_INVENTORY_SKEW_FACTOR = BID_ASK_SPREAD_PRECISION.mul(
|
|
368
|
+
new BN(10)
|
|
369
|
+
);
|
|
369
370
|
// inventory skew
|
|
370
371
|
const [openBids, openAsks] = calculateMarketOpenBidAsk(
|
|
371
372
|
baseAssetReserve,
|
|
@@ -373,34 +374,32 @@ export function calculateInventoryScale(
|
|
|
373
374
|
maxBaseAssetReserve
|
|
374
375
|
);
|
|
375
376
|
|
|
376
|
-
const minSideLiquidity = BN.
|
|
377
|
-
|
|
378
|
-
|
|
377
|
+
const minSideLiquidity = BN.min(openBids.abs(), openAsks.abs());
|
|
378
|
+
|
|
379
|
+
const inventoryScaleBN = BN.min(
|
|
380
|
+
baseAssetAmountWithAmm
|
|
381
|
+
.mul(PERCENTAGE_PRECISION)
|
|
382
|
+
.div(BN.max(minSideLiquidity, ONE))
|
|
383
|
+
.abs(),
|
|
384
|
+
PERCENTAGE_PRECISION
|
|
379
385
|
);
|
|
380
386
|
|
|
381
387
|
const inventoryScaleMaxBN = BN.max(
|
|
382
|
-
|
|
383
|
-
new BN(maxSpread
|
|
388
|
+
MAX_BID_ASK_INVENTORY_SKEW_FACTOR,
|
|
389
|
+
new BN(maxSpread)
|
|
384
390
|
.mul(BID_ASK_SPREAD_PRECISION)
|
|
385
391
|
.div(new BN(Math.max(directionalSpread, 1)))
|
|
386
392
|
);
|
|
387
393
|
|
|
388
|
-
const
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
const inventoryScale =
|
|
396
|
-
BN.min(inventoryScaleMaxBN, inventoryScaleBN).toNumber() /
|
|
397
|
-
BID_ASK_SPREAD_PRECISION.toNumber();
|
|
398
|
-
|
|
399
|
-
const inventoryScaleMax =
|
|
400
|
-
inventoryScaleMaxBN.toNumber() / BID_ASK_SPREAD_PRECISION.toNumber();
|
|
401
|
-
const inventorySpreadScale = Math.min(inventoryScaleMax, 1 + inventoryScale);
|
|
394
|
+
const inventoryScaleCapped =
|
|
395
|
+
BN.min(
|
|
396
|
+
inventoryScaleMaxBN,
|
|
397
|
+
BID_ASK_SPREAD_PRECISION.add(
|
|
398
|
+
inventoryScaleMaxBN.mul(inventoryScaleBN).div(PERCENTAGE_PRECISION)
|
|
399
|
+
)
|
|
400
|
+
).toNumber() / BID_ASK_SPREAD_PRECISION.toNumber();
|
|
402
401
|
|
|
403
|
-
return
|
|
402
|
+
return inventoryScaleCapped;
|
|
404
403
|
}
|
|
405
404
|
|
|
406
405
|
export function calculateEffectiveLeverage(
|
package/src/math/trade.ts
CHANGED
|
@@ -782,7 +782,7 @@ export function calculateEstimatedSpotEntryPrice(
|
|
|
782
782
|
cumulativeBaseFilled = cumulativeBaseFilled.add(baseFilled);
|
|
783
783
|
cumulativeQuoteFilled = cumulativeQuoteFilled.add(quoteFilled);
|
|
784
784
|
|
|
785
|
-
worstPrice =
|
|
785
|
+
worstPrice = dlobLimitOrderPrice;
|
|
786
786
|
|
|
787
787
|
dlobLimitOrder = dlobLimitOrders.next().value;
|
|
788
788
|
} else {
|
|
@@ -9,26 +9,32 @@ export class SerumSubscriber {
|
|
|
9
9
|
connection: Connection;
|
|
10
10
|
programId: PublicKey;
|
|
11
11
|
marketAddress: PublicKey;
|
|
12
|
-
|
|
12
|
+
subscriptionType: 'polling' | 'websocket';
|
|
13
|
+
accountLoader: BulkAccountLoader | undefined;
|
|
13
14
|
market: Market;
|
|
14
15
|
|
|
15
16
|
subscribed: boolean;
|
|
16
17
|
|
|
17
18
|
asksAddress: PublicKey;
|
|
18
19
|
asks: Orderbook;
|
|
19
|
-
asksCallbackId: string;
|
|
20
|
+
asksCallbackId: string | number;
|
|
20
21
|
lastAsksSlot: number;
|
|
21
22
|
|
|
22
23
|
bidsAddress: PublicKey;
|
|
23
24
|
bids: Orderbook;
|
|
24
|
-
bidsCallbackId: string;
|
|
25
|
+
bidsCallbackId: string | number;
|
|
25
26
|
lastBidsSlot: number;
|
|
26
27
|
|
|
27
28
|
public constructor(config: SerumMarketSubscriberConfig) {
|
|
28
29
|
this.connection = config.connection;
|
|
29
30
|
this.programId = config.programId;
|
|
30
31
|
this.marketAddress = config.marketAddress;
|
|
31
|
-
|
|
32
|
+
if (config.accountSubscription.type === 'polling') {
|
|
33
|
+
this.subscriptionType = 'polling';
|
|
34
|
+
this.accountLoader = config.accountSubscription.accountLoader;
|
|
35
|
+
} else {
|
|
36
|
+
this.subscriptionType = 'websocket';
|
|
37
|
+
}
|
|
32
38
|
}
|
|
33
39
|
|
|
34
40
|
public async subscribe(): Promise<void> {
|
|
@@ -46,24 +52,44 @@ export class SerumSubscriber {
|
|
|
46
52
|
this.asksAddress = this.market.asksAddress;
|
|
47
53
|
this.asks = await this.market.loadAsks(this.connection);
|
|
48
54
|
|
|
49
|
-
this.
|
|
50
|
-
this.
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
55
|
+
if (this.subscriptionType === 'websocket') {
|
|
56
|
+
this.asksCallbackId = this.connection.onAccountChange(
|
|
57
|
+
this.asksAddress,
|
|
58
|
+
(accountInfo, ctx) => {
|
|
59
|
+
this.lastAsksSlot = ctx.slot;
|
|
60
|
+
this.asks = Orderbook.decode(this.market, accountInfo.data);
|
|
61
|
+
}
|
|
62
|
+
);
|
|
63
|
+
} else {
|
|
64
|
+
this.asksCallbackId = await this.accountLoader.addAccount(
|
|
65
|
+
this.asksAddress,
|
|
66
|
+
(buffer, slot) => {
|
|
67
|
+
this.lastAsksSlot = slot;
|
|
68
|
+
this.asks = Orderbook.decode(this.market, buffer);
|
|
69
|
+
}
|
|
70
|
+
);
|
|
71
|
+
}
|
|
56
72
|
|
|
57
73
|
this.bidsAddress = this.market.bidsAddress;
|
|
58
74
|
this.bids = await this.market.loadBids(this.connection);
|
|
59
75
|
|
|
60
|
-
this.
|
|
61
|
-
this.
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
76
|
+
if (this.subscriptionType === 'websocket') {
|
|
77
|
+
this.bidsCallbackId = this.connection.onAccountChange(
|
|
78
|
+
this.bidsAddress,
|
|
79
|
+
(accountInfo, ctx) => {
|
|
80
|
+
this.lastBidsSlot = ctx.slot;
|
|
81
|
+
this.bids = Orderbook.decode(this.market, accountInfo.data);
|
|
82
|
+
}
|
|
83
|
+
);
|
|
84
|
+
} else {
|
|
85
|
+
this.bidsCallbackId = await this.accountLoader.addAccount(
|
|
86
|
+
this.bidsAddress,
|
|
87
|
+
(buffer, slot) => {
|
|
88
|
+
this.lastBidsSlot = slot;
|
|
89
|
+
this.bids = Orderbook.decode(this.market, buffer);
|
|
90
|
+
}
|
|
91
|
+
);
|
|
92
|
+
}
|
|
67
93
|
|
|
68
94
|
this.subscribed = true;
|
|
69
95
|
}
|
|
@@ -91,8 +117,24 @@ export class SerumSubscriber {
|
|
|
91
117
|
return;
|
|
92
118
|
}
|
|
93
119
|
|
|
94
|
-
|
|
95
|
-
|
|
120
|
+
// remove listeners
|
|
121
|
+
if (this.subscriptionType === 'websocket') {
|
|
122
|
+
await this.connection.removeAccountChangeListener(
|
|
123
|
+
this.asksCallbackId as number
|
|
124
|
+
);
|
|
125
|
+
await this.connection.removeAccountChangeListener(
|
|
126
|
+
this.bidsCallbackId as number
|
|
127
|
+
);
|
|
128
|
+
} else {
|
|
129
|
+
this.accountLoader.removeAccount(
|
|
130
|
+
this.asksAddress,
|
|
131
|
+
this.asksCallbackId as string
|
|
132
|
+
);
|
|
133
|
+
this.accountLoader.removeAccount(
|
|
134
|
+
this.bidsAddress,
|
|
135
|
+
this.bidsCallbackId as string
|
|
136
|
+
);
|
|
137
|
+
}
|
|
96
138
|
|
|
97
139
|
this.subscribed = false;
|
|
98
140
|
}
|
package/src/serum/types.ts
CHANGED
|
@@ -5,9 +5,13 @@ export type SerumMarketSubscriberConfig = {
|
|
|
5
5
|
connection: Connection;
|
|
6
6
|
programId: PublicKey;
|
|
7
7
|
marketAddress: PublicKey;
|
|
8
|
-
accountSubscription:
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
8
|
+
accountSubscription:
|
|
9
|
+
| {
|
|
10
|
+
// enables use to add web sockets in the future
|
|
11
|
+
type: 'polling';
|
|
12
|
+
accountLoader: BulkAccountLoader;
|
|
13
|
+
}
|
|
14
|
+
| {
|
|
15
|
+
type: 'websocket';
|
|
16
|
+
};
|
|
13
17
|
};
|
package/src/types.ts
CHANGED
|
@@ -76,7 +76,8 @@ export class DepositDirection {
|
|
|
76
76
|
|
|
77
77
|
export class OracleSource {
|
|
78
78
|
static readonly PYTH = { pyth: {} };
|
|
79
|
-
static readonly
|
|
79
|
+
static readonly PYTH_1K = { pyth1K: {} };
|
|
80
|
+
static readonly PYTH_1M = { pyth1M: {} };
|
|
80
81
|
// static readonly SWITCHBOARD = { switchboard: {} };
|
|
81
82
|
static readonly QUOTE_ASSET = { quoteAsset: {} };
|
|
82
83
|
}
|
package/tests/amm/test.ts
CHANGED
|
@@ -6,8 +6,10 @@ import {
|
|
|
6
6
|
QUOTE_PRECISION,
|
|
7
7
|
calculateSpreadBN,
|
|
8
8
|
ZERO,
|
|
9
|
+
ONE,
|
|
9
10
|
calculateLiveOracleStd,
|
|
10
11
|
calculateLiveOracleTwap,
|
|
12
|
+
calculateInventoryScale,
|
|
11
13
|
} from '../../src';
|
|
12
14
|
import { mockPerpMarkets } from '../dlob/helpers';
|
|
13
15
|
|
|
@@ -36,6 +38,176 @@ class AMMSpreadTerms {
|
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
describe('AMM Tests', () => {
|
|
41
|
+
it('Spread Maths', () => {
|
|
42
|
+
let iscale = calculateInventoryScale(
|
|
43
|
+
ZERO,
|
|
44
|
+
AMM_RESERVE_PRECISION,
|
|
45
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
46
|
+
AMM_RESERVE_PRECISION.mul(new BN(3)).div(new BN(2)),
|
|
47
|
+
250,
|
|
48
|
+
30000,
|
|
49
|
+
);
|
|
50
|
+
assert(iscale == 1);
|
|
51
|
+
|
|
52
|
+
iscale = calculateInventoryScale(
|
|
53
|
+
ONE,
|
|
54
|
+
AMM_RESERVE_PRECISION,
|
|
55
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
56
|
+
AMM_RESERVE_PRECISION.mul(new BN(3)).div(new BN(2)),
|
|
57
|
+
250,
|
|
58
|
+
30000,
|
|
59
|
+
);
|
|
60
|
+
assert(iscale == 1);
|
|
61
|
+
|
|
62
|
+
let baa = new BN(1000);
|
|
63
|
+
iscale = calculateInventoryScale(
|
|
64
|
+
baa,
|
|
65
|
+
AMM_RESERVE_PRECISION.add(baa),
|
|
66
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
67
|
+
AMM_RESERVE_PRECISION.mul(new BN(3)).div(new BN(2)),
|
|
68
|
+
250,
|
|
69
|
+
30000,
|
|
70
|
+
);
|
|
71
|
+
console.log(iscale);
|
|
72
|
+
assert(iscale == 1.00024);
|
|
73
|
+
|
|
74
|
+
baa = new BN(100000);
|
|
75
|
+
iscale = calculateInventoryScale(
|
|
76
|
+
baa,
|
|
77
|
+
AMM_RESERVE_PRECISION.add(baa),
|
|
78
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
79
|
+
AMM_RESERVE_PRECISION.mul(new BN(3)).div(new BN(2)),
|
|
80
|
+
250,
|
|
81
|
+
30000,
|
|
82
|
+
);
|
|
83
|
+
console.log(iscale);
|
|
84
|
+
assert(iscale == 1.024);
|
|
85
|
+
|
|
86
|
+
baa = new BN(1000000);
|
|
87
|
+
iscale = calculateInventoryScale(
|
|
88
|
+
baa,
|
|
89
|
+
AMM_RESERVE_PRECISION.add(baa),
|
|
90
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
91
|
+
AMM_RESERVE_PRECISION.mul(new BN(3)).div(new BN(2)),
|
|
92
|
+
250,
|
|
93
|
+
30000,
|
|
94
|
+
);
|
|
95
|
+
console.log(iscale);
|
|
96
|
+
assert(iscale == 1.24048);
|
|
97
|
+
|
|
98
|
+
baa = new BN(10000000); // 2%
|
|
99
|
+
iscale = calculateInventoryScale(
|
|
100
|
+
baa,
|
|
101
|
+
AMM_RESERVE_PRECISION.add(baa),
|
|
102
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
103
|
+
AMM_RESERVE_PRECISION.mul(new BN(3)).div(new BN(2)),
|
|
104
|
+
250,
|
|
105
|
+
30000,
|
|
106
|
+
);
|
|
107
|
+
console.log(iscale);
|
|
108
|
+
assert(iscale == 3.44896);
|
|
109
|
+
|
|
110
|
+
baa = new BN(50000000); // 10%
|
|
111
|
+
iscale = calculateInventoryScale(
|
|
112
|
+
baa,
|
|
113
|
+
AMM_RESERVE_PRECISION.add(baa),
|
|
114
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
115
|
+
AMM_RESERVE_PRECISION.mul(new BN(3)).div(new BN(2)),
|
|
116
|
+
250,
|
|
117
|
+
30000,
|
|
118
|
+
);
|
|
119
|
+
console.log(iscale);
|
|
120
|
+
assert(iscale == 14.33332);
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
baa = AMM_RESERVE_PRECISION.div(new BN(4)); // 50%
|
|
124
|
+
iscale = calculateInventoryScale(
|
|
125
|
+
baa,
|
|
126
|
+
AMM_RESERVE_PRECISION.add(baa),
|
|
127
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
128
|
+
AMM_RESERVE_PRECISION.mul(new BN(3)).div(new BN(2)),
|
|
129
|
+
250,
|
|
130
|
+
30000,
|
|
131
|
+
);
|
|
132
|
+
console.log(iscale);
|
|
133
|
+
assert(iscale == 120); //100%
|
|
134
|
+
|
|
135
|
+
baa = AMM_RESERVE_PRECISION.div(new BN(4)); // 50%
|
|
136
|
+
iscale = calculateInventoryScale(
|
|
137
|
+
baa,
|
|
138
|
+
AMM_RESERVE_PRECISION.add(baa),
|
|
139
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
140
|
+
AMM_RESERVE_PRECISION.mul(new BN(3)).div(new BN(2)),
|
|
141
|
+
250,
|
|
142
|
+
30000 * 2,
|
|
143
|
+
);
|
|
144
|
+
console.log(iscale);
|
|
145
|
+
assert(iscale == 120 * 2); //100%
|
|
146
|
+
|
|
147
|
+
baa = AMM_RESERVE_PRECISION.div(new BN(5)); // <50%
|
|
148
|
+
iscale = calculateInventoryScale(
|
|
149
|
+
baa,
|
|
150
|
+
AMM_RESERVE_PRECISION.add(baa),
|
|
151
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
152
|
+
AMM_RESERVE_PRECISION.mul(new BN(3)).div(new BN(2)),
|
|
153
|
+
250,
|
|
154
|
+
30000 * 2,
|
|
155
|
+
);
|
|
156
|
+
assert(iscale == 160.99984);
|
|
157
|
+
|
|
158
|
+
baa = new BN(855329058);
|
|
159
|
+
iscale = calculateInventoryScale(
|
|
160
|
+
baa,
|
|
161
|
+
AMM_RESERVE_PRECISION.add(baa),
|
|
162
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
163
|
+
AMM_RESERVE_PRECISION,
|
|
164
|
+
250,
|
|
165
|
+
30000,
|
|
166
|
+
); // >100%
|
|
167
|
+
assert(iscale == 120);
|
|
168
|
+
assert(250*iscale == 30000);
|
|
169
|
+
|
|
170
|
+
iscale = calculateInventoryScale(
|
|
171
|
+
baa,
|
|
172
|
+
AMM_RESERVE_PRECISION.add(baa), // ~85%
|
|
173
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
174
|
+
AMM_RESERVE_PRECISION.mul(new BN(3)).div(new BN(2)),
|
|
175
|
+
250,
|
|
176
|
+
30000,
|
|
177
|
+
);
|
|
178
|
+
assert(iscale == 120);
|
|
179
|
+
assert(250*iscale == 30000);
|
|
180
|
+
|
|
181
|
+
baa = new BN(-855329058); // ~85%
|
|
182
|
+
iscale = calculateInventoryScale(
|
|
183
|
+
baa,
|
|
184
|
+
AMM_RESERVE_PRECISION.add(baa),
|
|
185
|
+
AMM_RESERVE_PRECISION.div(new BN(2)),
|
|
186
|
+
AMM_RESERVE_PRECISION.mul(new BN(3)).div(new BN(2)),
|
|
187
|
+
250,
|
|
188
|
+
30000,
|
|
189
|
+
);
|
|
190
|
+
assert(iscale == 120);
|
|
191
|
+
assert(250*iscale == 30000);
|
|
192
|
+
|
|
193
|
+
// 'bonk' scale
|
|
194
|
+
iscale = calculateInventoryScale(
|
|
195
|
+
new BN('30228000000000000'),
|
|
196
|
+
new BN('2496788386034912600'),
|
|
197
|
+
new BN('2443167585342470000'),
|
|
198
|
+
new BN('2545411471321696000'),
|
|
199
|
+
3500,
|
|
200
|
+
100000,
|
|
201
|
+
);
|
|
202
|
+
console.log(iscale);
|
|
203
|
+
console.log(3500*iscale/1e6);
|
|
204
|
+
assert(iscale == 18.762285);
|
|
205
|
+
assert(3500*iscale/1e6 == 0.06566799749999999); //6.5%
|
|
206
|
+
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
});
|
|
210
|
+
|
|
39
211
|
it('Various Spreads', () => {
|
|
40
212
|
const baseSpread: number = 0.025 * 1e6;
|
|
41
213
|
const lastOracleReservePriceSpreadPct: BN = ZERO;
|
|
@@ -150,9 +322,9 @@ describe('AMM Tests', () => {
|
|
|
150
322
|
|
|
151
323
|
console.log(terms2);
|
|
152
324
|
assert(terms2.effectiveLeverageCapped >= 1.0002);
|
|
153
|
-
assert(terms2.inventorySpreadScale ==
|
|
154
|
-
assert(terms2.longSpread ==
|
|
155
|
-
assert(terms2.shortSpread ==
|
|
325
|
+
assert(terms2.inventorySpreadScale == 1.73492);
|
|
326
|
+
assert(terms2.longSpread == 4262);
|
|
327
|
+
assert(terms2.shortSpread == 43238);
|
|
156
328
|
});
|
|
157
329
|
|
|
158
330
|
it('Corner Case Spreads', () => {
|
|
@@ -183,8 +355,8 @@ describe('AMM Tests', () => {
|
|
|
183
355
|
|
|
184
356
|
console.log(terms2);
|
|
185
357
|
assert(terms2.effectiveLeverageCapped <= 1.000001);
|
|
186
|
-
assert(terms2.inventorySpreadScale == 1.
|
|
187
|
-
assert(terms2.longSpread ==
|
|
358
|
+
assert(terms2.inventorySpreadScale == 1.013527);
|
|
359
|
+
assert(terms2.longSpread == 1146);
|
|
188
360
|
assert(terms2.shortSpread == 6686);
|
|
189
361
|
});
|
|
190
362
|
|
package/tests/dlob/test.ts
CHANGED
|
@@ -5207,7 +5207,8 @@ describe('DLOB Spot Tests', () => {
|
|
|
5207
5207
|
expect(nodesToFillAfter[1].makerNode?.order?.orderId).to.equal(3);
|
|
5208
5208
|
});
|
|
5209
5209
|
|
|
5210
|
-
|
|
5210
|
+
// add back if dlob checks limit order age again
|
|
5211
|
+
it.skip('Test limit orders skipping more recent post onlys', () => {
|
|
5211
5212
|
const vAsk = new BN(15);
|
|
5212
5213
|
const vBid = new BN(8);
|
|
5213
5214
|
|