@drift-labs/sdk 0.1.36-master.7 → 0.2.0-master.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/accounts/bulkAccountLoader.d.ts +6 -6
- package/lib/accounts/bulkAccountLoader.js +81 -95
- package/lib/accounts/bulkUserSubscription.js +13 -57
- package/lib/accounts/fetch.d.ts +4 -0
- package/lib/accounts/fetch.js +18 -0
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +34 -38
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +224 -224
- package/lib/accounts/pollingOracleSubscriber.d.ts +3 -3
- package/lib/accounts/pollingOracleSubscriber.js +37 -49
- package/lib/accounts/pollingTokenAccountSubscriber.d.ts +3 -3
- package/lib/accounts/pollingTokenAccountSubscriber.js +35 -50
- package/lib/accounts/pollingUserAccountSubscriber.d.ts +7 -13
- package/lib/accounts/pollingUserAccountSubscriber.js +71 -134
- package/lib/accounts/types.d.ts +34 -41
- package/lib/accounts/webSocketAccountSubscriber.d.ts +6 -4
- package/lib/accounts/webSocketAccountSubscriber.js +39 -35
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +33 -28
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +153 -185
- package/lib/accounts/webSocketUserAccountSubscriber.d.ts +5 -11
- package/lib/accounts/webSocketUserAccountSubscriber.js +22 -67
- package/lib/addresses/marketAddresses.d.ts +4 -0
- package/lib/addresses/marketAddresses.js +15 -0
- package/lib/addresses/pda.d.ts +12 -0
- package/lib/addresses/pda.js +83 -0
- package/lib/admin.d.ts +12 -18
- package/lib/admin.js +398 -558
- package/lib/clearingHouse.d.ts +83 -109
- package/lib/clearingHouse.js +832 -893
- package/lib/clearingHouseConfig.d.ts +34 -0
- package/lib/clearingHouseConfig.js +2 -0
- package/lib/clearingHouseUser.d.ts +19 -22
- package/lib/clearingHouseUser.js +155 -141
- package/lib/clearingHouseUserConfig.d.ts +14 -0
- package/lib/clearingHouseUserConfig.js +2 -0
- package/lib/config.d.ts +12 -0
- package/lib/config.js +35 -4
- package/lib/constants/banks.d.ts +16 -0
- package/lib/constants/banks.js +41 -0
- package/lib/constants/markets.d.ts +8 -3
- package/lib/constants/markets.js +13 -206
- package/lib/constants/numericConstants.d.ts +17 -0
- package/lib/constants/numericConstants.js +23 -6
- package/lib/events/eventList.d.ts +22 -0
- package/lib/events/eventList.js +77 -0
- package/lib/events/eventSubscriber.d.ts +34 -0
- package/lib/events/eventSubscriber.js +126 -0
- package/lib/events/fetchLogs.d.ts +13 -0
- package/lib/events/fetchLogs.js +39 -0
- package/lib/events/pollingLogProvider.d.ts +15 -0
- package/lib/events/pollingLogProvider.js +53 -0
- package/lib/events/sort.d.ts +2 -0
- package/lib/events/sort.js +44 -0
- package/lib/events/txEventCache.d.ts +24 -0
- package/lib/events/txEventCache.js +71 -0
- package/lib/events/types.d.ts +49 -0
- package/lib/events/types.js +20 -0
- package/lib/events/webSocketLogProvider.d.ts +12 -0
- package/lib/events/webSocketLogProvider.js +30 -0
- package/lib/examples/makeTradeExample.js +26 -27
- package/lib/factory/bigNum.d.ts +118 -0
- package/lib/factory/bigNum.js +364 -0
- package/lib/factory/oracleClient.d.ts +1 -2
- package/lib/factory/oracleClient.js +6 -2
- package/lib/idl/clearing_house.json +2258 -2774
- package/lib/index.d.ts +14 -5
- package/lib/index.js +18 -5
- package/lib/math/amm.d.ts +24 -29
- package/lib/math/amm.js +209 -176
- package/lib/math/auction.d.ts +5 -0
- package/lib/math/auction.js +42 -0
- package/lib/math/bankBalance.d.ts +9 -0
- package/lib/math/bankBalance.js +75 -0
- package/lib/math/conversion.d.ts +0 -1
- package/lib/math/conversion.js +1 -5
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +158 -175
- package/lib/math/market.d.ts +6 -6
- package/lib/math/market.js +10 -9
- package/lib/math/orders.d.ts +6 -1
- package/lib/math/orders.js +38 -1
- package/lib/math/position.d.ts +7 -5
- package/lib/math/position.js +29 -27
- package/lib/math/repeg.d.ts +22 -0
- package/lib/math/repeg.js +128 -0
- package/lib/math/trade.d.ts +5 -4
- package/lib/math/trade.js +29 -24
- package/lib/mockUSDCFaucet.js +87 -116
- package/lib/oracles/oracleClientCache.d.ts +8 -0
- package/lib/oracles/oracleClientCache.js +19 -0
- package/lib/oracles/pythClient.d.ts +3 -5
- package/lib/oracles/pythClient.js +12 -31
- package/lib/oracles/quoteAssetOracleClient.d.ts +9 -0
- package/lib/oracles/quoteAssetOracleClient.js +21 -0
- package/lib/oracles/switchboardClient.d.ts +3 -5
- package/lib/oracles/switchboardClient.js +29 -50
- package/lib/oracles/types.d.ts +6 -1
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +8 -96
- package/lib/orders.d.ts +6 -7
- package/lib/orders.js +11 -89
- package/lib/slot/SlotSubscriber.d.ts +19 -0
- package/lib/slot/SlotSubscriber.js +26 -0
- package/lib/tx/retryTxSender.d.ts +2 -2
- package/lib/tx/retryTxSender.js +108 -123
- package/lib/tx/types.d.ts +5 -1
- package/lib/tx/utils.d.ts +1 -1
- package/lib/tx/utils.js +11 -2
- package/lib/types.d.ts +180 -110
- package/lib/types.js +54 -1
- package/lib/userName.d.ts +4 -0
- package/lib/userName.js +20 -0
- package/lib/util/computeUnits.js +10 -21
- package/lib/util/tps.js +11 -22
- package/lib/wallet.js +7 -20
- package/package.json +11 -4
- package/src/accounts/bulkAccountLoader.js +197 -0
- package/src/accounts/bulkAccountLoader.ts +21 -15
- package/src/accounts/bulkUserSubscription.js +33 -0
- package/src/accounts/bulkUserSubscription.ts +1 -45
- package/src/accounts/fetch.js +29 -0
- package/src/accounts/fetch.ts +33 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.js +311 -0
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +249 -214
- package/src/accounts/pollingOracleSubscriber.js +93 -0
- package/src/accounts/pollingOracleSubscriber.ts +16 -8
- package/src/accounts/pollingTokenAccountSubscriber.js +90 -0
- package/src/accounts/pollingTokenAccountSubscriber.ts +11 -8
- package/src/accounts/pollingUserAccountSubscriber.js +132 -0
- package/src/accounts/pollingUserAccountSubscriber.ts +21 -86
- package/src/accounts/types.js +10 -0
- package/src/accounts/types.ts +41 -70
- package/src/accounts/utils.js +7 -0
- package/src/accounts/webSocketAccountSubscriber.js +93 -0
- package/src/accounts/webSocketAccountSubscriber.ts +33 -16
- package/src/accounts/webSocketClearingHouseAccountSubscriber.js +233 -0
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +190 -240
- package/src/accounts/webSocketUserAccountSubscriber.js +62 -0
- package/src/accounts/webSocketUserAccountSubscriber.ts +11 -71
- package/src/addresses/marketAddresses.js +26 -0
- package/src/addresses/marketAddresses.ts +18 -0
- package/{lib/addresses.js → src/addresses/pda.js} +45 -34
- package/src/addresses/pda.ts +118 -0
- package/src/admin.ts +247 -336
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +1023 -1026
- package/src/clearingHouseConfig.ts +37 -0
- package/src/clearingHouseUser.ts +275 -185
- package/src/clearingHouseUserConfig.ts +18 -0
- package/src/config.ts +54 -1
- package/src/constants/banks.ts +50 -0
- package/src/constants/markets.ts +16 -207
- package/src/constants/numericConstants.ts +34 -5
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +94 -0
- package/src/events/eventSubscriber.js +139 -0
- package/src/events/eventSubscriber.ts +194 -0
- package/src/events/fetchLogs.js +50 -0
- package/src/events/fetchLogs.ts +80 -0
- package/src/events/pollingLogProvider.js +64 -0
- package/src/events/pollingLogProvider.ts +79 -0
- package/src/events/sort.js +44 -0
- package/src/events/sort.ts +65 -0
- package/src/events/txEventCache.js +71 -0
- package/src/events/txEventCache.ts +74 -0
- package/src/events/types.js +20 -0
- package/src/events/types.ts +98 -0
- package/src/events/webSocketLogProvider.js +41 -0
- package/src/events/webSocketLogProvider.ts +38 -0
- package/src/examples/makeTradeExample.js +80 -0
- package/src/examples/makeTradeExample.ts +20 -11
- package/src/factory/bigNum.js +364 -0
- package/src/factory/bigNum.ts +524 -0
- package/src/factory/oracleClient.js +20 -0
- package/src/factory/oracleClient.ts +7 -4
- package/src/idl/clearing_house.json +2258 -2774
- package/src/index.js +69 -0
- package/src/index.ts +14 -5
- package/src/math/amm.js +369 -0
- package/src/math/amm.ts +382 -243
- package/src/math/auction.js +42 -0
- package/src/math/auction.ts +43 -0
- package/src/math/bankBalance.js +75 -0
- package/src/math/bankBalance.ts +112 -0
- package/src/math/conversion.js +11 -0
- package/src/math/conversion.ts +1 -11
- package/src/math/funding.js +248 -0
- package/src/math/funding.ts +12 -9
- package/src/math/market.js +57 -0
- package/src/math/market.ts +37 -30
- package/src/math/oracles.js +26 -0
- package/src/math/orders.js +110 -0
- package/src/math/orders.ts +43 -1
- package/src/math/position.js +140 -0
- package/src/math/position.ts +52 -35
- package/src/math/repeg.js +128 -0
- package/src/math/repeg.ts +176 -0
- package/src/math/state.js +15 -0
- package/src/math/trade.js +253 -0
- package/src/math/trade.ts +55 -47
- package/src/math/utils.js +26 -0
- package/src/math/utils.js.map +1 -0
- package/src/mockUSDCFaucet.js +171 -0
- package/src/oracles/oracleClientCache.js +19 -0
- package/src/oracles/oracleClientCache.ts +20 -0
- package/src/oracles/pythClient.js +46 -0
- package/src/oracles/pythClient.ts +4 -11
- package/src/oracles/quoteAssetOracleClient.js +32 -0
- package/src/oracles/quoteAssetOracleClient.ts +25 -0
- package/src/oracles/switchboardClient.js +69 -0
- package/src/oracles/switchboardClient.ts +11 -24
- package/src/oracles/types.js +2 -0
- package/src/oracles/types.ts +7 -1
- package/src/orderParams.js +20 -0
- package/src/orderParams.ts +20 -141
- package/src/orders.js +134 -0
- package/src/orders.ts +25 -134
- package/src/slot/SlotSubscriber.js +39 -0
- package/src/slot/SlotSubscriber.ts +42 -0
- package/src/token/index.js +38 -0
- package/src/tx/retryTxSender.js +188 -0
- package/src/tx/retryTxSender.ts +6 -4
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +6 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +22 -3
- package/src/types.js +114 -0
- package/src/types.ts +176 -124
- package/src/userName.js +20 -0
- package/src/userName.ts +20 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/src/wallet.js +35 -0
- package/tests/bn/test.ts +255 -0
- package/tsconfig.json +12 -12
- package/lib/addresses.d.ts +0 -10
- package/lib/constants/accounts.d.ts +0 -15
- package/lib/constants/accounts.js +0 -18
- package/lib/factory/clearingHouse.d.ts +0 -35
- package/lib/factory/clearingHouse.js +0 -81
- package/lib/factory/clearingHouseUser.d.ts +0 -19
- package/lib/factory/clearingHouseUser.js +0 -34
- package/lib/math/insuranceFund.d.ts +0 -15
- package/lib/math/insuranceFund.js +0 -33
- package/lib/settlement.d.ts +0 -4
- package/lib/settlement.js +0 -10
- package/lib/tx/defaultTxSender.d.ts +0 -8
- package/lib/tx/defaultTxSender.js +0 -12
- package/src/addresses.ts +0 -82
- package/src/constants/accounts.ts +0 -26
- package/src/factory/clearingHouse.ts +0 -173
- package/src/factory/clearingHouseUser.ts +0 -73
- package/src/math/insuranceFund.ts +0 -29
- package/src/settlement.ts +0 -9
- package/src/tx/defaultTxSender.ts +0 -24
package/src/index.js
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
|
|
5
|
+
}) : (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
o[k2] = m[k];
|
|
8
|
+
}));
|
|
9
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
10
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
11
|
+
};
|
|
12
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
13
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.pyth = exports.PublicKey = exports.BN = void 0;
|
|
17
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
18
|
+
Object.defineProperty(exports, "BN", { enumerable: true, get: function () { return anchor_1.BN; } });
|
|
19
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
20
|
+
Object.defineProperty(exports, "PublicKey", { enumerable: true, get: function () { return web3_js_1.PublicKey; } });
|
|
21
|
+
const client_1 = __importDefault(require("@pythnetwork/client"));
|
|
22
|
+
exports.pyth = client_1.default;
|
|
23
|
+
__exportStar(require("./mockUSDCFaucet"), exports);
|
|
24
|
+
__exportStar(require("./oracles/types"), exports);
|
|
25
|
+
__exportStar(require("./oracles/pythClient"), exports);
|
|
26
|
+
__exportStar(require("./oracles/switchboardClient"), exports);
|
|
27
|
+
__exportStar(require("./types"), exports);
|
|
28
|
+
__exportStar(require("./constants/markets"), exports);
|
|
29
|
+
__exportStar(require("./accounts/fetch"), exports);
|
|
30
|
+
__exportStar(require("./accounts/webSocketClearingHouseAccountSubscriber"), exports);
|
|
31
|
+
__exportStar(require("./accounts/bulkAccountLoader"), exports);
|
|
32
|
+
__exportStar(require("./accounts/bulkUserSubscription"), exports);
|
|
33
|
+
__exportStar(require("./accounts/pollingClearingHouseAccountSubscriber"), exports);
|
|
34
|
+
__exportStar(require("./accounts/pollingOracleSubscriber"), exports);
|
|
35
|
+
__exportStar(require("./accounts/pollingTokenAccountSubscriber"), exports);
|
|
36
|
+
__exportStar(require("./accounts/types"), exports);
|
|
37
|
+
__exportStar(require("./addresses/pda"), exports);
|
|
38
|
+
__exportStar(require("./admin"), exports);
|
|
39
|
+
__exportStar(require("./clearingHouseUser"), exports);
|
|
40
|
+
__exportStar(require("./clearingHouseUserConfig"), exports);
|
|
41
|
+
__exportStar(require("./clearingHouse"), exports);
|
|
42
|
+
__exportStar(require("./factory/oracleClient"), exports);
|
|
43
|
+
__exportStar(require("./factory/bigNum"), exports);
|
|
44
|
+
__exportStar(require("./events/types"), exports);
|
|
45
|
+
__exportStar(require("./events/eventSubscriber"), exports);
|
|
46
|
+
__exportStar(require("./math/auction"), exports);
|
|
47
|
+
__exportStar(require("./math/conversion"), exports);
|
|
48
|
+
__exportStar(require("./math/funding"), exports);
|
|
49
|
+
__exportStar(require("./math/market"), exports);
|
|
50
|
+
__exportStar(require("./math/position"), exports);
|
|
51
|
+
__exportStar(require("./math/oracles"), exports);
|
|
52
|
+
__exportStar(require("./math/amm"), exports);
|
|
53
|
+
__exportStar(require("./math/trade"), exports);
|
|
54
|
+
__exportStar(require("./math/orders"), exports);
|
|
55
|
+
__exportStar(require("./math/repeg"), exports);
|
|
56
|
+
__exportStar(require("./orders"), exports);
|
|
57
|
+
__exportStar(require("./orderParams"), exports);
|
|
58
|
+
__exportStar(require("./slot/SlotSubscriber"), exports);
|
|
59
|
+
__exportStar(require("./wallet"), exports);
|
|
60
|
+
__exportStar(require("./types"), exports);
|
|
61
|
+
__exportStar(require("./math/utils"), exports);
|
|
62
|
+
__exportStar(require("./config"), exports);
|
|
63
|
+
__exportStar(require("./constants/numericConstants"), exports);
|
|
64
|
+
__exportStar(require("./tx/retryTxSender"), exports);
|
|
65
|
+
__exportStar(require("./util/computeUnits"), exports);
|
|
66
|
+
__exportStar(require("./util/tps"), exports);
|
|
67
|
+
__exportStar(require("./math/bankBalance"), exports);
|
|
68
|
+
__exportStar(require("./constants/banks"), exports);
|
|
69
|
+
__exportStar(require("./clearingHouseConfig"), exports);
|
package/src/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { BN } from '@project-serum/anchor';
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
|
+
import pyth from '@pythnetwork/client';
|
|
3
4
|
|
|
4
5
|
export * from './mockUSDCFaucet';
|
|
5
6
|
export * from './oracles/types';
|
|
@@ -7,6 +8,7 @@ export * from './oracles/pythClient';
|
|
|
7
8
|
export * from './oracles/switchboardClient';
|
|
8
9
|
export * from './types';
|
|
9
10
|
export * from './constants/markets';
|
|
11
|
+
export * from './accounts/fetch';
|
|
10
12
|
export * from './accounts/webSocketClearingHouseAccountSubscriber';
|
|
11
13
|
export * from './accounts/bulkAccountLoader';
|
|
12
14
|
export * from './accounts/bulkUserSubscription';
|
|
@@ -14,24 +16,28 @@ export * from './accounts/pollingClearingHouseAccountSubscriber';
|
|
|
14
16
|
export * from './accounts/pollingOracleSubscriber';
|
|
15
17
|
export * from './accounts/pollingTokenAccountSubscriber';
|
|
16
18
|
export * from './accounts/types';
|
|
17
|
-
export * from './addresses';
|
|
19
|
+
export * from './addresses/pda';
|
|
18
20
|
export * from './admin';
|
|
19
21
|
export * from './clearingHouseUser';
|
|
22
|
+
export * from './clearingHouseUserConfig';
|
|
20
23
|
export * from './clearingHouse';
|
|
21
|
-
export * from './factory/clearingHouse';
|
|
22
|
-
export * from './factory/clearingHouseUser';
|
|
23
24
|
export * from './factory/oracleClient';
|
|
25
|
+
export * from './factory/bigNum';
|
|
26
|
+
export * from './events/types';
|
|
27
|
+
export * from './events/eventSubscriber';
|
|
28
|
+
export * from './math/auction';
|
|
24
29
|
export * from './math/conversion';
|
|
25
30
|
export * from './math/funding';
|
|
26
|
-
export * from './math/insuranceFund';
|
|
27
31
|
export * from './math/market';
|
|
28
32
|
export * from './math/position';
|
|
29
33
|
export * from './math/oracles';
|
|
30
34
|
export * from './math/amm';
|
|
31
35
|
export * from './math/trade';
|
|
32
36
|
export * from './math/orders';
|
|
37
|
+
export * from './math/repeg';
|
|
33
38
|
export * from './orders';
|
|
34
39
|
export * from './orderParams';
|
|
40
|
+
export * from './slot/SlotSubscriber';
|
|
35
41
|
export * from './wallet';
|
|
36
42
|
export * from './types';
|
|
37
43
|
export * from './math/utils';
|
|
@@ -40,5 +46,8 @@ export * from './constants/numericConstants';
|
|
|
40
46
|
export * from './tx/retryTxSender';
|
|
41
47
|
export * from './util/computeUnits';
|
|
42
48
|
export * from './util/tps';
|
|
49
|
+
export * from './math/bankBalance';
|
|
50
|
+
export * from './constants/banks';
|
|
51
|
+
export * from './clearingHouseConfig';
|
|
43
52
|
|
|
44
|
-
export { BN, PublicKey };
|
|
53
|
+
export { BN, PublicKey, pyth };
|
package/src/math/amm.js
ADDED
|
@@ -0,0 +1,369 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.calculateQuoteAssetAmountSwapped = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateSpreadReserves = exports.calculateSpread = exports.calculateSpreadBN = exports.calculateMaxSpread = exports.calculateEffectiveLeverage = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = exports.calculateBidAskPrice = exports.calculateUpdatedAMMSpreadReserves = exports.calculateUpdatedAMM = exports.calculateNewAmm = exports.calculateOptimalPegAndBudget = exports.calculatePegFromTargetPrice = void 0;
|
|
4
|
+
const anchor_1 = require("@project-serum/anchor");
|
|
5
|
+
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
|
+
const types_1 = require("../types");
|
|
7
|
+
const assert_1 = require("../assert/assert");
|
|
8
|
+
const __1 = require("..");
|
|
9
|
+
const repeg_1 = require("./repeg");
|
|
10
|
+
function calculatePegFromTargetPrice(targetPrice, baseAssetReserve, quoteAssetReserve) {
|
|
11
|
+
return targetPrice
|
|
12
|
+
.mul(baseAssetReserve)
|
|
13
|
+
.div(quoteAssetReserve)
|
|
14
|
+
.add(numericConstants_1.PRICE_DIV_PEG.div(new anchor_1.BN(2)))
|
|
15
|
+
.div(numericConstants_1.PRICE_DIV_PEG);
|
|
16
|
+
}
|
|
17
|
+
exports.calculatePegFromTargetPrice = calculatePegFromTargetPrice;
|
|
18
|
+
function calculateOptimalPegAndBudget(amm, oraclePriceData) {
|
|
19
|
+
const markPriceBefore = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
20
|
+
const targetPrice = oraclePriceData.price;
|
|
21
|
+
const newPeg = calculatePegFromTargetPrice(targetPrice, amm.baseAssetReserve, amm.quoteAssetReserve);
|
|
22
|
+
const prePegCost = repeg_1.calculateRepegCost(amm, newPeg);
|
|
23
|
+
const totalFeeLB = amm.totalExchangeFee.div(new anchor_1.BN(2));
|
|
24
|
+
const budget = anchor_1.BN.max(numericConstants_1.ZERO, amm.totalFeeMinusDistributions.sub(totalFeeLB));
|
|
25
|
+
if (budget.lt(prePegCost)) {
|
|
26
|
+
const maxPriceSpread = new anchor_1.BN(amm.maxSpread)
|
|
27
|
+
.mul(targetPrice)
|
|
28
|
+
.div(numericConstants_1.BID_ASK_SPREAD_PRECISION);
|
|
29
|
+
let newTargetPrice;
|
|
30
|
+
let newOptimalPeg;
|
|
31
|
+
let newBudget;
|
|
32
|
+
const targetPriceGap = markPriceBefore.sub(targetPrice);
|
|
33
|
+
if (targetPriceGap.abs().gt(maxPriceSpread)) {
|
|
34
|
+
const markAdj = targetPriceGap.abs().sub(maxPriceSpread);
|
|
35
|
+
if (targetPriceGap.lt(new anchor_1.BN(0))) {
|
|
36
|
+
newTargetPrice = markPriceBefore.add(markAdj);
|
|
37
|
+
}
|
|
38
|
+
else {
|
|
39
|
+
newTargetPrice = markPriceBefore.sub(markAdj);
|
|
40
|
+
}
|
|
41
|
+
newOptimalPeg = calculatePegFromTargetPrice(newTargetPrice, amm.baseAssetReserve, amm.quoteAssetReserve);
|
|
42
|
+
newBudget = repeg_1.calculateRepegCost(amm, newOptimalPeg);
|
|
43
|
+
return [newTargetPrice, newOptimalPeg, newBudget, false];
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
return [targetPrice, newPeg, budget, true];
|
|
47
|
+
}
|
|
48
|
+
exports.calculateOptimalPegAndBudget = calculateOptimalPegAndBudget;
|
|
49
|
+
function calculateNewAmm(amm, oraclePriceData) {
|
|
50
|
+
let pKNumer = new anchor_1.BN(1);
|
|
51
|
+
let pKDenom = new anchor_1.BN(1);
|
|
52
|
+
const [targetPrice, _newPeg, budget, checkLowerBound] = calculateOptimalPegAndBudget(amm, oraclePriceData);
|
|
53
|
+
let prePegCost = repeg_1.calculateRepegCost(amm, _newPeg);
|
|
54
|
+
let newPeg = _newPeg;
|
|
55
|
+
if (prePegCost.gt(budget) && checkLowerBound) {
|
|
56
|
+
[pKNumer, pKDenom] = [new anchor_1.BN(999), new anchor_1.BN(1000)];
|
|
57
|
+
const deficitMadeup = repeg_1.calculateAdjustKCost(amm, pKNumer, pKDenom);
|
|
58
|
+
assert_1.assert(deficitMadeup.lte(new anchor_1.BN(0)));
|
|
59
|
+
prePegCost = budget.add(deficitMadeup.abs());
|
|
60
|
+
const newAmm = Object.assign({}, amm);
|
|
61
|
+
newAmm.baseAssetReserve = newAmm.baseAssetReserve.mul(pKNumer).div(pKDenom);
|
|
62
|
+
newAmm.sqrtK = newAmm.sqrtK.mul(pKNumer).div(pKDenom);
|
|
63
|
+
const invariant = newAmm.sqrtK.mul(newAmm.sqrtK);
|
|
64
|
+
newAmm.quoteAssetReserve = invariant.div(newAmm.baseAssetReserve);
|
|
65
|
+
const directionToClose = amm.netBaseAssetAmount.gt(numericConstants_1.ZERO)
|
|
66
|
+
? types_1.PositionDirection.SHORT
|
|
67
|
+
: types_1.PositionDirection.LONG;
|
|
68
|
+
const [newQuoteAssetReserve, _newBaseAssetReserve] = calculateAmmReservesAfterSwap(newAmm, 'base', amm.netBaseAssetAmount.abs(), getSwapDirection('base', directionToClose));
|
|
69
|
+
newAmm.terminalQuoteAssetReserve = newQuoteAssetReserve;
|
|
70
|
+
newPeg = repeg_1.calculateBudgetedPeg(newAmm, prePegCost, targetPrice);
|
|
71
|
+
prePegCost = repeg_1.calculateRepegCost(newAmm, newPeg);
|
|
72
|
+
}
|
|
73
|
+
return [prePegCost, pKNumer, pKDenom, newPeg];
|
|
74
|
+
}
|
|
75
|
+
exports.calculateNewAmm = calculateNewAmm;
|
|
76
|
+
function calculateUpdatedAMM(amm, oraclePriceData) {
|
|
77
|
+
if (amm.curveUpdateIntensity == 0) {
|
|
78
|
+
return amm;
|
|
79
|
+
}
|
|
80
|
+
const newAmm = Object.assign({}, amm);
|
|
81
|
+
const [prepegCost, pKNumer, pKDenom, newPeg] = calculateNewAmm(amm, oraclePriceData);
|
|
82
|
+
newAmm.baseAssetReserve = newAmm.baseAssetReserve.mul(pKNumer).div(pKDenom);
|
|
83
|
+
newAmm.sqrtK = newAmm.sqrtK.mul(pKNumer).div(pKDenom);
|
|
84
|
+
const invariant = newAmm.sqrtK.mul(newAmm.sqrtK);
|
|
85
|
+
newAmm.quoteAssetReserve = invariant.div(newAmm.baseAssetReserve);
|
|
86
|
+
newAmm.pegMultiplier = newPeg;
|
|
87
|
+
const directionToClose = amm.netBaseAssetAmount.gt(numericConstants_1.ZERO)
|
|
88
|
+
? types_1.PositionDirection.SHORT
|
|
89
|
+
: types_1.PositionDirection.LONG;
|
|
90
|
+
const [newQuoteAssetReserve, _newBaseAssetReserve] = calculateAmmReservesAfterSwap(newAmm, 'base', amm.netBaseAssetAmount.abs(), getSwapDirection('base', directionToClose));
|
|
91
|
+
newAmm.terminalQuoteAssetReserve = newQuoteAssetReserve;
|
|
92
|
+
newAmm.totalFeeMinusDistributions =
|
|
93
|
+
newAmm.totalFeeMinusDistributions.sub(prepegCost);
|
|
94
|
+
return newAmm;
|
|
95
|
+
}
|
|
96
|
+
exports.calculateUpdatedAMM = calculateUpdatedAMM;
|
|
97
|
+
function calculateUpdatedAMMSpreadReserves(amm, direction, oraclePriceData) {
|
|
98
|
+
const newAmm = calculateUpdatedAMM(amm, oraclePriceData);
|
|
99
|
+
const dirReserves = calculateSpreadReserves(newAmm, direction, oraclePriceData);
|
|
100
|
+
const result = {
|
|
101
|
+
baseAssetReserve: dirReserves.baseAssetReserve,
|
|
102
|
+
quoteAssetReserve: dirReserves.quoteAssetReserve,
|
|
103
|
+
sqrtK: newAmm.sqrtK,
|
|
104
|
+
newPeg: newAmm.pegMultiplier,
|
|
105
|
+
};
|
|
106
|
+
return result;
|
|
107
|
+
}
|
|
108
|
+
exports.calculateUpdatedAMMSpreadReserves = calculateUpdatedAMMSpreadReserves;
|
|
109
|
+
function calculateBidAskPrice(amm, oraclePriceData, withUpdate = true) {
|
|
110
|
+
let newAmm;
|
|
111
|
+
if (withUpdate) {
|
|
112
|
+
newAmm = calculateUpdatedAMM(amm, oraclePriceData);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
newAmm = amm;
|
|
116
|
+
}
|
|
117
|
+
const askReserves = calculateSpreadReserves(newAmm, types_1.PositionDirection.LONG, oraclePriceData);
|
|
118
|
+
const bidReserves = calculateSpreadReserves(newAmm, types_1.PositionDirection.SHORT, oraclePriceData);
|
|
119
|
+
const askPrice = calculatePrice(askReserves.baseAssetReserve, askReserves.quoteAssetReserve, newAmm.pegMultiplier);
|
|
120
|
+
const bidPrice = calculatePrice(bidReserves.baseAssetReserve, bidReserves.quoteAssetReserve, newAmm.pegMultiplier);
|
|
121
|
+
return [bidPrice, askPrice];
|
|
122
|
+
}
|
|
123
|
+
exports.calculateBidAskPrice = calculateBidAskPrice;
|
|
124
|
+
/**
|
|
125
|
+
* Calculates a price given an arbitrary base and quote amount (they must have the same precision)
|
|
126
|
+
*
|
|
127
|
+
* @param baseAssetReserves
|
|
128
|
+
* @param quoteAssetReserves
|
|
129
|
+
* @param pegMultiplier
|
|
130
|
+
* @returns price : Precision MARK_PRICE_PRECISION
|
|
131
|
+
*/
|
|
132
|
+
function calculatePrice(baseAssetReserves, quoteAssetReserves, pegMultiplier) {
|
|
133
|
+
if (baseAssetReserves.abs().lte(numericConstants_1.ZERO)) {
|
|
134
|
+
return new anchor_1.BN(0);
|
|
135
|
+
}
|
|
136
|
+
return quoteAssetReserves
|
|
137
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
138
|
+
.mul(pegMultiplier)
|
|
139
|
+
.div(numericConstants_1.PEG_PRECISION)
|
|
140
|
+
.div(baseAssetReserves);
|
|
141
|
+
}
|
|
142
|
+
exports.calculatePrice = calculatePrice;
|
|
143
|
+
/**
|
|
144
|
+
* Calculates what the amm reserves would be after swapping a quote or base asset amount.
|
|
145
|
+
*
|
|
146
|
+
* @param amm
|
|
147
|
+
* @param inputAssetType
|
|
148
|
+
* @param swapAmount
|
|
149
|
+
* @param swapDirection
|
|
150
|
+
* @returns quoteAssetReserve and baseAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
|
|
151
|
+
*/
|
|
152
|
+
function calculateAmmReservesAfterSwap(amm, inputAssetType, swapAmount, swapDirection) {
|
|
153
|
+
assert_1.assert(swapAmount.gte(numericConstants_1.ZERO), 'swapAmount must be greater than 0');
|
|
154
|
+
let newQuoteAssetReserve;
|
|
155
|
+
let newBaseAssetReserve;
|
|
156
|
+
if (inputAssetType === 'quote') {
|
|
157
|
+
swapAmount = swapAmount
|
|
158
|
+
.mul(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO)
|
|
159
|
+
.div(amm.pegMultiplier);
|
|
160
|
+
[newQuoteAssetReserve, newBaseAssetReserve] = calculateSwapOutput(amm.quoteAssetReserve, swapAmount, swapDirection, amm.sqrtK.mul(amm.sqrtK));
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
[newBaseAssetReserve, newQuoteAssetReserve] = calculateSwapOutput(amm.baseAssetReserve, swapAmount, swapDirection, amm.sqrtK.mul(amm.sqrtK));
|
|
164
|
+
}
|
|
165
|
+
return [newQuoteAssetReserve, newBaseAssetReserve];
|
|
166
|
+
}
|
|
167
|
+
exports.calculateAmmReservesAfterSwap = calculateAmmReservesAfterSwap;
|
|
168
|
+
function calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions) {
|
|
169
|
+
// inventory skew
|
|
170
|
+
const netBaseAssetValue = quoteAssetReserve
|
|
171
|
+
.sub(terminalQuoteAssetReserve)
|
|
172
|
+
.mul(pegMultiplier)
|
|
173
|
+
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
174
|
+
const localBaseAssetValue = netBaseAssetAmount
|
|
175
|
+
.mul(markPrice)
|
|
176
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
|
|
177
|
+
const effectiveLeverage = localBaseAssetValue.sub(netBaseAssetValue).toNumber() /
|
|
178
|
+
(Math.max(0, totalFeeMinusDistributions.toNumber()) + 1) +
|
|
179
|
+
1 / numericConstants_1.QUOTE_PRECISION.toNumber();
|
|
180
|
+
return effectiveLeverage;
|
|
181
|
+
}
|
|
182
|
+
exports.calculateEffectiveLeverage = calculateEffectiveLeverage;
|
|
183
|
+
function calculateMaxSpread(marginRatioInitial) {
|
|
184
|
+
const maxTargetSpread = new anchor_1.BN(marginRatioInitial)
|
|
185
|
+
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION.div(numericConstants_1.MARGIN_PRECISION))
|
|
186
|
+
.toNumber();
|
|
187
|
+
return maxTargetSpread;
|
|
188
|
+
}
|
|
189
|
+
exports.calculateMaxSpread = calculateMaxSpread;
|
|
190
|
+
function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPct, maxSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions) {
|
|
191
|
+
let longSpread = baseSpread / 2;
|
|
192
|
+
let shortSpread = baseSpread / 2;
|
|
193
|
+
if (lastOracleMarkSpreadPct.gt(numericConstants_1.ZERO)) {
|
|
194
|
+
shortSpread = Math.max(shortSpread, lastOracleMarkSpreadPct.abs().toNumber() + lastOracleConfPct.toNumber());
|
|
195
|
+
}
|
|
196
|
+
else if (lastOracleMarkSpreadPct.lt(numericConstants_1.ZERO)) {
|
|
197
|
+
longSpread = Math.max(longSpread, lastOracleMarkSpreadPct.abs().toNumber() + lastOracleConfPct.toNumber());
|
|
198
|
+
}
|
|
199
|
+
const maxTargetSpread = maxSpread;
|
|
200
|
+
const MAX_INVENTORY_SKEW = 5;
|
|
201
|
+
const effectiveLeverage = calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions);
|
|
202
|
+
if (totalFeeMinusDistributions.gt(numericConstants_1.ZERO)) {
|
|
203
|
+
const spreadScale = Math.min(MAX_INVENTORY_SKEW, 1 + effectiveLeverage);
|
|
204
|
+
if (netBaseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
205
|
+
longSpread *= spreadScale;
|
|
206
|
+
}
|
|
207
|
+
else {
|
|
208
|
+
shortSpread *= spreadScale;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
longSpread *= MAX_INVENTORY_SKEW;
|
|
213
|
+
shortSpread *= MAX_INVENTORY_SKEW;
|
|
214
|
+
}
|
|
215
|
+
const totalSpread = longSpread + shortSpread;
|
|
216
|
+
if (totalSpread > maxTargetSpread) {
|
|
217
|
+
if (longSpread > shortSpread) {
|
|
218
|
+
longSpread = Math.min(longSpread, maxTargetSpread);
|
|
219
|
+
shortSpread = maxTargetSpread - longSpread;
|
|
220
|
+
}
|
|
221
|
+
else {
|
|
222
|
+
shortSpread = Math.min(shortSpread, maxTargetSpread);
|
|
223
|
+
longSpread = maxTargetSpread - shortSpread;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
return [longSpread, shortSpread];
|
|
227
|
+
}
|
|
228
|
+
exports.calculateSpreadBN = calculateSpreadBN;
|
|
229
|
+
function calculateSpread(amm, direction, oraclePriceData) {
|
|
230
|
+
if (amm.baseSpread == 0 || amm.curveUpdateIntensity == 0) {
|
|
231
|
+
return amm.baseSpread / 2;
|
|
232
|
+
}
|
|
233
|
+
const markPrice = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
234
|
+
const targetPrice = (oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.price) || markPrice;
|
|
235
|
+
const confInterval = oraclePriceData.confidence || numericConstants_1.ZERO;
|
|
236
|
+
const targetMarkSpreadPct = markPrice
|
|
237
|
+
.sub(targetPrice)
|
|
238
|
+
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
239
|
+
.div(markPrice);
|
|
240
|
+
const confIntervalPct = confInterval
|
|
241
|
+
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
242
|
+
.div(markPrice);
|
|
243
|
+
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.netBaseAssetAmount, markPrice, amm.totalFeeMinusDistributions);
|
|
244
|
+
let spread;
|
|
245
|
+
if (types_1.isVariant(direction, 'long')) {
|
|
246
|
+
spread = longSpread;
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
spread = shortSpread;
|
|
250
|
+
}
|
|
251
|
+
return spread;
|
|
252
|
+
}
|
|
253
|
+
exports.calculateSpread = calculateSpread;
|
|
254
|
+
function calculateSpreadReserves(amm, direction, oraclePriceData) {
|
|
255
|
+
const spread = calculateSpread(amm, direction, oraclePriceData);
|
|
256
|
+
if (spread === 0) {
|
|
257
|
+
return {
|
|
258
|
+
baseAssetReserve: amm.baseAssetReserve,
|
|
259
|
+
quoteAssetReserve: amm.quoteAssetReserve,
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
const quoteAsserReserveDelta = amm.quoteAssetReserve.div(numericConstants_1.BID_ASK_SPREAD_PRECISION.div(new anchor_1.BN(spread / 2)));
|
|
263
|
+
let quoteAssetReserve;
|
|
264
|
+
if (types_1.isVariant(direction, 'long')) {
|
|
265
|
+
quoteAssetReserve = amm.quoteAssetReserve.add(quoteAsserReserveDelta);
|
|
266
|
+
}
|
|
267
|
+
else {
|
|
268
|
+
quoteAssetReserve = amm.quoteAssetReserve.sub(quoteAsserReserveDelta);
|
|
269
|
+
}
|
|
270
|
+
const baseAssetReserve = amm.sqrtK.mul(amm.sqrtK).div(quoteAssetReserve);
|
|
271
|
+
return {
|
|
272
|
+
baseAssetReserve,
|
|
273
|
+
quoteAssetReserve,
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
exports.calculateSpreadReserves = calculateSpreadReserves;
|
|
277
|
+
/**
|
|
278
|
+
* Helper function calculating constant product curve output. Agnostic to whether input asset is quote or base
|
|
279
|
+
*
|
|
280
|
+
* @param inputAssetReserve
|
|
281
|
+
* @param swapAmount
|
|
282
|
+
* @param swapDirection
|
|
283
|
+
* @param invariant
|
|
284
|
+
* @returns newInputAssetReserve and newOutputAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
|
|
285
|
+
*/
|
|
286
|
+
function calculateSwapOutput(inputAssetReserve, swapAmount, swapDirection, invariant) {
|
|
287
|
+
let newInputAssetReserve;
|
|
288
|
+
if (swapDirection === types_1.SwapDirection.ADD) {
|
|
289
|
+
newInputAssetReserve = inputAssetReserve.add(swapAmount);
|
|
290
|
+
}
|
|
291
|
+
else {
|
|
292
|
+
newInputAssetReserve = inputAssetReserve.sub(swapAmount);
|
|
293
|
+
}
|
|
294
|
+
const newOutputAssetReserve = invariant.div(newInputAssetReserve);
|
|
295
|
+
return [newInputAssetReserve, newOutputAssetReserve];
|
|
296
|
+
}
|
|
297
|
+
exports.calculateSwapOutput = calculateSwapOutput;
|
|
298
|
+
/**
|
|
299
|
+
* Translate long/shorting quote/base asset into amm operation
|
|
300
|
+
*
|
|
301
|
+
* @param inputAssetType
|
|
302
|
+
* @param positionDirection
|
|
303
|
+
*/
|
|
304
|
+
function getSwapDirection(inputAssetType, positionDirection) {
|
|
305
|
+
if (types_1.isVariant(positionDirection, 'long') && inputAssetType === 'base') {
|
|
306
|
+
return types_1.SwapDirection.REMOVE;
|
|
307
|
+
}
|
|
308
|
+
if (types_1.isVariant(positionDirection, 'short') && inputAssetType === 'quote') {
|
|
309
|
+
return types_1.SwapDirection.REMOVE;
|
|
310
|
+
}
|
|
311
|
+
return types_1.SwapDirection.ADD;
|
|
312
|
+
}
|
|
313
|
+
exports.getSwapDirection = getSwapDirection;
|
|
314
|
+
/**
|
|
315
|
+
* Helper function calculating terminal price of amm
|
|
316
|
+
*
|
|
317
|
+
* @param market
|
|
318
|
+
* @returns cost : Precision MARK_PRICE_PRECISION
|
|
319
|
+
*/
|
|
320
|
+
function calculateTerminalPrice(market) {
|
|
321
|
+
const directionToClose = market.amm.netBaseAssetAmount.gt(numericConstants_1.ZERO)
|
|
322
|
+
? types_1.PositionDirection.SHORT
|
|
323
|
+
: types_1.PositionDirection.LONG;
|
|
324
|
+
const [newQuoteAssetReserve, newBaseAssetReserve] = calculateAmmReservesAfterSwap(market.amm, 'base', market.amm.netBaseAssetAmount.abs(), getSwapDirection('base', directionToClose));
|
|
325
|
+
const terminalPrice = newQuoteAssetReserve
|
|
326
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
327
|
+
.mul(market.amm.pegMultiplier)
|
|
328
|
+
.div(numericConstants_1.PEG_PRECISION)
|
|
329
|
+
.div(newBaseAssetReserve);
|
|
330
|
+
return terminalPrice;
|
|
331
|
+
}
|
|
332
|
+
exports.calculateTerminalPrice = calculateTerminalPrice;
|
|
333
|
+
function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, oraclePriceData) {
|
|
334
|
+
const invariant = amm.sqrtK.mul(amm.sqrtK);
|
|
335
|
+
const newBaseAssetReserveSquared = invariant
|
|
336
|
+
.mul(numericConstants_1.MARK_PRICE_PRECISION)
|
|
337
|
+
.mul(amm.pegMultiplier)
|
|
338
|
+
.div(limit_price)
|
|
339
|
+
.div(numericConstants_1.PEG_PRECISION);
|
|
340
|
+
const newBaseAssetReserve = __1.squareRootBN(newBaseAssetReserveSquared);
|
|
341
|
+
const baseAssetReserveBefore = calculateSpreadReserves(amm, direction, oraclePriceData).baseAssetReserve;
|
|
342
|
+
if (newBaseAssetReserve.gt(baseAssetReserveBefore)) {
|
|
343
|
+
return [
|
|
344
|
+
newBaseAssetReserve.sub(baseAssetReserveBefore),
|
|
345
|
+
types_1.PositionDirection.SHORT,
|
|
346
|
+
];
|
|
347
|
+
}
|
|
348
|
+
else if (newBaseAssetReserve.lt(baseAssetReserveBefore)) {
|
|
349
|
+
return [
|
|
350
|
+
baseAssetReserveBefore.sub(newBaseAssetReserve),
|
|
351
|
+
types_1.PositionDirection.LONG,
|
|
352
|
+
];
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
console.log('tradeSize Too Small');
|
|
356
|
+
return [new anchor_1.BN(0), types_1.PositionDirection.LONG];
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
exports.calculateMaxBaseAssetAmountToTrade = calculateMaxBaseAssetAmountToTrade;
|
|
360
|
+
function calculateQuoteAssetAmountSwapped(quoteAssetReserves, pegMultiplier, swapDirection) {
|
|
361
|
+
let quoteAssetAmount = quoteAssetReserves
|
|
362
|
+
.mul(pegMultiplier)
|
|
363
|
+
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
364
|
+
if (types_1.isVariant(swapDirection, 'remove')) {
|
|
365
|
+
quoteAssetAmount = quoteAssetAmount.add(numericConstants_1.ONE);
|
|
366
|
+
}
|
|
367
|
+
return quoteAssetAmount;
|
|
368
|
+
}
|
|
369
|
+
exports.calculateQuoteAssetAmountSwapped = calculateQuoteAssetAmountSwapped;
|