@drift-labs/sdk 0.1.36-master.7 → 0.2.0-master.10
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 +6 -6
- package/lib/accounts/bulkAccountLoader.js +81 -95
- package/lib/accounts/bulkUserSubscription.js +13 -57
- package/lib/accounts/fetch.d.ts +4 -0
- package/lib/accounts/fetch.js +18 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
- package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
- package/lib/accounts/pollingOracleSubscriber.js +37 -49
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
- package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
- package/lib/accounts/types.d.ts +34 -41
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
- package/lib/accounts/webSocketAccountSubscriber.js +39 -35
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
- package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
- package/lib/addresses/marketAddresses.d.ts +4 -0
- package/lib/addresses/marketAddresses.js +15 -0
- package/lib/addresses/pda.d.ts +12 -0
- package/lib/addresses/pda.js +83 -0
- package/lib/admin.d.ts +12 -18
- package/lib/admin.js +398 -558
- package/lib/clearingHouse.d.ts +83 -109
- package/lib/clearingHouse.js +832 -893
- package/lib/clearingHouseConfig.d.ts +34 -0
- package/lib/clearingHouseConfig.js +2 -0
- package/lib/clearingHouseUser.d.ts +19 -22
- package/lib/clearingHouseUser.js +155 -141
- package/lib/clearingHouseUserConfig.d.ts +14 -0
- package/lib/clearingHouseUserConfig.js +2 -0
- package/lib/config.d.ts +12 -0
- package/lib/config.js +35 -4
- package/lib/constants/banks.d.ts +16 -0
- package/lib/constants/banks.js +41 -0
- package/lib/constants/markets.d.ts +8 -3
- package/lib/constants/markets.js +13 -206
- package/lib/constants/numericConstants.d.ts +17 -0
- package/lib/constants/numericConstants.js +23 -6
- package/lib/events/eventList.d.ts +22 -0
- package/lib/events/eventList.js +77 -0
- package/lib/events/eventSubscriber.d.ts +34 -0
- package/lib/events/eventSubscriber.js +126 -0
- package/lib/events/fetchLogs.d.ts +13 -0
- package/lib/events/fetchLogs.js +39 -0
- package/lib/events/pollingLogProvider.d.ts +15 -0
- package/lib/events/pollingLogProvider.js +53 -0
- package/lib/events/sort.d.ts +2 -0
- package/lib/events/sort.js +44 -0
- package/lib/events/txEventCache.d.ts +24 -0
- package/lib/events/txEventCache.js +71 -0
- package/lib/events/types.d.ts +49 -0
- package/lib/events/types.js +20 -0
- package/lib/events/webSocketLogProvider.d.ts +12 -0
- package/lib/events/webSocketLogProvider.js +30 -0
- package/lib/examples/makeTradeExample.js +26 -27
- package/lib/factory/bigNum.d.ts +118 -0
- package/lib/factory/bigNum.js +364 -0
- package/lib/factory/oracleClient.d.ts +1 -2
- package/lib/factory/oracleClient.js +6 -2
- package/lib/idl/clearing_house.json +2258 -2774
- package/lib/index.d.ts +14 -5
- package/lib/index.js +18 -5
- package/lib/math/amm.d.ts +24 -29
- package/lib/math/amm.js +209 -176
- package/lib/math/auction.d.ts +5 -0
- package/lib/math/auction.js +42 -0
- package/lib/math/bankBalance.d.ts +9 -0
- package/lib/math/bankBalance.js +75 -0
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -5
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +158 -175
- package/lib/math/market.d.ts +6 -6
- package/lib/math/market.js +10 -9
- package/lib/math/orders.d.ts +6 -1
- package/lib/math/orders.js +38 -1
- package/lib/math/position.d.ts +7 -5
- package/lib/math/position.js +29 -27
- package/lib/math/repeg.d.ts +22 -0
- package/lib/math/repeg.js +128 -0
- package/lib/math/trade.d.ts +5 -4
- package/lib/math/trade.js +29 -24
- package/lib/mockUSDCFaucet.js +87 -116
- package/lib/oracles/oracleClientCache.d.ts +8 -0
- package/lib/oracles/oracleClientCache.js +19 -0
- package/lib/oracles/pythClient.d.ts +3 -5
- package/lib/oracles/pythClient.js +12 -31
- package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
- package/lib/oracles/quoteAssetOracleClient.js +21 -0
- package/lib/oracles/switchboardClient.d.ts +3 -5
- package/lib/oracles/switchboardClient.js +29 -50
- package/lib/oracles/types.d.ts +6 -1
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/orders.d.ts +6 -7
- package/lib/orders.js +11 -89
- package/lib/slot/SlotSubscriber.d.ts +19 -0
- package/lib/slot/SlotSubscriber.js +26 -0
- package/lib/tx/retryTxSender.d.ts +2 -2
- package/lib/tx/retryTxSender.js +108 -123
- package/lib/tx/types.d.ts +5 -1
- package/lib/tx/utils.d.ts +1 -1
- package/lib/tx/utils.js +11 -2
- package/lib/types.d.ts +180 -110
- package/lib/types.js +54 -1
- package/lib/userName.d.ts +4 -0
- package/lib/userName.js +20 -0
- package/lib/util/computeUnits.js +10 -21
- package/lib/util/tps.js +11 -22
- package/lib/wallet.js +7 -20
- package/package.json +11 -4
- package/src/accounts/bulkAccountLoader.js +197 -0
- package/src/accounts/bulkAccountLoader.ts +21 -15
- package/src/accounts/bulkUserSubscription.js +33 -0
- package/src/accounts/bulkUserSubscription.ts +1 -45
- package/src/accounts/fetch.js +29 -0
- package/src/accounts/fetch.ts +33 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
- package/src/accounts/pollingOracleSubscriber.js +93 -0
- package/src/accounts/pollingOracleSubscriber.ts +16 -8
- package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
- package/src/accounts/pollingUserAccountSubscriber.js +132 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
- package/src/accounts/types.js +10 -0
- package/src/accounts/types.ts +41 -70
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +93 -0
- package/src/accounts/webSocketAccountSubscriber.ts +33 -16
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
- package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
- package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/marketAddresses.ts +18 -0
- package/{lib/addresses.js → src/addresses/pda.js} +45 -34
- package/src/addresses/pda.ts +118 -0
- package/src/admin.ts +247 -336
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +1023 -1026
- package/src/clearingHouseConfig.ts +37 -0
- package/src/clearingHouseUser.ts +275 -185
- package/src/clearingHouseUserConfig.ts +18 -0
- package/src/config.ts +54 -1
- package/src/constants/banks.ts +50 -0
- package/src/constants/markets.ts +16 -207
- package/src/constants/numericConstants.ts +34 -5
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +94 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/eventSubscriber.ts +194 -0
- package/src/events/fetchLogs.js +50 -0
- package/src/events/fetchLogs.ts +80 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/pollingLogProvider.ts +79 -0
- package/src/events/sort.js +44 -0
- package/src/events/sort.ts +65 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/txEventCache.ts +74 -0
- package/src/events/types.js +20 -0
- package/src/events/types.ts +98 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/events/webSocketLogProvider.ts +38 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/examples/makeTradeExample.ts +20 -11
- package/src/factory/bigNum.js +364 -0
- package/src/factory/bigNum.ts +524 -0
- package/src/factory/oracleClient.js +20 -0
- package/src/factory/oracleClient.ts +7 -4
- package/src/idl/clearing_house.json +2258 -2774
- package/src/index.js +69 -0
- package/src/index.ts +14 -5
- package/src/math/amm.js +369 -0
- package/src/math/amm.ts +382 -243
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +43 -0
- package/src/math/bankBalance.js +75 -0
- package/src/math/bankBalance.ts +112 -0
- package/src/math/conversion.js +11 -0
- package/src/math/conversion.ts +1 -11
- package/src/math/funding.js +248 -0
- package/src/math/funding.ts +12 -9
- package/src/math/market.js +57 -0
- package/src/math/market.ts +37 -30
- package/src/math/oracles.js +26 -0
- package/src/math/orders.js +110 -0
- package/src/math/orders.ts +43 -1
- package/src/math/position.js +140 -0
- package/src/math/position.ts +52 -35
- package/src/math/repeg.js +128 -0
- package/src/math/repeg.ts +176 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/trade.ts +55 -47
- package/src/math/utils.js +26 -0
- package/src/math/utils.js.map +1 -0
- package/src/mockUSDCFaucet.js +171 -0
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/oracleClientCache.ts +20 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/pythClient.ts +4 -11
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/quoteAssetOracleClient.ts +25 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/switchboardClient.ts +11 -24
- package/src/oracles/types.js +2 -0
- package/src/oracles/types.ts +7 -1
- package/src/orderParams.js +20 -0
- package/src/orderParams.ts +20 -141
- package/src/orders.js +134 -0
- package/src/orders.ts +25 -134
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +42 -0
- package/src/token/index.js +38 -0
- package/src/tx/retryTxSender.js +188 -0
- package/src/tx/retryTxSender.ts +6 -4
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +6 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +22 -3
- package/src/types.js +114 -0
- package/src/types.ts +176 -124
- package/src/userName.js +20 -0
- package/src/userName.ts +20 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/tests/bn/test.ts +255 -0
- package/tsconfig.json +12 -12
- package/lib/addresses.d.ts +0 -10
- package/lib/constants/accounts.d.ts +0 -15
- package/lib/constants/accounts.js +0 -18
- package/lib/factory/clearingHouse.d.ts +0 -35
- package/lib/factory/clearingHouse.js +0 -81
- package/lib/factory/clearingHouseUser.d.ts +0 -19
- package/lib/factory/clearingHouseUser.js +0 -34
- package/lib/math/insuranceFund.d.ts +0 -15
- package/lib/math/insuranceFund.js +0 -33
- package/lib/settlement.d.ts +0 -4
- package/lib/settlement.js +0 -10
- package/lib/tx/defaultTxSender.d.ts +0 -8
- package/lib/tx/defaultTxSender.js +0 -12
- package/src/addresses.ts +0 -82
- package/src/constants/accounts.ts +0 -26
- package/src/factory/clearingHouse.ts +0 -173
- package/src/factory/clearingHouseUser.ts +0 -73
- package/src/math/insuranceFund.ts +0 -29
- package/src/settlement.ts +0 -9
- package/src/tx/defaultTxSender.ts +0 -24
package/src/math/trade.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { MarketAccount, PositionDirection } from '../types';
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
3
|
import { assert } from '../assert/assert';
|
|
4
4
|
import {
|
|
@@ -17,10 +17,12 @@ import {
|
|
|
17
17
|
calculatePrice,
|
|
18
18
|
getSwapDirection,
|
|
19
19
|
AssetType,
|
|
20
|
-
|
|
20
|
+
calculateUpdatedAMMSpreadReserves,
|
|
21
|
+
calculateQuoteAssetAmountSwapped,
|
|
21
22
|
} from './amm';
|
|
22
23
|
import { squareRootBN } from './utils';
|
|
23
24
|
import { isVariant } from '../types';
|
|
25
|
+
import { OraclePriceData } from '../oracles/types';
|
|
24
26
|
|
|
25
27
|
const MAXPCT = new BN(1000); //percentage units are [0,1000] => [0,1]
|
|
26
28
|
|
|
@@ -57,64 +59,64 @@ export type PriceImpactUnit =
|
|
|
57
59
|
export function calculateTradeSlippage(
|
|
58
60
|
direction: PositionDirection,
|
|
59
61
|
amount: BN,
|
|
60
|
-
market:
|
|
62
|
+
market: MarketAccount,
|
|
61
63
|
inputAssetType: AssetType = 'quote',
|
|
64
|
+
oraclePriceData?: OraclePriceData,
|
|
62
65
|
useSpread = true
|
|
63
66
|
): [BN, BN, BN, BN] {
|
|
64
67
|
let oldPrice: BN;
|
|
65
68
|
|
|
66
69
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
67
70
|
if (isVariant(direction, 'long')) {
|
|
68
|
-
oldPrice = calculateAskPrice(market);
|
|
71
|
+
oldPrice = calculateAskPrice(market, oraclePriceData);
|
|
69
72
|
} else {
|
|
70
|
-
oldPrice = calculateBidPrice(market);
|
|
73
|
+
oldPrice = calculateBidPrice(market, oraclePriceData);
|
|
71
74
|
}
|
|
72
75
|
} else {
|
|
73
|
-
oldPrice = calculateMarkPrice(market);
|
|
76
|
+
oldPrice = calculateMarkPrice(market, oraclePriceData);
|
|
74
77
|
}
|
|
75
78
|
if (amount.eq(ZERO)) {
|
|
76
79
|
return [ZERO, ZERO, oldPrice, oldPrice];
|
|
77
80
|
}
|
|
78
|
-
const [
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
81
|
+
const [acquiredBaseReserve, acquiredQuoteReserve, acquiredQuoteAssetAmount] =
|
|
82
|
+
calculateTradeAcquiredAmounts(
|
|
83
|
+
direction,
|
|
84
|
+
amount,
|
|
85
|
+
market,
|
|
86
|
+
inputAssetType,
|
|
87
|
+
oraclePriceData,
|
|
88
|
+
useSpread
|
|
89
|
+
);
|
|
85
90
|
|
|
86
|
-
const entryPrice =
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
).mul(new BN(-1));
|
|
91
|
+
const entryPrice = acquiredQuoteAssetAmount
|
|
92
|
+
.mul(AMM_TO_QUOTE_PRECISION_RATIO)
|
|
93
|
+
.mul(MARK_PRICE_PRECISION)
|
|
94
|
+
.div(acquiredBaseReserve.abs());
|
|
91
95
|
|
|
92
96
|
let amm: Parameters<typeof calculateAmmReservesAfterSwap>[0];
|
|
93
97
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
94
|
-
const { baseAssetReserve, quoteAssetReserve } =
|
|
95
|
-
market.amm,
|
|
96
|
-
direction
|
|
97
|
-
);
|
|
98
|
+
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } =
|
|
99
|
+
calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
|
|
98
100
|
amm = {
|
|
99
101
|
baseAssetReserve,
|
|
100
102
|
quoteAssetReserve,
|
|
101
|
-
sqrtK:
|
|
102
|
-
pegMultiplier:
|
|
103
|
+
sqrtK: sqrtK,
|
|
104
|
+
pegMultiplier: newPeg,
|
|
103
105
|
};
|
|
104
106
|
} else {
|
|
105
107
|
amm = market.amm;
|
|
106
108
|
}
|
|
107
109
|
|
|
108
110
|
const newPrice = calculatePrice(
|
|
109
|
-
amm.baseAssetReserve.sub(
|
|
110
|
-
amm.quoteAssetReserve.sub(
|
|
111
|
+
amm.baseAssetReserve.sub(acquiredBaseReserve),
|
|
112
|
+
amm.quoteAssetReserve.sub(acquiredQuoteReserve),
|
|
111
113
|
amm.pegMultiplier
|
|
112
114
|
);
|
|
113
115
|
|
|
114
116
|
if (direction == PositionDirection.SHORT) {
|
|
115
|
-
assert(newPrice.
|
|
117
|
+
assert(newPrice.lte(oldPrice));
|
|
116
118
|
} else {
|
|
117
|
-
assert(oldPrice.
|
|
119
|
+
assert(oldPrice.lte(newPrice));
|
|
118
120
|
}
|
|
119
121
|
|
|
120
122
|
const pctMaxSlippage = newPrice
|
|
@@ -145,27 +147,26 @@ export function calculateTradeSlippage(
|
|
|
145
147
|
export function calculateTradeAcquiredAmounts(
|
|
146
148
|
direction: PositionDirection,
|
|
147
149
|
amount: BN,
|
|
148
|
-
market:
|
|
150
|
+
market: MarketAccount,
|
|
149
151
|
inputAssetType: AssetType = 'quote',
|
|
152
|
+
oraclePriceData: OraclePriceData,
|
|
150
153
|
useSpread = true
|
|
151
|
-
): [BN, BN] {
|
|
154
|
+
): [BN, BN, BN] {
|
|
152
155
|
if (amount.eq(ZERO)) {
|
|
153
|
-
return [ZERO, ZERO];
|
|
156
|
+
return [ZERO, ZERO, ZERO];
|
|
154
157
|
}
|
|
155
158
|
|
|
156
159
|
const swapDirection = getSwapDirection(inputAssetType, direction);
|
|
157
160
|
|
|
158
161
|
let amm: Parameters<typeof calculateAmmReservesAfterSwap>[0];
|
|
159
162
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
160
|
-
const { baseAssetReserve, quoteAssetReserve } =
|
|
161
|
-
market.amm,
|
|
162
|
-
direction
|
|
163
|
-
);
|
|
163
|
+
const { baseAssetReserve, quoteAssetReserve, sqrtK, newPeg } =
|
|
164
|
+
calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
|
|
164
165
|
amm = {
|
|
165
166
|
baseAssetReserve,
|
|
166
167
|
quoteAssetReserve,
|
|
167
|
-
sqrtK:
|
|
168
|
-
pegMultiplier:
|
|
168
|
+
sqrtK: sqrtK,
|
|
169
|
+
pegMultiplier: newPeg,
|
|
169
170
|
};
|
|
170
171
|
} else {
|
|
171
172
|
amm = market.amm;
|
|
@@ -176,7 +177,13 @@ export function calculateTradeAcquiredAmounts(
|
|
|
176
177
|
|
|
177
178
|
const acquiredBase = amm.baseAssetReserve.sub(newBaseAssetReserve);
|
|
178
179
|
const acquiredQuote = amm.quoteAssetReserve.sub(newQuoteAssetReserve);
|
|
179
|
-
|
|
180
|
+
const acquiredQuoteAssetamount = calculateQuoteAssetAmountSwapped(
|
|
181
|
+
acquiredQuote.abs(),
|
|
182
|
+
amm.pegMultiplier,
|
|
183
|
+
swapDirection
|
|
184
|
+
);
|
|
185
|
+
|
|
186
|
+
return [acquiredBase, acquiredQuote, acquiredQuoteAssetamount];
|
|
180
187
|
}
|
|
181
188
|
|
|
182
189
|
/**
|
|
@@ -197,19 +204,20 @@ export function calculateTradeAcquiredAmounts(
|
|
|
197
204
|
* ]
|
|
198
205
|
*/
|
|
199
206
|
export function calculateTargetPriceTrade(
|
|
200
|
-
market:
|
|
207
|
+
market: MarketAccount,
|
|
201
208
|
targetPrice: BN,
|
|
202
209
|
pct: BN = MAXPCT,
|
|
203
210
|
outputAssetType: AssetType = 'quote',
|
|
211
|
+
oraclePriceData?: OraclePriceData,
|
|
204
212
|
useSpread = true
|
|
205
213
|
): [PositionDirection, BN, BN, BN] {
|
|
206
214
|
assert(market.amm.baseAssetReserve.gt(ZERO));
|
|
207
215
|
assert(targetPrice.gt(ZERO));
|
|
208
216
|
assert(pct.lte(MAXPCT) && pct.gt(ZERO));
|
|
209
217
|
|
|
210
|
-
const markPriceBefore = calculateMarkPrice(market);
|
|
211
|
-
const bidPriceBefore = calculateBidPrice(market);
|
|
212
|
-
const askPriceBefore = calculateAskPrice(market);
|
|
218
|
+
const markPriceBefore = calculateMarkPrice(market, oraclePriceData);
|
|
219
|
+
const bidPriceBefore = calculateBidPrice(market, oraclePriceData);
|
|
220
|
+
const askPriceBefore = calculateAskPrice(market, oraclePriceData);
|
|
213
221
|
|
|
214
222
|
let direction;
|
|
215
223
|
if (targetPrice.gt(markPriceBefore)) {
|
|
@@ -230,19 +238,19 @@ export function calculateTargetPriceTrade(
|
|
|
230
238
|
let baseAssetReserveBefore: BN;
|
|
231
239
|
let quoteAssetReserveBefore: BN;
|
|
232
240
|
|
|
241
|
+
let peg = market.amm.pegMultiplier;
|
|
242
|
+
|
|
233
243
|
if (useSpread && market.amm.baseSpread > 0) {
|
|
234
|
-
const { baseAssetReserve, quoteAssetReserve } =
|
|
235
|
-
market.amm,
|
|
236
|
-
direction
|
|
237
|
-
);
|
|
244
|
+
const { baseAssetReserve, quoteAssetReserve, newPeg } =
|
|
245
|
+
calculateUpdatedAMMSpreadReserves(market.amm, direction, oraclePriceData);
|
|
238
246
|
baseAssetReserveBefore = baseAssetReserve;
|
|
239
247
|
quoteAssetReserveBefore = quoteAssetReserve;
|
|
248
|
+
peg = newPeg;
|
|
240
249
|
} else {
|
|
241
250
|
baseAssetReserveBefore = market.amm.baseAssetReserve;
|
|
242
251
|
quoteAssetReserveBefore = market.amm.quoteAssetReserve;
|
|
243
252
|
}
|
|
244
253
|
|
|
245
|
-
const peg = market.amm.pegMultiplier;
|
|
246
254
|
const invariant = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
247
255
|
const k = invariant.mul(MARK_PRICE_PRECISION);
|
|
248
256
|
|
|
@@ -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,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,20 @@
|
|
|
1
|
+
import { OracleClient } from './types';
|
|
2
|
+
import { OracleSource } from '../types';
|
|
3
|
+
import { getOracleClient } from '../factory/oracleClient';
|
|
4
|
+
import { Connection } from '@solana/web3.js';
|
|
5
|
+
|
|
6
|
+
export class OracleClientCache {
|
|
7
|
+
cache = new Map<string, OracleClient>();
|
|
8
|
+
public constructor() {}
|
|
9
|
+
|
|
10
|
+
public get(oracleSource: OracleSource, connection: Connection) {
|
|
11
|
+
const key = Object.keys(oracleSource)[0];
|
|
12
|
+
if (this.cache.has(key)) {
|
|
13
|
+
return this.cache.get(key);
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const client = getOracleClient(oracleSource, connection);
|
|
17
|
+
this.cache.set(key, client);
|
|
18
|
+
return client;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
@@ -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;
|
|
@@ -1,21 +1,16 @@
|
|
|
1
|
-
import { parsePriceData
|
|
1
|
+
import { parsePriceData } from '@pythnetwork/client';
|
|
2
2
|
import { Connection, PublicKey } from '@solana/web3.js';
|
|
3
|
-
import { OraclePriceData } from './types';
|
|
3
|
+
import { OracleClient, OraclePriceData } from './types';
|
|
4
4
|
import { BN } from '@project-serum/anchor';
|
|
5
5
|
import { MARK_PRICE_PRECISION, TEN } from '../constants/numericConstants';
|
|
6
6
|
|
|
7
|
-
export class PythClient {
|
|
7
|
+
export class PythClient implements OracleClient {
|
|
8
8
|
private connection: Connection;
|
|
9
9
|
|
|
10
10
|
public constructor(connection: Connection) {
|
|
11
11
|
this.connection = connection;
|
|
12
12
|
}
|
|
13
13
|
|
|
14
|
-
public async getPriceData(pricePublicKey: PublicKey): Promise<PriceData> {
|
|
15
|
-
const account = await this.connection.getAccountInfo(pricePublicKey);
|
|
16
|
-
return parsePriceData(account.data);
|
|
17
|
-
}
|
|
18
|
-
|
|
19
14
|
public async getOraclePriceData(
|
|
20
15
|
pricePublicKey: PublicKey
|
|
21
16
|
): Promise<OraclePriceData> {
|
|
@@ -23,9 +18,7 @@ export class PythClient {
|
|
|
23
18
|
return this.getOraclePriceDataFromBuffer(accountInfo.data);
|
|
24
19
|
}
|
|
25
20
|
|
|
26
|
-
public
|
|
27
|
-
buffer: Buffer
|
|
28
|
-
): Promise<OraclePriceData> {
|
|
21
|
+
public getOraclePriceDataFromBuffer(buffer: Buffer): OraclePriceData {
|
|
29
22
|
const priceData = parsePriceData(buffer);
|
|
30
23
|
return {
|
|
31
24
|
price: convertPythPrice(priceData.aggregate.price, priceData.exponent),
|
|
@@ -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,25 @@
|
|
|
1
|
+
import { PublicKey } from '@solana/web3.js';
|
|
2
|
+
import { OracleClient, OraclePriceData } from './types';
|
|
3
|
+
import { BN } from '@project-serum/anchor';
|
|
4
|
+
import { MARK_PRICE_PRECISION } from '../constants/numericConstants';
|
|
5
|
+
|
|
6
|
+
export const QUOTE_ORACLE_PRICE_DATA: OraclePriceData = {
|
|
7
|
+
price: MARK_PRICE_PRECISION,
|
|
8
|
+
slot: new BN(0),
|
|
9
|
+
confidence: new BN(1),
|
|
10
|
+
hasSufficientNumberOfDataPoints: true,
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
export class QuoteAssetOracleClient implements OracleClient {
|
|
14
|
+
public constructor() {}
|
|
15
|
+
|
|
16
|
+
public async getOraclePriceData(
|
|
17
|
+
_pricePublicKey: PublicKey
|
|
18
|
+
): Promise<OraclePriceData> {
|
|
19
|
+
return Promise.resolve(QUOTE_ORACLE_PRICE_DATA);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public getOraclePriceDataFromBuffer(_buffer: Buffer): OraclePriceData {
|
|
23
|
+
return QUOTE_ORACLE_PRICE_DATA;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
@@ -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
|
+
}
|