@drift-labs/sdk 0.2.0-master.24 → 0.2.0-master.25
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/addresses/pda.d.ts +1 -0
- package/lib/addresses/pda.js +5 -1
- package/lib/admin.js +7 -12
- package/lib/clearingHouse.d.ts +2 -0
- package/lib/clearingHouse.js +12 -5
- package/lib/clearingHouseUser.d.ts +3 -1
- package/lib/clearingHouseUser.js +6 -4
- package/lib/config.js +1 -1
- package/lib/events/eventSubscriber.d.ts +4 -2
- package/lib/events/eventSubscriber.js +16 -9
- package/lib/events/fetchLogs.d.ts +10 -1
- package/lib/events/fetchLogs.js +27 -7
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +6 -2
- package/lib/events/types.d.ts +3 -2
- package/lib/events/types.js +1 -0
- package/lib/examples/makeTradeExample.js +13 -1
- package/lib/idl/clearing_house.json +97 -45
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/amm.d.ts +3 -1
- package/lib/math/amm.js +41 -3
- package/lib/types.d.ts +25 -6
- package/lib/types.js +7 -1
- package/package.json +1 -1
- package/src/addresses/pda.ts +9 -0
- package/src/admin.ts +11 -30
- package/src/clearingHouse.ts +17 -5
- package/src/clearingHouseUser.ts +6 -5
- package/src/config.ts +1 -1
- package/src/events/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.ts +10 -2
- package/src/events/types.ts +4 -1
- package/src/examples/makeTradeExample.ts +20 -1
- package/src/idl/clearing_house.json +97 -45
- package/src/index.ts +1 -0
- package/src/math/amm.ts +67 -2
- package/src/types.ts +21 -6
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"isSigner": false
|
|
27
27
|
},
|
|
28
28
|
{
|
|
29
|
-
"name": "
|
|
29
|
+
"name": "clearingHouseSigner",
|
|
30
30
|
"isMut": false,
|
|
31
31
|
"isSigner": false
|
|
32
32
|
},
|
|
@@ -71,18 +71,13 @@
|
|
|
71
71
|
"isMut": true,
|
|
72
72
|
"isSigner": false
|
|
73
73
|
},
|
|
74
|
-
{
|
|
75
|
-
"name": "bankVaultAuthority",
|
|
76
|
-
"isMut": false,
|
|
77
|
-
"isSigner": false
|
|
78
|
-
},
|
|
79
74
|
{
|
|
80
75
|
"name": "insuranceFundVault",
|
|
81
76
|
"isMut": true,
|
|
82
77
|
"isSigner": false
|
|
83
78
|
},
|
|
84
79
|
{
|
|
85
|
-
"name": "
|
|
80
|
+
"name": "clearingHouseSigner",
|
|
86
81
|
"isMut": false,
|
|
87
82
|
"isSigner": false
|
|
88
83
|
},
|
|
@@ -316,8 +311,8 @@
|
|
|
316
311
|
"isSigner": false
|
|
317
312
|
},
|
|
318
313
|
{
|
|
319
|
-
"name": "
|
|
320
|
-
"isMut":
|
|
314
|
+
"name": "clearingHouseSigner",
|
|
315
|
+
"isMut": false,
|
|
321
316
|
"isSigner": false
|
|
322
317
|
},
|
|
323
318
|
{
|
|
@@ -974,8 +969,8 @@
|
|
|
974
969
|
"isSigner": false
|
|
975
970
|
},
|
|
976
971
|
{
|
|
977
|
-
"name": "
|
|
978
|
-
"isMut":
|
|
972
|
+
"name": "clearingHouseSigner",
|
|
973
|
+
"isMut": false,
|
|
979
974
|
"isSigner": false
|
|
980
975
|
},
|
|
981
976
|
{
|
|
@@ -1029,8 +1024,8 @@
|
|
|
1029
1024
|
"isSigner": false
|
|
1030
1025
|
},
|
|
1031
1026
|
{
|
|
1032
|
-
"name": "
|
|
1033
|
-
"isMut":
|
|
1027
|
+
"name": "clearingHouseSigner",
|
|
1028
|
+
"isMut": false,
|
|
1034
1029
|
"isSigner": false
|
|
1035
1030
|
},
|
|
1036
1031
|
{
|
|
@@ -1100,8 +1095,8 @@
|
|
|
1100
1095
|
"isSigner": false
|
|
1101
1096
|
},
|
|
1102
1097
|
{
|
|
1103
|
-
"name": "
|
|
1104
|
-
"isMut":
|
|
1098
|
+
"name": "clearingHouseSigner",
|
|
1099
|
+
"isMut": false,
|
|
1105
1100
|
"isSigner": false
|
|
1106
1101
|
},
|
|
1107
1102
|
{
|
|
@@ -1146,7 +1141,7 @@
|
|
|
1146
1141
|
"isSigner": false
|
|
1147
1142
|
},
|
|
1148
1143
|
{
|
|
1149
|
-
"name": "
|
|
1144
|
+
"name": "clearingHouseSigner",
|
|
1150
1145
|
"isMut": false,
|
|
1151
1146
|
"isSigner": false
|
|
1152
1147
|
},
|
|
@@ -1192,7 +1187,7 @@
|
|
|
1192
1187
|
"isSigner": false
|
|
1193
1188
|
},
|
|
1194
1189
|
{
|
|
1195
|
-
"name": "
|
|
1190
|
+
"name": "clearingHouseSigner",
|
|
1196
1191
|
"isMut": false,
|
|
1197
1192
|
"isSigner": false
|
|
1198
1193
|
},
|
|
@@ -2414,8 +2409,8 @@
|
|
|
2414
2409
|
"isSigner": false
|
|
2415
2410
|
},
|
|
2416
2411
|
{
|
|
2417
|
-
"name": "
|
|
2418
|
-
"isMut":
|
|
2412
|
+
"name": "clearingHouseSigner",
|
|
2413
|
+
"isMut": false,
|
|
2419
2414
|
"isSigner": false
|
|
2420
2415
|
},
|
|
2421
2416
|
{
|
|
@@ -2565,6 +2560,11 @@
|
|
|
2565
2560
|
{
|
|
2566
2561
|
"name": "removeInsuranceFundStake",
|
|
2567
2562
|
"accounts": [
|
|
2563
|
+
{
|
|
2564
|
+
"name": "state",
|
|
2565
|
+
"isMut": false,
|
|
2566
|
+
"isSigner": false
|
|
2567
|
+
},
|
|
2568
2568
|
{
|
|
2569
2569
|
"name": "bank",
|
|
2570
2570
|
"isMut": false,
|
|
@@ -2591,8 +2591,8 @@
|
|
|
2591
2591
|
"isSigner": false
|
|
2592
2592
|
},
|
|
2593
2593
|
{
|
|
2594
|
-
"name": "
|
|
2595
|
-
"isMut":
|
|
2594
|
+
"name": "clearingHouseSigner",
|
|
2595
|
+
"isMut": false,
|
|
2596
2596
|
"isSigner": false
|
|
2597
2597
|
},
|
|
2598
2598
|
{
|
|
@@ -2646,26 +2646,10 @@
|
|
|
2646
2646
|
"name": "vault",
|
|
2647
2647
|
"type": "publicKey"
|
|
2648
2648
|
},
|
|
2649
|
-
{
|
|
2650
|
-
"name": "vaultAuthority",
|
|
2651
|
-
"type": "publicKey"
|
|
2652
|
-
},
|
|
2653
|
-
{
|
|
2654
|
-
"name": "vaultAuthorityNonce",
|
|
2655
|
-
"type": "u8"
|
|
2656
|
-
},
|
|
2657
2649
|
{
|
|
2658
2650
|
"name": "insuranceFundVault",
|
|
2659
2651
|
"type": "publicKey"
|
|
2660
2652
|
},
|
|
2661
|
-
{
|
|
2662
|
-
"name": "insuranceFundVaultAuthority",
|
|
2663
|
-
"type": "publicKey"
|
|
2664
|
-
},
|
|
2665
|
-
{
|
|
2666
|
-
"name": "insuranceFundVaultAuthorityNonce",
|
|
2667
|
-
"type": "u8"
|
|
2668
|
-
},
|
|
2669
2653
|
{
|
|
2670
2654
|
"name": "revenuePool",
|
|
2671
2655
|
"type": {
|
|
@@ -2976,14 +2960,6 @@
|
|
|
2976
2960
|
"name": "insuranceVault",
|
|
2977
2961
|
"type": "publicKey"
|
|
2978
2962
|
},
|
|
2979
|
-
{
|
|
2980
|
-
"name": "insuranceVaultAuthority",
|
|
2981
|
-
"type": "publicKey"
|
|
2982
|
-
},
|
|
2983
|
-
{
|
|
2984
|
-
"name": "insuranceVaultNonce",
|
|
2985
|
-
"type": "u8"
|
|
2986
|
-
},
|
|
2987
2963
|
{
|
|
2988
2964
|
"name": "marginRatioInitial",
|
|
2989
2965
|
"type": "u128"
|
|
@@ -3072,6 +3048,14 @@
|
|
|
3072
3048
|
"name": "liquidationMarginBufferRatio",
|
|
3073
3049
|
"type": "u8"
|
|
3074
3050
|
},
|
|
3051
|
+
{
|
|
3052
|
+
"name": "signer",
|
|
3053
|
+
"type": "publicKey"
|
|
3054
|
+
},
|
|
3055
|
+
{
|
|
3056
|
+
"name": "signerNonce",
|
|
3057
|
+
"type": "u8"
|
|
3058
|
+
},
|
|
3075
3059
|
{
|
|
3076
3060
|
"name": "padding0",
|
|
3077
3061
|
"type": "u128"
|
|
@@ -3352,6 +3336,10 @@
|
|
|
3352
3336
|
"name": "quoteAssetAmount",
|
|
3353
3337
|
"type": "i128"
|
|
3354
3338
|
},
|
|
3339
|
+
{
|
|
3340
|
+
"name": "lpShares",
|
|
3341
|
+
"type": "u128"
|
|
3342
|
+
},
|
|
3355
3343
|
{
|
|
3356
3344
|
"name": "userPnl",
|
|
3357
3345
|
"type": "i128"
|
|
@@ -4478,6 +4466,23 @@
|
|
|
4478
4466
|
]
|
|
4479
4467
|
}
|
|
4480
4468
|
},
|
|
4469
|
+
{
|
|
4470
|
+
"name": "LPAction",
|
|
4471
|
+
"type": {
|
|
4472
|
+
"kind": "enum",
|
|
4473
|
+
"variants": [
|
|
4474
|
+
{
|
|
4475
|
+
"name": "AddLiquidity"
|
|
4476
|
+
},
|
|
4477
|
+
{
|
|
4478
|
+
"name": "RemoveLiquidity"
|
|
4479
|
+
},
|
|
4480
|
+
{
|
|
4481
|
+
"name": "SettleLiquidity"
|
|
4482
|
+
}
|
|
4483
|
+
]
|
|
4484
|
+
}
|
|
4485
|
+
},
|
|
4481
4486
|
{
|
|
4482
4487
|
"name": "LiquidationType",
|
|
4483
4488
|
"type": {
|
|
@@ -5173,6 +5178,53 @@
|
|
|
5173
5178
|
}
|
|
5174
5179
|
]
|
|
5175
5180
|
},
|
|
5181
|
+
{
|
|
5182
|
+
"name": "LPRecord",
|
|
5183
|
+
"fields": [
|
|
5184
|
+
{
|
|
5185
|
+
"name": "ts",
|
|
5186
|
+
"type": "i64",
|
|
5187
|
+
"index": false
|
|
5188
|
+
},
|
|
5189
|
+
{
|
|
5190
|
+
"name": "user",
|
|
5191
|
+
"type": "publicKey",
|
|
5192
|
+
"index": false
|
|
5193
|
+
},
|
|
5194
|
+
{
|
|
5195
|
+
"name": "action",
|
|
5196
|
+
"type": {
|
|
5197
|
+
"defined": "LPAction"
|
|
5198
|
+
},
|
|
5199
|
+
"index": false
|
|
5200
|
+
},
|
|
5201
|
+
{
|
|
5202
|
+
"name": "nShares",
|
|
5203
|
+
"type": "u128",
|
|
5204
|
+
"index": false
|
|
5205
|
+
},
|
|
5206
|
+
{
|
|
5207
|
+
"name": "marketIndex",
|
|
5208
|
+
"type": "u64",
|
|
5209
|
+
"index": false
|
|
5210
|
+
},
|
|
5211
|
+
{
|
|
5212
|
+
"name": "deltaBaseAssetAmount",
|
|
5213
|
+
"type": "i128",
|
|
5214
|
+
"index": false
|
|
5215
|
+
},
|
|
5216
|
+
{
|
|
5217
|
+
"name": "deltaQuoteAssetAmount",
|
|
5218
|
+
"type": "i128",
|
|
5219
|
+
"index": false
|
|
5220
|
+
},
|
|
5221
|
+
{
|
|
5222
|
+
"name": "pnl",
|
|
5223
|
+
"type": "i128",
|
|
5224
|
+
"index": false
|
|
5225
|
+
}
|
|
5226
|
+
]
|
|
5227
|
+
},
|
|
5176
5228
|
{
|
|
5177
5229
|
"name": "LiquidationRecord",
|
|
5178
5230
|
"fields": [
|
package/lib/index.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ export * from './factory/oracleClient';
|
|
|
29
29
|
export * from './factory/bigNum';
|
|
30
30
|
export * from './events/types';
|
|
31
31
|
export * from './events/eventSubscriber';
|
|
32
|
+
export * from './events/fetchLogs';
|
|
32
33
|
export * from './math/auction';
|
|
33
34
|
export * from './math/conversion';
|
|
34
35
|
export * from './math/funding';
|
package/lib/index.js
CHANGED
|
@@ -52,6 +52,7 @@ __exportStar(require("./factory/oracleClient"), exports);
|
|
|
52
52
|
__exportStar(require("./factory/bigNum"), exports);
|
|
53
53
|
__exportStar(require("./events/types"), exports);
|
|
54
54
|
__exportStar(require("./events/eventSubscriber"), exports);
|
|
55
|
+
__exportStar(require("./events/fetchLogs"), exports);
|
|
55
56
|
__exportStar(require("./math/auction"), exports);
|
|
56
57
|
__exportStar(require("./math/conversion"), exports);
|
|
57
58
|
__exportStar(require("./math/funding"), exports);
|
package/lib/math/amm.d.ts
CHANGED
|
@@ -33,9 +33,11 @@ export declare type AssetType = 'quote' | 'base';
|
|
|
33
33
|
* @returns quoteAssetReserve and baseAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
|
|
34
34
|
*/
|
|
35
35
|
export declare function calculateAmmReservesAfterSwap(amm: Pick<AMM, 'pegMultiplier' | 'quoteAssetReserve' | 'sqrtK' | 'baseAssetReserve'>, inputAssetType: AssetType, swapAmount: BN, swapDirection: SwapDirection): [BN, BN];
|
|
36
|
+
export declare function calculateMarketOpenBidAsk(baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): [BN, BN];
|
|
37
|
+
export declare function calculateInventoryScale(netBaseAssetAmount: BN, baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): number;
|
|
36
38
|
export declare function calculateEffectiveLeverage(baseSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, markPrice: BN, totalFeeMinusDistributions: BN): number;
|
|
37
39
|
export declare function calculateMaxSpread(marginRatioInitial: number): number;
|
|
38
|
-
export declare function calculateSpreadBN(baseSpread: number, lastOracleMarkSpreadPct: BN, lastOracleConfPct: BN, maxSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, markPrice: BN, totalFeeMinusDistributions: BN): [number, number];
|
|
40
|
+
export declare function calculateSpreadBN(baseSpread: number, lastOracleMarkSpreadPct: BN, lastOracleConfPct: BN, maxSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, markPrice: BN, totalFeeMinusDistributions: BN, baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): [number, number];
|
|
39
41
|
export declare function calculateSpread(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): number;
|
|
40
42
|
export declare function calculateSpreadReserves(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): {
|
|
41
43
|
baseAssetReserve: BN;
|
package/lib/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.calculateSpread = exports.calculateSpreadBN = exports.calculateMaxSpread = exports.calculateEffectiveLeverage = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = exports.calculateBidAskPrice = exports.calculateUpdatedAMMSpreadReserves = exports.calculateUpdatedAMM = exports.calculateNewAmm = exports.calculateOptimalPegAndBudget = exports.calculatePegFromTargetPrice = void 0;
|
|
3
|
+
exports.calculateMaxBaseAssetAmountFillable = exports.calculateQuoteAssetAmountSwapped = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateSpreadReserves = exports.calculateSpread = exports.calculateSpreadBN = exports.calculateMaxSpread = exports.calculateEffectiveLeverage = exports.calculateInventoryScale = 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("@project-serum/anchor");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const types_1 = require("../types");
|
|
@@ -165,6 +165,36 @@ function calculateAmmReservesAfterSwap(amm, inputAssetType, swapAmount, swapDire
|
|
|
165
165
|
return [newQuoteAssetReserve, newBaseAssetReserve];
|
|
166
166
|
}
|
|
167
167
|
exports.calculateAmmReservesAfterSwap = calculateAmmReservesAfterSwap;
|
|
168
|
+
function calculateMarketOpenBidAsk(baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve) {
|
|
169
|
+
// open orders
|
|
170
|
+
let openAsks;
|
|
171
|
+
if (maxBaseAssetReserve > baseAssetReserve) {
|
|
172
|
+
openAsks = maxBaseAssetReserve.sub(baseAssetReserve).mul(new anchor_1.BN(-1));
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
openAsks = numericConstants_1.ZERO;
|
|
176
|
+
}
|
|
177
|
+
let openBids;
|
|
178
|
+
if (minBaseAssetReserve < baseAssetReserve) {
|
|
179
|
+
openBids = baseAssetReserve.sub(minBaseAssetReserve);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
openBids = numericConstants_1.ZERO;
|
|
183
|
+
}
|
|
184
|
+
return [openBids, openAsks];
|
|
185
|
+
}
|
|
186
|
+
exports.calculateMarketOpenBidAsk = calculateMarketOpenBidAsk;
|
|
187
|
+
function calculateInventoryScale(netBaseAssetAmount, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve) {
|
|
188
|
+
// inventory skew
|
|
189
|
+
const [openBids, openAsks] = calculateMarketOpenBidAsk(baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve);
|
|
190
|
+
const totalLiquidity = anchor_1.BN.max(openBids.abs().add(openAsks.abs()), new anchor_1.BN(1));
|
|
191
|
+
const inventoryScale = anchor_1.BN.min(netBaseAssetAmount.abs(), totalLiquidity)
|
|
192
|
+
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION.mul(new anchor_1.BN(5)))
|
|
193
|
+
.div(totalLiquidity)
|
|
194
|
+
.toNumber() / numericConstants_1.BID_ASK_SPREAD_PRECISION.toNumber();
|
|
195
|
+
return inventoryScale;
|
|
196
|
+
}
|
|
197
|
+
exports.calculateInventoryScale = calculateInventoryScale;
|
|
168
198
|
function calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions) {
|
|
169
199
|
// inventory skew
|
|
170
200
|
const netBaseAssetValue = quoteAssetReserve
|
|
@@ -187,7 +217,7 @@ function calculateMaxSpread(marginRatioInitial) {
|
|
|
187
217
|
return maxTargetSpread;
|
|
188
218
|
}
|
|
189
219
|
exports.calculateMaxSpread = calculateMaxSpread;
|
|
190
|
-
function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPct, maxSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions) {
|
|
220
|
+
function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPct, maxSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve) {
|
|
191
221
|
let longSpread = baseSpread / 2;
|
|
192
222
|
let shortSpread = baseSpread / 2;
|
|
193
223
|
if (lastOracleMarkSpreadPct.gt(numericConstants_1.ZERO)) {
|
|
@@ -198,6 +228,14 @@ function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPc
|
|
|
198
228
|
}
|
|
199
229
|
const maxTargetSpread = maxSpread;
|
|
200
230
|
const MAX_INVENTORY_SKEW = 5;
|
|
231
|
+
const inventoryScale = calculateInventoryScale(netBaseAssetAmount, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve);
|
|
232
|
+
const inventorySpreadScale = Math.min(MAX_INVENTORY_SKEW, 1 + inventoryScale);
|
|
233
|
+
if (netBaseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
234
|
+
longSpread *= inventorySpreadScale;
|
|
235
|
+
}
|
|
236
|
+
else if (netBaseAssetAmount.lt(numericConstants_1.ZERO)) {
|
|
237
|
+
shortSpread *= inventorySpreadScale;
|
|
238
|
+
}
|
|
201
239
|
const effectiveLeverage = calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions);
|
|
202
240
|
if (totalFeeMinusDistributions.gt(numericConstants_1.ZERO)) {
|
|
203
241
|
const spreadScale = Math.min(MAX_INVENTORY_SKEW, 1 + effectiveLeverage);
|
|
@@ -240,7 +278,7 @@ function calculateSpread(amm, direction, oraclePriceData) {
|
|
|
240
278
|
const confIntervalPct = confInterval
|
|
241
279
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
242
280
|
.div(markPrice);
|
|
243
|
-
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.netBaseAssetAmount, markPrice, amm.totalFeeMinusDistributions);
|
|
281
|
+
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.netBaseAssetAmount, markPrice, amm.totalFeeMinusDistributions, amm.baseAssetReserve, amm.minBaseAssetReserve, amm.maxBaseAssetReserve);
|
|
244
282
|
let spread;
|
|
245
283
|
if ((0, types_1.isVariant)(direction, 'long')) {
|
|
246
284
|
spread = longSpread;
|
package/lib/types.d.ts
CHANGED
|
@@ -175,6 +175,27 @@ export declare type CurveRecord = {
|
|
|
175
175
|
oraclePrice: BN;
|
|
176
176
|
tradeId: BN;
|
|
177
177
|
};
|
|
178
|
+
export declare type LPRecord = {
|
|
179
|
+
ts: BN;
|
|
180
|
+
user: PublicKey;
|
|
181
|
+
action: LPAction;
|
|
182
|
+
nShares: BN;
|
|
183
|
+
marketIndex: BN;
|
|
184
|
+
deltaBaseAssetAmount: BN;
|
|
185
|
+
deltaQuoteAssetAmount: BN;
|
|
186
|
+
pnl: BN;
|
|
187
|
+
};
|
|
188
|
+
export declare class LPAction {
|
|
189
|
+
static readonly ADD_LIQUIDITY: {
|
|
190
|
+
addLiquidity: {};
|
|
191
|
+
};
|
|
192
|
+
static readonly REMOVE_LIQUIDITY: {
|
|
193
|
+
removeLiquidity: {};
|
|
194
|
+
};
|
|
195
|
+
static readonly SETTLE_LIQUIDITY: {
|
|
196
|
+
settleLiquidity: {};
|
|
197
|
+
};
|
|
198
|
+
}
|
|
178
199
|
export declare type FundingRateRecord = {
|
|
179
200
|
ts: BN;
|
|
180
201
|
recordId: BN;
|
|
@@ -243,6 +264,7 @@ export declare type LiquidatePerpRecord = {
|
|
|
243
264
|
oraclePrice: BN;
|
|
244
265
|
baseAssetAmount: BN;
|
|
245
266
|
quoteAssetAmount: BN;
|
|
267
|
+
lpShares: BN;
|
|
246
268
|
userPnl: BN;
|
|
247
269
|
liquidatorPnl: BN;
|
|
248
270
|
canceledOrdersFee: BN;
|
|
@@ -337,8 +359,6 @@ export declare type StateAccount = {
|
|
|
337
359
|
exchangePaused: boolean;
|
|
338
360
|
adminControlsPrices: boolean;
|
|
339
361
|
insuranceVault: PublicKey;
|
|
340
|
-
insuranceVaultAuthority: PublicKey;
|
|
341
|
-
insuranceVaultNonce: number;
|
|
342
362
|
marginRatioInitial: BN;
|
|
343
363
|
marginRatioMaintenance: BN;
|
|
344
364
|
marginRatioPartial: BN;
|
|
@@ -360,6 +380,8 @@ export declare type StateAccount = {
|
|
|
360
380
|
numberOfMarkets: BN;
|
|
361
381
|
numberOfBanks: BN;
|
|
362
382
|
minOrderQuoteAssetAmount: BN;
|
|
383
|
+
signer: PublicKey;
|
|
384
|
+
signerNonce: number;
|
|
363
385
|
maxAuctionDuration: number;
|
|
364
386
|
minAuctionDuration: number;
|
|
365
387
|
};
|
|
@@ -387,11 +409,7 @@ export declare type BankAccount = {
|
|
|
387
409
|
pubkey: PublicKey;
|
|
388
410
|
mint: PublicKey;
|
|
389
411
|
vault: PublicKey;
|
|
390
|
-
vaultAuthority: PublicKey;
|
|
391
|
-
vaultAuthorityNonce: number;
|
|
392
412
|
insuranceFundVault: PublicKey;
|
|
393
|
-
insuranceFundVaultAuthority: PublicKey;
|
|
394
|
-
insuranceFundVaultAuthorityNonce: number;
|
|
395
413
|
insuranceWithdrawEscrowPeriod: BN;
|
|
396
414
|
revenuePool: PoolBalance;
|
|
397
415
|
totalIfShares: BN;
|
|
@@ -455,6 +473,7 @@ export declare type AMM = {
|
|
|
455
473
|
cumulativeFeePerLp: BN;
|
|
456
474
|
cumulativeNetBaseAssetAmountPerLp: BN;
|
|
457
475
|
userLpShares: BN;
|
|
476
|
+
netUnsettledLpBaseAssetAmount: BN;
|
|
458
477
|
minimumQuoteAssetTradeSize: BN;
|
|
459
478
|
baseAssetAmountStepSize: BN;
|
|
460
479
|
maxBaseAssetAmountRatio: number;
|
package/lib/types.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DefaultOrderParams = exports.LiquidationType = exports.TradeSide = exports.isOneOfVariant = exports.isVariant = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.BankBalanceType = exports.SwapDirection = void 0;
|
|
3
|
+
exports.DefaultOrderParams = exports.LiquidationType = exports.LPAction = exports.TradeSide = exports.isOneOfVariant = exports.isVariant = exports.OrderTriggerCondition = exports.OrderActionExplanation = exports.OrderAction = exports.OrderDiscountTier = exports.OrderStatus = exports.OrderType = exports.OracleSource = exports.DepositDirection = exports.PositionDirection = exports.BankBalanceType = exports.SwapDirection = void 0;
|
|
4
4
|
const _1 = require(".");
|
|
5
5
|
// # Utility Types / Enums / Constants
|
|
6
6
|
class SwapDirection {
|
|
@@ -94,6 +94,12 @@ var TradeSide;
|
|
|
94
94
|
TradeSide[TradeSide["Buy"] = 1] = "Buy";
|
|
95
95
|
TradeSide[TradeSide["Sell"] = 2] = "Sell";
|
|
96
96
|
})(TradeSide = exports.TradeSide || (exports.TradeSide = {}));
|
|
97
|
+
class LPAction {
|
|
98
|
+
}
|
|
99
|
+
exports.LPAction = LPAction;
|
|
100
|
+
LPAction.ADD_LIQUIDITY = { addLiquidity: {} };
|
|
101
|
+
LPAction.REMOVE_LIQUIDITY = { removeLiquidity: {} };
|
|
102
|
+
LPAction.SETTLE_LIQUIDITY = { settleLiquidity: {} };
|
|
97
103
|
class LiquidationType {
|
|
98
104
|
}
|
|
99
105
|
exports.LiquidationType = LiquidationType;
|
package/package.json
CHANGED
package/src/addresses/pda.ts
CHANGED
|
@@ -176,3 +176,12 @@ export function getInsuranceFundStakeAccountPublicKey(
|
|
|
176
176
|
programId
|
|
177
177
|
)[0];
|
|
178
178
|
}
|
|
179
|
+
|
|
180
|
+
export function getClearingHouseSignerPublicKey(
|
|
181
|
+
programId: PublicKey
|
|
182
|
+
): PublicKey {
|
|
183
|
+
return anchor.web3.PublicKey.findProgramAddressSync(
|
|
184
|
+
[Buffer.from(anchor.utils.bytes.utf8.encode('clearing_house_signer'))],
|
|
185
|
+
programId
|
|
186
|
+
)[0];
|
|
187
|
+
}
|
package/src/admin.ts
CHANGED
|
@@ -13,12 +13,10 @@ import { BN } from '@project-serum/anchor';
|
|
|
13
13
|
import * as anchor from '@project-serum/anchor';
|
|
14
14
|
import {
|
|
15
15
|
getClearingHouseStateAccountPublicKeyAndNonce,
|
|
16
|
-
getBankVaultAuthorityPublicKey,
|
|
17
16
|
getBankPublicKey,
|
|
18
17
|
getBankVaultPublicKey,
|
|
19
18
|
getMarketPublicKey,
|
|
20
19
|
getInsuranceFundVaultPublicKey,
|
|
21
|
-
getInsuranceFundVaultAuthorityPublicKey,
|
|
22
20
|
} from './addresses/pda';
|
|
23
21
|
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
|
|
24
22
|
import { ClearingHouse } from './clearingHouse';
|
|
@@ -38,20 +36,16 @@ export class Admin extends ClearingHouse {
|
|
|
38
36
|
throw new Error('Clearing house already initialized');
|
|
39
37
|
}
|
|
40
38
|
|
|
39
|
+
const [clearingHouseStatePublicKey] =
|
|
40
|
+
await getClearingHouseStateAccountPublicKeyAndNonce(
|
|
41
|
+
this.program.programId
|
|
42
|
+
);
|
|
43
|
+
|
|
41
44
|
const [insuranceVaultPublicKey] = await PublicKey.findProgramAddress(
|
|
42
45
|
[Buffer.from(anchor.utils.bytes.utf8.encode('insurance_vault'))],
|
|
43
46
|
this.program.programId
|
|
44
47
|
);
|
|
45
48
|
|
|
46
|
-
const [insuranceVaultAuthority] = await PublicKey.findProgramAddress(
|
|
47
|
-
[insuranceVaultPublicKey.toBuffer()],
|
|
48
|
-
this.program.programId
|
|
49
|
-
);
|
|
50
|
-
|
|
51
|
-
const [clearingHouseStatePublicKey] =
|
|
52
|
-
await getClearingHouseStateAccountPublicKeyAndNonce(
|
|
53
|
-
this.program.programId
|
|
54
|
-
);
|
|
55
49
|
const initializeTx = await this.program.transaction.initialize(
|
|
56
50
|
adminControlsPrices,
|
|
57
51
|
{
|
|
@@ -59,9 +53,9 @@ export class Admin extends ClearingHouse {
|
|
|
59
53
|
admin: this.wallet.publicKey,
|
|
60
54
|
state: clearingHouseStatePublicKey,
|
|
61
55
|
quoteAssetMint: usdcMint,
|
|
62
|
-
insuranceVault: insuranceVaultPublicKey,
|
|
63
|
-
insuranceVaultAuthority: insuranceVaultAuthority,
|
|
64
56
|
rent: SYSVAR_RENT_PUBKEY,
|
|
57
|
+
insuranceVault: insuranceVaultPublicKey,
|
|
58
|
+
clearingHouseSigner: this.getSignerPublicKey(),
|
|
65
59
|
systemProgram: anchor.web3.SystemProgram.programId,
|
|
66
60
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
67
61
|
},
|
|
@@ -99,22 +93,11 @@ export class Admin extends ClearingHouse {
|
|
|
99
93
|
bankIndex
|
|
100
94
|
);
|
|
101
95
|
|
|
102
|
-
const bankVaultAuthority = await getBankVaultAuthorityPublicKey(
|
|
103
|
-
this.program.programId,
|
|
104
|
-
bankIndex
|
|
105
|
-
);
|
|
106
|
-
|
|
107
96
|
const insuranceFundVault = await getInsuranceFundVaultPublicKey(
|
|
108
97
|
this.program.programId,
|
|
109
98
|
bankIndex
|
|
110
99
|
);
|
|
111
100
|
|
|
112
|
-
const insuranceFundVaultAuthority =
|
|
113
|
-
await getInsuranceFundVaultAuthorityPublicKey(
|
|
114
|
-
this.program.programId,
|
|
115
|
-
bankIndex
|
|
116
|
-
);
|
|
117
|
-
|
|
118
101
|
const initializeTx = await this.program.transaction.initializeBank(
|
|
119
102
|
optimalUtilization,
|
|
120
103
|
optimalRate,
|
|
@@ -132,9 +115,8 @@ export class Admin extends ClearingHouse {
|
|
|
132
115
|
state: await this.getStatePublicKey(),
|
|
133
116
|
bank,
|
|
134
117
|
bankVault,
|
|
135
|
-
bankVaultAuthority,
|
|
136
118
|
insuranceFundVault,
|
|
137
|
-
|
|
119
|
+
clearingHouseSigner: this.getSignerPublicKey(),
|
|
138
120
|
bankMint: mint,
|
|
139
121
|
oracle,
|
|
140
122
|
rent: SYSVAR_RENT_PUBKEY,
|
|
@@ -355,7 +337,7 @@ export class Admin extends ClearingHouse {
|
|
|
355
337
|
state: await this.getStatePublicKey(),
|
|
356
338
|
bank: bank.pubkey,
|
|
357
339
|
insuranceVault: state.insuranceVault,
|
|
358
|
-
|
|
340
|
+
clearingHouseSigner: this.getSignerPublicKey(),
|
|
359
341
|
recipient: recipient,
|
|
360
342
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
361
343
|
},
|
|
@@ -379,7 +361,7 @@ export class Admin extends ClearingHouse {
|
|
|
379
361
|
market: marketPublicKey,
|
|
380
362
|
bank: bank.pubkey,
|
|
381
363
|
bankVault: bank.vault,
|
|
382
|
-
|
|
364
|
+
clearingHouseSigner: this.getSignerPublicKey(),
|
|
383
365
|
recipient: recipient,
|
|
384
366
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
385
367
|
},
|
|
@@ -399,10 +381,9 @@ export class Admin extends ClearingHouse {
|
|
|
399
381
|
state: await this.getStatePublicKey(),
|
|
400
382
|
market: await getMarketPublicKey(this.program.programId, marketIndex),
|
|
401
383
|
insuranceVault: state.insuranceVault,
|
|
402
|
-
|
|
384
|
+
clearingHouseSigner: this.getSignerPublicKey(),
|
|
403
385
|
bank: bank.pubkey,
|
|
404
386
|
bankVault: bank.vault,
|
|
405
|
-
bankVaultAuthority: bank.vaultAuthority,
|
|
406
387
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
407
388
|
},
|
|
408
389
|
});
|
package/src/clearingHouse.ts
CHANGED
|
@@ -39,6 +39,7 @@ import { TokenFaucet } from './tokenFaucet';
|
|
|
39
39
|
import { EventEmitter } from 'events';
|
|
40
40
|
import StrictEventEmitter from 'strict-event-emitter-types';
|
|
41
41
|
import {
|
|
42
|
+
getClearingHouseSignerPublicKey,
|
|
42
43
|
getClearingHouseStateAccountPublicKey,
|
|
43
44
|
getInsuranceFundStakeAccountPublicKey,
|
|
44
45
|
getMarketPublicKey,
|
|
@@ -260,6 +261,17 @@ export class ClearingHouse {
|
|
|
260
261
|
return this.statePublicKey;
|
|
261
262
|
}
|
|
262
263
|
|
|
264
|
+
signerPublicKey?: PublicKey;
|
|
265
|
+
public getSignerPublicKey(): PublicKey {
|
|
266
|
+
if (this.signerPublicKey) {
|
|
267
|
+
return this.signerPublicKey;
|
|
268
|
+
}
|
|
269
|
+
this.signerPublicKey = getClearingHouseSignerPublicKey(
|
|
270
|
+
this.program.programId
|
|
271
|
+
);
|
|
272
|
+
return this.signerPublicKey;
|
|
273
|
+
}
|
|
274
|
+
|
|
263
275
|
public getStateAccount(): StateAccount {
|
|
264
276
|
return this.accountSubscriber.getStateAccountAndSlot().data;
|
|
265
277
|
}
|
|
@@ -1064,7 +1076,7 @@ export class ClearingHouse {
|
|
|
1064
1076
|
state: await this.getStatePublicKey(),
|
|
1065
1077
|
bank: bank.pubkey,
|
|
1066
1078
|
bankVault: bank.vault,
|
|
1067
|
-
|
|
1079
|
+
clearingHouseSigner: this.getSignerPublicKey(),
|
|
1068
1080
|
user: userAccountPublicKey,
|
|
1069
1081
|
userStats: this.getUserStatsAccountPublicKey(),
|
|
1070
1082
|
userTokenAccount: userTokenAccount,
|
|
@@ -2221,7 +2233,7 @@ export class ClearingHouse {
|
|
|
2221
2233
|
liquidator: liquidatorPublicKey,
|
|
2222
2234
|
bankVault: bank.vault,
|
|
2223
2235
|
insuranceFundVault: bank.insuranceFundVault,
|
|
2224
|
-
|
|
2236
|
+
clearingHouseSigner: this.getSignerPublicKey(),
|
|
2225
2237
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
2226
2238
|
},
|
|
2227
2239
|
remainingAccounts: remainingAccounts,
|
|
@@ -2270,7 +2282,7 @@ export class ClearingHouse {
|
|
|
2270
2282
|
liquidator: liquidatorPublicKey,
|
|
2271
2283
|
bankVault: bank.vault,
|
|
2272
2284
|
insuranceFundVault: bank.insuranceFundVault,
|
|
2273
|
-
|
|
2285
|
+
clearingHouseSigner: this.getSignerPublicKey(),
|
|
2274
2286
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
2275
2287
|
},
|
|
2276
2288
|
remainingAccounts: remainingAccounts,
|
|
@@ -2659,7 +2671,7 @@ export class ClearingHouse {
|
|
|
2659
2671
|
userStats: this.getUserStatsAccountPublicKey(),
|
|
2660
2672
|
authority: this.wallet.publicKey,
|
|
2661
2673
|
insuranceFundVault: bank.insuranceFundVault,
|
|
2662
|
-
|
|
2674
|
+
clearingHouseSigner: this.getSignerPublicKey(),
|
|
2663
2675
|
userTokenAccount: collateralAccountPublicKey,
|
|
2664
2676
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
2665
2677
|
},
|
|
@@ -2681,7 +2693,7 @@ export class ClearingHouse {
|
|
|
2681
2693
|
state: await this.getStatePublicKey(),
|
|
2682
2694
|
bank: bank.pubkey,
|
|
2683
2695
|
bankVault: bank.vault,
|
|
2684
|
-
|
|
2696
|
+
clearingHouseSigner: this.getSignerPublicKey(),
|
|
2685
2697
|
insuranceFundVault: bank.insuranceFundVault,
|
|
2686
2698
|
tokenProgram: TOKEN_PROGRAM_ID,
|
|
2687
2699
|
},
|