@drift-labs/sdk 0.2.0-master.7 → 0.2.0-master.8
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/clearingHouse.js +38 -25
- package/lib/constants/banks.js +9 -1
- package/package.json +1 -1
- package/src/accounts/bulkAccountLoader.js +197 -0
- package/src/accounts/bulkUserSubscription.js +33 -0
- package/src/accounts/fetch.js +29 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
- package/src/accounts/pollingOracleSubscriber.js +93 -0
- package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
- package/src/accounts/pollingUserAccountSubscriber.js +132 -0
- package/src/accounts/types.js +10 -0
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +93 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
- package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/pda.js +104 -0
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +43 -27
- package/src/constants/banks.ts +9 -1
- package/src/events/eventList.js +77 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/fetchLogs.js +50 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/sort.js +44 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/types.js +20 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/factory/bigNum.js +364 -0
- package/src/factory/oracleClient.js +20 -0
- package/src/index.js +69 -0
- package/src/math/amm.js +369 -0
- package/src/math/auction.js +42 -0
- package/src/math/bankBalance.js +75 -0
- package/src/math/conversion.js +11 -0
- package/src/math/funding.js +248 -0
- package/src/math/market.js +57 -0
- package/src/math/oracles.js +26 -0
- package/src/math/orders.js +110 -0
- package/src/math/position.js +140 -0
- 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 +0 -1
- package/src/mockUSDCFaucet.js +171 -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/orders.js +134 -0
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/token/index.js +38 -0
- package/src/tx/retryTxSender.js +188 -0
- package/src/tx/types.js +2 -0
- package/src/tx/utils.js +17 -0
- package/src/types.js +114 -0
- package/src/userName.js +20 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/src/util/computeUnits.js +0 -17
- package/src/util/computeUnits.js.map +0 -1
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateBudgetedPeg = exports.calculateBudgetedK = exports.calculateBudgetedKBN = exports.calculateRepegCost = exports.calculateAdjustKCost = void 0;
|
|
4
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
5
|
+
const assert_1 = require("../assert/assert");
|
|
6
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
7
|
+
/**
|
|
8
|
+
* Helper function calculating adjust k cost
|
|
9
|
+
* @param amm
|
|
10
|
+
* @param numerator
|
|
11
|
+
* @param denomenator
|
|
12
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
13
|
+
*/
|
|
14
|
+
function calculateAdjustKCost(amm, numerator, denomenator) {
|
|
15
|
+
// const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
16
|
+
const x = amm.baseAssetReserve;
|
|
17
|
+
const y = amm.quoteAssetReserve;
|
|
18
|
+
const d = amm.netBaseAssetAmount;
|
|
19
|
+
const Q = amm.pegMultiplier;
|
|
20
|
+
const quoteScale = y.mul(d).mul(Q); //.div(AMM_RESERVE_PRECISION);
|
|
21
|
+
const p = numerator.mul(numericConstants_1.MARK_PRICE_PRECISION).div(denomenator);
|
|
22
|
+
const cost = quoteScale
|
|
23
|
+
.div(x.add(d))
|
|
24
|
+
.sub(quoteScale
|
|
25
|
+
.mul(p)
|
|
26
|
+
.div(numericConstants_1.MARK_PRICE_PRECISION)
|
|
27
|
+
.div(x.mul(p).div(numericConstants_1.MARK_PRICE_PRECISION).add(d)))
|
|
28
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
29
|
+
.div(numericConstants_1.PEG_PRECISION);
|
|
30
|
+
return cost.mul(new anchor_1.BN(-1));
|
|
31
|
+
}
|
|
32
|
+
exports.calculateAdjustKCost = calculateAdjustKCost;
|
|
33
|
+
/**
|
|
34
|
+
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
35
|
+
*
|
|
36
|
+
* @param amm
|
|
37
|
+
* @param newPeg
|
|
38
|
+
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
39
|
+
*/
|
|
40
|
+
function calculateRepegCost(amm, newPeg) {
|
|
41
|
+
const dqar = amm.quoteAssetReserve.sub(amm.terminalQuoteAssetReserve);
|
|
42
|
+
const cost = dqar
|
|
43
|
+
.mul(newPeg.sub(amm.pegMultiplier))
|
|
44
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO)
|
|
45
|
+
.div(numericConstants_1.PEG_PRECISION);
|
|
46
|
+
return cost;
|
|
47
|
+
}
|
|
48
|
+
exports.calculateRepegCost = calculateRepegCost;
|
|
49
|
+
function calculateBudgetedKBN(x, y, budget, Q, d) {
|
|
50
|
+
assert_1.assert(Q.gt(new anchor_1.BN(0)));
|
|
51
|
+
const C = budget.mul(new anchor_1.BN(-1));
|
|
52
|
+
let dSign = new anchor_1.BN(1);
|
|
53
|
+
if (d.lt(new anchor_1.BN(0))) {
|
|
54
|
+
dSign = new anchor_1.BN(-1);
|
|
55
|
+
}
|
|
56
|
+
const pegged_y_d_d = y
|
|
57
|
+
.mul(d)
|
|
58
|
+
.mul(d)
|
|
59
|
+
.mul(Q)
|
|
60
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
61
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
62
|
+
.div(numericConstants_1.PEG_PRECISION);
|
|
63
|
+
const numer1 = pegged_y_d_d;
|
|
64
|
+
const numer2 = C.mul(d)
|
|
65
|
+
.div(numericConstants_1.QUOTE_PRECISION)
|
|
66
|
+
.mul(x.add(d))
|
|
67
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
68
|
+
.mul(dSign);
|
|
69
|
+
const denom1 = C.mul(x)
|
|
70
|
+
.mul(x.add(d))
|
|
71
|
+
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
72
|
+
.div(numericConstants_1.QUOTE_PRECISION);
|
|
73
|
+
const denom2 = pegged_y_d_d;
|
|
74
|
+
const numerator = numer1.sub(numer2).div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
75
|
+
const denominator = denom1.add(denom2).div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
76
|
+
return [numerator, denominator];
|
|
77
|
+
}
|
|
78
|
+
exports.calculateBudgetedKBN = calculateBudgetedKBN;
|
|
79
|
+
function calculateBudgetedK(amm, cost) {
|
|
80
|
+
// wolframalpha.com
|
|
81
|
+
// (1/(x+d) - p/(x*p+d))*y*d*Q = C solve for p
|
|
82
|
+
// p = (d(y*d*Q - C(x+d))) / (C*x(x+d) + y*d*d*Q)
|
|
83
|
+
// numer
|
|
84
|
+
// = y*d*d*Q - Cxd - Cdd
|
|
85
|
+
// = y/x*Q*d*d - Cd - Cd/x
|
|
86
|
+
// = mark - C/d - C/(x)
|
|
87
|
+
// = mark/C - 1/d - 1/x
|
|
88
|
+
// denom
|
|
89
|
+
// = C*x*x + C*x*d + y*d*d*Q
|
|
90
|
+
// = x/d**2 + 1 / d + mark/C
|
|
91
|
+
// todo: assumes k = x * y
|
|
92
|
+
// otherwise use: (y(1-p) + (kp^2/(x*p+d)) - k/(x+d)) * Q = C solve for p
|
|
93
|
+
const x = amm.baseAssetReserve;
|
|
94
|
+
const y = amm.quoteAssetReserve;
|
|
95
|
+
const d = amm.netBaseAssetAmount;
|
|
96
|
+
const Q = amm.pegMultiplier;
|
|
97
|
+
const [numerator, denominator] = calculateBudgetedKBN(x, y, cost, Q, d);
|
|
98
|
+
return [numerator, denominator];
|
|
99
|
+
}
|
|
100
|
+
exports.calculateBudgetedK = calculateBudgetedK;
|
|
101
|
+
function calculateBudgetedPeg(amm, cost, targetPrice) {
|
|
102
|
+
// wolframalpha.com
|
|
103
|
+
// (1/(x+d) - p/(x*p+d))*y*d*Q = C solve for p
|
|
104
|
+
// p = (d(y*d*Q - C(x+d))) / (C*x(x+d) + y*y*d*Q)
|
|
105
|
+
// todo: assumes k = x * y
|
|
106
|
+
// otherwise use: (y(1-p) + (kp^2/(x*p+d)) - k/(x+d)) * Q = C solve for p
|
|
107
|
+
const targetPeg = targetPrice
|
|
108
|
+
.mul(amm.baseAssetReserve)
|
|
109
|
+
.div(amm.quoteAssetReserve)
|
|
110
|
+
.div(numericConstants_1.PRICE_DIV_PEG);
|
|
111
|
+
const k = amm.sqrtK.mul(amm.sqrtK);
|
|
112
|
+
const x = amm.baseAssetReserve;
|
|
113
|
+
const y = amm.quoteAssetReserve;
|
|
114
|
+
const d = amm.netBaseAssetAmount;
|
|
115
|
+
const Q = amm.pegMultiplier;
|
|
116
|
+
const C = cost.mul(new anchor_1.BN(-1));
|
|
117
|
+
const deltaQuoteAssetReserves = y.sub(k.div(x.add(d)));
|
|
118
|
+
const pegChangeDirection = targetPeg.sub(Q);
|
|
119
|
+
const useTargetPeg = (deltaQuoteAssetReserves.lt(numericConstants_1.ZERO) && pegChangeDirection.gt(numericConstants_1.ZERO)) ||
|
|
120
|
+
(deltaQuoteAssetReserves.gt(numericConstants_1.ZERO) && pegChangeDirection.lt(numericConstants_1.ZERO));
|
|
121
|
+
if (deltaQuoteAssetReserves.eq(numericConstants_1.ZERO) || useTargetPeg) {
|
|
122
|
+
return targetPeg;
|
|
123
|
+
}
|
|
124
|
+
const deltaPegMultiplier = C.mul(numericConstants_1.MARK_PRICE_PRECISION).div(deltaQuoteAssetReserves.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO));
|
|
125
|
+
const newPeg = Q.sub(deltaPegMultiplier.mul(numericConstants_1.PEG_PRECISION).div(numericConstants_1.MARK_PRICE_PRECISION));
|
|
126
|
+
return newPeg;
|
|
127
|
+
}
|
|
128
|
+
exports.calculateBudgetedPeg = calculateBudgetedPeg;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getExchangeFee = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Get the clearing house percent fee charged on notional of taking trades
|
|
6
|
+
*
|
|
7
|
+
* @param state
|
|
8
|
+
* @returns Precision : basis points (bps)
|
|
9
|
+
*/
|
|
10
|
+
function getExchangeFee(state) {
|
|
11
|
+
const exchangeFee = state.feeStructure.feeNumerator.toNumber() /
|
|
12
|
+
state.feeStructure.feeDenominator.toNumber();
|
|
13
|
+
return exchangeFee;
|
|
14
|
+
}
|
|
15
|
+
exports.getExchangeFee = getExchangeFee;
|
|
@@ -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;
|
package/src/math/utils.js
CHANGED
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
10
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
11
|
+
}) : function(o, v) {
|
|
12
|
+
o["default"] = v;
|
|
13
|
+
});
|
|
14
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
15
|
+
if (mod && mod.__esModule) return mod;
|
|
16
|
+
var result = {};
|
|
17
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
18
|
+
__setModuleDefault(result, mod);
|
|
19
|
+
return result;
|
|
20
|
+
};
|
|
21
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
22
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
23
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
24
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
25
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
26
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
27
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
28
|
+
});
|
|
29
|
+
};
|
|
30
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
31
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
32
|
+
};
|
|
33
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
34
|
+
exports.MockUSDCFaucet = void 0;
|
|
35
|
+
const anchor = __importStar(require("@project-serum/anchor"));
|
|
36
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
37
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
38
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
39
|
+
const mock_usdc_faucet_json_1 = __importDefault(require("./idl/mock_usdc_faucet.json"));
|
|
40
|
+
class MockUSDCFaucet {
|
|
41
|
+
constructor(connection, wallet, programId, opts) {
|
|
42
|
+
this.connection = connection;
|
|
43
|
+
this.wallet = wallet;
|
|
44
|
+
this.opts = opts || anchor_1.AnchorProvider.defaultOptions();
|
|
45
|
+
const provider = new anchor_1.AnchorProvider(connection, wallet, this.opts);
|
|
46
|
+
this.provider = provider;
|
|
47
|
+
this.program = new anchor_1.Program(mock_usdc_faucet_json_1.default, programId, provider);
|
|
48
|
+
}
|
|
49
|
+
getMockUSDCFaucetStatePublicKeyAndNonce() {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
return anchor.web3.PublicKey.findProgramAddress([Buffer.from(anchor.utils.bytes.utf8.encode('mock_usdc_faucet'))], this.program.programId);
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
getMockUSDCFaucetStatePublicKey() {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
if (this.mockUSDCFaucetStatePublicKey) {
|
|
57
|
+
return this.mockUSDCFaucetStatePublicKey;
|
|
58
|
+
}
|
|
59
|
+
this.mockUSDCFaucetStatePublicKey = (yield this.getMockUSDCFaucetStatePublicKeyAndNonce())[0];
|
|
60
|
+
return this.mockUSDCFaucetStatePublicKey;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
initialize() {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
const stateAccountRPCResponse = yield this.connection.getParsedAccountInfo(yield this.getMockUSDCFaucetStatePublicKey());
|
|
66
|
+
if (stateAccountRPCResponse.value !== null) {
|
|
67
|
+
throw new Error('Faucet already initialized');
|
|
68
|
+
}
|
|
69
|
+
const fakeUSDCMint = anchor.web3.Keypair.generate();
|
|
70
|
+
const createUSDCMintAccountIx = web3_js_1.SystemProgram.createAccount({
|
|
71
|
+
fromPubkey: this.wallet.publicKey,
|
|
72
|
+
newAccountPubkey: fakeUSDCMint.publicKey,
|
|
73
|
+
lamports: yield spl_token_1.Token.getMinBalanceRentForExemptMint(this.connection),
|
|
74
|
+
space: spl_token_1.MintLayout.span,
|
|
75
|
+
programId: spl_token_1.TOKEN_PROGRAM_ID,
|
|
76
|
+
});
|
|
77
|
+
const [mintAuthority, _mintAuthorityNonce] = yield web3_js_1.PublicKey.findProgramAddress([fakeUSDCMint.publicKey.toBuffer()], this.program.programId);
|
|
78
|
+
const initUSDCMintIx = spl_token_1.Token.createInitMintInstruction(spl_token_1.TOKEN_PROGRAM_ID, fakeUSDCMint.publicKey, 6, mintAuthority, null);
|
|
79
|
+
const [mockUSDCFaucetStatePublicKey, mockUSDCFaucetStateNonce] = yield this.getMockUSDCFaucetStatePublicKeyAndNonce();
|
|
80
|
+
return yield this.program.rpc.initialize(mockUSDCFaucetStateNonce, {
|
|
81
|
+
accounts: {
|
|
82
|
+
mockUsdcFaucetState: mockUSDCFaucetStatePublicKey,
|
|
83
|
+
admin: this.wallet.publicKey,
|
|
84
|
+
mintAccount: fakeUSDCMint.publicKey,
|
|
85
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
86
|
+
systemProgram: anchor.web3.SystemProgram.programId,
|
|
87
|
+
},
|
|
88
|
+
instructions: [createUSDCMintAccountIx, initUSDCMintIx],
|
|
89
|
+
signers: [fakeUSDCMint],
|
|
90
|
+
});
|
|
91
|
+
});
|
|
92
|
+
}
|
|
93
|
+
fetchState() {
|
|
94
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
95
|
+
return yield this.program.account.mockUsdcFaucetState.fetch(yield this.getMockUSDCFaucetStatePublicKey());
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
mintToUser(userTokenAccount, amount) {
|
|
99
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
100
|
+
const state = yield this.fetchState();
|
|
101
|
+
return yield this.program.rpc.mintToUser(amount, {
|
|
102
|
+
accounts: {
|
|
103
|
+
mockUsdcFaucetState: yield this.getMockUSDCFaucetStatePublicKey(),
|
|
104
|
+
mintAccount: state.mint,
|
|
105
|
+
userTokenAccount,
|
|
106
|
+
mintAuthority: state.mintAuthority,
|
|
107
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
createAssociatedTokenAccountAndMintTo(userPublicKey, amount) {
|
|
113
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
114
|
+
const [associatedTokenPublicKey, createAssociatedAccountIx, mintToTx] = yield this.createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount);
|
|
115
|
+
const tx = new web3_js_1.Transaction().add(createAssociatedAccountIx).add(mintToTx);
|
|
116
|
+
const txSig = yield this.program.provider.sendAndConfirm(tx, [], this.opts);
|
|
117
|
+
return [associatedTokenPublicKey, txSig];
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
createAssociatedTokenAccountAndMintToInstructions(userPublicKey, amount) {
|
|
121
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
122
|
+
const state = yield this.fetchState();
|
|
123
|
+
const associateTokenPublicKey = yield this.getAssosciatedMockUSDMintAddress({ userPubKey: userPublicKey });
|
|
124
|
+
const createAssociatedAccountIx = spl_token_1.Token.createAssociatedTokenAccountInstruction(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, associateTokenPublicKey, userPublicKey, this.wallet.publicKey);
|
|
125
|
+
const mintToIx = yield this.program.instruction.mintToUser(amount, {
|
|
126
|
+
accounts: {
|
|
127
|
+
mockUsdcFaucetState: yield this.getMockUSDCFaucetStatePublicKey(),
|
|
128
|
+
mintAccount: state.mint,
|
|
129
|
+
userTokenAccount: associateTokenPublicKey,
|
|
130
|
+
mintAuthority: state.mintAuthority,
|
|
131
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
return [associateTokenPublicKey, createAssociatedAccountIx, mintToIx];
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
getAssosciatedMockUSDMintAddress(props) {
|
|
138
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
139
|
+
const state = yield this.fetchState();
|
|
140
|
+
return spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, state.mint, props.userPubKey);
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
getTokenAccountInfo(props) {
|
|
144
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
145
|
+
const assosciatedKey = yield this.getAssosciatedMockUSDMintAddress(props);
|
|
146
|
+
const state = yield this.fetchState();
|
|
147
|
+
const token = new spl_token_1.Token(this.connection, state.mint, spl_token_1.TOKEN_PROGRAM_ID,
|
|
148
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
149
|
+
// @ts-ignore
|
|
150
|
+
this.provider.payer);
|
|
151
|
+
return yield token.getAccountInfo(assosciatedKey);
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
subscribeToTokenAccount(props) {
|
|
155
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
156
|
+
try {
|
|
157
|
+
const tokenAccountKey = yield this.getAssosciatedMockUSDMintAddress(props);
|
|
158
|
+
props.callback(yield this.getTokenAccountInfo(props));
|
|
159
|
+
// Couldn't find a way to do it using anchor framework subscription, someone on serum discord recommended this way
|
|
160
|
+
this.connection.onAccountChange(tokenAccountKey, (_accountInfo /* accountInfo is a buffer which we don't know how to deserialize */) => __awaiter(this, void 0, void 0, function* () {
|
|
161
|
+
props.callback(yield this.getTokenAccountInfo(props));
|
|
162
|
+
}));
|
|
163
|
+
return true;
|
|
164
|
+
}
|
|
165
|
+
catch (e) {
|
|
166
|
+
return false;
|
|
167
|
+
}
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
exports.MockUSDCFaucet = MockUSDCFaucet;
|
|
@@ -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;
|