@drift-labs/sdk 0.1.31 → 0.1.32
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 +1 -0
- package/lib/accounts/bulkAccountLoader.js +7 -0
- package/lib/constants/markets.js +11 -0
- package/lib/math/funding.js +4 -2
- package/lib/tx/retryTxSender.d.ts +1 -0
- package/lib/tx/retryTxSender.js +9 -0
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.ts +8 -0
- package/src/constants/markets.ts +11 -0
- package/src/math/funding.ts +10 -3
- package/src/tx/retryTxSender.ts +12 -0
- package/src/math/market.js +0 -30
- package/src/math/market.js.map +0 -1
|
@@ -210,5 +210,12 @@ class BulkAccountLoader {
|
|
|
210
210
|
console.log(msg);
|
|
211
211
|
}
|
|
212
212
|
}
|
|
213
|
+
updatePollingFrequency(pollingFrequency) {
|
|
214
|
+
this.stopPolling();
|
|
215
|
+
this.pollingFrequency = pollingFrequency;
|
|
216
|
+
if (this.accountsToLoad.size > 0) {
|
|
217
|
+
this.startPolling();
|
|
218
|
+
}
|
|
219
|
+
}
|
|
213
220
|
}
|
|
214
221
|
exports.BulkAccountLoader = BulkAccountLoader;
|
package/lib/constants/markets.js
CHANGED
|
@@ -210,4 +210,15 @@ exports.Markets = [
|
|
|
210
210
|
launchTs: 1650507526000,
|
|
211
211
|
oracleSource: __1.OracleSource.SWITCHBOARD,
|
|
212
212
|
},
|
|
213
|
+
{
|
|
214
|
+
fullName: 'Curve DAO Token',
|
|
215
|
+
category: ['Exchange'],
|
|
216
|
+
symbol: 'CRV-PERP',
|
|
217
|
+
baseAssetSymbol: 'CRV',
|
|
218
|
+
marketIndex: new __1.BN(19),
|
|
219
|
+
devnetPublicKey: 'AuEiE5XG5kD1NFoKgEPrWuN9CdT44A3K7yGDuiUPvh9S',
|
|
220
|
+
mainnetPublicKey: '61NzdQ9y6KhMhWxyJ8kXEe25NxdixVL1YUnGYLDMaUxH',
|
|
221
|
+
launchTs: 1651017354000,
|
|
222
|
+
oracleSource: __1.OracleSource.SWITCHBOARD,
|
|
223
|
+
},
|
|
213
224
|
];
|
package/lib/math/funding.js
CHANGED
|
@@ -39,7 +39,7 @@ function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustm
|
|
|
39
39
|
const lastMarkTwapWithMantissa = market.amm.lastMarkPriceTwap;
|
|
40
40
|
const lastMarkPriceTwapTs = market.amm.lastMarkPriceTwapTs;
|
|
41
41
|
const timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
|
|
42
|
-
const markTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, secondsInHour.sub(timeSinceLastMarkChange));
|
|
42
|
+
const markTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, anchor_1.BN.max(numericConstants_1.ZERO, secondsInHour.sub(timeSinceLastMarkChange)));
|
|
43
43
|
const baseAssetPriceWithMantissa = (0, market_1.calculateMarkPrice)(market);
|
|
44
44
|
const markTwapWithMantissa = markTwapTimeSinceLastUpdate
|
|
45
45
|
.mul(lastMarkTwapWithMantissa)
|
|
@@ -49,6 +49,7 @@ function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustm
|
|
|
49
49
|
// note: oracle twap depends on `when the chord is struck` (market is trade)
|
|
50
50
|
const lastOracleTwapWithMantissa = market.amm.lastOraclePriceTwap;
|
|
51
51
|
const lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
|
|
52
|
+
const oracleInvalidDuration = anchor_1.BN.max(numericConstants_1.ZERO, lastMarkPriceTwapTs.sub(lastOraclePriceTwapTs));
|
|
52
53
|
const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
|
|
53
54
|
const oracleTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, secondsInHour.sub(timeSinceLastOracleTwapUpdate));
|
|
54
55
|
const oraclePrice = oraclePriceData.price;
|
|
@@ -64,7 +65,8 @@ function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustm
|
|
|
64
65
|
oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
|
|
65
66
|
.mul(lastOracleTwapWithMantissa)
|
|
66
67
|
.add(timeSinceLastMarkChange.mul(oraclePrice))
|
|
67
|
-
.
|
|
68
|
+
.add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
|
|
69
|
+
.div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate).add(oracleInvalidDuration));
|
|
68
70
|
}
|
|
69
71
|
const twapSpread = lastMarkTwapWithMantissa.sub(lastOracleTwapWithMantissa);
|
|
70
72
|
const twapSpreadPct = twapSpread
|
|
@@ -18,5 +18,6 @@ export declare class RetryTxSender implements TxSender {
|
|
|
18
18
|
sleep(reference: ResolveReference): Promise<void>;
|
|
19
19
|
promiseTimeout<T>(promises: Promise<T>[], timeoutMs: number): Promise<T | null>;
|
|
20
20
|
sendToAdditionalConnections(rawTx: Buffer, opts: ConfirmOptions): void;
|
|
21
|
+
addAdditionalConnection(newConnection: Connection): void;
|
|
21
22
|
}
|
|
22
23
|
export {};
|
package/lib/tx/retryTxSender.js
CHANGED
|
@@ -173,5 +173,14 @@ class RetryTxSender {
|
|
|
173
173
|
});
|
|
174
174
|
});
|
|
175
175
|
}
|
|
176
|
+
addAdditionalConnection(newConnection) {
|
|
177
|
+
const alreadyUsingConnection = this.additionalConnections.filter((connection) => {
|
|
178
|
+
// @ts-ignore
|
|
179
|
+
return connection._rpcEndpoint === newConnection.rpcEndpoint;
|
|
180
|
+
}).length > 0;
|
|
181
|
+
if (!alreadyUsingConnection) {
|
|
182
|
+
this.additionalConnections.push(newConnection);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
176
185
|
}
|
|
177
186
|
exports.RetryTxSender = RetryTxSender;
|
package/package.json
CHANGED
|
@@ -257,4 +257,12 @@ export class BulkAccountLoader {
|
|
|
257
257
|
console.log(msg);
|
|
258
258
|
}
|
|
259
259
|
}
|
|
260
|
+
|
|
261
|
+
public updatePollingFrequency(pollingFrequency: number): void {
|
|
262
|
+
this.stopPolling();
|
|
263
|
+
this.pollingFrequency = pollingFrequency;
|
|
264
|
+
if (this.accountsToLoad.size > 0) {
|
|
265
|
+
this.startPolling();
|
|
266
|
+
}
|
|
267
|
+
}
|
|
260
268
|
}
|
package/src/constants/markets.ts
CHANGED
|
@@ -220,4 +220,15 @@ export const Markets: MarketConfig[] = [
|
|
|
220
220
|
launchTs: 1650507526000,
|
|
221
221
|
oracleSource: OracleSource.SWITCHBOARD,
|
|
222
222
|
},
|
|
223
|
+
{
|
|
224
|
+
fullName: 'Curve DAO Token',
|
|
225
|
+
category: ['Exchange'],
|
|
226
|
+
symbol: 'CRV-PERP',
|
|
227
|
+
baseAssetSymbol: 'CRV',
|
|
228
|
+
marketIndex: new BN(19),
|
|
229
|
+
devnetPublicKey: 'AuEiE5XG5kD1NFoKgEPrWuN9CdT44A3K7yGDuiUPvh9S',
|
|
230
|
+
mainnetPublicKey: '61NzdQ9y6KhMhWxyJ8kXEe25NxdixVL1YUnGYLDMaUxH',
|
|
231
|
+
launchTs: 1651017354000,
|
|
232
|
+
oracleSource: OracleSource.SWITCHBOARD,
|
|
233
|
+
},
|
|
223
234
|
];
|
package/src/math/funding.ts
CHANGED
|
@@ -43,9 +43,13 @@ export async function calculateAllEstimatedFundingRate(
|
|
|
43
43
|
const lastMarkPriceTwapTs = market.amm.lastMarkPriceTwapTs;
|
|
44
44
|
|
|
45
45
|
const timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
|
|
46
|
-
const markTwapTimeSinceLastUpdate =
|
|
46
|
+
const markTwapTimeSinceLastUpdate =
|
|
47
|
+
BN.max(
|
|
47
48
|
secondsInHour,
|
|
48
|
-
|
|
49
|
+
BN.max(
|
|
50
|
+
ZERO,
|
|
51
|
+
secondsInHour.sub(timeSinceLastMarkChange)
|
|
52
|
+
)
|
|
49
53
|
);
|
|
50
54
|
const baseAssetPriceWithMantissa = calculateMarkPrice(market);
|
|
51
55
|
|
|
@@ -59,6 +63,8 @@ export async function calculateAllEstimatedFundingRate(
|
|
|
59
63
|
const lastOracleTwapWithMantissa = market.amm.lastOraclePriceTwap;
|
|
60
64
|
const lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
|
|
61
65
|
|
|
66
|
+
const oracleInvalidDuration = BN.max(ZERO, lastMarkPriceTwapTs.sub(lastOraclePriceTwapTs));
|
|
67
|
+
|
|
62
68
|
const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
|
|
63
69
|
const oracleTwapTimeSinceLastUpdate = BN.max(
|
|
64
70
|
secondsInHour,
|
|
@@ -80,7 +86,8 @@ export async function calculateAllEstimatedFundingRate(
|
|
|
80
86
|
oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
|
|
81
87
|
.mul(lastOracleTwapWithMantissa)
|
|
82
88
|
.add(timeSinceLastMarkChange.mul(oraclePrice))
|
|
83
|
-
.
|
|
89
|
+
.add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
|
|
90
|
+
.div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate).add(oracleInvalidDuration));
|
|
84
91
|
}
|
|
85
92
|
|
|
86
93
|
const twapSpread = lastMarkTwapWithMantissa.sub(lastOracleTwapWithMantissa);
|
package/src/tx/retryTxSender.ts
CHANGED
|
@@ -227,4 +227,16 @@ export class RetryTxSender implements TxSender {
|
|
|
227
227
|
});
|
|
228
228
|
});
|
|
229
229
|
}
|
|
230
|
+
|
|
231
|
+
public addAdditionalConnection(newConnection: Connection): void {
|
|
232
|
+
const alreadyUsingConnection =
|
|
233
|
+
this.additionalConnections.filter((connection) => {
|
|
234
|
+
// @ts-ignore
|
|
235
|
+
return connection._rpcEndpoint === newConnection.rpcEndpoint;
|
|
236
|
+
}).length > 0;
|
|
237
|
+
|
|
238
|
+
if (!alreadyUsingConnection) {
|
|
239
|
+
this.additionalConnections.push(newConnection);
|
|
240
|
+
}
|
|
241
|
+
}
|
|
230
242
|
}
|
package/src/math/market.js
DELETED
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateOracleMarkSpread = exports.calculateNewMarketAfterTrade = exports.calculateMarkPrice = void 0;
|
|
4
|
-
const amm_1 = require("./amm");
|
|
5
|
-
/**
|
|
6
|
-
* Calculates market mark price
|
|
7
|
-
*
|
|
8
|
-
* @param market
|
|
9
|
-
* @return markPrice : Precision MARK_PRICE_PRECISION
|
|
10
|
-
*/
|
|
11
|
-
function calculateMarkPrice(market) {
|
|
12
|
-
return (0, amm_1.calculatePrice)(market.amm.baseAssetReserve, market.amm.quoteAssetReserve, market.amm.pegMultiplier);
|
|
13
|
-
}
|
|
14
|
-
exports.calculateMarkPrice = calculateMarkPrice;
|
|
15
|
-
function calculateNewMarketAfterTrade(baseAssetAmount, direction, market) {
|
|
16
|
-
const [newQuoteAssetReserve, newBaseAssetReserve] = (0, amm_1.calculateAmmReservesAfterSwap)(market.amm, 'base', baseAssetAmount.abs(), (0, amm_1.getSwapDirection)('base', direction));
|
|
17
|
-
const newAmm = Object.assign({}, market.amm);
|
|
18
|
-
const newMarket = Object.assign({}, market);
|
|
19
|
-
newMarket.amm = newAmm;
|
|
20
|
-
newMarket.amm.quoteAssetReserve = newQuoteAssetReserve;
|
|
21
|
-
newMarket.amm.baseAssetReserve = newBaseAssetReserve;
|
|
22
|
-
return newMarket;
|
|
23
|
-
}
|
|
24
|
-
exports.calculateNewMarketAfterTrade = calculateNewMarketAfterTrade;
|
|
25
|
-
function calculateOracleMarkSpread(market, oraclePriceData) {
|
|
26
|
-
const markPrice = calculateMarkPrice(market);
|
|
27
|
-
return oraclePriceData.price.sub(markPrice);
|
|
28
|
-
}
|
|
29
|
-
exports.calculateOracleMarkSpread = calculateOracleMarkSpread;
|
|
30
|
-
//# sourceMappingURL=market.js.map
|
package/src/math/market.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"market.js","sourceRoot":"","sources":["market.ts"],"names":[],"mappings":";;;AAEA,+BAIe;AAGf;;;;;GAKG;AACH,SAAgB,kBAAkB,CAAC,MAAc;IAChD,OAAO,IAAA,oBAAc,EACpB,MAAM,CAAC,GAAG,CAAC,gBAAgB,EAC3B,MAAM,CAAC,GAAG,CAAC,iBAAiB,EAC5B,MAAM,CAAC,GAAG,CAAC,aAAa,CACxB,CAAC;AACH,CAAC;AAND,gDAMC;AAED,SAAgB,4BAA4B,CAC3C,eAAmB,EACnB,SAA4B,EAC5B,MAAc;IAEd,MAAM,CAAC,oBAAoB,EAAE,mBAAmB,CAAC,GAChD,IAAA,mCAA6B,EAC5B,MAAM,CAAC,GAAG,EACV,MAAM,EACN,eAAe,CAAC,GAAG,EAAE,EACrB,IAAA,sBAAgB,EAAC,MAAM,EAAE,SAAS,CAAC,CACnC,CAAC;IAEH,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,GAAG,CAAC,CAAC;IAC7C,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;IAC5C,SAAS,CAAC,GAAG,GAAG,MAAM,CAAC;IACvB,SAAS,CAAC,GAAG,CAAC,iBAAiB,GAAG,oBAAoB,CAAC;IACvD,SAAS,CAAC,GAAG,CAAC,gBAAgB,GAAG,mBAAmB,CAAC;IAErD,OAAO,SAAS,CAAC;AAClB,CAAC;AApBD,oEAoBC;AAED,SAAgB,yBAAyB,CACxC,MAAc,EACd,eAAgC;IAEhC,MAAM,SAAS,GAAG,kBAAkB,CAAC,MAAM,CAAC,CAAC;IAC7C,OAAO,eAAe,CAAC,KAAK,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;AAC7C,CAAC;AAND,8DAMC"}
|