@drift-labs/sdk 0.1.28 → 0.1.29
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.js +13 -3
- package/lib/clearingHouse.d.ts +4 -1
- package/lib/clearingHouse.js +49 -0
- package/lib/constants/markets.js +11 -0
- package/lib/idl/clearing_house.json +63 -2
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/amm.d.ts +0 -20
- package/lib/math/amm.js +1 -151
- package/lib/math/repeg.d.ts +32 -0
- package/lib/math/repeg.js +178 -0
- package/lib/oracles/pythClient.js +1 -1
- package/lib/orderParams.d.ts +1 -1
- package/lib/orderParams.js +2 -2
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +14 -4
- package/src/clearingHouse.ts +71 -0
- package/src/constants/markets.ts +12 -1
- package/src/idl/clearing_house.json +63 -2
- package/src/index.ts +1 -0
- package/src/math/amm.ts +1 -212
- package/src/math/repeg.ts +253 -0
- package/src/oracles/pythClient.ts +1 -1
- package/src/orderParams.ts +3 -2
- package/src/accounts/bulkAccountLoader.js +0 -180
- package/src/accounts/bulkAccountLoader.js.map +0 -1
- package/src/accounts/bulkUserSubscription.js +0 -56
- package/src/accounts/bulkUserSubscription.js.map +0 -1
- package/src/accounts/pollingUserAccountSubscriber.js +0 -139
- package/src/accounts/pollingUserAccountSubscriber.js.map +0 -1
- package/src/accounts/types.js +0 -11
- package/src/accounts/types.js.map +0 -1
- package/src/accounts/webSocketUserAccountSubscriber.js +0 -78
- package/src/accounts/webSocketUserAccountSubscriber.js.map +0 -1
- package/src/addresses.js +0 -59
- package/src/addresses.js.map +0 -1
- package/src/admin.js +0 -443
- package/src/admin.js.map +0 -1
- package/src/clearingHouseUser.js +0 -581
- package/src/clearingHouseUser.js.map +0 -1
- package/src/config.js +0 -37
- package/src/config.js.map +0 -1
- package/src/factory/clearingHouse.js +0 -65
- package/src/factory/clearingHouse.js.map +0 -1
- package/src/factory/clearingHouseUser.js +0 -35
- package/src/factory/clearingHouseUser.js.map +0 -1
- package/src/factory/oracleClient.js +0 -17
- package/src/factory/oracleClient.js.map +0 -1
- package/src/index.js +0 -56
- package/src/index.js.map +0 -1
- package/src/math/amm.js +0 -285
- package/src/math/amm.js.map +0 -1
- package/src/mockUSDCFaucet.js +0 -143
- package/src/mockUSDCFaucet.js.map +0 -1
- package/src/oracles/pythClient.js +0 -39
- package/src/oracles/pythClient.js.map +0 -1
- package/src/orderParams.js +0 -109
- package/src/orderParams.js.map +0 -1
- package/src/orders.js +0 -172
- package/src/orders.js.map +0 -1
- package/src/types.js +0 -61
- package/src/types.js.map +0 -1
- package/src/wallet.js +0 -23
- package/src/wallet.js.map +0 -1
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateBudgetedPeg = exports.calculateBudgetedK = exports.calculateReserveRebalanceCost = exports.calculateRepegCost = exports.calculateAdjustKCost = void 0;
|
|
4
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
5
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
+
const position_1 = require("./position");
|
|
7
|
+
const amm_1 = require("./amm");
|
|
8
|
+
const __1 = require("..");
|
|
9
|
+
/**
|
|
10
|
+
* Helper function calculating adjust k cost
|
|
11
|
+
* @param market
|
|
12
|
+
* @param marketIndex
|
|
13
|
+
* @param numerator
|
|
14
|
+
* @param denomenator
|
|
15
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
16
|
+
*/
|
|
17
|
+
function calculateAdjustKCost(market, marketIndex, numerator, denomenator) {
|
|
18
|
+
const netUserPosition = {
|
|
19
|
+
baseAssetAmount: market.baseAssetAmount,
|
|
20
|
+
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
21
|
+
marketIndex: new anchor_1.BN(marketIndex),
|
|
22
|
+
quoteAssetAmount: numericConstants_1.ZERO,
|
|
23
|
+
openOrders: numericConstants_1.ZERO,
|
|
24
|
+
};
|
|
25
|
+
const currentValue = (0, position_1.calculateBaseAssetValue)(market, netUserPosition);
|
|
26
|
+
const marketNewK = Object.assign({}, market);
|
|
27
|
+
marketNewK.amm = Object.assign({}, market.amm);
|
|
28
|
+
marketNewK.amm.baseAssetReserve = market.amm.baseAssetReserve
|
|
29
|
+
.mul(numerator)
|
|
30
|
+
.div(denomenator);
|
|
31
|
+
marketNewK.amm.quoteAssetReserve = market.amm.quoteAssetReserve
|
|
32
|
+
.mul(numerator)
|
|
33
|
+
.div(denomenator);
|
|
34
|
+
marketNewK.amm.sqrtK = market.amm.sqrtK.mul(numerator).div(denomenator);
|
|
35
|
+
netUserPosition.quoteAssetAmount = currentValue;
|
|
36
|
+
const cost = (0, __1.calculatePositionPNL)(marketNewK, netUserPosition);
|
|
37
|
+
return cost;
|
|
38
|
+
}
|
|
39
|
+
exports.calculateAdjustKCost = calculateAdjustKCost;
|
|
40
|
+
/**
|
|
41
|
+
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
42
|
+
*
|
|
43
|
+
* @param market
|
|
44
|
+
* @param marketIndex
|
|
45
|
+
* @param newPeg
|
|
46
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
47
|
+
*/
|
|
48
|
+
function calculateRepegCost(market, marketIndex, newPeg) {
|
|
49
|
+
const netUserPosition = {
|
|
50
|
+
baseAssetAmount: market.baseAssetAmount,
|
|
51
|
+
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
52
|
+
marketIndex: new anchor_1.BN(marketIndex),
|
|
53
|
+
quoteAssetAmount: new anchor_1.BN(0),
|
|
54
|
+
openOrders: numericConstants_1.ZERO,
|
|
55
|
+
};
|
|
56
|
+
const currentValue = (0, position_1.calculateBaseAssetValue)(market, netUserPosition);
|
|
57
|
+
netUserPosition.quoteAssetAmount = currentValue;
|
|
58
|
+
const prevMarketPrice = (0, __1.calculateMarkPrice)(market);
|
|
59
|
+
const marketNewPeg = Object.assign({}, market);
|
|
60
|
+
marketNewPeg.amm = Object.assign({}, market.amm);
|
|
61
|
+
// const marketNewPeg = JSON.parse(JSON.stringify(market));
|
|
62
|
+
marketNewPeg.amm.pegMultiplier = newPeg;
|
|
63
|
+
console.log('Price moves from', (0, __1.convertToNumber)(prevMarketPrice), 'to', (0, __1.convertToNumber)((0, __1.calculateMarkPrice)(marketNewPeg)));
|
|
64
|
+
const cost = (0, __1.calculatePositionPNL)(marketNewPeg, netUserPosition);
|
|
65
|
+
return cost;
|
|
66
|
+
}
|
|
67
|
+
exports.calculateRepegCost = calculateRepegCost;
|
|
68
|
+
/**
|
|
69
|
+
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
70
|
+
*
|
|
71
|
+
* @param market
|
|
72
|
+
* @param marketIndex
|
|
73
|
+
* @param newPeg
|
|
74
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
75
|
+
*/
|
|
76
|
+
function calculateReserveRebalanceCost(market, marketIndex) {
|
|
77
|
+
const netUserPosition = {
|
|
78
|
+
baseAssetAmount: market.baseAssetAmount,
|
|
79
|
+
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
80
|
+
marketIndex: new anchor_1.BN(marketIndex),
|
|
81
|
+
quoteAssetAmount: new anchor_1.BN(0),
|
|
82
|
+
openOrders: numericConstants_1.ZERO,
|
|
83
|
+
};
|
|
84
|
+
const currentValue = (0, position_1.calculateBaseAssetValue)(market, netUserPosition);
|
|
85
|
+
netUserPosition.quoteAssetAmount = currentValue;
|
|
86
|
+
const prevMarketPrice = (0, __1.calculateMarkPrice)(market);
|
|
87
|
+
const marketNewPeg = Object.assign({}, market);
|
|
88
|
+
marketNewPeg.amm = Object.assign({}, market.amm);
|
|
89
|
+
// const marketNewPeg = JSON.parse(JSON.stringify(market));
|
|
90
|
+
const newPeg = (0, amm_1.calculateTerminalPrice)(market)
|
|
91
|
+
.mul(numericConstants_1.PEG_PRECISION)
|
|
92
|
+
.div(numericConstants_1.MARK_PRICE_PRECISION);
|
|
93
|
+
// const newPeg = prevMarketPrice.mul(PEG_PRECISION).div(MARK_PRICE_PRECISION);
|
|
94
|
+
const newBaseReserve = market.amm.baseAssetReserve.add(market.baseAssetAmount);
|
|
95
|
+
const newQuoteReserve = market.amm.sqrtK
|
|
96
|
+
.mul(market.amm.sqrtK)
|
|
97
|
+
.div(newBaseReserve);
|
|
98
|
+
console.log('current reserves on close, quote:', (0, __1.convertToNumber)(newQuoteReserve, numericConstants_1.AMM_RESERVE_PRECISION), 'base:', (0, __1.convertToNumber)(newBaseReserve, numericConstants_1.AMM_RESERVE_PRECISION));
|
|
99
|
+
let newSqrtK;
|
|
100
|
+
if (newPeg.lt(market.amm.pegMultiplier)) {
|
|
101
|
+
newSqrtK = newBaseReserve;
|
|
102
|
+
}
|
|
103
|
+
else {
|
|
104
|
+
newSqrtK = newQuoteReserve;
|
|
105
|
+
}
|
|
106
|
+
marketNewPeg.amm.baseAssetReserve = newSqrtK.sub(market.baseAssetAmount); // newSqrtK.sub(market.baseAssetAmount);
|
|
107
|
+
marketNewPeg.amm.quoteAssetReserve = newSqrtK
|
|
108
|
+
.mul(newSqrtK)
|
|
109
|
+
.div(marketNewPeg.amm.baseAssetReserve);
|
|
110
|
+
marketNewPeg.amm.sqrtK = newSqrtK;
|
|
111
|
+
marketNewPeg.amm.pegMultiplier = newPeg;
|
|
112
|
+
console.log('Price moves from', (0, __1.convertToNumber)(prevMarketPrice), 'to', (0, __1.convertToNumber)((0, __1.calculateMarkPrice)(marketNewPeg)));
|
|
113
|
+
const cost = (0, __1.calculatePositionPNL)(marketNewPeg, netUserPosition);
|
|
114
|
+
return cost;
|
|
115
|
+
}
|
|
116
|
+
exports.calculateReserveRebalanceCost = calculateReserveRebalanceCost;
|
|
117
|
+
function calculateBudgetedK(market, cost) {
|
|
118
|
+
// wolframalpha.com
|
|
119
|
+
// (1/(x+d) - p/(x*p+d))*y*d*Q = C solve for p
|
|
120
|
+
// p = (d(y*d*Q - C(x+d))) / (C*x(x+d) + y*y*d*Q)
|
|
121
|
+
// todo: assumes k = x * y
|
|
122
|
+
// otherwise use: (y(1-p) + (kp^2/(x*p+d)) - k/(x+d)) * Q = C solve for p
|
|
123
|
+
// const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
124
|
+
const x = market.amm.baseAssetReserve;
|
|
125
|
+
const y = market.amm.quoteAssetReserve;
|
|
126
|
+
const d = market.baseAssetAmount;
|
|
127
|
+
const Q = market.amm.pegMultiplier;
|
|
128
|
+
const C = cost.mul(new anchor_1.BN(-1));
|
|
129
|
+
const numer1 = y.mul(d).mul(Q).div(numericConstants_1.AMM_RESERVE_PRECISION).div(numericConstants_1.PEG_PRECISION);
|
|
130
|
+
const numer2 = C.mul(x.add(d)).div(numericConstants_1.QUOTE_PRECISION);
|
|
131
|
+
const denom1 = C.mul(x)
|
|
132
|
+
.mul(x.add(d))
|
|
133
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
134
|
+
.div(numericConstants_1.QUOTE_PRECISION);
|
|
135
|
+
const denom2 = y
|
|
136
|
+
.mul(d)
|
|
137
|
+
.mul(d)
|
|
138
|
+
.mul(Q)
|
|
139
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
140
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
141
|
+
.div(numericConstants_1.PEG_PRECISION);
|
|
142
|
+
const numerator = d
|
|
143
|
+
.mul(numer1.add(numer2))
|
|
144
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
145
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
146
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
147
|
+
const denominator = denom1
|
|
148
|
+
.add(denom2)
|
|
149
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
150
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
151
|
+
console.log(numerator, denominator);
|
|
152
|
+
// const p = (numerator).div(denominator);
|
|
153
|
+
// const formulaCost = (numer21.sub(numer20).sub(numer1)).mul(market.amm.pegMultiplier).div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO)
|
|
154
|
+
// console.log(convertToNumber(formulaCost, QUOTE_PRECISION))
|
|
155
|
+
return [numerator, denominator];
|
|
156
|
+
}
|
|
157
|
+
exports.calculateBudgetedK = calculateBudgetedK;
|
|
158
|
+
function calculateBudgetedPeg(market, cost) {
|
|
159
|
+
// wolframalpha.com
|
|
160
|
+
// (1/(x+d) - p/(x*p+d))*y*d*Q = C solve for p
|
|
161
|
+
// p = (d(y*d*Q - C(x+d))) / (C*x(x+d) + y*y*d*Q)
|
|
162
|
+
// todo: assumes k = x * y
|
|
163
|
+
// otherwise use: (y(1-p) + (kp^2/(x*p+d)) - k/(x+d)) * Q = C solve for p
|
|
164
|
+
const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
165
|
+
const x = market.amm.baseAssetReserve;
|
|
166
|
+
const y = market.amm.quoteAssetReserve;
|
|
167
|
+
const d = market.baseAssetAmount;
|
|
168
|
+
const Q = market.amm.pegMultiplier;
|
|
169
|
+
const C = cost.mul(new anchor_1.BN(-1));
|
|
170
|
+
const deltaQuoteAssetReserves = y.sub(k.div(x.add(d)));
|
|
171
|
+
const deltaPegMultiplier = C.mul(numericConstants_1.MARK_PRICE_PRECISION).div(deltaQuoteAssetReserves.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO));
|
|
172
|
+
// .mul(PEG_PRECISION)
|
|
173
|
+
// .div(QUOTE_PRECISION);
|
|
174
|
+
console.log(Q.toNumber(), 'change by', deltaPegMultiplier.toNumber() / numericConstants_1.MARK_PRICE_PRECISION.toNumber());
|
|
175
|
+
const newPeg = Q.sub(deltaPegMultiplier.mul(numericConstants_1.PEG_PRECISION).div(numericConstants_1.MARK_PRICE_PRECISION));
|
|
176
|
+
return newPeg;
|
|
177
|
+
}
|
|
178
|
+
exports.calculateBudgetedPeg = calculateBudgetedPeg;
|
|
@@ -33,7 +33,7 @@ class PythClient {
|
|
|
33
33
|
return __awaiter(this, void 0, void 0, function* () {
|
|
34
34
|
const priceData = (0, client_1.parsePriceData)(buffer);
|
|
35
35
|
return {
|
|
36
|
-
price: convertPythPrice(priceData.price, priceData.exponent),
|
|
36
|
+
price: convertPythPrice(priceData.aggregate.price, priceData.exponent),
|
|
37
37
|
slot: new anchor_1.BN(priceData.lastSlot.toString()),
|
|
38
38
|
confidence: convertPythPrice(priceData.confidence, priceData.exponent),
|
|
39
39
|
twap: convertPythPrice(priceData.twap.value, priceData.exponent),
|
package/lib/orderParams.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { OrderParams, OrderTriggerCondition, PositionDirection } from './types';
|
|
3
3
|
import { BN } from '@project-serum/anchor';
|
|
4
|
-
export declare function getLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number, postOnly?: boolean, oraclePriceOffset?: BN): OrderParams;
|
|
4
|
+
export declare function getLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number, postOnly?: boolean, oraclePriceOffset?: BN, immediateOrCancel?: boolean): OrderParams;
|
|
5
5
|
export declare function getTriggerMarketOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number): OrderParams;
|
|
6
6
|
export declare function getTriggerLimitOrderParams(marketIndex: BN, direction: PositionDirection, baseAssetAmount: BN, price: BN, triggerPrice: BN, triggerCondition: OrderTriggerCondition, reduceOnly: boolean, discountToken?: boolean, referrer?: boolean, userOrderId?: number): OrderParams;
|
|
7
7
|
export declare function getMarketOrderParams(marketIndex: BN, direction: PositionDirection, quoteAssetAmount: BN, baseAssetAmount: BN, reduceOnly: boolean, price?: BN, discountToken?: boolean, referrer?: boolean): OrderParams;
|
package/lib/orderParams.js
CHANGED
|
@@ -3,7 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
|
|
4
4
|
const types_1 = require("./types");
|
|
5
5
|
const numericConstants_1 = require("./constants/numericConstants");
|
|
6
|
-
function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false, userOrderId = 0, postOnly = false, oraclePriceOffset = numericConstants_1.ZERO) {
|
|
6
|
+
function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, reduceOnly, discountToken = false, referrer = false, userOrderId = 0, postOnly = false, oraclePriceOffset = numericConstants_1.ZERO, immediateOrCancel = false) {
|
|
7
7
|
return {
|
|
8
8
|
orderType: types_1.OrderType.LIMIT,
|
|
9
9
|
userOrderId,
|
|
@@ -14,7 +14,7 @@ function getLimitOrderParams(marketIndex, direction, baseAssetAmount, price, red
|
|
|
14
14
|
price,
|
|
15
15
|
reduceOnly,
|
|
16
16
|
postOnly,
|
|
17
|
-
immediateOrCancel
|
|
17
|
+
immediateOrCancel,
|
|
18
18
|
positionLimit: numericConstants_1.ZERO,
|
|
19
19
|
padding0: true,
|
|
20
20
|
padding1: numericConstants_1.ZERO,
|
package/package.json
CHANGED
|
@@ -9,6 +9,7 @@ type AccountToLoad = {
|
|
|
9
9
|
|
|
10
10
|
const GET_MULTIPLE_ACCOUNTS_CHUNK_SIZE = 99;
|
|
11
11
|
|
|
12
|
+
const oneMinute = 60 * 1000;
|
|
12
13
|
const fiveMinutes = 5 * 60 * 1000;
|
|
13
14
|
|
|
14
15
|
export class BulkAccountLoader {
|
|
@@ -104,10 +105,6 @@ export class BulkAccountLoader {
|
|
|
104
105
|
this.loadPromiseResolver = resolver;
|
|
105
106
|
});
|
|
106
107
|
|
|
107
|
-
if (this.loggingEnabled) {
|
|
108
|
-
console.log('Loading accounts');
|
|
109
|
-
}
|
|
110
|
-
|
|
111
108
|
try {
|
|
112
109
|
const chunks = this.chunks(
|
|
113
110
|
Array.from(this.accountsToLoad.values()),
|
|
@@ -163,6 +160,11 @@ export class BulkAccountLoader {
|
|
|
163
160
|
args
|
|
164
161
|
);
|
|
165
162
|
|
|
163
|
+
const oneMinuteSinceLastUpdate = Date.now() - this.lastUpdate > oneMinute;
|
|
164
|
+
if (this.loggingEnabled && oneMinuteSinceLastUpdate) {
|
|
165
|
+
console.log('rpcResponse', JSON.stringify(rpcResponse));
|
|
166
|
+
}
|
|
167
|
+
|
|
166
168
|
const newSlot = rpcResponse.result.context.slot;
|
|
167
169
|
|
|
168
170
|
for (const i in accountsToLoad) {
|
|
@@ -177,6 +179,10 @@ export class BulkAccountLoader {
|
|
|
177
179
|
newBuffer = Buffer.from(raw, dataType);
|
|
178
180
|
}
|
|
179
181
|
|
|
182
|
+
if (this.loggingEnabled && oneMinuteSinceLastUpdate) {
|
|
183
|
+
console.log('oldRPCResponse', oldRPCResponse);
|
|
184
|
+
}
|
|
185
|
+
|
|
180
186
|
if (!oldRPCResponse) {
|
|
181
187
|
this.accountData.set(key, {
|
|
182
188
|
slot: newSlot,
|
|
@@ -199,6 +205,10 @@ export class BulkAccountLoader {
|
|
|
199
205
|
});
|
|
200
206
|
this.handleAccountCallbacks(accountToLoad, newBuffer);
|
|
201
207
|
this.lastUpdate = Date.now();
|
|
208
|
+
} else if (this.loggingEnabled) {
|
|
209
|
+
console.log('unable to update account for newest slot');
|
|
210
|
+
console.log('oldBuffer', oldBuffer);
|
|
211
|
+
console.log('newBuffer', newBuffer);
|
|
202
212
|
}
|
|
203
213
|
}
|
|
204
214
|
}
|
package/src/clearingHouse.ts
CHANGED
|
@@ -21,6 +21,7 @@ import {
|
|
|
21
21
|
OrderParams,
|
|
22
22
|
Order,
|
|
23
23
|
ExtendedCurveHistoryAccount,
|
|
24
|
+
UserPositionsAccount,
|
|
24
25
|
} from './types';
|
|
25
26
|
import * as anchor from '@project-serum/anchor';
|
|
26
27
|
import clearingHouseIDL from './idl/clearing_house.json';
|
|
@@ -922,6 +923,51 @@ export class ClearingHouse {
|
|
|
922
923
|
});
|
|
923
924
|
}
|
|
924
925
|
|
|
926
|
+
public async cancelAllOrders(
|
|
927
|
+
oracles?: PublicKey[]
|
|
928
|
+
): Promise<TransactionSignature> {
|
|
929
|
+
return await this.txSender.send(
|
|
930
|
+
wrapInTx(await this.getCancelAllOrdersIx(oracles)),
|
|
931
|
+
[],
|
|
932
|
+
this.opts
|
|
933
|
+
);
|
|
934
|
+
}
|
|
935
|
+
|
|
936
|
+
public async getCancelAllOrdersIx(
|
|
937
|
+
oracles: PublicKey[]
|
|
938
|
+
): Promise<TransactionInstruction> {
|
|
939
|
+
const userAccountPublicKey = await this.getUserAccountPublicKey();
|
|
940
|
+
const userAccount = await this.getUserAccount();
|
|
941
|
+
|
|
942
|
+
const state = this.getStateAccount();
|
|
943
|
+
const orderState = this.getOrderStateAccount();
|
|
944
|
+
|
|
945
|
+
const remainingAccounts = [];
|
|
946
|
+
for (const oracle of oracles) {
|
|
947
|
+
remainingAccounts.push({
|
|
948
|
+
pubkey: oracle,
|
|
949
|
+
isWritable: false,
|
|
950
|
+
isSigner: false,
|
|
951
|
+
});
|
|
952
|
+
}
|
|
953
|
+
|
|
954
|
+
return await this.program.instruction.cancelAllOrders({
|
|
955
|
+
accounts: {
|
|
956
|
+
state: await this.getStatePublicKey(),
|
|
957
|
+
user: userAccountPublicKey,
|
|
958
|
+
authority: this.wallet.publicKey,
|
|
959
|
+
markets: state.markets,
|
|
960
|
+
userOrders: await this.getUserOrdersAccountPublicKey(),
|
|
961
|
+
userPositions: userAccount.positions,
|
|
962
|
+
fundingPaymentHistory: state.fundingPaymentHistory,
|
|
963
|
+
fundingRateHistory: state.fundingRateHistory,
|
|
964
|
+
orderState: await this.getOrderStatePublicKey(),
|
|
965
|
+
orderHistory: orderState.orderHistory,
|
|
966
|
+
},
|
|
967
|
+
remainingAccounts,
|
|
968
|
+
});
|
|
969
|
+
}
|
|
970
|
+
|
|
925
971
|
public async fillOrder(
|
|
926
972
|
userAccountPublicKey: PublicKey,
|
|
927
973
|
userOrdersAccountPublicKey: PublicKey,
|
|
@@ -1160,6 +1206,31 @@ export class ClearingHouse {
|
|
|
1160
1206
|
);
|
|
1161
1207
|
}
|
|
1162
1208
|
|
|
1209
|
+
public async closeAllPositions(
|
|
1210
|
+
userPositionsAccount: UserPositionsAccount,
|
|
1211
|
+
discountToken?: PublicKey,
|
|
1212
|
+
referrer?: PublicKey
|
|
1213
|
+
): Promise<TransactionSignature> {
|
|
1214
|
+
const ixs: TransactionInstruction[] = [];
|
|
1215
|
+
for (const userPosition of userPositionsAccount.positions) {
|
|
1216
|
+
if (userPosition.baseAssetAmount.eq(ZERO)) {
|
|
1217
|
+
continue;
|
|
1218
|
+
}
|
|
1219
|
+
|
|
1220
|
+
ixs.push(
|
|
1221
|
+
await this.getClosePositionIx(
|
|
1222
|
+
userPosition.marketIndex,
|
|
1223
|
+
discountToken,
|
|
1224
|
+
referrer
|
|
1225
|
+
)
|
|
1226
|
+
);
|
|
1227
|
+
}
|
|
1228
|
+
|
|
1229
|
+
const tx = new Transaction().add(...ixs);
|
|
1230
|
+
|
|
1231
|
+
return this.txSender.send(tx, [], this.opts);
|
|
1232
|
+
}
|
|
1233
|
+
|
|
1163
1234
|
public async liquidate(
|
|
1164
1235
|
liquidateeUserAccountPublicKey: PublicKey
|
|
1165
1236
|
): Promise<TransactionSignature> {
|
package/src/constants/markets.ts
CHANGED
|
@@ -186,7 +186,18 @@ export const Markets: MarketConfig[] = [
|
|
|
186
186
|
mainnetPublicKey: 'FsSM3s38PX9K7Dn6eGzuE29S2Dsk1Sss1baytTQdCaQj',
|
|
187
187
|
launchTs: 1648607439000,
|
|
188
188
|
oracleSource: OracleSource.PYTH,
|
|
189
|
-
},
|
|
189
|
+
},
|
|
190
|
+
{
|
|
191
|
+
fullName: 'Near',
|
|
192
|
+
category: ['L1', 'Infra'],
|
|
193
|
+
symbol: 'NEAR-PERP',
|
|
194
|
+
baseAssetSymbol: 'NEAR',
|
|
195
|
+
marketIndex: new BN(16),
|
|
196
|
+
devnetPublicKey: '3gnSbT7bhoTdGkFVZc1dW1PvjreWzpUNUD5ppXwv1N59',
|
|
197
|
+
mainnetPublicKey: 'ECSFWQ1bnnpqPVvoy9237t2wddZAaHisW88mYxuEHKWf',
|
|
198
|
+
launchTs: 1649105516000,
|
|
199
|
+
oracleSource: OracleSource.PYTH,
|
|
200
|
+
},
|
|
190
201
|
// {
|
|
191
202
|
// symbol: 'mSOL-PERP',
|
|
192
203
|
// baseAssetSymbol: 'mSOL',
|
|
@@ -677,6 +677,57 @@
|
|
|
677
677
|
}
|
|
678
678
|
]
|
|
679
679
|
},
|
|
680
|
+
{
|
|
681
|
+
"name": "cancelAllOrders",
|
|
682
|
+
"accounts": [
|
|
683
|
+
{
|
|
684
|
+
"name": "state",
|
|
685
|
+
"isMut": false,
|
|
686
|
+
"isSigner": false
|
|
687
|
+
},
|
|
688
|
+
{
|
|
689
|
+
"name": "orderState",
|
|
690
|
+
"isMut": false,
|
|
691
|
+
"isSigner": false
|
|
692
|
+
},
|
|
693
|
+
{
|
|
694
|
+
"name": "user",
|
|
695
|
+
"isMut": false,
|
|
696
|
+
"isSigner": false
|
|
697
|
+
},
|
|
698
|
+
{
|
|
699
|
+
"name": "authority",
|
|
700
|
+
"isMut": false,
|
|
701
|
+
"isSigner": true
|
|
702
|
+
},
|
|
703
|
+
{
|
|
704
|
+
"name": "markets",
|
|
705
|
+
"isMut": false,
|
|
706
|
+
"isSigner": false
|
|
707
|
+
},
|
|
708
|
+
{
|
|
709
|
+
"name": "userPositions",
|
|
710
|
+
"isMut": true,
|
|
711
|
+
"isSigner": false
|
|
712
|
+
},
|
|
713
|
+
{
|
|
714
|
+
"name": "userOrders",
|
|
715
|
+
"isMut": true,
|
|
716
|
+
"isSigner": false
|
|
717
|
+
},
|
|
718
|
+
{
|
|
719
|
+
"name": "fundingPaymentHistory",
|
|
720
|
+
"isMut": true,
|
|
721
|
+
"isSigner": false
|
|
722
|
+
},
|
|
723
|
+
{
|
|
724
|
+
"name": "orderHistory",
|
|
725
|
+
"isMut": true,
|
|
726
|
+
"isSigner": false
|
|
727
|
+
}
|
|
728
|
+
],
|
|
729
|
+
"args": []
|
|
730
|
+
},
|
|
680
731
|
{
|
|
681
732
|
"name": "expireOrders",
|
|
682
733
|
"accounts": [
|
|
@@ -1444,6 +1495,11 @@
|
|
|
1444
1495
|
"isMut": true,
|
|
1445
1496
|
"isSigner": false
|
|
1446
1497
|
},
|
|
1498
|
+
{
|
|
1499
|
+
"name": "userOrders",
|
|
1500
|
+
"isMut": true,
|
|
1501
|
+
"isSigner": false
|
|
1502
|
+
},
|
|
1447
1503
|
{
|
|
1448
1504
|
"name": "authority",
|
|
1449
1505
|
"isMut": false,
|
|
@@ -3236,8 +3292,8 @@
|
|
|
3236
3292
|
"type": "u128"
|
|
3237
3293
|
},
|
|
3238
3294
|
{
|
|
3239
|
-
"name": "
|
|
3240
|
-
"type": "
|
|
3295
|
+
"name": "netRevenueSinceLastFunding",
|
|
3296
|
+
"type": "i64"
|
|
3241
3297
|
},
|
|
3242
3298
|
{
|
|
3243
3299
|
"name": "padding2",
|
|
@@ -4244,6 +4300,11 @@
|
|
|
4244
4300
|
"code": 6060,
|
|
4245
4301
|
"name": "CantExpireOrders",
|
|
4246
4302
|
"msg": "CantExpireOrders"
|
|
4303
|
+
},
|
|
4304
|
+
{
|
|
4305
|
+
"code": 6061,
|
|
4306
|
+
"name": "InvalidRepegPriceImpact",
|
|
4307
|
+
"msg": "AMM repeg mark price impact vs oracle too large"
|
|
4247
4308
|
}
|
|
4248
4309
|
]
|
|
4249
4310
|
}
|
package/src/index.ts
CHANGED