@drift-labs/sdk 0.1.36-master.5 → 0.2.0-master.0

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.
Files changed (197) hide show
  1. package/lib/accounts/bulkAccountLoader.d.ts +7 -6
  2. package/lib/accounts/bulkAccountLoader.js +83 -93
  3. package/lib/accounts/bulkUserSubscription.js +13 -57
  4. package/lib/accounts/fetch.d.ts +4 -0
  5. package/lib/accounts/fetch.js +18 -0
  6. package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
  7. package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
  8. package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
  9. package/lib/accounts/pollingOracleSubscriber.js +37 -49
  10. package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
  11. package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
  12. package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
  13. package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
  14. package/lib/accounts/types.d.ts +34 -41
  15. package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
  16. package/lib/accounts/webSocketAccountSubscriber.js +39 -35
  17. package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
  18. package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
  19. package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
  20. package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
  21. package/lib/addresses/marketAddresses.d.ts +4 -0
  22. package/lib/addresses/marketAddresses.js +15 -0
  23. package/lib/addresses/pda.d.ts +12 -0
  24. package/lib/addresses/pda.js +83 -0
  25. package/lib/admin.d.ts +8 -12
  26. package/lib/admin.js +366 -490
  27. package/lib/clearingHouse.d.ts +84 -103
  28. package/lib/clearingHouse.js +779 -810
  29. package/lib/clearingHouseConfig.d.ts +34 -0
  30. package/lib/clearingHouseConfig.js +2 -0
  31. package/lib/clearingHouseUser.d.ts +19 -18
  32. package/lib/clearingHouseUser.js +157 -115
  33. package/lib/clearingHouseUserConfig.d.ts +14 -0
  34. package/lib/clearingHouseUserConfig.js +2 -0
  35. package/lib/config.d.ts +12 -0
  36. package/lib/config.js +35 -4
  37. package/lib/constants/banks.d.ts +16 -0
  38. package/lib/constants/banks.js +34 -0
  39. package/lib/constants/markets.d.ts +8 -3
  40. package/lib/constants/markets.js +13 -206
  41. package/lib/constants/numericConstants.d.ts +16 -0
  42. package/lib/constants/numericConstants.js +22 -6
  43. package/lib/events/eventList.d.ts +22 -0
  44. package/lib/events/eventList.js +77 -0
  45. package/lib/events/eventSubscriber.d.ts +34 -0
  46. package/lib/events/eventSubscriber.js +126 -0
  47. package/lib/events/fetchLogs.d.ts +13 -0
  48. package/lib/events/fetchLogs.js +39 -0
  49. package/lib/events/pollingLogProvider.d.ts +15 -0
  50. package/lib/events/pollingLogProvider.js +53 -0
  51. package/lib/events/sort.d.ts +2 -0
  52. package/lib/events/sort.js +44 -0
  53. package/lib/events/txEventCache.d.ts +24 -0
  54. package/lib/events/txEventCache.js +71 -0
  55. package/lib/events/types.d.ts +49 -0
  56. package/lib/events/types.js +20 -0
  57. package/lib/events/webSocketLogProvider.d.ts +12 -0
  58. package/lib/events/webSocketLogProvider.js +30 -0
  59. package/lib/examples/makeTradeExample.js +26 -27
  60. package/lib/factory/bigNum.d.ts +112 -0
  61. package/lib/factory/bigNum.js +356 -0
  62. package/lib/factory/oracleClient.d.ts +1 -2
  63. package/lib/factory/oracleClient.js +6 -2
  64. package/lib/idl/clearing_house.json +1739 -2287
  65. package/lib/index.d.ts +13 -4
  66. package/lib/index.js +13 -4
  67. package/lib/math/amm.d.ts +19 -29
  68. package/lib/math/amm.js +129 -179
  69. package/lib/math/auction.d.ts +5 -0
  70. package/lib/math/auction.js +39 -0
  71. package/lib/math/bankBalance.d.ts +9 -0
  72. package/lib/math/bankBalance.js +75 -0
  73. package/lib/math/conversion.d.ts +0 -1
  74. package/lib/math/conversion.js +1 -5
  75. package/lib/math/funding.d.ts +6 -6
  76. package/lib/math/funding.js +158 -175
  77. package/lib/math/market.d.ts +6 -6
  78. package/lib/math/market.js +10 -9
  79. package/lib/math/oracles.d.ts +3 -0
  80. package/lib/math/oracles.js +26 -0
  81. package/lib/math/orders.d.ts +5 -0
  82. package/lib/math/orders.js +31 -1
  83. package/lib/math/position.d.ts +7 -4
  84. package/lib/math/position.js +27 -9
  85. package/lib/math/repeg.d.ts +22 -0
  86. package/lib/math/repeg.js +128 -0
  87. package/lib/math/trade.d.ts +13 -8
  88. package/lib/math/trade.js +85 -22
  89. package/lib/mockUSDCFaucet.js +87 -116
  90. package/lib/oracles/oracleClientCache.d.ts +8 -0
  91. package/lib/oracles/oracleClientCache.js +19 -0
  92. package/lib/oracles/pythClient.d.ts +3 -5
  93. package/lib/oracles/pythClient.js +12 -30
  94. package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
  95. package/lib/oracles/quoteAssetOracleClient.js +21 -0
  96. package/lib/oracles/switchboardClient.d.ts +3 -5
  97. package/lib/oracles/switchboardClient.js +29 -47
  98. package/lib/oracles/types.d.ts +7 -1
  99. package/lib/orders.d.ts +6 -6
  100. package/lib/orders.js +10 -9
  101. package/lib/slot/SlotSubscriber.d.ts +12 -0
  102. package/lib/slot/SlotSubscriber.js +23 -0
  103. package/lib/tx/retryTxSender.d.ts +2 -2
  104. package/lib/tx/retryTxSender.js +108 -123
  105. package/lib/tx/types.d.ts +5 -1
  106. package/lib/tx/utils.d.ts +1 -1
  107. package/lib/tx/utils.js +11 -2
  108. package/lib/types.d.ts +105 -98
  109. package/lib/types.js +13 -1
  110. package/lib/userName.d.ts +4 -0
  111. package/lib/userName.js +20 -0
  112. package/lib/util/computeUnits.js +10 -21
  113. package/lib/util/tps.js +11 -22
  114. package/lib/wallet.js +7 -20
  115. package/package.json +10 -3
  116. package/src/accounts/bulkAccountLoader.ts +26 -15
  117. package/src/accounts/bulkUserSubscription.ts +1 -45
  118. package/src/accounts/fetch.ts +33 -0
  119. package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
  120. package/src/accounts/pollingOracleSubscriber.ts +16 -8
  121. package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
  122. package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
  123. package/src/accounts/types.ts +41 -70
  124. package/src/accounts/webSocketAccountSubscriber.ts +33 -16
  125. package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
  126. package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
  127. package/src/addresses/marketAddresses.ts +18 -0
  128. package/src/addresses/pda.ts +118 -0
  129. package/src/admin.ts +209 -267
  130. package/src/clearingHouse.ts +921 -829
  131. package/src/clearingHouseConfig.ts +37 -0
  132. package/src/clearingHouseUser.ts +280 -127
  133. package/src/clearingHouseUserConfig.ts +18 -0
  134. package/src/config.ts +54 -1
  135. package/src/constants/banks.ts +43 -0
  136. package/src/constants/markets.ts +16 -207
  137. package/src/constants/numericConstants.ts +33 -5
  138. package/src/events/eventList.ts +94 -0
  139. package/src/events/eventSubscriber.ts +194 -0
  140. package/src/events/fetchLogs.ts +80 -0
  141. package/src/events/pollingLogProvider.ts +79 -0
  142. package/src/events/sort.ts +65 -0
  143. package/src/events/txEventCache.ts +74 -0
  144. package/src/events/types.ts +98 -0
  145. package/src/events/webSocketLogProvider.ts +38 -0
  146. package/src/examples/makeTradeExample.ts +20 -11
  147. package/src/factory/bigNum.ts +507 -0
  148. package/src/factory/oracleClient.ts +7 -4
  149. package/src/idl/clearing_house.json +1739 -2287
  150. package/src/index.ts +13 -4
  151. package/src/math/amm.ts +229 -245
  152. package/src/math/auction.ts +39 -0
  153. package/src/math/bankBalance.ts +112 -0
  154. package/src/math/conversion.ts +1 -11
  155. package/src/math/funding.ts +12 -9
  156. package/src/math/market.ts +37 -30
  157. package/src/math/oracles.ts +36 -0
  158. package/src/math/orders.ts +38 -0
  159. package/src/math/position.ts +48 -13
  160. package/src/math/repeg.ts +175 -0
  161. package/src/math/trade.ts +114 -36
  162. package/src/math/utils.js +27 -0
  163. package/src/math/utils.js.map +1 -0
  164. package/src/oracles/oracleClientCache.ts +20 -0
  165. package/src/oracles/pythClient.ts +5 -11
  166. package/src/oracles/quoteAssetOracleClient.ts +25 -0
  167. package/src/oracles/switchboardClient.ts +16 -24
  168. package/src/oracles/types.ts +8 -1
  169. package/src/orders.ts +35 -20
  170. package/src/slot/SlotSubscriber.ts +32 -0
  171. package/src/tx/retryTxSender.ts +6 -4
  172. package/src/tx/types.ts +6 -1
  173. package/src/tx/utils.ts +22 -3
  174. package/src/types.ts +108 -110
  175. package/src/userName.ts +20 -0
  176. package/src/util/computeUnits.js +17 -0
  177. package/src/util/computeUnits.js.map +1 -0
  178. package/tests/bn/test.ts +255 -0
  179. package/tsconfig.json +12 -12
  180. package/lib/addresses.d.ts +0 -9
  181. package/lib/addresses.js +0 -87
  182. package/lib/constants/accounts.d.ts +0 -15
  183. package/lib/constants/accounts.js +0 -18
  184. package/lib/factory/clearingHouse.d.ts +0 -35
  185. package/lib/factory/clearingHouse.js +0 -81
  186. package/lib/factory/clearingHouseUser.d.ts +0 -19
  187. package/lib/factory/clearingHouseUser.js +0 -34
  188. package/lib/math/insuranceFund.d.ts +0 -15
  189. package/lib/math/insuranceFund.js +0 -33
  190. package/lib/tx/defaultTxSender.d.ts +0 -8
  191. package/lib/tx/defaultTxSender.js +0 -12
  192. package/src/addresses.ts +0 -71
  193. package/src/constants/accounts.ts +0 -26
  194. package/src/factory/clearingHouse.ts +0 -173
  195. package/src/factory/clearingHouseUser.ts +0 -73
  196. package/src/math/insuranceFund.ts +0 -29
  197. package/src/tx/defaultTxSender.ts +0 -24
@@ -0,0 +1,75 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.calculateInterestAccumulated = exports.getTokenAmount = exports.getBalance = void 0;
4
+ const types_1 = require("../types");
5
+ const anchor_1 = require("@project-serum/anchor");
6
+ const numericConstants_1 = require("../constants/numericConstants");
7
+ function getBalance(tokenAmount, bank, balanceType) {
8
+ const precisionIncrease = numericConstants_1.TEN.pow(new anchor_1.BN(16 - bank.decimals));
9
+ const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
10
+ ? bank.cumulativeDepositInterest
11
+ : bank.cumulativeBorrowInterest;
12
+ let balance = tokenAmount.mul(precisionIncrease).div(cumulativeInterest);
13
+ if (!balance.eq(numericConstants_1.ZERO) && (0, types_1.isVariant)(balanceType, 'borrow')) {
14
+ balance = balance.add(numericConstants_1.ONE);
15
+ }
16
+ return balance;
17
+ }
18
+ exports.getBalance = getBalance;
19
+ function getTokenAmount(balanceAmount, bank, balanceType) {
20
+ const precisionDecrease = numericConstants_1.TEN.pow(new anchor_1.BN(16 - bank.decimals));
21
+ const cumulativeInterest = (0, types_1.isVariant)(balanceType, 'deposit')
22
+ ? bank.cumulativeDepositInterest
23
+ : bank.cumulativeBorrowInterest;
24
+ return balanceAmount.mul(cumulativeInterest).div(precisionDecrease);
25
+ }
26
+ exports.getTokenAmount = getTokenAmount;
27
+ function calculateInterestAccumulated(bank, now) {
28
+ const token_deposit_amount = getTokenAmount(bank.depositBalance, bank, types_1.BankBalanceType.DEPOSIT);
29
+ const token_borrow_amount = getTokenAmount(bank.borrowBalance, bank, types_1.BankBalanceType.BORROW);
30
+ let utilization;
31
+ if (token_borrow_amount.eq(numericConstants_1.ZERO) && token_deposit_amount.eq(numericConstants_1.ZERO)) {
32
+ utilization = numericConstants_1.ZERO;
33
+ }
34
+ else if (token_deposit_amount.eq(numericConstants_1.ZERO)) {
35
+ utilization = numericConstants_1.BANK_UTILIZATION_PRECISION;
36
+ }
37
+ else {
38
+ utilization = token_borrow_amount
39
+ .mul(numericConstants_1.BANK_UTILIZATION_PRECISION)
40
+ .div(token_deposit_amount);
41
+ }
42
+ let interest_rate;
43
+ if (utilization.gt(bank.optimalUtilization)) {
44
+ const surplusUtilization = utilization.sub(bank.optimalUtilization);
45
+ const borrowRateSlope = bank.maxBorrowRate
46
+ .sub(bank.optimalBorrowRate)
47
+ .mul(numericConstants_1.BANK_UTILIZATION_PRECISION)
48
+ .div(numericConstants_1.BANK_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
49
+ interest_rate = bank.optimalBorrowRate.add(surplusUtilization.mul(borrowRateSlope).div(numericConstants_1.BANK_UTILIZATION_PRECISION));
50
+ }
51
+ else {
52
+ const borrowRateSlope = bank.optimalBorrowRate
53
+ .mul(numericConstants_1.BANK_UTILIZATION_PRECISION)
54
+ .div(numericConstants_1.BANK_UTILIZATION_PRECISION.sub(bank.optimalUtilization));
55
+ interest_rate = utilization
56
+ .mul(borrowRateSlope)
57
+ .div(numericConstants_1.BANK_UTILIZATION_PRECISION);
58
+ }
59
+ const timeSinceLastUpdate = now.sub(bank.lastUpdated);
60
+ const modifiedBorrowRate = interest_rate.mul(timeSinceLastUpdate);
61
+ const modifiedDepositRate = modifiedBorrowRate
62
+ .mul(utilization)
63
+ .div(numericConstants_1.BANK_UTILIZATION_PRECISION);
64
+ const borrowInterest = bank.cumulativeBorrowInterest
65
+ .mul(modifiedBorrowRate)
66
+ .div(numericConstants_1.ONE_YEAR)
67
+ .div(numericConstants_1.BANK_INTEREST_PRECISION)
68
+ .add(numericConstants_1.ONE);
69
+ const depositInterest = bank.cumulativeDepositInterest
70
+ .mul(modifiedDepositRate)
71
+ .div(numericConstants_1.ONE_YEAR)
72
+ .div(numericConstants_1.BANK_INTEREST_PRECISION);
73
+ return { borrowInterest, depositInterest };
74
+ }
75
+ exports.calculateInterestAccumulated = calculateInterestAccumulated;
@@ -1,3 +1,2 @@
1
1
  import { BN } from '../';
2
2
  export declare const convertToNumber: (bigNumber: BN, precision?: BN) => number;
3
- export declare const convertBaseAssetAmountToNumber: (baseAssetAmount: BN) => number;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.convertBaseAssetAmountToNumber = exports.convertToNumber = void 0;
3
+ exports.convertToNumber = void 0;
4
4
  const numericConstants_1 = require("../constants/numericConstants");
5
5
  const convertToNumber = (bigNumber, precision = numericConstants_1.MARK_PRICE_PRECISION) => {
6
6
  if (!bigNumber)
@@ -9,7 +9,3 @@ const convertToNumber = (bigNumber, precision = numericConstants_1.MARK_PRICE_PR
9
9
  bigNumber.mod(precision).toNumber() / precision.toNumber());
10
10
  };
11
11
  exports.convertToNumber = convertToNumber;
12
- const convertBaseAssetAmountToNumber = (baseAssetAmount) => {
13
- return (0, exports.convertToNumber)(baseAssetAmount, numericConstants_1.MARK_PRICE_PRECISION.mul(numericConstants_1.PEG_PRECISION));
14
- };
15
- exports.convertBaseAssetAmountToNumber = convertBaseAssetAmountToNumber;
@@ -1,6 +1,6 @@
1
1
  /// <reference types="bn.js" />
2
2
  import { BN } from '@project-serum/anchor';
3
- import { Market } from '../types';
3
+ import { MarketAccount } from '../types';
4
4
  import { OraclePriceData } from '../oracles/types';
5
5
  /**
6
6
  *
@@ -9,7 +9,7 @@ import { OraclePriceData } from '../oracles/types';
9
9
  * @param periodAdjustment
10
10
  * @returns Estimated funding rate. : Precision //TODO-PRECISION
11
11
  */
12
- export declare function calculateAllEstimatedFundingRate(market: Market, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN, BN]>;
12
+ export declare function calculateAllEstimatedFundingRate(market: MarketAccount, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN, BN]>;
13
13
  /**
14
14
  *
15
15
  * @param market
@@ -18,7 +18,7 @@ export declare function calculateAllEstimatedFundingRate(market: Market, oracleP
18
18
  * @param estimationMethod
19
19
  * @returns Estimated funding rate. : Precision //TODO-PRECISION
20
20
  */
21
- export declare function calculateEstimatedFundingRate(market: Market, oraclePriceData?: OraclePriceData, periodAdjustment?: BN, estimationMethod?: 'interpolated' | 'lowerbound' | 'capped'): Promise<BN>;
21
+ export declare function calculateEstimatedFundingRate(market: MarketAccount, oraclePriceData?: OraclePriceData, periodAdjustment?: BN, estimationMethod?: 'interpolated' | 'lowerbound' | 'capped'): Promise<BN>;
22
22
  /**
23
23
  *
24
24
  * @param market
@@ -26,7 +26,7 @@ export declare function calculateEstimatedFundingRate(market: Market, oraclePric
26
26
  * @param periodAdjustment
27
27
  * @returns Estimated funding rate. : Precision //TODO-PRECISION
28
28
  */
29
- export declare function calculateLongShortFundingRate(market: Market, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN]>;
29
+ export declare function calculateLongShortFundingRate(market: MarketAccount, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN]>;
30
30
  /**
31
31
  *
32
32
  * @param market
@@ -34,10 +34,10 @@ export declare function calculateLongShortFundingRate(market: Market, oraclePric
34
34
  * @param periodAdjustment
35
35
  * @returns Estimated funding rate. : Precision //TODO-PRECISION
36
36
  */
37
- export declare function calculateLongShortFundingRateAndLiveTwaps(market: Market, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN]>;
37
+ export declare function calculateLongShortFundingRateAndLiveTwaps(market: MarketAccount, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN]>;
38
38
  /**
39
39
  *
40
40
  * @param market
41
41
  * @returns Estimated fee pool size
42
42
  */
43
- export declare function calculateFundingPool(market: Market): BN;
43
+ export declare function calculateFundingPool(market: MarketAccount): BN;
@@ -1,13 +1,4 @@
1
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
2
  Object.defineProperty(exports, "__esModule", { value: true });
12
3
  exports.calculateFundingPool = exports.calculateLongShortFundingRateAndLiveTwaps = exports.calculateLongShortFundingRate = exports.calculateEstimatedFundingRate = exports.calculateAllEstimatedFundingRate = void 0;
13
4
  const anchor_1 = require("@project-serum/anchor");
@@ -20,112 +11,88 @@ const market_1 = require("./market");
20
11
  * @param periodAdjustment
21
12
  * @returns Estimated funding rate. : Precision //TODO-PRECISION
22
13
  */
23
- function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
24
- return __awaiter(this, void 0, void 0, function* () {
25
- // periodAdjustment
26
- // 1: hourly
27
- // 24: daily
28
- // 24 * 365.25: annualized
29
- const secondsInHour = new anchor_1.BN(3600);
30
- const hoursInDay = new anchor_1.BN(24);
31
- const ONE = new anchor_1.BN(1);
32
- if (!market.initialized) {
33
- return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
34
- }
35
- const payFreq = new anchor_1.BN(market.amm.fundingPeriod);
36
- // todo: sufficiently differs from blockchain timestamp?
37
- const now = new anchor_1.BN((Date.now() / 1000).toFixed(0));
38
- const timeSinceLastUpdate = now.sub(market.amm.lastFundingRateTs);
39
- // calculate real-time mark twap
40
- const lastMarkTwapWithMantissa = market.amm.lastMarkPriceTwap;
41
- const lastMarkPriceTwapTs = market.amm.lastMarkPriceTwapTs;
42
- const timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
43
- const markTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, anchor_1.BN.max(numericConstants_1.ZERO, secondsInHour.sub(timeSinceLastMarkChange)));
44
- const baseAssetPriceWithMantissa = (0, market_1.calculateMarkPrice)(market);
45
- const markTwapWithMantissa = markTwapTimeSinceLastUpdate
46
- .mul(lastMarkTwapWithMantissa)
47
- .add(timeSinceLastMarkChange.mul(baseAssetPriceWithMantissa))
48
- .div(timeSinceLastMarkChange.add(markTwapTimeSinceLastUpdate));
49
- // calculate real-time (predicted) oracle twap
50
- // note: oracle twap depends on `when the chord is struck` (market is trade)
51
- const lastOracleTwapWithMantissa = market.amm.lastOraclePriceTwap;
52
- const lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
53
- const oracleInvalidDuration = anchor_1.BN.max(numericConstants_1.ZERO, lastMarkPriceTwapTs.sub(lastOraclePriceTwapTs));
54
- const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
55
- const oracleTwapTimeSinceLastUpdate = anchor_1.BN.max(ONE, anchor_1.BN.min(secondsInHour, anchor_1.BN.max(ONE, secondsInHour.sub(timeSinceLastOracleTwapUpdate))));
56
- let oracleTwapWithMantissa = lastOracleTwapWithMantissa;
57
- // if passing live oracle data, improve predicted calc estimate
58
- if (oraclePriceData) {
59
- const oraclePrice = oraclePriceData.price;
60
- const oracleLiveVsTwap = oraclePrice
61
- .sub(lastOracleTwapWithMantissa)
62
- .abs()
63
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
64
- .mul(new anchor_1.BN(100))
65
- .div(lastOracleTwapWithMantissa);
66
- // verify pyth live input is within 10% of last twap for live update
67
- if (oracleLiveVsTwap.lte(numericConstants_1.MARK_PRICE_PRECISION.mul(new anchor_1.BN(10)))) {
68
- oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
69
- .mul(lastOracleTwapWithMantissa)
70
- .add(timeSinceLastMarkChange.mul(oraclePrice))
71
- .div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate));
72
- }
73
- }
74
- const shrunkLastOracleTwapwithMantissa = oracleTwapTimeSinceLastUpdate
75
- .mul(lastOracleTwapWithMantissa)
76
- .add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
77
- .div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
78
- const twapSpread = lastMarkTwapWithMantissa.sub(shrunkLastOracleTwapwithMantissa);
79
- const twapSpreadPct = twapSpread
14
+ async function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
15
+ // periodAdjustment
16
+ // 1: hourly
17
+ // 24: daily
18
+ // 24 * 365.25: annualized
19
+ const secondsInHour = new anchor_1.BN(3600);
20
+ const hoursInDay = new anchor_1.BN(24);
21
+ const ONE = new anchor_1.BN(1);
22
+ if (!market.initialized) {
23
+ return [numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO, numericConstants_1.ZERO];
24
+ }
25
+ const payFreq = new anchor_1.BN(market.amm.fundingPeriod);
26
+ // todo: sufficiently differs from blockchain timestamp?
27
+ const now = new anchor_1.BN((Date.now() / 1000).toFixed(0));
28
+ const timeSinceLastUpdate = now.sub(market.amm.lastFundingRateTs);
29
+ // calculate real-time mark twap
30
+ const lastMarkTwapWithMantissa = market.amm.lastMarkPriceTwap;
31
+ const lastMarkPriceTwapTs = market.amm.lastMarkPriceTwapTs;
32
+ const timeSinceLastMarkChange = now.sub(lastMarkPriceTwapTs);
33
+ const markTwapTimeSinceLastUpdate = anchor_1.BN.max(secondsInHour, anchor_1.BN.max(numericConstants_1.ZERO, secondsInHour.sub(timeSinceLastMarkChange)));
34
+ const baseAssetPriceWithMantissa = (0, market_1.calculateMarkPrice)(market, oraclePriceData);
35
+ const markTwapWithMantissa = markTwapTimeSinceLastUpdate
36
+ .mul(lastMarkTwapWithMantissa)
37
+ .add(timeSinceLastMarkChange.mul(baseAssetPriceWithMantissa))
38
+ .div(timeSinceLastMarkChange.add(markTwapTimeSinceLastUpdate));
39
+ // calculate real-time (predicted) oracle twap
40
+ // note: oracle twap depends on `when the chord is struck` (market is trade)
41
+ const lastOracleTwapWithMantissa = market.amm.lastOraclePriceTwap;
42
+ const lastOraclePriceTwapTs = market.amm.lastOraclePriceTwapTs;
43
+ const oracleInvalidDuration = anchor_1.BN.max(numericConstants_1.ZERO, lastMarkPriceTwapTs.sub(lastOraclePriceTwapTs));
44
+ const timeSinceLastOracleTwapUpdate = now.sub(lastOraclePriceTwapTs);
45
+ const oracleTwapTimeSinceLastUpdate = anchor_1.BN.max(ONE, anchor_1.BN.min(secondsInHour, anchor_1.BN.max(ONE, secondsInHour.sub(timeSinceLastOracleTwapUpdate))));
46
+ let oracleTwapWithMantissa = lastOracleTwapWithMantissa;
47
+ // if passing live oracle data, improve predicted calc estimate
48
+ if (oraclePriceData) {
49
+ const oraclePrice = oraclePriceData.price;
50
+ const oracleLiveVsTwap = oraclePrice
51
+ .sub(lastOracleTwapWithMantissa)
52
+ .abs()
80
53
  .mul(numericConstants_1.MARK_PRICE_PRECISION)
81
54
  .mul(new anchor_1.BN(100))
82
- .div(shrunkLastOracleTwapwithMantissa);
83
- const lowerboundEst = twapSpreadPct
84
- .mul(payFreq)
85
- .mul(anchor_1.BN.min(secondsInHour, timeSinceLastUpdate))
86
- .mul(periodAdjustment)
87
- .div(secondsInHour)
88
- .div(secondsInHour)
89
- .div(hoursInDay);
90
- const interpEst = twapSpreadPct.mul(periodAdjustment).div(hoursInDay);
91
- const interpRateQuote = twapSpreadPct
92
- .mul(periodAdjustment)
93
- .div(hoursInDay)
94
- .div(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION));
95
- let feePoolSize = calculateFundingPool(market);
96
- if (interpRateQuote.lt(new anchor_1.BN(0))) {
97
- feePoolSize = feePoolSize.mul(new anchor_1.BN(-1));
98
- }
99
- let cappedAltEst;
100
- let largerSide;
101
- let smallerSide;
102
- if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
103
- largerSide = market.baseAssetAmountLong.abs();
104
- smallerSide = market.baseAssetAmountShort.abs();
105
- if (twapSpread.gt(new anchor_1.BN(0))) {
106
- return [
107
- markTwapWithMantissa,
108
- oracleTwapWithMantissa,
109
- lowerboundEst,
110
- interpEst,
111
- interpEst,
112
- ];
113
- }
114
- }
115
- else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
116
- largerSide = market.baseAssetAmountShort.abs();
117
- smallerSide = market.baseAssetAmountLong.abs();
118
- if (twapSpread.lt(new anchor_1.BN(0))) {
119
- return [
120
- markTwapWithMantissa,
121
- oracleTwapWithMantissa,
122
- lowerboundEst,
123
- interpEst,
124
- interpEst,
125
- ];
126
- }
127
- }
128
- else {
55
+ .div(lastOracleTwapWithMantissa);
56
+ // verify pyth live input is within 10% of last twap for live update
57
+ if (oracleLiveVsTwap.lte(numericConstants_1.MARK_PRICE_PRECISION.mul(new anchor_1.BN(10)))) {
58
+ oracleTwapWithMantissa = oracleTwapTimeSinceLastUpdate
59
+ .mul(lastOracleTwapWithMantissa)
60
+ .add(timeSinceLastMarkChange.mul(oraclePrice))
61
+ .div(timeSinceLastMarkChange.add(oracleTwapTimeSinceLastUpdate));
62
+ }
63
+ }
64
+ const shrunkLastOracleTwapwithMantissa = oracleTwapTimeSinceLastUpdate
65
+ .mul(lastOracleTwapWithMantissa)
66
+ .add(oracleInvalidDuration.mul(lastMarkTwapWithMantissa))
67
+ .div(oracleTwapTimeSinceLastUpdate.add(oracleInvalidDuration));
68
+ const twapSpread = lastMarkTwapWithMantissa.sub(shrunkLastOracleTwapwithMantissa);
69
+ const twapSpreadPct = twapSpread
70
+ .mul(numericConstants_1.MARK_PRICE_PRECISION)
71
+ .mul(new anchor_1.BN(100))
72
+ .div(shrunkLastOracleTwapwithMantissa);
73
+ const lowerboundEst = twapSpreadPct
74
+ .mul(payFreq)
75
+ .mul(anchor_1.BN.min(secondsInHour, timeSinceLastUpdate))
76
+ .mul(periodAdjustment)
77
+ .div(secondsInHour)
78
+ .div(secondsInHour)
79
+ .div(hoursInDay);
80
+ const interpEst = twapSpreadPct.mul(periodAdjustment).div(hoursInDay);
81
+ const interpRateQuote = twapSpreadPct
82
+ .mul(periodAdjustment)
83
+ .div(hoursInDay)
84
+ .div(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION));
85
+ let feePoolSize = calculateFundingPool(market);
86
+ if (interpRateQuote.lt(new anchor_1.BN(0))) {
87
+ feePoolSize = feePoolSize.mul(new anchor_1.BN(-1));
88
+ }
89
+ let cappedAltEst;
90
+ let largerSide;
91
+ let smallerSide;
92
+ if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
93
+ largerSide = market.baseAssetAmountLong.abs();
94
+ smallerSide = market.baseAssetAmountShort.abs();
95
+ if (twapSpread.gt(new anchor_1.BN(0))) {
129
96
  return [
130
97
  markTwapWithMantissa,
131
98
  oracleTwapWithMantissa,
@@ -134,33 +101,55 @@ function calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustm
134
101
  interpEst,
135
102
  ];
136
103
  }
137
- if (largerSide.gt(numericConstants_1.ZERO)) {
138
- // funding smaller flow
139
- cappedAltEst = smallerSide.mul(twapSpread).div(hoursInDay);
140
- const feePoolTopOff = feePoolSize
141
- .mul(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION))
142
- .mul(numericConstants_1.AMM_RESERVE_PRECISION);
143
- cappedAltEst = cappedAltEst.add(feePoolTopOff).div(largerSide);
144
- cappedAltEst = cappedAltEst
145
- .mul(numericConstants_1.MARK_PRICE_PRECISION)
146
- .mul(new anchor_1.BN(100))
147
- .div(oracleTwapWithMantissa)
148
- .mul(periodAdjustment);
149
- if (cappedAltEst.abs().gte(interpEst.abs())) {
150
- cappedAltEst = interpEst;
151
- }
152
- }
153
- else {
154
- cappedAltEst = interpEst;
104
+ }
105
+ else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
106
+ largerSide = market.baseAssetAmountShort.abs();
107
+ smallerSide = market.baseAssetAmountLong.abs();
108
+ if (twapSpread.lt(new anchor_1.BN(0))) {
109
+ return [
110
+ markTwapWithMantissa,
111
+ oracleTwapWithMantissa,
112
+ lowerboundEst,
113
+ interpEst,
114
+ interpEst,
115
+ ];
155
116
  }
117
+ }
118
+ else {
156
119
  return [
157
120
  markTwapWithMantissa,
158
121
  oracleTwapWithMantissa,
159
122
  lowerboundEst,
160
- cappedAltEst,
123
+ interpEst,
161
124
  interpEst,
162
125
  ];
163
- });
126
+ }
127
+ if (largerSide.gt(numericConstants_1.ZERO)) {
128
+ // funding smaller flow
129
+ cappedAltEst = smallerSide.mul(twapSpread).div(hoursInDay);
130
+ const feePoolTopOff = feePoolSize
131
+ .mul(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.QUOTE_PRECISION))
132
+ .mul(numericConstants_1.AMM_RESERVE_PRECISION);
133
+ cappedAltEst = cappedAltEst.add(feePoolTopOff).div(largerSide);
134
+ cappedAltEst = cappedAltEst
135
+ .mul(numericConstants_1.MARK_PRICE_PRECISION)
136
+ .mul(new anchor_1.BN(100))
137
+ .div(oracleTwapWithMantissa)
138
+ .mul(periodAdjustment);
139
+ if (cappedAltEst.abs().gte(interpEst.abs())) {
140
+ cappedAltEst = interpEst;
141
+ }
142
+ }
143
+ else {
144
+ cappedAltEst = interpEst;
145
+ }
146
+ return [
147
+ markTwapWithMantissa,
148
+ oracleTwapWithMantissa,
149
+ lowerboundEst,
150
+ cappedAltEst,
151
+ interpEst,
152
+ ];
164
153
  }
165
154
  exports.calculateAllEstimatedFundingRate = calculateAllEstimatedFundingRate;
166
155
  /**
@@ -171,20 +160,18 @@ exports.calculateAllEstimatedFundingRate = calculateAllEstimatedFundingRate;
171
160
  * @param estimationMethod
172
161
  * @returns Estimated funding rate. : Precision //TODO-PRECISION
173
162
  */
174
- function calculateEstimatedFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1), estimationMethod) {
175
- return __awaiter(this, void 0, void 0, function* () {
176
- const [_1, _2, lowerboundEst, cappedAltEst, interpEst] = yield calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
177
- if (estimationMethod == 'lowerbound') {
178
- //assuming remaining funding period has no gap
179
- return lowerboundEst;
180
- }
181
- else if (estimationMethod == 'capped') {
182
- return cappedAltEst;
183
- }
184
- else {
185
- return interpEst;
186
- }
187
- });
163
+ async function calculateEstimatedFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1), estimationMethod) {
164
+ const [_1, _2, lowerboundEst, cappedAltEst, interpEst] = await calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
165
+ if (estimationMethod == 'lowerbound') {
166
+ //assuming remaining funding period has no gap
167
+ return lowerboundEst;
168
+ }
169
+ else if (estimationMethod == 'capped') {
170
+ return cappedAltEst;
171
+ }
172
+ else {
173
+ return interpEst;
174
+ }
188
175
  }
189
176
  exports.calculateEstimatedFundingRate = calculateEstimatedFundingRate;
190
177
  /**
@@ -194,19 +181,17 @@ exports.calculateEstimatedFundingRate = calculateEstimatedFundingRate;
194
181
  * @param periodAdjustment
195
182
  * @returns Estimated funding rate. : Precision //TODO-PRECISION
196
183
  */
197
- function calculateLongShortFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
198
- return __awaiter(this, void 0, void 0, function* () {
199
- const [_1, _2, _, cappedAltEst, interpEst] = yield calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
200
- if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort)) {
201
- return [cappedAltEst, interpEst];
202
- }
203
- else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort)) {
204
- return [interpEst, cappedAltEst];
205
- }
206
- else {
207
- return [interpEst, interpEst];
208
- }
209
- });
184
+ async function calculateLongShortFundingRate(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
185
+ const [_1, _2, _, cappedAltEst, interpEst] = await calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
186
+ if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort)) {
187
+ return [cappedAltEst, interpEst];
188
+ }
189
+ else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort)) {
190
+ return [interpEst, cappedAltEst];
191
+ }
192
+ else {
193
+ return [interpEst, interpEst];
194
+ }
210
195
  }
211
196
  exports.calculateLongShortFundingRate = calculateLongShortFundingRate;
212
197
  /**
@@ -216,19 +201,17 @@ exports.calculateLongShortFundingRate = calculateLongShortFundingRate;
216
201
  * @param periodAdjustment
217
202
  * @returns Estimated funding rate. : Precision //TODO-PRECISION
218
203
  */
219
- function calculateLongShortFundingRateAndLiveTwaps(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
220
- return __awaiter(this, void 0, void 0, function* () {
221
- const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] = yield calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
222
- if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
223
- return [markTwapLive, oracleTwapLive, cappedAltEst, interpEst];
224
- }
225
- else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
226
- return [markTwapLive, oracleTwapLive, interpEst, cappedAltEst];
227
- }
228
- else {
229
- return [markTwapLive, oracleTwapLive, interpEst, interpEst];
230
- }
231
- });
204
+ async function calculateLongShortFundingRateAndLiveTwaps(market, oraclePriceData, periodAdjustment = new anchor_1.BN(1)) {
205
+ const [markTwapLive, oracleTwapLive, _2, cappedAltEst, interpEst] = await calculateAllEstimatedFundingRate(market, oraclePriceData, periodAdjustment);
206
+ if (market.baseAssetAmountLong.gt(market.baseAssetAmountShort.abs())) {
207
+ return [markTwapLive, oracleTwapLive, cappedAltEst, interpEst];
208
+ }
209
+ else if (market.baseAssetAmountLong.lt(market.baseAssetAmountShort.abs())) {
210
+ return [markTwapLive, oracleTwapLive, interpEst, cappedAltEst];
211
+ }
212
+ else {
213
+ return [markTwapLive, oracleTwapLive, interpEst, interpEst];
214
+ }
232
215
  }
233
216
  exports.calculateLongShortFundingRateAndLiveTwaps = calculateLongShortFundingRateAndLiveTwaps;
234
217
  /**
@@ -238,10 +221,10 @@ exports.calculateLongShortFundingRateAndLiveTwaps = calculateLongShortFundingRat
238
221
  */
239
222
  function calculateFundingPool(market) {
240
223
  // todo
241
- const totalFeeLB = market.amm.totalFee.div(new anchor_1.BN(2));
224
+ const totalFeeLB = market.amm.totalExchangeFee.div(new anchor_1.BN(2));
242
225
  const feePool = anchor_1.BN.max(numericConstants_1.ZERO, market.amm.totalFeeMinusDistributions
243
226
  .sub(totalFeeLB)
244
- .mul(new anchor_1.BN(2))
227
+ .mul(new anchor_1.BN(1))
245
228
  .div(new anchor_1.BN(3)));
246
229
  return feePool;
247
230
  }
@@ -1,6 +1,6 @@
1
1
  /// <reference types="bn.js" />
2
2
  import { BN } from '@project-serum/anchor';
3
- import { Market, PositionDirection } from '../types';
3
+ import { MarketAccount, PositionDirection } from '../types';
4
4
  import { OraclePriceData } from '../oracles/types';
5
5
  /**
6
6
  * Calculates market mark price
@@ -8,21 +8,21 @@ import { OraclePriceData } from '../oracles/types';
8
8
  * @param market
9
9
  * @return markPrice : Precision MARK_PRICE_PRECISION
10
10
  */
11
- export declare function calculateMarkPrice(market: Market): BN;
11
+ export declare function calculateMarkPrice(market: MarketAccount, oraclePriceData: OraclePriceData): BN;
12
12
  /**
13
13
  * Calculates market bid price
14
14
  *
15
15
  * @param market
16
16
  * @return bidPrice : Precision MARK_PRICE_PRECISION
17
17
  */
18
- export declare function calculateBidPrice(market: Market): BN;
18
+ export declare function calculateBidPrice(market: MarketAccount, oraclePriceData: OraclePriceData): BN;
19
19
  /**
20
20
  * Calculates market ask price
21
21
  *
22
22
  * @param market
23
23
  * @return bidPrice : Precision MARK_PRICE_PRECISION
24
24
  */
25
- export declare function calculateAskPrice(market: Market): BN;
26
- export declare function calculateNewMarketAfterTrade(baseAssetAmount: BN, direction: PositionDirection, market: Market): Market;
27
- export declare function calculateMarkOracleSpread(market: Market, oraclePriceData: OraclePriceData): BN;
25
+ export declare function calculateAskPrice(market: MarketAccount, oraclePriceData: OraclePriceData): BN;
26
+ export declare function calculateNewMarketAfterTrade(baseAssetAmount: BN, direction: PositionDirection, market: MarketAccount): MarketAccount;
27
+ export declare function calculateMarkOracleSpread(market: MarketAccount, oraclePriceData: OraclePriceData): BN;
28
28
  export declare function calculateOracleSpread(price: BN, oraclePriceData: OraclePriceData): BN;
@@ -9,8 +9,9 @@ const amm_1 = require("./amm");
9
9
  * @param market
10
10
  * @return markPrice : Precision MARK_PRICE_PRECISION
11
11
  */
12
- function calculateMarkPrice(market) {
13
- return (0, amm_1.calculatePrice)(market.amm.baseAssetReserve, market.amm.quoteAssetReserve, market.amm.pegMultiplier);
12
+ function calculateMarkPrice(market, oraclePriceData) {
13
+ const newAmm = (0, amm_1.calculateUpdatedAMM)(market.amm, oraclePriceData);
14
+ return (0, amm_1.calculatePrice)(newAmm.baseAssetReserve, newAmm.quoteAssetReserve, newAmm.pegMultiplier);
14
15
  }
15
16
  exports.calculateMarkPrice = calculateMarkPrice;
16
17
  /**
@@ -19,9 +20,9 @@ exports.calculateMarkPrice = calculateMarkPrice;
19
20
  * @param market
20
21
  * @return bidPrice : Precision MARK_PRICE_PRECISION
21
22
  */
22
- function calculateBidPrice(market) {
23
- const { baseAssetReserve, quoteAssetReserve } = (0, amm_1.calculateSpreadReserves)(market.amm, types_1.PositionDirection.SHORT);
24
- return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve, market.amm.pegMultiplier);
23
+ function calculateBidPrice(market, oraclePriceData) {
24
+ const { baseAssetReserve, quoteAssetReserve, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, types_1.PositionDirection.SHORT, oraclePriceData);
25
+ return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve, newPeg);
25
26
  }
26
27
  exports.calculateBidPrice = calculateBidPrice;
27
28
  /**
@@ -30,9 +31,9 @@ exports.calculateBidPrice = calculateBidPrice;
30
31
  * @param market
31
32
  * @return bidPrice : Precision MARK_PRICE_PRECISION
32
33
  */
33
- function calculateAskPrice(market) {
34
- const { baseAssetReserve, quoteAssetReserve } = (0, amm_1.calculateSpreadReserves)(market.amm, types_1.PositionDirection.LONG);
35
- return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve, market.amm.pegMultiplier);
34
+ function calculateAskPrice(market, oraclePriceData) {
35
+ const { baseAssetReserve, quoteAssetReserve, newPeg } = (0, amm_1.calculateUpdatedAMMSpreadReserves)(market.amm, types_1.PositionDirection.LONG, oraclePriceData);
36
+ return (0, amm_1.calculatePrice)(baseAssetReserve, quoteAssetReserve, newPeg);
36
37
  }
37
38
  exports.calculateAskPrice = calculateAskPrice;
38
39
  function calculateNewMarketAfterTrade(baseAssetAmount, direction, market) {
@@ -46,7 +47,7 @@ function calculateNewMarketAfterTrade(baseAssetAmount, direction, market) {
46
47
  }
47
48
  exports.calculateNewMarketAfterTrade = calculateNewMarketAfterTrade;
48
49
  function calculateMarkOracleSpread(market, oraclePriceData) {
49
- const markPrice = calculateMarkPrice(market);
50
+ const markPrice = calculateMarkPrice(market, oraclePriceData);
50
51
  return calculateOracleSpread(markPrice, oraclePriceData);
51
52
  }
52
53
  exports.calculateMarkOracleSpread = calculateMarkOracleSpread;
@@ -0,0 +1,3 @@
1
+ import { AMM, OracleGuardRails } from '../types';
2
+ import { OraclePriceData } from '../oracles/types';
3
+ export declare function isOracleValid(amm: AMM, oraclePriceData: OraclePriceData, oracleGuardRails: OracleGuardRails, slot: number): boolean;