@drift-labs/sdk 0.1.36-master.7 → 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.
- 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 +8 -17
- package/lib/admin.js +366 -558
- package/lib/clearingHouse.d.ts +84 -109
- package/lib/clearingHouse.js +778 -899
- 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 +34 -0
- package/lib/constants/markets.d.ts +8 -3
- package/lib/constants/markets.js +13 -206
- package/lib/constants/numericConstants.d.ts +16 -0
- package/lib/constants/numericConstants.js +22 -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 +112 -0
- package/lib/factory/bigNum.js +356 -0
- package/lib/factory/oracleClient.d.ts +1 -2
- package/lib/factory/oracleClient.js +6 -2
- package/lib/idl/clearing_house.json +2213 -2951
- package/lib/index.d.ts +12 -4
- package/lib/index.js +12 -4
- package/lib/math/amm.d.ts +19 -29
- package/lib/math/amm.js +129 -179
- package/lib/math/auction.d.ts +5 -0
- package/lib/math/auction.js +39 -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 +5 -0
- package/lib/math/orders.js +31 -1
- package/lib/math/position.d.ts +7 -5
- package/lib/math/position.js +27 -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 -21
- 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/orders.d.ts +6 -6
- package/lib/orders.js +10 -9
- package/lib/slot/SlotSubscriber.d.ts +12 -0
- package/lib/slot/SlotSubscriber.js +23 -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 +105 -109
- package/lib/types.js +13 -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 +10 -3
- package/src/accounts/bulkAccountLoader.ts +21 -15
- package/src/accounts/bulkUserSubscription.ts +1 -45
- package/src/accounts/fetch.ts +33 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
- package/src/accounts/pollingOracleSubscriber.ts +16 -8
- package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
- package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
- package/src/accounts/types.ts +41 -70
- package/src/accounts/webSocketAccountSubscriber.ts +33 -16
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
- package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
- package/src/addresses/marketAddresses.ts +18 -0
- package/src/addresses/pda.ts +118 -0
- package/src/admin.ts +205 -346
- package/src/clearingHouse.ts +918 -961
- 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 +43 -0
- package/src/constants/markets.ts +16 -207
- package/src/constants/numericConstants.ts +33 -5
- package/src/events/eventList.ts +94 -0
- package/src/events/eventSubscriber.ts +194 -0
- package/src/events/fetchLogs.ts +80 -0
- package/src/events/pollingLogProvider.ts +79 -0
- package/src/events/sort.ts +65 -0
- package/src/events/txEventCache.ts +74 -0
- package/src/events/types.ts +98 -0
- package/src/events/webSocketLogProvider.ts +38 -0
- package/src/examples/makeTradeExample.ts +20 -11
- package/src/factory/bigNum.ts +507 -0
- package/src/factory/oracleClient.ts +7 -4
- package/src/idl/clearing_house.json +2213 -2951
- package/src/index.ts +12 -4
- package/src/math/amm.ts +229 -245
- package/src/math/auction.ts +39 -0
- package/src/math/bankBalance.ts +112 -0
- package/src/math/conversion.ts +1 -11
- package/src/math/funding.ts +12 -9
- package/src/math/market.ts +37 -30
- package/src/math/orders.ts +38 -0
- package/src/math/position.ts +48 -35
- package/src/math/repeg.ts +175 -0
- package/src/math/trade.ts +45 -35
- package/src/math/utils.js +27 -0
- package/src/math/utils.js.map +1 -0
- package/src/oracles/oracleClientCache.ts +20 -0
- package/src/oracles/pythClient.ts +4 -11
- package/src/oracles/quoteAssetOracleClient.ts +25 -0
- package/src/oracles/switchboardClient.ts +11 -24
- package/src/oracles/types.ts +7 -1
- package/src/orders.ts +35 -20
- package/src/slot/SlotSubscriber.ts +32 -0
- package/src/tx/retryTxSender.ts +6 -4
- package/src/tx/types.ts +6 -1
- package/src/tx/utils.ts +22 -3
- package/src/types.ts +108 -122
- package/src/userName.ts +20 -0
- package/src/util/computeUnits.js +17 -0
- package/src/util/computeUnits.js.map +1 -0
- package/tests/bn/test.ts +255 -0
- package/tsconfig.json +12 -12
- package/lib/addresses.d.ts +0 -10
- package/lib/addresses.js +0 -93
- 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/lib/index.d.ts
CHANGED
|
@@ -6,6 +6,7 @@ export * from './oracles/pythClient';
|
|
|
6
6
|
export * from './oracles/switchboardClient';
|
|
7
7
|
export * from './types';
|
|
8
8
|
export * from './constants/markets';
|
|
9
|
+
export * from './accounts/fetch';
|
|
9
10
|
export * from './accounts/webSocketClearingHouseAccountSubscriber';
|
|
10
11
|
export * from './accounts/bulkAccountLoader';
|
|
11
12
|
export * from './accounts/bulkUserSubscription';
|
|
@@ -13,24 +14,28 @@ export * from './accounts/pollingClearingHouseAccountSubscriber';
|
|
|
13
14
|
export * from './accounts/pollingOracleSubscriber';
|
|
14
15
|
export * from './accounts/pollingTokenAccountSubscriber';
|
|
15
16
|
export * from './accounts/types';
|
|
16
|
-
export * from './addresses';
|
|
17
|
+
export * from './addresses/pda';
|
|
17
18
|
export * from './admin';
|
|
18
19
|
export * from './clearingHouseUser';
|
|
20
|
+
export * from './clearingHouseUserConfig';
|
|
19
21
|
export * from './clearingHouse';
|
|
20
|
-
export * from './factory/clearingHouse';
|
|
21
|
-
export * from './factory/clearingHouseUser';
|
|
22
22
|
export * from './factory/oracleClient';
|
|
23
|
+
export * from './factory/bigNum';
|
|
24
|
+
export * from './events/types';
|
|
25
|
+
export * from './events/eventSubscriber';
|
|
26
|
+
export * from './math/auction';
|
|
23
27
|
export * from './math/conversion';
|
|
24
28
|
export * from './math/funding';
|
|
25
|
-
export * from './math/insuranceFund';
|
|
26
29
|
export * from './math/market';
|
|
27
30
|
export * from './math/position';
|
|
28
31
|
export * from './math/oracles';
|
|
29
32
|
export * from './math/amm';
|
|
30
33
|
export * from './math/trade';
|
|
31
34
|
export * from './math/orders';
|
|
35
|
+
export * from './math/repeg';
|
|
32
36
|
export * from './orders';
|
|
33
37
|
export * from './orderParams';
|
|
38
|
+
export * from './slot/SlotSubscriber';
|
|
34
39
|
export * from './wallet';
|
|
35
40
|
export * from './types';
|
|
36
41
|
export * from './math/utils';
|
|
@@ -39,4 +44,7 @@ export * from './constants/numericConstants';
|
|
|
39
44
|
export * from './tx/retryTxSender';
|
|
40
45
|
export * from './util/computeUnits';
|
|
41
46
|
export * from './util/tps';
|
|
47
|
+
export * from './math/bankBalance';
|
|
48
|
+
export * from './constants/banks';
|
|
49
|
+
export * from './clearingHouseConfig';
|
|
42
50
|
export { BN, PublicKey };
|
package/lib/index.js
CHANGED
|
@@ -25,6 +25,7 @@ __exportStar(require("./oracles/pythClient"), exports);
|
|
|
25
25
|
__exportStar(require("./oracles/switchboardClient"), exports);
|
|
26
26
|
__exportStar(require("./types"), exports);
|
|
27
27
|
__exportStar(require("./constants/markets"), exports);
|
|
28
|
+
__exportStar(require("./accounts/fetch"), exports);
|
|
28
29
|
__exportStar(require("./accounts/webSocketClearingHouseAccountSubscriber"), exports);
|
|
29
30
|
__exportStar(require("./accounts/bulkAccountLoader"), exports);
|
|
30
31
|
__exportStar(require("./accounts/bulkUserSubscription"), exports);
|
|
@@ -32,24 +33,28 @@ __exportStar(require("./accounts/pollingClearingHouseAccountSubscriber"), export
|
|
|
32
33
|
__exportStar(require("./accounts/pollingOracleSubscriber"), exports);
|
|
33
34
|
__exportStar(require("./accounts/pollingTokenAccountSubscriber"), exports);
|
|
34
35
|
__exportStar(require("./accounts/types"), exports);
|
|
35
|
-
__exportStar(require("./addresses"), exports);
|
|
36
|
+
__exportStar(require("./addresses/pda"), exports);
|
|
36
37
|
__exportStar(require("./admin"), exports);
|
|
37
38
|
__exportStar(require("./clearingHouseUser"), exports);
|
|
39
|
+
__exportStar(require("./clearingHouseUserConfig"), exports);
|
|
38
40
|
__exportStar(require("./clearingHouse"), exports);
|
|
39
|
-
__exportStar(require("./factory/clearingHouse"), exports);
|
|
40
|
-
__exportStar(require("./factory/clearingHouseUser"), exports);
|
|
41
41
|
__exportStar(require("./factory/oracleClient"), exports);
|
|
42
|
+
__exportStar(require("./factory/bigNum"), exports);
|
|
43
|
+
__exportStar(require("./events/types"), exports);
|
|
44
|
+
__exportStar(require("./events/eventSubscriber"), exports);
|
|
45
|
+
__exportStar(require("./math/auction"), exports);
|
|
42
46
|
__exportStar(require("./math/conversion"), exports);
|
|
43
47
|
__exportStar(require("./math/funding"), exports);
|
|
44
|
-
__exportStar(require("./math/insuranceFund"), exports);
|
|
45
48
|
__exportStar(require("./math/market"), exports);
|
|
46
49
|
__exportStar(require("./math/position"), exports);
|
|
47
50
|
__exportStar(require("./math/oracles"), exports);
|
|
48
51
|
__exportStar(require("./math/amm"), exports);
|
|
49
52
|
__exportStar(require("./math/trade"), exports);
|
|
50
53
|
__exportStar(require("./math/orders"), exports);
|
|
54
|
+
__exportStar(require("./math/repeg"), exports);
|
|
51
55
|
__exportStar(require("./orders"), exports);
|
|
52
56
|
__exportStar(require("./orderParams"), exports);
|
|
57
|
+
__exportStar(require("./slot/SlotSubscriber"), exports);
|
|
53
58
|
__exportStar(require("./wallet"), exports);
|
|
54
59
|
__exportStar(require("./types"), exports);
|
|
55
60
|
__exportStar(require("./math/utils"), exports);
|
|
@@ -58,3 +63,6 @@ __exportStar(require("./constants/numericConstants"), exports);
|
|
|
58
63
|
__exportStar(require("./tx/retryTxSender"), exports);
|
|
59
64
|
__exportStar(require("./util/computeUnits"), exports);
|
|
60
65
|
__exportStar(require("./util/tps"), exports);
|
|
66
|
+
__exportStar(require("./math/bankBalance"), exports);
|
|
67
|
+
__exportStar(require("./constants/banks"), exports);
|
|
68
|
+
__exportStar(require("./clearingHouseConfig"), exports);
|
package/lib/math/amm.d.ts
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
|
-
import { AMM, PositionDirection, SwapDirection,
|
|
3
|
+
import { AMM, PositionDirection, SwapDirection, MarketAccount } from '../types';
|
|
4
|
+
import { OraclePriceData } from '../oracles/types';
|
|
5
|
+
export declare function calculateNewAmm(amm: AMM, oraclePriceData: OraclePriceData): [BN, BN, BN, BN];
|
|
6
|
+
export declare function calculateUpdatedAMM(amm: AMM, oraclePriceData: OraclePriceData): AMM;
|
|
7
|
+
export declare function calculateUpdatedAMMSpreadReserves(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): {
|
|
8
|
+
baseAssetReserve: BN;
|
|
9
|
+
quoteAssetReserve: BN;
|
|
10
|
+
sqrtK: BN;
|
|
11
|
+
newPeg: BN;
|
|
12
|
+
};
|
|
13
|
+
export declare function calculateBidAskPrice(amm: AMM, oraclePriceData: OraclePriceData): [BN, BN];
|
|
4
14
|
/**
|
|
5
15
|
* Calculates a price given an arbitrary base and quote amount (they must have the same precision)
|
|
6
16
|
*
|
|
7
|
-
* @param
|
|
8
|
-
* @param
|
|
9
|
-
* @param
|
|
17
|
+
* @param baseAssetReserves
|
|
18
|
+
* @param quoteAssetReserves
|
|
19
|
+
* @param pegMultiplier
|
|
10
20
|
* @returns price : Precision MARK_PRICE_PRECISION
|
|
11
21
|
*/
|
|
12
|
-
export declare function calculatePrice(
|
|
22
|
+
export declare function calculatePrice(baseAssetReserves: BN, quoteAssetReserves: BN, pegMultiplier: BN): BN;
|
|
13
23
|
export declare type AssetType = 'quote' | 'base';
|
|
14
24
|
/**
|
|
15
25
|
* Calculates what the amm reserves would be after swapping a quote or base asset amount.
|
|
@@ -21,8 +31,8 @@ export declare type AssetType = 'quote' | 'base';
|
|
|
21
31
|
* @returns quoteAssetReserve and baseAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
|
|
22
32
|
*/
|
|
23
33
|
export declare function calculateAmmReservesAfterSwap(amm: Pick<AMM, 'pegMultiplier' | 'quoteAssetReserve' | 'sqrtK' | 'baseAssetReserve'>, inputAssetType: AssetType, swapAmount: BN, swapDirection: SwapDirection): [BN, BN];
|
|
24
|
-
export declare function calculateSpread(amm: AMM, direction: PositionDirection): number;
|
|
25
|
-
export declare function calculateSpreadReserves(amm: AMM, direction: PositionDirection): {
|
|
34
|
+
export declare function calculateSpread(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): number;
|
|
35
|
+
export declare function calculateSpreadReserves(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): {
|
|
26
36
|
baseAssetReserve: BN;
|
|
27
37
|
quoteAssetReserve: BN;
|
|
28
38
|
};
|
|
@@ -43,32 +53,12 @@ export declare function calculateSwapOutput(inputAssetReserve: BN, swapAmount: B
|
|
|
43
53
|
* @param positionDirection
|
|
44
54
|
*/
|
|
45
55
|
export declare function getSwapDirection(inputAssetType: AssetType, positionDirection: PositionDirection): SwapDirection;
|
|
46
|
-
/**
|
|
47
|
-
* Helper function calculating adjust k cost
|
|
48
|
-
* @param market
|
|
49
|
-
* @param marketIndex
|
|
50
|
-
* @param numerator
|
|
51
|
-
* @param denomenator
|
|
52
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
53
|
-
*/
|
|
54
|
-
export declare function calculateAdjustKCost(market: Market, marketIndex: BN, numerator: BN, denomenator: BN): BN;
|
|
55
|
-
/**
|
|
56
|
-
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
57
|
-
*
|
|
58
|
-
* @param market
|
|
59
|
-
* @param marketIndex
|
|
60
|
-
* @param newPeg
|
|
61
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
62
|
-
*/
|
|
63
|
-
export declare function calculateRepegCost(market: Market, marketIndex: BN, newPeg: BN): BN;
|
|
64
56
|
/**
|
|
65
57
|
* Helper function calculating terminal price of amm
|
|
66
58
|
*
|
|
67
59
|
* @param market
|
|
68
60
|
* @returns cost : Precision MARK_PRICE_PRECISION
|
|
69
61
|
*/
|
|
70
|
-
export declare function calculateTerminalPrice(market:
|
|
71
|
-
export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, limit_price: BN, direction: PositionDirection,
|
|
72
|
-
export declare function calculateBudgetedK(market: Market, cost: BN): [BN, BN];
|
|
73
|
-
export declare function calculateBudgetedPeg(market: Market, cost: BN): BN;
|
|
62
|
+
export declare function calculateTerminalPrice(market: MarketAccount): BN;
|
|
63
|
+
export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, limit_price: BN, direction: PositionDirection, oraclePriceData?: OraclePriceData): [BN, PositionDirection];
|
|
74
64
|
export declare function calculateQuoteAssetAmountSwapped(quoteAssetReserves: BN, pegMultiplier: BN, swapDirection: SwapDirection): BN;
|
package/lib/math/amm.js
CHANGED
|
@@ -1,29 +1,104 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.calculateQuoteAssetAmountSwapped = exports.
|
|
3
|
+
exports.calculateQuoteAssetAmountSwapped = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateSpreadReserves = exports.calculateSpread = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = exports.calculateBidAskPrice = exports.calculateUpdatedAMMSpreadReserves = exports.calculateUpdatedAMM = exports.calculateNewAmm = void 0;
|
|
4
4
|
const anchor_1 = require("@project-serum/anchor");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
-
const position_1 = require("./position");
|
|
7
6
|
const types_1 = require("../types");
|
|
8
7
|
const assert_1 = require("../assert/assert");
|
|
9
8
|
const __1 = require("..");
|
|
9
|
+
const repeg_1 = require("./repeg");
|
|
10
|
+
function calculateNewAmm(amm, oraclePriceData) {
|
|
11
|
+
let pKNumer = new anchor_1.BN(1);
|
|
12
|
+
let pKDenom = new anchor_1.BN(1);
|
|
13
|
+
const targetPrice = oraclePriceData.price;
|
|
14
|
+
let newPeg = targetPrice
|
|
15
|
+
.mul(amm.baseAssetReserve)
|
|
16
|
+
.div(amm.quoteAssetReserve)
|
|
17
|
+
.add(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.PEG_PRECISION).div(new anchor_1.BN(2)))
|
|
18
|
+
.div(numericConstants_1.MARK_PRICE_PRECISION.div(numericConstants_1.PEG_PRECISION));
|
|
19
|
+
let prePegCost = (0, repeg_1.calculateRepegCost)(amm, newPeg);
|
|
20
|
+
const totalFeeLB = amm.totalExchangeFee.div(new anchor_1.BN(2));
|
|
21
|
+
const budget = anchor_1.BN.max(numericConstants_1.ZERO, amm.totalFeeMinusDistributions.sub(totalFeeLB));
|
|
22
|
+
if (prePegCost.gt(budget)) {
|
|
23
|
+
[pKNumer, pKDenom] = [new anchor_1.BN(999), new anchor_1.BN(1000)];
|
|
24
|
+
const deficitMadeup = (0, repeg_1.calculateAdjustKCost)(amm, pKNumer, pKDenom);
|
|
25
|
+
(0, assert_1.assert)(deficitMadeup.lte(new anchor_1.BN(0)));
|
|
26
|
+
prePegCost = budget.add(deficitMadeup.abs());
|
|
27
|
+
const newAmm = Object.assign({}, amm);
|
|
28
|
+
newAmm.baseAssetReserve = newAmm.baseAssetReserve.mul(pKNumer).div(pKDenom);
|
|
29
|
+
newAmm.sqrtK = newAmm.sqrtK.mul(pKNumer).div(pKDenom);
|
|
30
|
+
const invariant = newAmm.sqrtK.mul(newAmm.sqrtK);
|
|
31
|
+
newAmm.quoteAssetReserve = invariant.div(newAmm.baseAssetReserve);
|
|
32
|
+
const directionToClose = amm.netBaseAssetAmount.gt(numericConstants_1.ZERO)
|
|
33
|
+
? types_1.PositionDirection.SHORT
|
|
34
|
+
: types_1.PositionDirection.LONG;
|
|
35
|
+
const [newQuoteAssetReserve, _newBaseAssetReserve] = calculateAmmReservesAfterSwap(newAmm, 'base', amm.netBaseAssetAmount.abs(), getSwapDirection('base', directionToClose));
|
|
36
|
+
newAmm.terminalQuoteAssetReserve = newQuoteAssetReserve;
|
|
37
|
+
newPeg = (0, repeg_1.calculateBudgetedPeg)(newAmm, prePegCost, targetPrice);
|
|
38
|
+
prePegCost = (0, repeg_1.calculateRepegCost)(newAmm, newPeg);
|
|
39
|
+
}
|
|
40
|
+
return [prePegCost, pKNumer, pKDenom, newPeg];
|
|
41
|
+
}
|
|
42
|
+
exports.calculateNewAmm = calculateNewAmm;
|
|
43
|
+
function calculateUpdatedAMM(amm, oraclePriceData) {
|
|
44
|
+
if (amm.curveUpdateIntensity == 0) {
|
|
45
|
+
return amm;
|
|
46
|
+
}
|
|
47
|
+
const newAmm = Object.assign({}, amm);
|
|
48
|
+
const [prepegCost, pKNumer, pKDenom, newPeg] = calculateNewAmm(amm, oraclePriceData);
|
|
49
|
+
newAmm.baseAssetReserve = newAmm.baseAssetReserve.mul(pKNumer).div(pKDenom);
|
|
50
|
+
newAmm.sqrtK = newAmm.sqrtK.mul(pKNumer).div(pKDenom);
|
|
51
|
+
const invariant = newAmm.sqrtK.mul(newAmm.sqrtK);
|
|
52
|
+
newAmm.quoteAssetReserve = invariant.div(newAmm.baseAssetReserve);
|
|
53
|
+
newAmm.pegMultiplier = newPeg;
|
|
54
|
+
const directionToClose = amm.netBaseAssetAmount.gt(numericConstants_1.ZERO)
|
|
55
|
+
? types_1.PositionDirection.SHORT
|
|
56
|
+
: types_1.PositionDirection.LONG;
|
|
57
|
+
const [newQuoteAssetReserve, _newBaseAssetReserve] = calculateAmmReservesAfterSwap(newAmm, 'base', amm.netBaseAssetAmount.abs(), getSwapDirection('base', directionToClose));
|
|
58
|
+
newAmm.terminalQuoteAssetReserve = newQuoteAssetReserve;
|
|
59
|
+
newAmm.totalFeeMinusDistributions =
|
|
60
|
+
newAmm.totalFeeMinusDistributions.sub(prepegCost);
|
|
61
|
+
return newAmm;
|
|
62
|
+
}
|
|
63
|
+
exports.calculateUpdatedAMM = calculateUpdatedAMM;
|
|
64
|
+
function calculateUpdatedAMMSpreadReserves(amm, direction, oraclePriceData) {
|
|
65
|
+
const newAmm = calculateUpdatedAMM(amm, oraclePriceData);
|
|
66
|
+
const dirReserves = calculateSpreadReserves(newAmm, direction, oraclePriceData);
|
|
67
|
+
const result = {
|
|
68
|
+
baseAssetReserve: dirReserves.baseAssetReserve,
|
|
69
|
+
quoteAssetReserve: dirReserves.quoteAssetReserve,
|
|
70
|
+
sqrtK: newAmm.sqrtK,
|
|
71
|
+
newPeg: newAmm.pegMultiplier,
|
|
72
|
+
};
|
|
73
|
+
return result;
|
|
74
|
+
}
|
|
75
|
+
exports.calculateUpdatedAMMSpreadReserves = calculateUpdatedAMMSpreadReserves;
|
|
76
|
+
function calculateBidAskPrice(amm, oraclePriceData) {
|
|
77
|
+
const newAmm = calculateUpdatedAMM(amm, oraclePriceData);
|
|
78
|
+
const askReserves = calculateSpreadReserves(newAmm, types_1.PositionDirection.LONG, oraclePriceData);
|
|
79
|
+
const bidReserves = calculateSpreadReserves(newAmm, types_1.PositionDirection.SHORT, oraclePriceData);
|
|
80
|
+
const askPrice = calculatePrice(askReserves.baseAssetReserve, askReserves.quoteAssetReserve, newAmm.pegMultiplier);
|
|
81
|
+
const bidPrice = calculatePrice(bidReserves.baseAssetReserve, bidReserves.quoteAssetReserve, newAmm.pegMultiplier);
|
|
82
|
+
return [bidPrice, askPrice];
|
|
83
|
+
}
|
|
84
|
+
exports.calculateBidAskPrice = calculateBidAskPrice;
|
|
10
85
|
/**
|
|
11
86
|
* Calculates a price given an arbitrary base and quote amount (they must have the same precision)
|
|
12
87
|
*
|
|
13
|
-
* @param
|
|
14
|
-
* @param
|
|
15
|
-
* @param
|
|
88
|
+
* @param baseAssetReserves
|
|
89
|
+
* @param quoteAssetReserves
|
|
90
|
+
* @param pegMultiplier
|
|
16
91
|
* @returns price : Precision MARK_PRICE_PRECISION
|
|
17
92
|
*/
|
|
18
|
-
function calculatePrice(
|
|
19
|
-
if (
|
|
93
|
+
function calculatePrice(baseAssetReserves, quoteAssetReserves, pegMultiplier) {
|
|
94
|
+
if (baseAssetReserves.abs().lte(numericConstants_1.ZERO)) {
|
|
20
95
|
return new anchor_1.BN(0);
|
|
21
96
|
}
|
|
22
|
-
return
|
|
97
|
+
return quoteAssetReserves
|
|
23
98
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
24
|
-
.mul(
|
|
99
|
+
.mul(pegMultiplier)
|
|
25
100
|
.div(numericConstants_1.PEG_PRECISION)
|
|
26
|
-
.div(
|
|
101
|
+
.div(baseAssetReserves);
|
|
27
102
|
}
|
|
28
103
|
exports.calculatePrice = calculatePrice;
|
|
29
104
|
/**
|
|
@@ -51,27 +126,57 @@ function calculateAmmReservesAfterSwap(amm, inputAssetType, swapAmount, swapDire
|
|
|
51
126
|
return [newQuoteAssetReserve, newBaseAssetReserve];
|
|
52
127
|
}
|
|
53
128
|
exports.calculateAmmReservesAfterSwap = calculateAmmReservesAfterSwap;
|
|
54
|
-
function calculateSpread(amm, direction) {
|
|
55
|
-
let spread;
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
spread = amm.baseSpread;
|
|
129
|
+
function calculateSpread(amm, direction, oraclePriceData) {
|
|
130
|
+
let spread = amm.baseSpread / 2;
|
|
131
|
+
if (amm.baseSpread == 0 || amm.curveUpdateIntensity == 0) {
|
|
132
|
+
return spread;
|
|
59
133
|
}
|
|
60
|
-
|
|
61
|
-
|
|
134
|
+
const markPrice = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
135
|
+
const targetPrice = (oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.price) || markPrice;
|
|
136
|
+
const targetMarkSpreadPct = markPrice
|
|
137
|
+
.sub(targetPrice)
|
|
138
|
+
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
139
|
+
.div(markPrice);
|
|
140
|
+
// oracle retreat
|
|
141
|
+
if (((0, types_1.isVariant)(direction, 'long') && targetMarkSpreadPct.lt(numericConstants_1.ZERO)) ||
|
|
142
|
+
((0, types_1.isVariant)(direction, 'short') && targetMarkSpreadPct.gt(numericConstants_1.ZERO))) {
|
|
143
|
+
spread = Math.max(spread, targetMarkSpreadPct.abs().toNumber());
|
|
144
|
+
}
|
|
145
|
+
// inventory skew
|
|
146
|
+
const MAX_INVENTORY_SKEW = 5;
|
|
147
|
+
if ((amm.netBaseAssetAmount.gt(numericConstants_1.ZERO) && (0, types_1.isVariant)(direction, 'long')) ||
|
|
148
|
+
(amm.netBaseAssetAmount.lt(numericConstants_1.ZERO) && (0, types_1.isVariant)(direction, 'short')) ||
|
|
149
|
+
amm.totalFeeMinusDistributions.eq(numericConstants_1.ZERO)) {
|
|
150
|
+
const netCostBasis = amm.quoteAssetAmountLong.sub(amm.quoteAssetAmountShort);
|
|
151
|
+
const netBaseAssetValue = amm.quoteAssetReserve
|
|
152
|
+
.sub(amm.terminalQuoteAssetReserve)
|
|
153
|
+
.mul(amm.pegMultiplier)
|
|
154
|
+
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
155
|
+
const localBaseAssetValue = amm.netBaseAssetAmount
|
|
156
|
+
.mul(markPrice)
|
|
157
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
|
|
158
|
+
const netPnl = netBaseAssetValue.sub(netCostBasis);
|
|
159
|
+
const localPnl = localBaseAssetValue.sub(netCostBasis);
|
|
160
|
+
let effectiveLeverage = MAX_INVENTORY_SKEW;
|
|
161
|
+
if (amm.totalFeeMinusDistributions.gt(numericConstants_1.ZERO)) {
|
|
162
|
+
effectiveLeverage =
|
|
163
|
+
localPnl.sub(netPnl).toNumber() /
|
|
164
|
+
amm.totalFeeMinusDistributions.toNumber();
|
|
165
|
+
}
|
|
166
|
+
spread *= Math.min(MAX_INVENTORY_SKEW, 1 + effectiveLeverage);
|
|
62
167
|
}
|
|
63
168
|
return spread;
|
|
64
169
|
}
|
|
65
170
|
exports.calculateSpread = calculateSpread;
|
|
66
|
-
function calculateSpreadReserves(amm, direction) {
|
|
67
|
-
const spread = calculateSpread(amm, direction);
|
|
171
|
+
function calculateSpreadReserves(amm, direction, oraclePriceData) {
|
|
172
|
+
const spread = calculateSpread(amm, direction, oraclePriceData);
|
|
68
173
|
if (spread === 0) {
|
|
69
174
|
return {
|
|
70
175
|
baseAssetReserve: amm.baseAssetReserve,
|
|
71
176
|
quoteAssetReserve: amm.quoteAssetReserve,
|
|
72
177
|
};
|
|
73
178
|
}
|
|
74
|
-
const quoteAsserReserveDelta = amm.quoteAssetReserve.div(numericConstants_1.BID_ASK_SPREAD_PRECISION.div(new anchor_1.BN(spread /
|
|
179
|
+
const quoteAsserReserveDelta = amm.quoteAssetReserve.div(numericConstants_1.BID_ASK_SPREAD_PRECISION.div(new anchor_1.BN(spread / 2)));
|
|
75
180
|
let quoteAssetReserve;
|
|
76
181
|
if ((0, types_1.isVariant)(direction, 'long')) {
|
|
77
182
|
quoteAssetReserve = amm.quoteAssetReserve.add(quoteAsserReserveDelta);
|
|
@@ -123,92 +228,6 @@ function getSwapDirection(inputAssetType, positionDirection) {
|
|
|
123
228
|
return types_1.SwapDirection.ADD;
|
|
124
229
|
}
|
|
125
230
|
exports.getSwapDirection = getSwapDirection;
|
|
126
|
-
/**
|
|
127
|
-
* Helper function calculating adjust k cost
|
|
128
|
-
* @param market
|
|
129
|
-
* @param marketIndex
|
|
130
|
-
* @param numerator
|
|
131
|
-
* @param denomenator
|
|
132
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
133
|
-
*/
|
|
134
|
-
function calculateAdjustKCost(market, marketIndex, numerator, denomenator) {
|
|
135
|
-
const netUserPosition = {
|
|
136
|
-
baseAssetAmount: market.baseAssetAmount,
|
|
137
|
-
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
138
|
-
marketIndex: new anchor_1.BN(marketIndex),
|
|
139
|
-
quoteAssetAmount: new anchor_1.BN(0),
|
|
140
|
-
openOrders: new anchor_1.BN(0),
|
|
141
|
-
};
|
|
142
|
-
const currentValue = (0, position_1.calculateBaseAssetValue)(market, netUserPosition);
|
|
143
|
-
const marketNewK = Object.assign({}, market);
|
|
144
|
-
marketNewK.amm = Object.assign({}, market.amm);
|
|
145
|
-
marketNewK.amm.baseAssetReserve = market.amm.baseAssetReserve
|
|
146
|
-
.mul(numerator)
|
|
147
|
-
.div(denomenator);
|
|
148
|
-
marketNewK.amm.quoteAssetReserve = market.amm.quoteAssetReserve
|
|
149
|
-
.mul(numerator)
|
|
150
|
-
.div(denomenator);
|
|
151
|
-
marketNewK.amm.sqrtK = market.amm.sqrtK.mul(numerator).div(denomenator);
|
|
152
|
-
netUserPosition.quoteAssetAmount = currentValue;
|
|
153
|
-
const cost = (0, __1.calculatePositionPNL)(marketNewK, netUserPosition);
|
|
154
|
-
const p = numericConstants_1.PEG_PRECISION.mul(numerator).div(denomenator);
|
|
155
|
-
const x = market.amm.baseAssetReserve;
|
|
156
|
-
const y = market.amm.quoteAssetReserve;
|
|
157
|
-
const delta = market.baseAssetAmount;
|
|
158
|
-
const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
159
|
-
const numer1 = numericConstants_1.PEG_PRECISION.sub(p).mul(y).div(numericConstants_1.PEG_PRECISION);
|
|
160
|
-
const numer20 = k
|
|
161
|
-
.mul(p)
|
|
162
|
-
.mul(p)
|
|
163
|
-
.div(numericConstants_1.PEG_PRECISION)
|
|
164
|
-
.div(numericConstants_1.PEG_PRECISION)
|
|
165
|
-
.div(x.mul(p).div(numericConstants_1.PEG_PRECISION).add(delta));
|
|
166
|
-
const numer21 = k.div(x.add(delta));
|
|
167
|
-
const formulaCost = numer21
|
|
168
|
-
.sub(numer20)
|
|
169
|
-
.sub(numer1)
|
|
170
|
-
.mul(market.amm.pegMultiplier)
|
|
171
|
-
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
172
|
-
console.log((0, __1.convertToNumber)(formulaCost, numericConstants_1.QUOTE_PRECISION));
|
|
173
|
-
// p.div(p.mul(x).add(delta)).sub()
|
|
174
|
-
return cost;
|
|
175
|
-
}
|
|
176
|
-
exports.calculateAdjustKCost = calculateAdjustKCost;
|
|
177
|
-
/**
|
|
178
|
-
* Helper function calculating adjust pegMultiplier (repeg) cost
|
|
179
|
-
*
|
|
180
|
-
* @param market
|
|
181
|
-
* @param marketIndex
|
|
182
|
-
* @param newPeg
|
|
183
|
-
* @returns cost : Precision QUOTE_ASSET_PRECISION
|
|
184
|
-
*/
|
|
185
|
-
function calculateRepegCost(market, marketIndex, newPeg) {
|
|
186
|
-
const netUserPosition = {
|
|
187
|
-
baseAssetAmount: market.baseAssetAmount,
|
|
188
|
-
lastCumulativeFundingRate: market.amm.cumulativeFundingRate,
|
|
189
|
-
marketIndex: new anchor_1.BN(marketIndex),
|
|
190
|
-
quoteAssetAmount: new anchor_1.BN(0),
|
|
191
|
-
openOrders: new anchor_1.BN(0),
|
|
192
|
-
};
|
|
193
|
-
const currentValue = (0, position_1.calculateBaseAssetValue)(market, netUserPosition);
|
|
194
|
-
netUserPosition.quoteAssetAmount = currentValue;
|
|
195
|
-
const prevMarketPrice = (0, __1.calculateMarkPrice)(market);
|
|
196
|
-
const marketNewPeg = Object.assign({}, market);
|
|
197
|
-
marketNewPeg.amm = Object.assign({}, market.amm);
|
|
198
|
-
// const marketNewPeg = JSON.parse(JSON.stringify(market));
|
|
199
|
-
marketNewPeg.amm.pegMultiplier = newPeg;
|
|
200
|
-
console.log('Price moves from', (0, __1.convertToNumber)(prevMarketPrice), 'to', (0, __1.convertToNumber)((0, __1.calculateMarkPrice)(marketNewPeg)));
|
|
201
|
-
const cost = (0, __1.calculatePositionPNL)(marketNewPeg, netUserPosition);
|
|
202
|
-
const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
203
|
-
const newQuoteAssetReserve = k.div(market.amm.baseAssetReserve.add(netUserPosition.baseAssetAmount));
|
|
204
|
-
const deltaQuoteAssetReserves = newQuoteAssetReserve.sub(market.amm.quoteAssetReserve);
|
|
205
|
-
const cost2 = deltaQuoteAssetReserves
|
|
206
|
-
.mul(market.amm.pegMultiplier.sub(newPeg))
|
|
207
|
-
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
208
|
-
console.log((0, __1.convertToNumber)(cost2, numericConstants_1.QUOTE_PRECISION));
|
|
209
|
-
return cost;
|
|
210
|
-
}
|
|
211
|
-
exports.calculateRepegCost = calculateRepegCost;
|
|
212
231
|
/**
|
|
213
232
|
* Helper function calculating terminal price of amm
|
|
214
233
|
*
|
|
@@ -216,10 +235,10 @@ exports.calculateRepegCost = calculateRepegCost;
|
|
|
216
235
|
* @returns cost : Precision MARK_PRICE_PRECISION
|
|
217
236
|
*/
|
|
218
237
|
function calculateTerminalPrice(market) {
|
|
219
|
-
const directionToClose = market.
|
|
238
|
+
const directionToClose = market.amm.netBaseAssetAmount.gt(numericConstants_1.ZERO)
|
|
220
239
|
? types_1.PositionDirection.SHORT
|
|
221
240
|
: types_1.PositionDirection.LONG;
|
|
222
|
-
const [newQuoteAssetReserve, newBaseAssetReserve] = calculateAmmReservesAfterSwap(market.amm, 'base', market.
|
|
241
|
+
const [newQuoteAssetReserve, newBaseAssetReserve] = calculateAmmReservesAfterSwap(market.amm, 'base', market.amm.netBaseAssetAmount.abs(), getSwapDirection('base', directionToClose));
|
|
223
242
|
const terminalPrice = newQuoteAssetReserve
|
|
224
243
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
225
244
|
.mul(market.amm.pegMultiplier)
|
|
@@ -228,7 +247,7 @@ function calculateTerminalPrice(market) {
|
|
|
228
247
|
return terminalPrice;
|
|
229
248
|
}
|
|
230
249
|
exports.calculateTerminalPrice = calculateTerminalPrice;
|
|
231
|
-
function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction,
|
|
250
|
+
function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, oraclePriceData) {
|
|
232
251
|
const invariant = amm.sqrtK.mul(amm.sqrtK);
|
|
233
252
|
const newBaseAssetReserveSquared = invariant
|
|
234
253
|
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
@@ -236,13 +255,7 @@ function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, useSpre
|
|
|
236
255
|
.div(limit_price)
|
|
237
256
|
.div(numericConstants_1.PEG_PRECISION);
|
|
238
257
|
const newBaseAssetReserve = (0, __1.squareRootBN)(newBaseAssetReserveSquared);
|
|
239
|
-
|
|
240
|
-
if (useSpread) {
|
|
241
|
-
baseAssetReserveBefore = calculateSpreadReserves(amm, direction).baseAssetReserve;
|
|
242
|
-
}
|
|
243
|
-
else {
|
|
244
|
-
baseAssetReserveBefore = amm.baseAssetReserve;
|
|
245
|
-
}
|
|
258
|
+
const baseAssetReserveBefore = calculateSpreadReserves(amm, direction, oraclePriceData).baseAssetReserve;
|
|
246
259
|
if (newBaseAssetReserve.gt(baseAssetReserveBefore)) {
|
|
247
260
|
return [
|
|
248
261
|
newBaseAssetReserve.sub(baseAssetReserveBefore),
|
|
@@ -261,69 +274,6 @@ function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, useSpre
|
|
|
261
274
|
}
|
|
262
275
|
}
|
|
263
276
|
exports.calculateMaxBaseAssetAmountToTrade = calculateMaxBaseAssetAmountToTrade;
|
|
264
|
-
function calculateBudgetedK(market, cost) {
|
|
265
|
-
// wolframalpha.com
|
|
266
|
-
// (1/(x+d) - p/(x*p+d))*y*d*Q = C solve for p
|
|
267
|
-
// p = (d(y*d*Q - C(x+d))) / (C*x(x+d) + y*y*d*Q)
|
|
268
|
-
// todo: assumes k = x * y
|
|
269
|
-
// otherwise use: (y(1-p) + (kp^2/(x*p+d)) - k/(x+d)) * Q = C solve for p
|
|
270
|
-
// const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
271
|
-
const x = market.amm.baseAssetReserve;
|
|
272
|
-
const y = market.amm.quoteAssetReserve;
|
|
273
|
-
const d = market.baseAssetAmount;
|
|
274
|
-
const Q = market.amm.pegMultiplier;
|
|
275
|
-
const C = cost.mul(new anchor_1.BN(-1));
|
|
276
|
-
const numer1 = y.mul(d).mul(Q).div(numericConstants_1.AMM_RESERVE_PRECISION).div(numericConstants_1.PEG_PRECISION);
|
|
277
|
-
const numer2 = C.mul(x.add(d)).div(numericConstants_1.QUOTE_PRECISION);
|
|
278
|
-
const denom1 = C.mul(x)
|
|
279
|
-
.mul(x.add(d))
|
|
280
|
-
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
281
|
-
.div(numericConstants_1.QUOTE_PRECISION);
|
|
282
|
-
const denom2 = y
|
|
283
|
-
.mul(d)
|
|
284
|
-
.mul(d)
|
|
285
|
-
.mul(Q)
|
|
286
|
-
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
287
|
-
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
288
|
-
.div(numericConstants_1.PEG_PRECISION);
|
|
289
|
-
const numerator = d
|
|
290
|
-
.mul(numer1.add(numer2))
|
|
291
|
-
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
292
|
-
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
293
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
294
|
-
const denominator = denom1
|
|
295
|
-
.add(denom2)
|
|
296
|
-
.div(numericConstants_1.AMM_RESERVE_PRECISION)
|
|
297
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO);
|
|
298
|
-
console.log(numerator, denominator);
|
|
299
|
-
// const p = (numerator).div(denominator);
|
|
300
|
-
// const formulaCost = (numer21.sub(numer20).sub(numer1)).mul(market.amm.pegMultiplier).div(AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO)
|
|
301
|
-
// console.log(convertToNumber(formulaCost, QUOTE_PRECISION))
|
|
302
|
-
return [numerator, denominator];
|
|
303
|
-
}
|
|
304
|
-
exports.calculateBudgetedK = calculateBudgetedK;
|
|
305
|
-
function calculateBudgetedPeg(market, cost) {
|
|
306
|
-
// wolframalpha.com
|
|
307
|
-
// (1/(x+d) - p/(x*p+d))*y*d*Q = C solve for p
|
|
308
|
-
// p = (d(y*d*Q - C(x+d))) / (C*x(x+d) + y*y*d*Q)
|
|
309
|
-
// todo: assumes k = x * y
|
|
310
|
-
// otherwise use: (y(1-p) + (kp^2/(x*p+d)) - k/(x+d)) * Q = C solve for p
|
|
311
|
-
const k = market.amm.sqrtK.mul(market.amm.sqrtK);
|
|
312
|
-
const x = market.amm.baseAssetReserve;
|
|
313
|
-
const y = market.amm.quoteAssetReserve;
|
|
314
|
-
const d = market.baseAssetAmount;
|
|
315
|
-
const Q = market.amm.pegMultiplier;
|
|
316
|
-
const C = cost.mul(new anchor_1.BN(-1));
|
|
317
|
-
const deltaQuoteAssetReserves = y.sub(k.div(x.add(d)));
|
|
318
|
-
const deltaPegMultiplier = C.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
319
|
-
.div(deltaQuoteAssetReserves.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO))
|
|
320
|
-
.mul(numericConstants_1.PEG_PRECISION)
|
|
321
|
-
.div(numericConstants_1.QUOTE_PRECISION);
|
|
322
|
-
console.log(Q.toNumber(), 'change by', deltaPegMultiplier.toNumber() / numericConstants_1.MARK_PRICE_PRECISION.toNumber());
|
|
323
|
-
const newPeg = Q.sub(deltaPegMultiplier.mul(numericConstants_1.PEG_PRECISION).div(numericConstants_1.MARK_PRICE_PRECISION));
|
|
324
|
-
return newPeg;
|
|
325
|
-
}
|
|
326
|
-
exports.calculateBudgetedPeg = calculateBudgetedPeg;
|
|
327
277
|
function calculateQuoteAssetAmountSwapped(quoteAssetReserves, pegMultiplier, swapDirection) {
|
|
328
278
|
let quoteAssetAmount = quoteAssetReserves
|
|
329
279
|
.mul(pegMultiplier)
|
|
@@ -0,0 +1,39 @@
|
|
|
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
|
+
return new _1.BN(slot).sub(order.slot).gte(new _1.BN(order.auctionDuration));
|
|
8
|
+
}
|
|
9
|
+
exports.isAuctionComplete = isAuctionComplete;
|
|
10
|
+
function getAuctionPrice(order, slot) {
|
|
11
|
+
const slotsElapsed = new _1.BN(slot).sub(order.slot);
|
|
12
|
+
const deltaDenominator = new _1.BN(order.auctionDuration);
|
|
13
|
+
const deltaNumerator = _1.BN.min(slotsElapsed, deltaDenominator);
|
|
14
|
+
if (deltaDenominator.eq(_1.ZERO)) {
|
|
15
|
+
return order.auctionEndPrice;
|
|
16
|
+
}
|
|
17
|
+
let priceDelta;
|
|
18
|
+
if ((0, types_1.isVariant)(order.direction, 'long')) {
|
|
19
|
+
priceDelta = order.auctionEndPrice
|
|
20
|
+
.sub(order.auctionStartPrice)
|
|
21
|
+
.mul(deltaNumerator)
|
|
22
|
+
.div(deltaDenominator);
|
|
23
|
+
}
|
|
24
|
+
else {
|
|
25
|
+
priceDelta = order.auctionStartPrice
|
|
26
|
+
.sub(order.auctionEndPrice)
|
|
27
|
+
.mul(deltaNumerator)
|
|
28
|
+
.div(deltaDenominator);
|
|
29
|
+
}
|
|
30
|
+
let price;
|
|
31
|
+
if ((0, types_1.isVariant)(order.direction, 'long')) {
|
|
32
|
+
price = order.auctionStartPrice.add(priceDelta);
|
|
33
|
+
}
|
|
34
|
+
else {
|
|
35
|
+
price = order.auctionStartPrice.sub(priceDelta);
|
|
36
|
+
}
|
|
37
|
+
return price;
|
|
38
|
+
}
|
|
39
|
+
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
|
+
};
|