@drift-labs/sdk 0.2.0-master.21 → 0.2.0-master.24
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/types.d.ts +1 -0
- package/lib/addresses/pda.d.ts +3 -0
- package/lib/addresses/pda.js +23 -1
- package/lib/admin.d.ts +3 -0
- package/lib/admin.js +31 -0
- package/lib/clearingHouse.d.ts +9 -2
- package/lib/clearingHouse.js +132 -4
- package/lib/clearingHouseUser.js +5 -5
- package/lib/config.js +1 -1
- package/lib/constants/banks.d.ts +2 -0
- package/lib/constants/banks.js +9 -0
- package/lib/constants/numericConstants.d.ts +2 -0
- package/lib/constants/numericConstants.js +4 -1
- package/lib/events/sort.js +7 -10
- package/lib/events/types.d.ts +2 -1
- package/lib/events/types.js +1 -0
- package/lib/idl/clearing_house.json +1008 -147
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/math/amm.js +2 -2
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +1 -1
- package/lib/math/margin.js +5 -7
- package/lib/math/position.js +1 -1
- package/lib/types.d.ts +56 -20
- package/lib/types.js +0 -3
- package/package.json +1 -1
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/pda.ts +47 -0
- package/src/admin.ts +65 -0
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +217 -9
- package/src/clearingHouseUser.ts +10 -6
- package/src/config.ts +1 -1
- package/src/constants/banks.js +42 -0
- package/src/constants/banks.ts +16 -0
- package/src/constants/markets.js +42 -0
- package/src/constants/numericConstants.js +41 -0
- package/src/constants/numericConstants.ts +4 -0
- package/src/events/eventList.js +77 -0
- package/src/events/sort.ts +10 -14
- package/src/events/txEventCache.js +71 -0
- package/src/events/types.ts +3 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/factory/bigNum.js +390 -0
- package/src/factory/oracleClient.js +20 -0
- package/src/idl/clearing_house.json +1008 -147
- package/src/index.ts +1 -0
- package/src/math/amm.ts +2 -2
- package/src/math/auction.js +42 -0
- package/src/math/conversion.js +11 -0
- package/src/math/funding.js +248 -0
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +3 -10
- package/src/math/oracles.js +26 -0
- package/src/math/position.ts +2 -2
- package/src/math/repeg.js +128 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/utils.js +26 -0
- package/src/math/utils.js.map +1 -0
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/types.js +2 -0
- package/src/orderParams.js +20 -0
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/token/index.js +38 -0
- package/src/tokenFaucet.js +189 -0
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/types.ts +64 -20
- package/src/userName.js +20 -0
- package/src/util/computeUnits.js +27 -0
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/my-script/.env +0 -7
- package/my-script/getUserStats.ts +0 -124
- package/my-script/multiConnections.ts +0 -137
- package/my-script/test-regex.ts +0 -11
- package/my-script/utils.ts +0 -52
- package/my-script/ww18NdhuLSQPCrHSx7V68eZJpe2y311heWeXJfSmP3Q.json +0 -1
- package/my-script/ww2z7N9TG1PLLUQGQF2VKzCFaPtQ5FBhRfeEAuy6c5C.json +0 -1
- package/my-script/ww3StJtTubhwssqAhvSSAc5ifCgKjzmF8hz7Gt2DmSa.json +0 -1
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateTargetPriceTrade = exports.calculateTradeAcquiredAmounts = exports.calculateTradeSlippage = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
6
|
+
const assert_1 = require("../assert/assert");
|
|
7
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
8
|
+
const market_1 = require("./market");
|
|
9
|
+
const amm_1 = require("./amm");
|
|
10
|
+
const utils_1 = require("./utils");
|
|
11
|
+
const types_2 = require("../types");
|
|
12
|
+
const MAXPCT = new anchor_1.BN(1000); //percentage units are [0,1000] => [0,1]
|
|
13
|
+
/**
|
|
14
|
+
* Calculates avg/max slippage (price impact) for candidate trade
|
|
15
|
+
* @param direction
|
|
16
|
+
* @param amount
|
|
17
|
+
* @param market
|
|
18
|
+
* @param inputAssetType which asset is being traded
|
|
19
|
+
* @param useSpread whether to consider spread with calculating slippage
|
|
20
|
+
* @return [pctAvgSlippage, pctMaxSlippage, entryPrice, newPrice]
|
|
21
|
+
*
|
|
22
|
+
* 'pctAvgSlippage' => the percentage change to entryPrice (average est slippage in execution) : Precision MARK_PRICE_PRECISION
|
|
23
|
+
*
|
|
24
|
+
* 'pctMaxSlippage' => the percentage change to maxPrice (highest est slippage in execution) : Precision MARK_PRICE_PRECISION
|
|
25
|
+
*
|
|
26
|
+
* 'entryPrice' => the average price of the trade : Precision MARK_PRICE_PRECISION
|
|
27
|
+
*
|
|
28
|
+
* 'newPrice' => the price of the asset after the trade : Precision MARK_PRICE_PRECISION
|
|
29
|
+
*/
|
|
30
|
+
function calculateTradeSlippage(direction, amount, market, inputAssetType = 'quote', oraclePriceData, useSpread = true) {
|
|
31
|
+
let oldPrice;
|
|
32
|
+
if (useSpread && market.amm.baseSpread > 0) {
|
|
33
|
+
if (types_2.isVariant(direction, 'long')) {
|
|
34
|
+
oldPrice = market_1.calculateAskPrice(market, oraclePriceData);
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
oldPrice = market_1.calculateBidPrice(market, oraclePriceData);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
oldPrice = market_1.calculateMarkPrice(market, oraclePriceData);
|
|
42
|
+
}
|
|
43
|
+
if (amount.eq(numericConstants_1.ZERO)) {
|
|
44
|
+
return [numericConstants_1.ZERO, numericConstants_1.ZERO, oldPrice, oldPrice];
|
|
45
|
+
}
|
|
46
|
+
const [acquiredBaseReserve, acquiredQuoteReserve, acquiredQuoteAssetAmount] = calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType, oraclePriceData, useSpread);
|
|
47
|
+
const entryPrice = acquiredQuoteAssetAmount
|
|
48
|
+
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
49
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
50
|
+
.div(acquiredBaseReserve.abs());
|
|
51
|
+
let amm;
|
|
52
|
+
if (useSpread && market.amm.baseSpread > 0) {
|
|
53
|
+
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
|
|
54
|
+
amm = {
|
|
55
|
+
baseAssetReserve,
|
|
56
|
+
quoteAssetReserve,
|
|
57
|
+
sqrtK: sqrtK,
|
|
58
|
+
pegMultiplier: newPeg,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
else {
|
|
62
|
+
amm = market.amm;
|
|
63
|
+
}
|
|
64
|
+
const newPrice = amm_1.calculatePrice(amm.baseAssetReserve.sub(acquiredBaseReserve), amm.quoteAssetReserve.sub(acquiredQuoteReserve), amm.pegMultiplier);
|
|
65
|
+
if (direction == types_1.PositionDirection.SHORT) {
|
|
66
|
+
assert_1.assert(newPrice.lte(oldPrice));
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
assert_1.assert(oldPrice.lte(newPrice));
|
|
70
|
+
}
|
|
71
|
+
const pctMaxSlippage = newPrice
|
|
72
|
+
.sub(oldPrice)
|
|
73
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
74
|
+
.div(oldPrice)
|
|
75
|
+
.abs();
|
|
76
|
+
const pctAvgSlippage = entryPrice
|
|
77
|
+
.sub(oldPrice)
|
|
78
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
79
|
+
.div(oldPrice)
|
|
80
|
+
.abs();
|
|
81
|
+
return [pctAvgSlippage, pctMaxSlippage, entryPrice, newPrice];
|
|
82
|
+
}
|
|
83
|
+
exports.calculateTradeSlippage = calculateTradeSlippage;
|
|
84
|
+
/**
|
|
85
|
+
* Calculates acquired amounts for trade executed
|
|
86
|
+
* @param direction
|
|
87
|
+
* @param amount
|
|
88
|
+
* @param market
|
|
89
|
+
* @param inputAssetType
|
|
90
|
+
* @param useSpread
|
|
91
|
+
* @return
|
|
92
|
+
* | 'acquiredBase' => positive/negative change in user's base : BN AMM_RESERVE_PRECISION
|
|
93
|
+
* | 'acquiredQuote' => positive/negative change in user's quote : BN TODO-PRECISION
|
|
94
|
+
*/
|
|
95
|
+
function calculateTradeAcquiredAmounts(direction, amount, market, inputAssetType = 'quote', oraclePriceData, useSpread = true) {
|
|
96
|
+
if (amount.eq(numericConstants_1.ZERO)) {
|
|
97
|
+
return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
|
|
98
|
+
}
|
|
99
|
+
const swapDirection = amm_1.getSwapDirection(inputAssetType, direction);
|
|
100
|
+
let amm;
|
|
101
|
+
if (useSpread && market.amm.baseSpread > 0) {
|
|
102
|
+
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
|
|
103
|
+
amm = {
|
|
104
|
+
baseAssetReserve,
|
|
105
|
+
quoteAssetReserve,
|
|
106
|
+
sqrtK: sqrtK,
|
|
107
|
+
pegMultiplier: newPeg,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
amm = market.amm;
|
|
112
|
+
}
|
|
113
|
+
const [newQuoteAssetReserve, newBaseAssetReserve] = amm_1.calculateAmmReservesAfterSwap(amm, inputAssetType, amount, swapDirection);
|
|
114
|
+
const acquiredBase = amm.baseAssetReserve.sub(newBaseAssetReserve);
|
|
115
|
+
const acquiredQuote = amm.quoteAssetReserve.sub(newQuoteAssetReserve);
|
|
116
|
+
const acquiredQuoteAssetamount = amm_1.calculateQuoteAssetAmountSwapped(acquiredQuote.abs(), amm.pegMultiplier, swapDirection);
|
|
117
|
+
return [acquiredBase, acquiredQuote, acquiredQuoteAssetamount];
|
|
118
|
+
}
|
|
119
|
+
exports.calculateTradeAcquiredAmounts = calculateTradeAcquiredAmounts;
|
|
120
|
+
/**
|
|
121
|
+
* calculateTargetPriceTrade
|
|
122
|
+
* simple function for finding arbitraging trades
|
|
123
|
+
* @param market
|
|
124
|
+
* @param targetPrice
|
|
125
|
+
* @param pct optional default is 100% gap filling, can set smaller.
|
|
126
|
+
* @param outputAssetType which asset to trade.
|
|
127
|
+
* @param useSpread whether or not to consider the spread when calculating the trade size
|
|
128
|
+
* @returns trade direction/size in order to push price to a targetPrice,
|
|
129
|
+
*
|
|
130
|
+
* [
|
|
131
|
+
* direction => direction of trade required, PositionDirection
|
|
132
|
+
* tradeSize => size of trade required, TODO-PRECISION
|
|
133
|
+
* entryPrice => the entry price for the trade, MARK_PRICE_PRECISION
|
|
134
|
+
* targetPrice => the target price MARK_PRICE_PRECISION
|
|
135
|
+
* ]
|
|
136
|
+
*/
|
|
137
|
+
function calculateTargetPriceTrade(market, targetPrice, pct = MAXPCT, outputAssetType = 'quote', oraclePriceData, useSpread = true) {
|
|
138
|
+
assert_1.assert(market.amm.baseAssetReserve.gt(numericConstants_1.ZERO));
|
|
139
|
+
assert_1.assert(targetPrice.gt(numericConstants_1.ZERO));
|
|
140
|
+
assert_1.assert(pct.lte(MAXPCT) && pct.gt(numericConstants_1.ZERO));
|
|
141
|
+
const markPriceBefore = market_1.calculateMarkPrice(market, oraclePriceData);
|
|
142
|
+
const bidPriceBefore = market_1.calculateBidPrice(market, oraclePriceData);
|
|
143
|
+
const askPriceBefore = market_1.calculateAskPrice(market, oraclePriceData);
|
|
144
|
+
let direction;
|
|
145
|
+
if (targetPrice.gt(markPriceBefore)) {
|
|
146
|
+
const priceGap = targetPrice.sub(markPriceBefore);
|
|
147
|
+
const priceGapScaled = priceGap.mul(pct).div(MAXPCT);
|
|
148
|
+
targetPrice = markPriceBefore.add(priceGapScaled);
|
|
149
|
+
direction = types_1.PositionDirection.LONG;
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
const priceGap = markPriceBefore.sub(targetPrice);
|
|
153
|
+
const priceGapScaled = priceGap.mul(pct).div(MAXPCT);
|
|
154
|
+
targetPrice = markPriceBefore.sub(priceGapScaled);
|
|
155
|
+
direction = types_1.PositionDirection.SHORT;
|
|
156
|
+
}
|
|
157
|
+
let tradeSize;
|
|
158
|
+
let baseSize;
|
|
159
|
+
let baseAssetReserveBefore;
|
|
160
|
+
let quoteAssetReserveBefore;
|
|
161
|
+
let peg = market.amm.pegMultiplier;
|
|
162
|
+
if (useSpread && market.amm.baseSpread > 0) {
|
|
163
|
+
const { baseAssetReserve, quoteAssetReserve, newPeg } = amm_1.calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
|
|
164
|
+
baseAssetReserveBefore = baseAssetReserve;
|
|
165
|
+
quoteAssetReserveBefore = quoteAssetReserve;
|
|
166
|
+
peg = newPeg;
|
|
167
|
+
}
|
|
168
|
+
else {
|
|
169
|
+
baseAssetReserveBefore = market.amm.baseAssetReserve;
|
|
170
|
+
quoteAssetReserveBefore = market.amm.quoteAssetReserve;
|
|
171
|
+
}
|
|
172
|
+
const invariant = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
173
|
+
const k = invariant.mul(numericConstants_1.MARK_PRICE_PRECISION);
|
|
174
|
+
let baseAssetReserveAfter;
|
|
175
|
+
let quoteAssetReserveAfter;
|
|
176
|
+
const biasModifier = new anchor_1.BN(1);
|
|
177
|
+
let markPriceAfter;
|
|
178
|
+
if (useSpread &&
|
|
179
|
+
targetPrice.lt(askPriceBefore) &&
|
|
180
|
+
targetPrice.gt(bidPriceBefore)) {
|
|
181
|
+
// no trade, market is at target
|
|
182
|
+
if (markPriceBefore.gt(targetPrice)) {
|
|
183
|
+
direction = types_1.PositionDirection.SHORT;
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
direction = types_1.PositionDirection.LONG;
|
|
187
|
+
}
|
|
188
|
+
tradeSize = numericConstants_1.ZERO;
|
|
189
|
+
return [direction, tradeSize, targetPrice, targetPrice];
|
|
190
|
+
}
|
|
191
|
+
else if (markPriceBefore.gt(targetPrice)) {
|
|
192
|
+
// overestimate y2
|
|
193
|
+
baseAssetReserveAfter = utils_1.squareRootBN(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).sub(biasModifier)).sub(new anchor_1.BN(1));
|
|
194
|
+
quoteAssetReserveAfter = k
|
|
195
|
+
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
196
|
+
.div(baseAssetReserveAfter);
|
|
197
|
+
markPriceAfter = amm_1.calculatePrice(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
|
|
198
|
+
direction = types_1.PositionDirection.SHORT;
|
|
199
|
+
tradeSize = quoteAssetReserveBefore
|
|
200
|
+
.sub(quoteAssetReserveAfter)
|
|
201
|
+
.mul(peg)
|
|
202
|
+
.div(numericConstants_1.PEG_PRECISION)
|
|
203
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
204
|
+
baseSize = baseAssetReserveAfter.sub(baseAssetReserveBefore);
|
|
205
|
+
}
|
|
206
|
+
else if (markPriceBefore.lt(targetPrice)) {
|
|
207
|
+
// underestimate y2
|
|
208
|
+
baseAssetReserveAfter = utils_1.squareRootBN(k.div(targetPrice).mul(peg).div(numericConstants_1.PEG_PRECISION).add(biasModifier)).add(new anchor_1.BN(1));
|
|
209
|
+
quoteAssetReserveAfter = k
|
|
210
|
+
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
211
|
+
.div(baseAssetReserveAfter);
|
|
212
|
+
markPriceAfter = amm_1.calculatePrice(baseAssetReserveAfter, quoteAssetReserveAfter, peg);
|
|
213
|
+
direction = types_1.PositionDirection.LONG;
|
|
214
|
+
tradeSize = quoteAssetReserveAfter
|
|
215
|
+
.sub(quoteAssetReserveBefore)
|
|
216
|
+
.mul(peg)
|
|
217
|
+
.div(numericConstants_1.PEG_PRECISION)
|
|
218
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
219
|
+
baseSize = baseAssetReserveBefore.sub(baseAssetReserveAfter);
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
// no trade, market is at target
|
|
223
|
+
direction = types_1.PositionDirection.LONG;
|
|
224
|
+
tradeSize = numericConstants_1.ZERO;
|
|
225
|
+
return [direction, tradeSize, targetPrice, targetPrice];
|
|
226
|
+
}
|
|
227
|
+
let tp1 = targetPrice;
|
|
228
|
+
let tp2 = markPriceAfter;
|
|
229
|
+
let originalDiff = targetPrice.sub(markPriceBefore);
|
|
230
|
+
if (direction == types_1.PositionDirection.SHORT) {
|
|
231
|
+
tp1 = markPriceAfter;
|
|
232
|
+
tp2 = targetPrice;
|
|
233
|
+
originalDiff = markPriceBefore.sub(targetPrice);
|
|
234
|
+
}
|
|
235
|
+
const entryPrice = tradeSize
|
|
236
|
+
.mul(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
237
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
238
|
+
.div(baseSize.abs());
|
|
239
|
+
assert_1.assert(tp1.sub(tp2).lte(originalDiff), 'Target Price Calculation incorrect');
|
|
240
|
+
assert_1.assert(tp2.lte(tp1) || tp2.sub(tp1).abs() < 100000, 'Target Price Calculation incorrect' +
|
|
241
|
+
tp2.toString() +
|
|
242
|
+
'>=' +
|
|
243
|
+
tp1.toString() +
|
|
244
|
+
'err: ' +
|
|
245
|
+
tp2.sub(tp1).abs().toString());
|
|
246
|
+
if (outputAssetType == 'quote') {
|
|
247
|
+
return [direction, tradeSize, entryPrice, targetPrice];
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
return [direction, baseSize, entryPrice, targetPrice];
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
exports.calculateTargetPriceTrade = calculateTargetPriceTrade;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.squareRootBN = void 0;
|
|
4
|
+
const __1 = require("../");
|
|
5
|
+
const squareRootBN = (n, closeness = new __1.BN(1)) => {
|
|
6
|
+
// Assuming the sqrt of n as n only
|
|
7
|
+
let x = n;
|
|
8
|
+
// The closed guess will be stored in the root
|
|
9
|
+
let root;
|
|
10
|
+
// To count the number of iterations
|
|
11
|
+
let count = 0;
|
|
12
|
+
const TWO = new __1.BN(2);
|
|
13
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
14
|
+
while (count < Number.MAX_SAFE_INTEGER) {
|
|
15
|
+
count++;
|
|
16
|
+
// Calculate more closed x
|
|
17
|
+
root = x.add(n.div(x)).div(TWO);
|
|
18
|
+
// Check for closeness
|
|
19
|
+
if (x.sub(root).abs().lte(closeness))
|
|
20
|
+
break;
|
|
21
|
+
// Update root
|
|
22
|
+
x = root;
|
|
23
|
+
}
|
|
24
|
+
return root;
|
|
25
|
+
};
|
|
26
|
+
exports.squareRootBN = squareRootBN;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.js","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":";;;AAAA,2BAAyB;AAElB,MAAM,YAAY,GAAG,CAAC,CAAC,EAAE,SAAS,GAAG,IAAI,MAAE,CAAC,CAAC,CAAC,EAAE,EAAE;IACxD,mCAAmC;IACnC,IAAI,CAAC,GAAG,CAAC,CAAC;IAEV,8CAA8C;IAC9C,IAAI,IAAI,CAAC;IAET,oCAAoC;IACpC,IAAI,KAAK,GAAG,CAAC,CAAC;IACd,MAAM,GAAG,GAAG,IAAI,MAAE,CAAC,CAAC,CAAC,CAAC;IAEtB,6DAA6D;IAC7D,OAAO,KAAK,GAAG,MAAM,CAAC,gBAAgB,EAAE;QACvC,KAAK,EAAE,CAAC;QAER,0BAA0B;QAC1B,IAAI,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;QAEhC,sBAAsB;QACtB,IAAI,CAAC,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,SAAS,CAAC;YAAE,MAAM;QAE5C,cAAc;QACd,CAAC,GAAG,IAAI,CAAC;KACT;IAED,OAAO,IAAI,CAAC;AACb,CAAC,CAAC;AA1BW,QAAA,YAAY,gBA0BvB"}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.OracleClientCache = void 0;
|
|
4
|
+
const oracleClient_1 = require("../factory/oracleClient");
|
|
5
|
+
class OracleClientCache {
|
|
6
|
+
constructor() {
|
|
7
|
+
this.cache = new Map();
|
|
8
|
+
}
|
|
9
|
+
get(oracleSource, connection) {
|
|
10
|
+
const key = Object.keys(oracleSource)[0];
|
|
11
|
+
if (this.cache.has(key)) {
|
|
12
|
+
return this.cache.get(key);
|
|
13
|
+
}
|
|
14
|
+
const client = oracleClient_1.getOracleClient(oracleSource, connection);
|
|
15
|
+
this.cache.set(key, client);
|
|
16
|
+
return client;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
exports.OracleClientCache = OracleClientCache;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.convertPythPrice = exports.PythClient = void 0;
|
|
13
|
+
const client_1 = require("@pythnetwork/client");
|
|
14
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
15
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
16
|
+
class PythClient {
|
|
17
|
+
constructor(connection) {
|
|
18
|
+
this.connection = connection;
|
|
19
|
+
}
|
|
20
|
+
getOraclePriceData(pricePublicKey) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const accountInfo = yield this.connection.getAccountInfo(pricePublicKey);
|
|
23
|
+
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
getOraclePriceDataFromBuffer(buffer) {
|
|
27
|
+
const priceData = client_1.parsePriceData(buffer);
|
|
28
|
+
return {
|
|
29
|
+
price: convertPythPrice(priceData.aggregate.price, priceData.exponent),
|
|
30
|
+
slot: new anchor_1.BN(priceData.lastSlot.toString()),
|
|
31
|
+
confidence: convertPythPrice(priceData.confidence, priceData.exponent),
|
|
32
|
+
twap: convertPythPrice(priceData.twap.value, priceData.exponent),
|
|
33
|
+
twapConfidence: convertPythPrice(priceData.twac.value, priceData.exponent),
|
|
34
|
+
hasSufficientNumberOfDataPoints: true,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.PythClient = PythClient;
|
|
39
|
+
function convertPythPrice(price, exponent) {
|
|
40
|
+
exponent = Math.abs(exponent);
|
|
41
|
+
const pythPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(exponent).abs());
|
|
42
|
+
return new anchor_1.BN(price * Math.pow(10, exponent))
|
|
43
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
44
|
+
.div(pythPrecision);
|
|
45
|
+
}
|
|
46
|
+
exports.convertPythPrice = convertPythPrice;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.QuoteAssetOracleClient = exports.QUOTE_ORACLE_PRICE_DATA = void 0;
|
|
13
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
14
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
15
|
+
exports.QUOTE_ORACLE_PRICE_DATA = {
|
|
16
|
+
price: numericConstants_1.MARK_PRICE_PRECISION,
|
|
17
|
+
slot: new anchor_1.BN(0),
|
|
18
|
+
confidence: new anchor_1.BN(1),
|
|
19
|
+
hasSufficientNumberOfDataPoints: true,
|
|
20
|
+
};
|
|
21
|
+
class QuoteAssetOracleClient {
|
|
22
|
+
constructor() { }
|
|
23
|
+
getOraclePriceData(_pricePublicKey) {
|
|
24
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
25
|
+
return Promise.resolve(exports.QUOTE_ORACLE_PRICE_DATA);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
getOraclePriceDataFromBuffer(_buffer) {
|
|
29
|
+
return exports.QUOTE_ORACLE_PRICE_DATA;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
exports.QuoteAssetOracleClient = QuoteAssetOracleClient;
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.SwitchboardClient = void 0;
|
|
16
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
17
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
18
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
19
|
+
const wallet_1 = require("../wallet");
|
|
20
|
+
const switchboard_v2_json_1 = __importDefault(require("../idl/switchboard_v2.json"));
|
|
21
|
+
let program;
|
|
22
|
+
class SwitchboardClient {
|
|
23
|
+
constructor(connection) {
|
|
24
|
+
this.connection = connection;
|
|
25
|
+
}
|
|
26
|
+
getOraclePriceData(pricePublicKey) {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const accountInfo = yield this.connection.getAccountInfo(pricePublicKey);
|
|
29
|
+
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
getOraclePriceDataFromBuffer(buffer) {
|
|
33
|
+
const program = this.getProgram();
|
|
34
|
+
const aggregatorAccountData = program.account.aggregatorAccountData.coder.accounts.decode('AggregatorAccountData', buffer);
|
|
35
|
+
const price = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound.result);
|
|
36
|
+
const confidence = convertSwitchboardDecimal(aggregatorAccountData.latestConfirmedRound
|
|
37
|
+
.stdDeviation);
|
|
38
|
+
const hasSufficientNumberOfDataPoints = aggregatorAccountData.latestConfirmedRound.numSuccess >=
|
|
39
|
+
aggregatorAccountData.minOracleResults;
|
|
40
|
+
const slot = aggregatorAccountData.latestConfirmedRound.roundOpenSlot;
|
|
41
|
+
return {
|
|
42
|
+
price,
|
|
43
|
+
slot,
|
|
44
|
+
confidence,
|
|
45
|
+
hasSufficientNumberOfDataPoints,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
getProgram() {
|
|
49
|
+
if (program) {
|
|
50
|
+
return program;
|
|
51
|
+
}
|
|
52
|
+
program = getSwitchboardProgram(this.connection);
|
|
53
|
+
return program;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
exports.SwitchboardClient = SwitchboardClient;
|
|
57
|
+
function getSwitchboardProgram(connection) {
|
|
58
|
+
const DEFAULT_KEYPAIR = web3_js_1.Keypair.fromSeed(new Uint8Array(32).fill(1));
|
|
59
|
+
const programId = web3_js_1.PublicKey.default;
|
|
60
|
+
const wallet = new wallet_1.Wallet(DEFAULT_KEYPAIR);
|
|
61
|
+
const provider = new anchor_1.AnchorProvider(connection, wallet, {});
|
|
62
|
+
return new anchor_1.Program(switchboard_v2_json_1.default, programId, provider);
|
|
63
|
+
}
|
|
64
|
+
function convertSwitchboardDecimal(switchboardDecimal) {
|
|
65
|
+
const switchboardPrecision = numericConstants_1.TEN.pow(new anchor_1.BN(switchboardDecimal.scale));
|
|
66
|
+
return switchboardDecimal.mantissa
|
|
67
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
68
|
+
.div(switchboardPrecision);
|
|
69
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getMarketOrderParams = exports.getTriggerLimitOrderParams = exports.getTriggerMarketOrderParams = exports.getLimitOrderParams = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
function getLimitOrderParams(params) {
|
|
6
|
+
return Object.assign({}, params, { orderType: types_1.OrderType.LIMIT });
|
|
7
|
+
}
|
|
8
|
+
exports.getLimitOrderParams = getLimitOrderParams;
|
|
9
|
+
function getTriggerMarketOrderParams(params) {
|
|
10
|
+
return Object.assign({}, params, { orderType: types_1.OrderType.TRIGGER_MARKET });
|
|
11
|
+
}
|
|
12
|
+
exports.getTriggerMarketOrderParams = getTriggerMarketOrderParams;
|
|
13
|
+
function getTriggerLimitOrderParams(params) {
|
|
14
|
+
return Object.assign({}, params, { orderType: types_1.OrderType.TRIGGER_LIMIT });
|
|
15
|
+
}
|
|
16
|
+
exports.getTriggerLimitOrderParams = getTriggerLimitOrderParams;
|
|
17
|
+
function getMarketOrderParams(params) {
|
|
18
|
+
return Object.assign({}, params, { orderType: types_1.OrderType.MARKET });
|
|
19
|
+
}
|
|
20
|
+
exports.getMarketOrderParams = getMarketOrderParams;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.SlotSubscriber = void 0;
|
|
13
|
+
const events_1 = require("events");
|
|
14
|
+
class SlotSubscriber {
|
|
15
|
+
constructor(connection, _config) {
|
|
16
|
+
this.connection = connection;
|
|
17
|
+
this.eventEmitter = new events_1.EventEmitter();
|
|
18
|
+
}
|
|
19
|
+
subscribe() {
|
|
20
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
21
|
+
this.currentSlot = yield this.connection.getSlot('confirmed');
|
|
22
|
+
this.subscriptionId = this.connection.onSlotChange((slotInfo) => {
|
|
23
|
+
this.currentSlot = slotInfo.slot;
|
|
24
|
+
this.eventEmitter.emit('newSlot', slotInfo.slot);
|
|
25
|
+
});
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
getSlot() {
|
|
29
|
+
return this.currentSlot;
|
|
30
|
+
}
|
|
31
|
+
unsubscribe() {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
if (this.subscriptionId) {
|
|
34
|
+
yield this.connection.removeSlotChangeListener(this.subscriptionId);
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
exports.SlotSubscriber = SlotSubscriber;
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.parseTokenAccount = void 0;
|
|
4
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
function parseTokenAccount(data) {
|
|
7
|
+
const accountInfo = spl_token_1.AccountLayout.decode(data);
|
|
8
|
+
accountInfo.mint = new web3_js_1.PublicKey(accountInfo.mint);
|
|
9
|
+
accountInfo.owner = new web3_js_1.PublicKey(accountInfo.owner);
|
|
10
|
+
accountInfo.amount = spl_token_1.u64.fromBuffer(accountInfo.amount);
|
|
11
|
+
if (accountInfo.delegateOption === 0) {
|
|
12
|
+
accountInfo.delegate = null;
|
|
13
|
+
// eslint-disable-next-line new-cap
|
|
14
|
+
accountInfo.delegatedAmount = new spl_token_1.u64(0);
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
accountInfo.delegate = new web3_js_1.PublicKey(accountInfo.delegate);
|
|
18
|
+
accountInfo.delegatedAmount = spl_token_1.u64.fromBuffer(accountInfo.delegatedAmount);
|
|
19
|
+
}
|
|
20
|
+
accountInfo.isInitialized = accountInfo.state !== 0;
|
|
21
|
+
accountInfo.isFrozen = accountInfo.state === 2;
|
|
22
|
+
if (accountInfo.isNativeOption === 1) {
|
|
23
|
+
accountInfo.rentExemptReserve = spl_token_1.u64.fromBuffer(accountInfo.isNative);
|
|
24
|
+
accountInfo.isNative = true;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
accountInfo.rentExemptReserve = null;
|
|
28
|
+
accountInfo.isNative = false;
|
|
29
|
+
}
|
|
30
|
+
if (accountInfo.closeAuthorityOption === 0) {
|
|
31
|
+
accountInfo.closeAuthority = null;
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
accountInfo.closeAuthority = new web3_js_1.PublicKey(accountInfo.closeAuthority);
|
|
35
|
+
}
|
|
36
|
+
return accountInfo;
|
|
37
|
+
}
|
|
38
|
+
exports.parseTokenAccount = parseTokenAccount;
|