@drift-labs/sdk 0.1.29-master.1 → 0.1.30-master.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 +44 -32
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +15 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +50 -23
- package/lib/clearingHouse.d.ts +4 -2
- package/lib/clearingHouse.js +49 -14
- package/lib/constants/accounts.d.ts +15 -0
- package/lib/constants/accounts.js +18 -0
- package/lib/constants/markets.js +11 -0
- package/lib/factory/clearingHouse.d.ts +14 -4
- package/lib/factory/clearingHouse.js +23 -6
- package/lib/idl/clearing_house.json +53 -33
- package/lib/math/amm.d.ts +18 -0
- package/lib/math/amm.js +88 -1
- package/lib/oracles/pythClient.js +1 -1
- package/lib/orderParams.d.ts +1 -1
- package/lib/orderParams.js +2 -2
- package/lib/tx/retryTxSender.d.ts +5 -2
- package/lib/tx/retryTxSender.js +14 -1
- package/lib/util/promiseTimeout.d.ts +1 -0
- package/lib/util/promiseTimeout.js +14 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +51 -40
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +73 -30
- package/src/clearingHouse.ts +71 -13
- package/src/constants/accounts.ts +26 -0
- package/src/constants/markets.ts +11 -0
- package/src/factory/clearingHouse.ts +47 -7
- package/src/idl/clearing_house.json +53 -33
- package/src/math/amm.ts +127 -2
- package/src/oracles/pythClient.ts +1 -1
- package/src/orderParams.ts +3 -2
- package/src/tx/retryTxSender.ts +19 -1
- package/src/util/promiseTimeout.ts +14 -0
- package/lib/math/repeg.d.ts +0 -32
- package/lib/math/repeg.js +0 -178
- package/src/accounts/bulkUserSubscription.js +0 -56
- package/src/accounts/bulkUserSubscription.js.map +0 -1
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +0 -210
- package/src/accounts/pollingClearingHouseAccountSubscriber.js.map +0 -1
- package/src/accounts/pollingOracleSubscriber.js +0 -65
- package/src/accounts/pollingOracleSubscriber.js.map +0 -1
- package/src/accounts/pollingTokenAccountSubscriber.js +0 -65
- package/src/accounts/pollingTokenAccountSubscriber.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/utils.js +0 -8
- package/src/accounts/utils.js.map +0 -1
- package/src/accounts/webSocketAccountSubscriber.js +0 -64
- package/src/accounts/webSocketAccountSubscriber.js.map +0 -1
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +0 -212
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js.map +0 -1
- package/src/accounts/webSocketUserAccountSubscriber.js +0 -78
- package/src/accounts/webSocketUserAccountSubscriber.js.map +0 -1
- package/src/constants/markets.js +0 -167
- package/src/constants/markets.js.map +0 -1
- package/src/constants/numericConstants.js +0 -22
- package/src/constants/numericConstants.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/math/amm.js +0 -285
- package/src/math/amm.js.map +0 -1
- package/src/math/repeg.ts +0 -253
- package/src/oracles/pythClient.js +0 -39
- package/src/oracles/pythClient.js.map +0 -1
- package/src/tx/retryTxSender.js +0 -137
- package/src/tx/retryTxSender.js.map +0 -1
package/src/math/amm.ts
CHANGED
|
@@ -2,12 +2,13 @@ import { BN } from '@project-serum/anchor';
|
|
|
2
2
|
import {
|
|
3
3
|
AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO,
|
|
4
4
|
MARK_PRICE_PRECISION,
|
|
5
|
-
AMM_RESERVE_PRECISION,
|
|
6
5
|
PEG_PRECISION,
|
|
7
6
|
ZERO,
|
|
8
7
|
AMM_TO_QUOTE_PRECISION_RATIO,
|
|
9
8
|
QUOTE_PRECISION,
|
|
9
|
+
AMM_RESERVE_PRECISION,
|
|
10
10
|
} from '../constants/numericConstants';
|
|
11
|
+
import { calculateBaseAssetValue } from './position';
|
|
11
12
|
import {
|
|
12
13
|
AMM,
|
|
13
14
|
PositionDirection,
|
|
@@ -16,7 +17,12 @@ import {
|
|
|
16
17
|
isVariant,
|
|
17
18
|
} from '../types';
|
|
18
19
|
import { assert } from '../assert/assert';
|
|
19
|
-
import {
|
|
20
|
+
import {
|
|
21
|
+
calculatePositionPNL,
|
|
22
|
+
calculateMarkPrice,
|
|
23
|
+
convertToNumber,
|
|
24
|
+
squareRootBN,
|
|
25
|
+
} from '..';
|
|
20
26
|
|
|
21
27
|
/**
|
|
22
28
|
* Calculates a price given an arbitrary base and quote amount (they must have the same precision)
|
|
@@ -133,6 +139,125 @@ export function getSwapDirection(
|
|
|
133
139
|
return SwapDirection.ADD;
|
|
134
140
|
}
|
|
135
141
|
|
|
142
|
+
/**
|
|
143
|
+
* Helper function calculating adjust k cost
|
|
144
|
+
* @param market
|
|
145
|
+
* @param marketIndex
|
|
146
|
+
* @param numerator
|
|
147
|
+
* @param denomenator
|
|
148
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
149
|
+
*/
|
|
150
|
+
export function calculateAdjustKCost(
|
|
151
|
+
market: Market,
|
|
152
|
+
marketIndex: BN,
|
|
153
|
+
numerator: BN,
|
|
154
|
+
denomenator: BN
|
|
155
|
+
): BN {
|
|
156
|
+
const netUserPosition = {
|
|
157
|
+
baseAssetAmount: market.baseAssetAmount,
|
|
158
|
+
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
159
|
+
marketIndex: new BN(marketIndex),
|
|
160
|
+
quoteAssetAmount: new BN(0),
|
|
161
|
+
openOrders: new BN(0),
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
const currentValue = calculateBaseAssetValue(market, netUserPosition);
|
|
165
|
+
|
|
166
|
+
const marketNewK = Object.assign({}, market);
|
|
167
|
+
marketNewK.amm = Object.assign({}, market.amm);
|
|
168
|
+
|
|
169
|
+
marketNewK.amm.baseAssetReserve = market.amm.baseAssetReserve
|
|
170
|
+
.mul(numerator)
|
|
171
|
+
.div(denomenator);
|
|
172
|
+
marketNewK.amm.quoteAssetReserve = market.amm.quoteAssetReserve
|
|
173
|
+
.mul(numerator)
|
|
174
|
+
.div(denomenator);
|
|
175
|
+
marketNewK.amm.sqrtK = market.amm.sqrtK.mul(numerator).div(denomenator);
|
|
176
|
+
|
|
177
|
+
netUserPosition.quoteAssetAmount = currentValue;
|
|
178
|
+
|
|
179
|
+
const cost = calculatePositionPNL(marketNewK, netUserPosition);
|
|
180
|
+
|
|
181
|
+
const p = PEG_PRECISION.mul(numerator).div(denomenator);
|
|
182
|
+
const x = market.amm.baseAssetReserve;
|
|
183
|
+
const y = market.amm.quoteAssetReserve;
|
|
184
|
+
const delta = market.baseAssetAmount;
|
|
185
|
+
const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
186
|
+
|
|
187
|
+
const numer1 = PEG_PRECISION.sub(p).mul(y).div(PEG_PRECISION);
|
|
188
|
+
const numer20 = k
|
|
189
|
+
.mul(p)
|
|
190
|
+
.mul(p)
|
|
191
|
+
.div(PEG_PRECISION)
|
|
192
|
+
.div(PEG_PRECISION)
|
|
193
|
+
.div(x.mul(p).div(PEG_PRECISION).add(delta));
|
|
194
|
+
const numer21 = k.div(x.add(delta));
|
|
195
|
+
|
|
196
|
+
const formulaCost = numer21
|
|
197
|
+
.sub(numer20)
|
|
198
|
+
.sub(numer1)
|
|
199
|
+
.mul(market.amm.pegMultiplier)
|
|
200
|
+
.div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
201
|
+
console.log(convertToNumber(formulaCost, QUOTE_PRECISION));
|
|
202
|
+
|
|
203
|
+
// p.div(p.mul(x).add(delta)).sub()
|
|
204
|
+
|
|
205
|
+
return cost;
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
210
|
+
*
|
|
211
|
+
* @param market
|
|
212
|
+
* @param marketIndex
|
|
213
|
+
* @param newPeg
|
|
214
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
215
|
+
*/
|
|
216
|
+
export function calculateRepegCost(
|
|
217
|
+
market: Market,
|
|
218
|
+
marketIndex: BN,
|
|
219
|
+
newPeg: BN
|
|
220
|
+
): BN {
|
|
221
|
+
const netUserPosition = {
|
|
222
|
+
baseAssetAmount: market.baseAssetAmount,
|
|
223
|
+
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
224
|
+
marketIndex: new BN(marketIndex),
|
|
225
|
+
quoteAssetAmount: new BN(0),
|
|
226
|
+
openOrders: new BN(0),
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const currentValue = calculateBaseAssetValue(market, netUserPosition);
|
|
230
|
+
netUserPosition.quoteAssetAmount = currentValue;
|
|
231
|
+
const prevMarketPrice = calculateMarkPrice(market);
|
|
232
|
+
const marketNewPeg = Object.assign({}, market);
|
|
233
|
+
marketNewPeg.amm = Object.assign({}, market.amm);
|
|
234
|
+
|
|
235
|
+
// const marketNewPeg = JSON.parse(JSON.stringify(market));
|
|
236
|
+
marketNewPeg.amm.pegMultiplier = newPeg;
|
|
237
|
+
|
|
238
|
+
console.log(
|
|
239
|
+
'Price moves from',
|
|
240
|
+
convertToNumber(prevMarketPrice),
|
|
241
|
+
'to',
|
|
242
|
+
convertToNumber(calculateMarkPrice(marketNewPeg))
|
|
243
|
+
);
|
|
244
|
+
|
|
245
|
+
const cost = calculatePositionPNL(marketNewPeg, netUserPosition);
|
|
246
|
+
|
|
247
|
+
const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
248
|
+
const newQuoteAssetReserve = k.div(
|
|
249
|
+
market.amm.baseAssetReserve.add(netUserPosition.baseAssetAmount)
|
|
250
|
+
);
|
|
251
|
+
const deltaQuoteAssetReserves = newQuoteAssetReserve.sub(
|
|
252
|
+
market.amm.quoteAssetReserve
|
|
253
|
+
);
|
|
254
|
+
const cost2 = deltaQuoteAssetReserves
|
|
255
|
+
.mul(market.amm.pegMultiplier.sub(newPeg))
|
|
256
|
+
.div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
257
|
+
console.log(convertToNumber(cost2, QUOTE_PRECISION));
|
|
258
|
+
return cost;
|
|
259
|
+
}
|
|
260
|
+
|
|
136
261
|
/**
|
|
137
262
|
* Helper function calculating terminal price of amm
|
|
138
263
|
*
|
|
@@ -28,7 +28,7 @@ export class PythClient {
|
|
|
28
28
|
): Promise<OraclePriceData> {
|
|
29
29
|
const priceData = parsePriceData(buffer);
|
|
30
30
|
return {
|
|
31
|
-
price: convertPythPrice(priceData.price, priceData.exponent),
|
|
31
|
+
price: convertPythPrice(priceData.aggregate.price, priceData.exponent),
|
|
32
32
|
slot: new BN(priceData.lastSlot.toString()),
|
|
33
33
|
confidence: convertPythPrice(priceData.confidence, priceData.exponent),
|
|
34
34
|
twap: convertPythPrice(priceData.twap.value, priceData.exponent),
|
package/src/orderParams.ts
CHANGED
|
@@ -17,7 +17,8 @@ export function getLimitOrderParams(
|
|
|
17
17
|
referrer = false,
|
|
18
18
|
userOrderId = 0,
|
|
19
19
|
postOnly = false,
|
|
20
|
-
oraclePriceOffset = ZERO
|
|
20
|
+
oraclePriceOffset = ZERO,
|
|
21
|
+
immediateOrCancel = false
|
|
21
22
|
): OrderParams {
|
|
22
23
|
return {
|
|
23
24
|
orderType: OrderType.LIMIT,
|
|
@@ -29,7 +30,7 @@ export function getLimitOrderParams(
|
|
|
29
30
|
price,
|
|
30
31
|
reduceOnly,
|
|
31
32
|
postOnly,
|
|
32
|
-
immediateOrCancel
|
|
33
|
+
immediateOrCancel,
|
|
33
34
|
positionLimit: ZERO,
|
|
34
35
|
padding0: true,
|
|
35
36
|
padding1: ZERO,
|
package/src/tx/retryTxSender.ts
CHANGED
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
SignatureResult,
|
|
9
9
|
Transaction,
|
|
10
10
|
TransactionSignature,
|
|
11
|
+
Connection,
|
|
11
12
|
} from '@solana/web3.js';
|
|
12
13
|
import { Provider } from '@project-serum/anchor';
|
|
13
14
|
import assert from 'assert';
|
|
@@ -24,15 +25,18 @@ export class RetryTxSender implements TxSender {
|
|
|
24
25
|
provider: Provider;
|
|
25
26
|
timeout: number;
|
|
26
27
|
retrySleep: number;
|
|
28
|
+
additionalConnections: Connection[];
|
|
27
29
|
|
|
28
30
|
public constructor(
|
|
29
31
|
provider: Provider,
|
|
30
32
|
timeout?: number,
|
|
31
|
-
retrySleep?: number
|
|
33
|
+
retrySleep?: number,
|
|
34
|
+
additionalConnections = new Array<Connection>()
|
|
32
35
|
) {
|
|
33
36
|
this.provider = provider;
|
|
34
37
|
this.timeout = timeout ?? DEFAULT_TIMEOUT;
|
|
35
38
|
this.retrySleep = retrySleep ?? DEFAULT_RETRY;
|
|
39
|
+
this.additionalConnections = additionalConnections;
|
|
36
40
|
}
|
|
37
41
|
|
|
38
42
|
async send(
|
|
@@ -54,6 +58,7 @@ export class RetryTxSender implements TxSender {
|
|
|
54
58
|
|
|
55
59
|
const txid: TransactionSignature =
|
|
56
60
|
await this.provider.connection.sendRawTransaction(rawTransaction, opts);
|
|
61
|
+
this.sendToAdditionalConnections(rawTransaction, opts);
|
|
57
62
|
|
|
58
63
|
let done = false;
|
|
59
64
|
const resolveReference: ResolveReference = {
|
|
@@ -76,6 +81,7 @@ export class RetryTxSender implements TxSender {
|
|
|
76
81
|
console.error(e);
|
|
77
82
|
stopWaiting();
|
|
78
83
|
});
|
|
84
|
+
this.sendToAdditionalConnections(rawTransaction, opts);
|
|
79
85
|
}
|
|
80
86
|
}
|
|
81
87
|
})();
|
|
@@ -193,4 +199,16 @@ export class RetryTxSender implements TxSender {
|
|
|
193
199
|
return result;
|
|
194
200
|
});
|
|
195
201
|
}
|
|
202
|
+
|
|
203
|
+
sendToAdditionalConnections(rawTx: Buffer, opts: ConfirmOptions): void {
|
|
204
|
+
this.additionalConnections.map((connection) => {
|
|
205
|
+
connection.sendRawTransaction(rawTx, opts).catch((e) => {
|
|
206
|
+
console.error(
|
|
207
|
+
// @ts-ignore
|
|
208
|
+
`error sending tx to additional connection ${connection._rpcEndpoint}`
|
|
209
|
+
);
|
|
210
|
+
console.error(e);
|
|
211
|
+
});
|
|
212
|
+
});
|
|
213
|
+
}
|
|
196
214
|
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export function promiseTimeout<T>(
|
|
2
|
+
promise: Promise<T>,
|
|
3
|
+
timeoutMs: number
|
|
4
|
+
): Promise<T | null> {
|
|
5
|
+
let timeoutId: ReturnType<typeof setTimeout>;
|
|
6
|
+
const timeoutPromise: Promise<null> = new Promise((resolve) => {
|
|
7
|
+
timeoutId = setTimeout(() => resolve(null), timeoutMs);
|
|
8
|
+
});
|
|
9
|
+
|
|
10
|
+
return Promise.race([promise, timeoutPromise]).then((result: T | null) => {
|
|
11
|
+
clearTimeout(timeoutId);
|
|
12
|
+
return result;
|
|
13
|
+
});
|
|
14
|
+
}
|
package/lib/math/repeg.d.ts
DELETED
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/// <reference types="bn.js" />
|
|
2
|
-
import { BN } from '@project-serum/anchor';
|
|
3
|
-
import { Market } from '../types';
|
|
4
|
-
/**
|
|
5
|
-
* Helper function calculating adjust k cost
|
|
6
|
-
* @param market
|
|
7
|
-
* @param marketIndex
|
|
8
|
-
* @param numerator
|
|
9
|
-
* @param denomenator
|
|
10
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
11
|
-
*/
|
|
12
|
-
export declare function calculateAdjustKCost(market: Market, marketIndex: BN, numerator: BN, denomenator: BN): BN;
|
|
13
|
-
/**
|
|
14
|
-
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
15
|
-
*
|
|
16
|
-
* @param market
|
|
17
|
-
* @param marketIndex
|
|
18
|
-
* @param newPeg
|
|
19
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
20
|
-
*/
|
|
21
|
-
export declare function calculateRepegCost(market: Market, marketIndex: BN, newPeg: BN): BN;
|
|
22
|
-
/**
|
|
23
|
-
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
24
|
-
*
|
|
25
|
-
* @param market
|
|
26
|
-
* @param marketIndex
|
|
27
|
-
* @param newPeg
|
|
28
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
29
|
-
*/
|
|
30
|
-
export declare function calculateReserveRebalanceCost(market: Market, marketIndex: BN): BN;
|
|
31
|
-
export declare function calculateBudgetedK(market: Market, cost: BN): [BN, BN];
|
|
32
|
-
export declare function calculateBudgetedPeg(market: Market, cost: BN): BN;
|
package/lib/math/repeg.js
DELETED
|
@@ -1,178 +0,0 @@
|
|
|
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;
|
|
@@ -1,56 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.bulkPollingUserSubscribe = void 0;
|
|
4
|
-
/**
|
|
5
|
-
* @param users
|
|
6
|
-
* @param accountLoader
|
|
7
|
-
*/
|
|
8
|
-
async function bulkPollingUserSubscribe(users, accountLoader) {
|
|
9
|
-
if (users.length === 0) {
|
|
10
|
-
await accountLoader.load();
|
|
11
|
-
return;
|
|
12
|
-
}
|
|
13
|
-
// Fetch all the accounts first
|
|
14
|
-
const program = users[0].clearingHouse.program;
|
|
15
|
-
let userProgramAccounts;
|
|
16
|
-
let orderProgramAccounts;
|
|
17
|
-
await Promise.all([
|
|
18
|
-
(async () => {
|
|
19
|
-
userProgramAccounts = await program.account.user.all();
|
|
20
|
-
})(),
|
|
21
|
-
(async () => {
|
|
22
|
-
orderProgramAccounts = await program.account.userOrders.all();
|
|
23
|
-
})(),
|
|
24
|
-
]);
|
|
25
|
-
// Create a map of the authority to keys
|
|
26
|
-
const authorityToKeys = new Map();
|
|
27
|
-
const userToAuthority = new Map();
|
|
28
|
-
for (const userProgramAccount of userProgramAccounts) {
|
|
29
|
-
const userAccountPublicKey = userProgramAccount.publicKey;
|
|
30
|
-
const userAccount = userProgramAccount.account;
|
|
31
|
-
authorityToKeys.set(userAccount.authority.toString(), {
|
|
32
|
-
user: userAccountPublicKey,
|
|
33
|
-
userPositions: userAccount.positions,
|
|
34
|
-
userOrders: undefined,
|
|
35
|
-
});
|
|
36
|
-
userToAuthority.set(userAccountPublicKey.toString(), userAccount.authority.toString());
|
|
37
|
-
}
|
|
38
|
-
for (const orderProgramAccount of orderProgramAccounts) {
|
|
39
|
-
const userOrderAccountPublicKey = orderProgramAccount.publicKey;
|
|
40
|
-
const userOrderAccount = orderProgramAccount.account;
|
|
41
|
-
const authority = userToAuthority.get(userOrderAccount.user.toString());
|
|
42
|
-
const userPublicKeys = authorityToKeys.get(authority);
|
|
43
|
-
userPublicKeys.userOrders = userOrderAccountPublicKey;
|
|
44
|
-
}
|
|
45
|
-
await Promise.all(users.map((user) => {
|
|
46
|
-
// Pull the keys from the authority map so we can skip fetching them in addToAccountLoader
|
|
47
|
-
const userPublicKeys = authorityToKeys.get(user.authority.toString());
|
|
48
|
-
return user.accountSubscriber.addToAccountLoader(userPublicKeys);
|
|
49
|
-
}));
|
|
50
|
-
await accountLoader.load();
|
|
51
|
-
await Promise.all(users.map(async (user) => {
|
|
52
|
-
return user.subscribe();
|
|
53
|
-
}));
|
|
54
|
-
}
|
|
55
|
-
exports.bulkPollingUserSubscribe = bulkPollingUserSubscribe;
|
|
56
|
-
//# sourceMappingURL=bulkUserSubscription.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"bulkUserSubscription.js","sourceRoot":"","sources":["bulkUserSubscription.ts"],"names":[],"mappings":";;;AAOA;;;GAGG;AACI,KAAK,UAAU,wBAAwB,CAC7C,KAA0B,EAC1B,aAAgC;IAEhC,IAAI,KAAK,CAAC,MAAM,KAAK,CAAC,EAAE;QACvB,MAAM,aAAa,CAAC,IAAI,EAAE,CAAC;QAC3B,OAAO;KACP;IAED,+BAA+B;IAC/B,MAAM,OAAO,GAAG,KAAK,CAAC,CAAC,CAAC,CAAC,aAAa,CAAC,OAAO,CAAC;IAC/C,IAAI,mBAAqC,CAAC;IAC1C,IAAI,oBAAsC,CAAC;IAC3C,MAAM,OAAO,CAAC,GAAG,CAAC;QACjB,CAAC,KAAK,IAAI,EAAE;YACX,mBAAmB,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC;QACxD,CAAC,CAAC,EAAE;QACJ,CAAC,KAAK,IAAI,EAAE;YACX,oBAAoB,GAAG,MAAM,OAAO,CAAC,OAAO,CAAC,UAAU,CAAC,GAAG,EAAE,CAAC;QAC/D,CAAC,CAAC,EAAE;KACJ,CAAC,CAAC;IAEH,wCAAwC;IACxC,MAAM,eAAe,GAAG,IAAI,GAAG,EAA0B,CAAC;IAC1D,MAAM,eAAe,GAAG,IAAI,GAAG,EAAkB,CAAC;IAClD,KAAK,MAAM,kBAAkB,IAAI,mBAAmB,EAAE;QACrD,MAAM,oBAAoB,GAAG,kBAAkB,CAAC,SAAS,CAAC;QAC1D,MAAM,WAAW,GAAG,kBAAkB,CAAC,OAAsB,CAAC;QAE9D,eAAe,CAAC,GAAG,CAAC,WAAW,CAAC,SAAS,CAAC,QAAQ,EAAE,EAAE;YACrD,IAAI,EAAE,oBAAoB;YAC1B,aAAa,EAAE,WAAW,CAAC,SAAS;YACpC,UAAU,EAAE,SAAS;SACrB,CAAC,CAAC;QAEH,eAAe,CAAC,GAAG,CAClB,oBAAoB,CAAC,QAAQ,EAAE,EAC/B,WAAW,CAAC,SAAS,CAAC,QAAQ,EAAE,CAChC,CAAC;KACF;IACD,KAAK,MAAM,mBAAmB,IAAI,oBAAoB,EAAE;QACvD,MAAM,yBAAyB,GAAG,mBAAmB,CAAC,SAAS,CAAC;QAChE,MAAM,gBAAgB,GAAG,mBAAmB,CAAC,OAA4B,CAAC;QAE1E,MAAM,SAAS,GAAG,eAAe,CAAC,GAAG,CAAC,gBAAgB,CAAC,IAAI,CAAC,QAAQ,EAAE,CAAC,CAAC;QACxE,MAAM,cAAc,GAAG,eAAe,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QACtD,cAAc,CAAC,UAAU,GAAG,yBAAyB,CAAC;KACtD;IAED,MAAM,OAAO,CAAC,GAAG,CAChB,KAAK,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE;QAClB,0FAA0F;QAC1F,MAAM,cAAc,GAAG,eAAe,CAAC,GAAG,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,EAAE,CAAC,CAAC;QACtE,OACC,IAAI,CAAC,iBACL,CAAC,kBAAkB,CAAC,cAAc,CAAC,CAAC;IACtC,CAAC,CAAC,CACF,CAAC;IAEF,MAAM,aAAa,CAAC,IAAI,EAAE,CAAC;IAE3B,MAAM,OAAO,CAAC,GAAG,CAChB,KAAK,CAAC,GAAG,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE;QACxB,OAAO,IAAI,CAAC,SAAS,EAAE,CAAC;IACzB,CAAC,CAAC,CACF,CAAC;AACH,CAAC;AAlED,4DAkEC"}
|