@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
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getAuctionPrice = exports.isAuctionComplete = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
const _1 = require("../.");
|
|
6
|
+
function isAuctionComplete(order, slot) {
|
|
7
|
+
if (order.auctionDuration === 0) {
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
return new _1.BN(slot).sub(order.slot).gt(new _1.BN(order.auctionDuration));
|
|
11
|
+
}
|
|
12
|
+
exports.isAuctionComplete = isAuctionComplete;
|
|
13
|
+
function getAuctionPrice(order, slot) {
|
|
14
|
+
const slotsElapsed = new _1.BN(slot).sub(order.slot);
|
|
15
|
+
const deltaDenominator = new _1.BN(order.auctionDuration);
|
|
16
|
+
const deltaNumerator = _1.BN.min(slotsElapsed, deltaDenominator);
|
|
17
|
+
if (deltaDenominator.eq(_1.ZERO)) {
|
|
18
|
+
return order.auctionEndPrice;
|
|
19
|
+
}
|
|
20
|
+
let priceDelta;
|
|
21
|
+
if ((0, types_1.isVariant)(order.direction, 'long')) {
|
|
22
|
+
priceDelta = order.auctionEndPrice
|
|
23
|
+
.sub(order.auctionStartPrice)
|
|
24
|
+
.mul(deltaNumerator)
|
|
25
|
+
.div(deltaDenominator);
|
|
26
|
+
}
|
|
27
|
+
else {
|
|
28
|
+
priceDelta = order.auctionStartPrice
|
|
29
|
+
.sub(order.auctionEndPrice)
|
|
30
|
+
.mul(deltaNumerator)
|
|
31
|
+
.div(deltaDenominator);
|
|
32
|
+
}
|
|
33
|
+
let price;
|
|
34
|
+
if ((0, types_1.isVariant)(order.direction, 'long')) {
|
|
35
|
+
price = order.auctionStartPrice.add(priceDelta);
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
price = order.auctionStartPrice.sub(priceDelta);
|
|
39
|
+
}
|
|
40
|
+
return price;
|
|
41
|
+
}
|
|
42
|
+
exports.getAuctionPrice = getAuctionPrice;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
/// <reference types="bn.js" />
|
|
2
|
+
import { BankAccount, BankBalanceType } from '../types';
|
|
3
|
+
import { BN } from '@project-serum/anchor';
|
|
4
|
+
export declare function getBalance(tokenAmount: BN, bank: BankAccount, balanceType: BankBalanceType): BN;
|
|
5
|
+
export declare function getTokenAmount(balanceAmount: BN, bank: BankAccount, balanceType: BankBalanceType): BN;
|
|
6
|
+
export declare function calculateInterestAccumulated(bank: BankAccount, now: BN): {
|
|
7
|
+
borrowInterest: BN;
|
|
8
|
+
depositInterest: BN;
|
|
9
|
+
};
|
|
@@ -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;
|
package/lib/math/conversion.d.ts
CHANGED
package/lib/math/conversion.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
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;
|
package/lib/math/funding.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
|
-
import {
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
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:
|
|
43
|
+
export declare function calculateFundingPool(market: MarketAccount): BN;
|
package/lib/math/funding.js
CHANGED
|
@@ -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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
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(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
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
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
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
|
-
|
|
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
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
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
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
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
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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.
|
|
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(
|
|
227
|
+
.mul(new anchor_1.BN(1))
|
|
245
228
|
.div(new anchor_1.BN(3)));
|
|
246
229
|
return feePool;
|
|
247
230
|
}
|
package/lib/math/market.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
|
-
import {
|
|
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:
|
|
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:
|
|
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:
|
|
26
|
-
export declare function calculateNewMarketAfterTrade(baseAssetAmount: BN, direction: PositionDirection, market:
|
|
27
|
-
export declare function calculateMarkOracleSpread(market:
|
|
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;
|