@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.31
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/README.md +13 -13
- package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.js +0 -1
- package/lib/accounts/fetch.d.ts +2 -1
- package/lib/accounts/fetch.js +9 -1
- package/lib/accounts/pollingClearingHouseAccountSubscriber.d.ts +16 -16
- package/lib/accounts/pollingClearingHouseAccountSubscriber.js +31 -28
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +22 -11
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +17 -17
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.js +37 -35
- package/lib/accounts/webSocketUserStatsAccountSubsriber.d.ts +20 -0
- package/lib/accounts/webSocketUserStatsAccountSubsriber.js +47 -0
- package/lib/addresses/marketAddresses.d.ts +1 -3
- package/lib/addresses/pda.d.ts +10 -4
- package/lib/addresses/pda.js +51 -14
- package/lib/admin.d.ts +42 -26
- package/lib/admin.js +228 -81
- package/lib/clearingHouse.d.ts +130 -49
- package/lib/clearingHouse.js +1256 -367
- package/lib/clearingHouseConfig.d.ts +4 -4
- package/lib/clearingHouseUser.d.ts +51 -39
- package/lib/clearingHouseUser.js +408 -191
- package/lib/clearingHouseUserStats.d.ts +18 -0
- package/lib/clearingHouseUserStats.js +49 -0
- package/lib/clearingHouseUserStatsConfig.d.ts +14 -0
- package/lib/clearingHouseUserStatsConfig.js +2 -0
- package/lib/config.d.ts +7 -9
- package/lib/config.js +21 -21
- package/lib/constants/numericConstants.d.ts +25 -12
- package/lib/constants/numericConstants.js +35 -21
- package/lib/constants/perpMarkets.d.ts +18 -0
- package/lib/constants/{markets.js → perpMarkets.js} +7 -7
- package/lib/constants/spotMarkets.d.ts +19 -0
- package/lib/constants/spotMarkets.js +53 -0
- package/lib/dlob/DLOB.d.ts +74 -0
- package/lib/dlob/DLOB.js +595 -0
- package/lib/dlob/DLOBNode.d.ts +54 -0
- package/lib/dlob/DLOBNode.js +85 -0
- package/lib/dlob/NodeList.d.ts +26 -0
- package/lib/dlob/NodeList.js +139 -0
- package/lib/events/eventList.js +3 -0
- package/lib/events/eventSubscriber.d.ts +4 -2
- package/lib/events/eventSubscriber.js +16 -9
- package/lib/events/fetchLogs.d.ts +10 -1
- package/lib/events/fetchLogs.js +27 -7
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +6 -2
- package/lib/events/sort.js +8 -11
- package/lib/events/types.d.ts +8 -2
- package/lib/events/types.js +6 -0
- package/lib/examples/makeTradeExample.js +20 -8
- package/lib/factory/bigNum.d.ts +2 -0
- package/lib/factory/bigNum.js +48 -10
- package/lib/idl/clearing_house.json +4889 -1529
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +21 -6
- package/lib/index.js +25 -6
- package/lib/math/amm.d.ts +9 -6
- package/lib/math/amm.js +91 -66
- package/lib/math/auction.js +4 -1
- package/lib/math/conversion.js +1 -1
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +11 -10
- package/lib/math/insurance.d.ts +4 -0
- package/lib/math/insurance.js +27 -0
- package/lib/math/margin.d.ts +11 -0
- package/lib/math/margin.js +77 -0
- package/lib/math/market.d.ts +14 -9
- package/lib/math/market.js +72 -10
- package/lib/math/oracles.d.ts +4 -0
- package/lib/math/oracles.js +36 -8
- package/lib/math/orders.d.ts +6 -2
- package/lib/math/orders.js +78 -3
- package/lib/math/position.d.ts +21 -13
- package/lib/math/position.js +76 -36
- package/lib/math/repeg.js +14 -5
- package/lib/math/spotBalance.d.ts +22 -0
- package/lib/math/spotBalance.js +192 -0
- package/lib/math/spotMarket.d.ts +4 -0
- package/lib/math/spotMarket.js +8 -0
- package/lib/math/spotPosition.d.ts +6 -0
- package/lib/math/spotPosition.js +23 -0
- package/lib/math/trade.d.ts +10 -10
- package/lib/math/trade.js +32 -39
- package/lib/oracles/pythClient.js +1 -1
- package/lib/oracles/quoteAssetOracleClient.js +1 -1
- package/lib/oracles/switchboardClient.js +1 -1
- package/lib/orderParams.d.ts +14 -5
- package/lib/orderParams.js +12 -92
- package/lib/serum/serumSubscriber.d.ts +23 -0
- package/lib/serum/serumSubscriber.js +41 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/serum/types.js +2 -0
- package/lib/slot/SlotSubscriber.d.ts +7 -0
- package/lib/slot/SlotSubscriber.js +3 -0
- package/lib/{mockUSDCFaucet.d.ts → tokenFaucet.d.ts} +8 -5
- package/lib/{mockUSDCFaucet.js → tokenFaucet.js} +63 -51
- package/lib/tx/retryTxSender.d.ts +1 -1
- package/lib/tx/retryTxSender.js +13 -4
- package/lib/tx/types.d.ts +1 -1
- package/lib/tx/utils.js +1 -1
- package/lib/types.d.ts +566 -138
- package/lib/types.js +129 -5
- package/lib/userMap/userMap.d.ts +25 -0
- package/lib/userMap/userMap.js +73 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/lib/util/computeUnits.js +1 -1
- package/lib/util/getTokenAddress.d.ts +2 -0
- package/lib/util/getTokenAddress.js +9 -0
- package/package.json +12 -6
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +0 -1
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/pollingClearingHouseAccountSubscriber.ts +46 -42
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +31 -11
- package/src/accounts/webSocketClearingHouseAccountSubscriber.ts +64 -59
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.ts +1 -2
- package/src/addresses/pda.ts +88 -14
- package/src/admin.ts +452 -147
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +2066 -467
- package/src/clearingHouseConfig.ts +4 -3
- package/src/clearingHouseUser.ts +747 -294
- package/src/clearingHouseUserStats.ts +75 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +30 -31
- package/src/constants/numericConstants.ts +48 -25
- package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
- package/src/constants/spotMarkets.ts +73 -0
- package/src/dlob/DLOB.ts +953 -0
- package/src/dlob/DLOBNode.ts +167 -0
- package/src/dlob/NodeList.ts +189 -0
- package/src/events/eventList.js +77 -0
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.ts +20 -12
- package/src/events/fetchLogs.ts +35 -8
- package/src/events/pollingLogProvider.ts +10 -2
- package/src/events/sort.ts +11 -15
- package/src/events/types.ts +19 -1
- package/src/examples/makeTradeExample.js +157 -0
- package/src/examples/makeTradeExample.ts +32 -14
- package/src/factory/bigNum.ts +65 -13
- package/src/idl/clearing_house.json +4889 -1529
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +21 -6
- package/src/math/amm.ts +171 -88
- package/src/math/auction.ts +5 -1
- package/src/math/conversion.ts +2 -2
- package/src/math/funding.ts +20 -18
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +127 -0
- package/src/math/market.ts +145 -14
- package/src/math/oracles.ts +63 -9
- package/src/math/orders.ts +138 -4
- package/src/math/position.ts +119 -58
- package/src/math/repeg.ts +16 -6
- package/src/math/spotBalance.ts +319 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/trade.ts +53 -59
- package/src/oracles/pythClient.ts +2 -2
- package/src/oracles/quoteAssetOracleClient.ts +2 -2
- package/src/oracles/switchboardClient.ts +2 -2
- package/src/orderParams.ts +24 -137
- package/src/serum/serumSubscriber.ts +80 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/token/index.js +38 -0
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
- package/src/tx/retryTxSender.ts +16 -5
- package/src/tx/types.js +2 -0
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.js +17 -0
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +533 -140
- package/src/userMap/userMap.ts +100 -0
- package/src/userMap/userStatsMap.ts +110 -0
- package/src/util/computeUnits.js +21 -11
- package/src/util/computeUnits.ts +1 -1
- package/src/util/getTokenAddress.js +9 -0
- package/src/util/getTokenAddress.ts +18 -0
- package/src/util/promiseTimeout.js +14 -0
- package/src/util/tps.js +27 -0
- package/tests/bn/test.ts +24 -10
- package/tests/dlob/helpers.ts +397 -0
- package/tests/dlob/test.ts +3688 -0
- package/lib/constants/banks.d.ts +0 -16
- package/lib/constants/banks.js +0 -34
- package/lib/constants/markets.d.ts +0 -19
- package/lib/math/bankBalance.d.ts +0 -9
- package/lib/math/bankBalance.js +0 -75
- package/lib/math/state.d.ts +0 -8
- package/lib/math/state.js +0 -15
- package/lib/orders.d.ts +0 -8
- package/lib/orders.js +0 -134
- package/src/constants/banks.ts +0 -43
- package/src/math/bankBalance.ts +0 -112
- package/src/math/state.ts +0 -14
- package/src/math/utils.js +0 -27
- package/src/math/utils.js.map +0 -1
- package/src/orders.ts +0 -244
- package/src/util/computeUnits.js.map +0 -1
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.
|
|
3
|
-
"name": "
|
|
2
|
+
"version": "0.1.0",
|
|
3
|
+
"name": "token_faucet",
|
|
4
4
|
"instructions": [
|
|
5
5
|
{
|
|
6
6
|
"name": "initialize",
|
|
7
7
|
"accounts": [
|
|
8
8
|
{
|
|
9
|
-
"name": "
|
|
9
|
+
"name": "faucetConfig",
|
|
10
10
|
"isMut": true,
|
|
11
11
|
"isSigner": false
|
|
12
12
|
},
|
|
13
13
|
{
|
|
14
14
|
"name": "admin",
|
|
15
|
-
"isMut":
|
|
15
|
+
"isMut": true,
|
|
16
16
|
"isSigner": true
|
|
17
17
|
},
|
|
18
18
|
{
|
|
19
19
|
"name": "mintAccount",
|
|
20
|
-
"isMut":
|
|
20
|
+
"isMut": true,
|
|
21
21
|
"isSigner": false
|
|
22
22
|
},
|
|
23
23
|
{
|
|
@@ -29,20 +29,20 @@
|
|
|
29
29
|
"name": "systemProgram",
|
|
30
30
|
"isMut": false,
|
|
31
31
|
"isSigner": false
|
|
32
|
-
}
|
|
33
|
-
],
|
|
34
|
-
"args": [
|
|
32
|
+
},
|
|
35
33
|
{
|
|
36
|
-
"name": "
|
|
37
|
-
"
|
|
34
|
+
"name": "tokenProgram",
|
|
35
|
+
"isMut": false,
|
|
36
|
+
"isSigner": false
|
|
38
37
|
}
|
|
39
|
-
]
|
|
38
|
+
],
|
|
39
|
+
"args": []
|
|
40
40
|
},
|
|
41
41
|
{
|
|
42
42
|
"name": "mintToUser",
|
|
43
43
|
"accounts": [
|
|
44
44
|
{
|
|
45
|
-
"name": "
|
|
45
|
+
"name": "faucetConfig",
|
|
46
46
|
"isMut": false,
|
|
47
47
|
"isSigner": false
|
|
48
48
|
},
|
|
@@ -73,11 +73,42 @@
|
|
|
73
73
|
"type": "u64"
|
|
74
74
|
}
|
|
75
75
|
]
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
"name": "transferMintAuthority",
|
|
79
|
+
"accounts": [
|
|
80
|
+
{
|
|
81
|
+
"name": "faucetConfig",
|
|
82
|
+
"isMut": false,
|
|
83
|
+
"isSigner": false
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
"name": "admin",
|
|
87
|
+
"isMut": true,
|
|
88
|
+
"isSigner": true
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
"name": "mintAccount",
|
|
92
|
+
"isMut": true,
|
|
93
|
+
"isSigner": false
|
|
94
|
+
},
|
|
95
|
+
{
|
|
96
|
+
"name": "mintAuthority",
|
|
97
|
+
"isMut": false,
|
|
98
|
+
"isSigner": false
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
"name": "tokenProgram",
|
|
102
|
+
"isMut": false,
|
|
103
|
+
"isSigner": false
|
|
104
|
+
}
|
|
105
|
+
],
|
|
106
|
+
"args": []
|
|
76
107
|
}
|
|
77
108
|
],
|
|
78
109
|
"accounts": [
|
|
79
110
|
{
|
|
80
|
-
"name": "
|
|
111
|
+
"name": "FaucetConfig",
|
|
81
112
|
"type": {
|
|
82
113
|
"kind": "struct",
|
|
83
114
|
"fields": [
|
|
@@ -103,17 +134,9 @@
|
|
|
103
134
|
],
|
|
104
135
|
"errors": [
|
|
105
136
|
{
|
|
106
|
-
"code":
|
|
137
|
+
"code": 6000,
|
|
107
138
|
"name": "InvalidMintAccountAuthority",
|
|
108
139
|
"msg": "Program not mint authority"
|
|
109
|
-
},
|
|
110
|
-
{
|
|
111
|
-
"code": 301,
|
|
112
|
-
"name": "Unauthorized",
|
|
113
|
-
"msg": "Signer must be MockUSDCFaucet admin"
|
|
114
140
|
}
|
|
115
|
-
]
|
|
116
|
-
"metadata": {
|
|
117
|
-
"address": "2z2DLVD3tBWc86pbvvy5qN31v1NXprM6zA5MDr2FMx64"
|
|
118
|
-
}
|
|
141
|
+
]
|
|
119
142
|
}
|
package/lib/index.d.ts
CHANGED
|
@@ -1,29 +1,37 @@
|
|
|
1
1
|
import { BN } from '@project-serum/anchor';
|
|
2
2
|
import { PublicKey } from '@solana/web3.js';
|
|
3
|
-
|
|
3
|
+
import pyth from '@pythnetwork/client';
|
|
4
|
+
export * from './tokenFaucet';
|
|
4
5
|
export * from './oracles/types';
|
|
5
6
|
export * from './oracles/pythClient';
|
|
6
7
|
export * from './oracles/switchboardClient';
|
|
7
8
|
export * from './types';
|
|
8
|
-
export * from './constants/
|
|
9
|
+
export * from './constants/perpMarkets';
|
|
9
10
|
export * from './accounts/fetch';
|
|
10
11
|
export * from './accounts/webSocketClearingHouseAccountSubscriber';
|
|
11
12
|
export * from './accounts/bulkAccountLoader';
|
|
12
13
|
export * from './accounts/bulkUserSubscription';
|
|
14
|
+
export * from './accounts/bulkUserStatsSubscription';
|
|
13
15
|
export * from './accounts/pollingClearingHouseAccountSubscriber';
|
|
14
16
|
export * from './accounts/pollingOracleSubscriber';
|
|
15
17
|
export * from './accounts/pollingTokenAccountSubscriber';
|
|
18
|
+
export * from './accounts/pollingUserAccountSubscriber';
|
|
19
|
+
export * from './accounts/pollingUserStatsAccountSubscriber';
|
|
16
20
|
export * from './accounts/types';
|
|
17
21
|
export * from './addresses/pda';
|
|
18
22
|
export * from './admin';
|
|
19
23
|
export * from './clearingHouseUser';
|
|
20
24
|
export * from './clearingHouseUserConfig';
|
|
25
|
+
export * from './clearingHouseUserStats';
|
|
26
|
+
export * from './clearingHouseUserStatsConfig';
|
|
21
27
|
export * from './clearingHouse';
|
|
22
28
|
export * from './factory/oracleClient';
|
|
23
29
|
export * from './factory/bigNum';
|
|
24
30
|
export * from './events/types';
|
|
25
31
|
export * from './events/eventSubscriber';
|
|
32
|
+
export * from './events/fetchLogs';
|
|
26
33
|
export * from './math/auction';
|
|
34
|
+
export * from './math/spotMarket';
|
|
27
35
|
export * from './math/conversion';
|
|
28
36
|
export * from './math/funding';
|
|
29
37
|
export * from './math/market';
|
|
@@ -33,7 +41,8 @@ export * from './math/amm';
|
|
|
33
41
|
export * from './math/trade';
|
|
34
42
|
export * from './math/orders';
|
|
35
43
|
export * from './math/repeg';
|
|
36
|
-
export * from './
|
|
44
|
+
export * from './math/margin';
|
|
45
|
+
export * from './math/insurance';
|
|
37
46
|
export * from './orderParams';
|
|
38
47
|
export * from './slot/SlotSubscriber';
|
|
39
48
|
export * from './wallet';
|
|
@@ -41,10 +50,16 @@ export * from './types';
|
|
|
41
50
|
export * from './math/utils';
|
|
42
51
|
export * from './config';
|
|
43
52
|
export * from './constants/numericConstants';
|
|
53
|
+
export * from './serum/serumSubscriber';
|
|
44
54
|
export * from './tx/retryTxSender';
|
|
45
55
|
export * from './util/computeUnits';
|
|
46
56
|
export * from './util/tps';
|
|
47
|
-
export * from './math/
|
|
48
|
-
export * from './constants/
|
|
57
|
+
export * from './math/spotBalance';
|
|
58
|
+
export * from './constants/spotMarkets';
|
|
49
59
|
export * from './clearingHouseConfig';
|
|
50
|
-
export
|
|
60
|
+
export * from './dlob/DLOB';
|
|
61
|
+
export * from './dlob/DLOBNode';
|
|
62
|
+
export * from './dlob/NodeList';
|
|
63
|
+
export * from './userMap/userMap';
|
|
64
|
+
export * from './userMap/userStatsMap';
|
|
65
|
+
export { BN, PublicKey, pyth };
|
package/lib/index.js
CHANGED
|
@@ -13,36 +13,48 @@ var __createBinding = (this && this.__createBinding) || (Object.create ? (functi
|
|
|
13
13
|
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
17
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
18
|
+
};
|
|
16
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
exports.PublicKey = exports.BN = void 0;
|
|
20
|
+
exports.pyth = exports.PublicKey = exports.BN = void 0;
|
|
18
21
|
const anchor_1 = require("@project-serum/anchor");
|
|
19
22
|
Object.defineProperty(exports, "BN", { enumerable: true, get: function () { return anchor_1.BN; } });
|
|
20
23
|
const web3_js_1 = require("@solana/web3.js");
|
|
21
24
|
Object.defineProperty(exports, "PublicKey", { enumerable: true, get: function () { return web3_js_1.PublicKey; } });
|
|
22
|
-
|
|
25
|
+
const client_1 = __importDefault(require("@pythnetwork/client"));
|
|
26
|
+
exports.pyth = client_1.default;
|
|
27
|
+
__exportStar(require("./tokenFaucet"), exports);
|
|
23
28
|
__exportStar(require("./oracles/types"), exports);
|
|
24
29
|
__exportStar(require("./oracles/pythClient"), exports);
|
|
25
30
|
__exportStar(require("./oracles/switchboardClient"), exports);
|
|
26
31
|
__exportStar(require("./types"), exports);
|
|
27
|
-
__exportStar(require("./constants/
|
|
32
|
+
__exportStar(require("./constants/perpMarkets"), exports);
|
|
28
33
|
__exportStar(require("./accounts/fetch"), exports);
|
|
29
34
|
__exportStar(require("./accounts/webSocketClearingHouseAccountSubscriber"), exports);
|
|
30
35
|
__exportStar(require("./accounts/bulkAccountLoader"), exports);
|
|
31
36
|
__exportStar(require("./accounts/bulkUserSubscription"), exports);
|
|
37
|
+
__exportStar(require("./accounts/bulkUserStatsSubscription"), exports);
|
|
32
38
|
__exportStar(require("./accounts/pollingClearingHouseAccountSubscriber"), exports);
|
|
33
39
|
__exportStar(require("./accounts/pollingOracleSubscriber"), exports);
|
|
34
40
|
__exportStar(require("./accounts/pollingTokenAccountSubscriber"), exports);
|
|
41
|
+
__exportStar(require("./accounts/pollingUserAccountSubscriber"), exports);
|
|
42
|
+
__exportStar(require("./accounts/pollingUserStatsAccountSubscriber"), exports);
|
|
35
43
|
__exportStar(require("./accounts/types"), exports);
|
|
36
44
|
__exportStar(require("./addresses/pda"), exports);
|
|
37
45
|
__exportStar(require("./admin"), exports);
|
|
38
46
|
__exportStar(require("./clearingHouseUser"), exports);
|
|
39
47
|
__exportStar(require("./clearingHouseUserConfig"), exports);
|
|
48
|
+
__exportStar(require("./clearingHouseUserStats"), exports);
|
|
49
|
+
__exportStar(require("./clearingHouseUserStatsConfig"), exports);
|
|
40
50
|
__exportStar(require("./clearingHouse"), exports);
|
|
41
51
|
__exportStar(require("./factory/oracleClient"), exports);
|
|
42
52
|
__exportStar(require("./factory/bigNum"), exports);
|
|
43
53
|
__exportStar(require("./events/types"), exports);
|
|
44
54
|
__exportStar(require("./events/eventSubscriber"), exports);
|
|
55
|
+
__exportStar(require("./events/fetchLogs"), exports);
|
|
45
56
|
__exportStar(require("./math/auction"), exports);
|
|
57
|
+
__exportStar(require("./math/spotMarket"), exports);
|
|
46
58
|
__exportStar(require("./math/conversion"), exports);
|
|
47
59
|
__exportStar(require("./math/funding"), exports);
|
|
48
60
|
__exportStar(require("./math/market"), exports);
|
|
@@ -52,7 +64,8 @@ __exportStar(require("./math/amm"), exports);
|
|
|
52
64
|
__exportStar(require("./math/trade"), exports);
|
|
53
65
|
__exportStar(require("./math/orders"), exports);
|
|
54
66
|
__exportStar(require("./math/repeg"), exports);
|
|
55
|
-
__exportStar(require("./
|
|
67
|
+
__exportStar(require("./math/margin"), exports);
|
|
68
|
+
__exportStar(require("./math/insurance"), exports);
|
|
56
69
|
__exportStar(require("./orderParams"), exports);
|
|
57
70
|
__exportStar(require("./slot/SlotSubscriber"), exports);
|
|
58
71
|
__exportStar(require("./wallet"), exports);
|
|
@@ -60,9 +73,15 @@ __exportStar(require("./types"), exports);
|
|
|
60
73
|
__exportStar(require("./math/utils"), exports);
|
|
61
74
|
__exportStar(require("./config"), exports);
|
|
62
75
|
__exportStar(require("./constants/numericConstants"), exports);
|
|
76
|
+
__exportStar(require("./serum/serumSubscriber"), exports);
|
|
63
77
|
__exportStar(require("./tx/retryTxSender"), exports);
|
|
64
78
|
__exportStar(require("./util/computeUnits"), exports);
|
|
65
79
|
__exportStar(require("./util/tps"), exports);
|
|
66
|
-
__exportStar(require("./math/
|
|
67
|
-
__exportStar(require("./constants/
|
|
80
|
+
__exportStar(require("./math/spotBalance"), exports);
|
|
81
|
+
__exportStar(require("./constants/spotMarkets"), exports);
|
|
68
82
|
__exportStar(require("./clearingHouseConfig"), exports);
|
|
83
|
+
__exportStar(require("./dlob/DLOB"), exports);
|
|
84
|
+
__exportStar(require("./dlob/DLOBNode"), exports);
|
|
85
|
+
__exportStar(require("./dlob/NodeList"), exports);
|
|
86
|
+
__exportStar(require("./userMap/userMap"), exports);
|
|
87
|
+
__exportStar(require("./userMap/userStatsMap"), exports);
|
package/lib/math/amm.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
|
-
import { AMM, PositionDirection, SwapDirection,
|
|
3
|
+
import { AMM, PositionDirection, SwapDirection, PerpMarketAccount } from '../types';
|
|
4
4
|
import { OraclePriceData } from '../oracles/types';
|
|
5
5
|
export declare function calculatePegFromTargetPrice(targetPrice: BN, baseAssetReserve: BN, quoteAssetReserve: BN): BN;
|
|
6
6
|
export declare function calculateOptimalPegAndBudget(amm: AMM, oraclePriceData: OraclePriceData): [BN, BN, BN, boolean];
|
|
@@ -19,7 +19,7 @@ export declare function calculateBidAskPrice(amm: AMM, oraclePriceData: OraclePr
|
|
|
19
19
|
* @param baseAssetReserves
|
|
20
20
|
* @param quoteAssetReserves
|
|
21
21
|
* @param pegMultiplier
|
|
22
|
-
* @returns price : Precision
|
|
22
|
+
* @returns price : Precision PRICE_PRECISION
|
|
23
23
|
*/
|
|
24
24
|
export declare function calculatePrice(baseAssetReserves: BN, quoteAssetReserves: BN, pegMultiplier: BN): BN;
|
|
25
25
|
export declare type AssetType = 'quote' | 'base';
|
|
@@ -33,9 +33,11 @@ export declare type AssetType = 'quote' | 'base';
|
|
|
33
33
|
* @returns quoteAssetReserve and baseAssetReserve after swap. : Precision AMM_RESERVE_PRECISION
|
|
34
34
|
*/
|
|
35
35
|
export declare function calculateAmmReservesAfterSwap(amm: Pick<AMM, 'pegMultiplier' | 'quoteAssetReserve' | 'sqrtK' | 'baseAssetReserve'>, inputAssetType: AssetType, swapAmount: BN, swapDirection: SwapDirection): [BN, BN];
|
|
36
|
-
export declare function
|
|
36
|
+
export declare function calculateMarketOpenBidAsk(baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): [BN, BN];
|
|
37
|
+
export declare function calculateInventoryScale(netBaseAssetAmount: BN, baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): number;
|
|
38
|
+
export declare function calculateEffectiveLeverage(baseSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, reservePrice: BN, totalFeeMinusDistributions: BN): number;
|
|
37
39
|
export declare function calculateMaxSpread(marginRatioInitial: number): number;
|
|
38
|
-
export declare function calculateSpreadBN(baseSpread: number,
|
|
40
|
+
export declare function calculateSpreadBN(baseSpread: number, lastOracleReservePriceSpreadPct: BN, lastOracleConfPct: BN, maxSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, reservePrice: BN, totalFeeMinusDistributions: BN, baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): [number, number];
|
|
39
41
|
export declare function calculateSpread(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): number;
|
|
40
42
|
export declare function calculateSpreadReserves(amm: AMM, direction: PositionDirection, oraclePriceData: OraclePriceData): {
|
|
41
43
|
baseAssetReserve: BN;
|
|
@@ -62,8 +64,9 @@ export declare function getSwapDirection(inputAssetType: AssetType, positionDire
|
|
|
62
64
|
* Helper function calculating terminal price of amm
|
|
63
65
|
*
|
|
64
66
|
* @param market
|
|
65
|
-
* @returns cost : Precision
|
|
67
|
+
* @returns cost : Precision PRICE_PRECISION
|
|
66
68
|
*/
|
|
67
|
-
export declare function calculateTerminalPrice(market:
|
|
69
|
+
export declare function calculateTerminalPrice(market: PerpMarketAccount): BN;
|
|
68
70
|
export declare function calculateMaxBaseAssetAmountToTrade(amm: AMM, limit_price: BN, direction: PositionDirection, oraclePriceData?: OraclePriceData): [BN, PositionDirection];
|
|
69
71
|
export declare function calculateQuoteAssetAmountSwapped(quoteAssetReserves: BN, pegMultiplier: BN, swapDirection: SwapDirection): BN;
|
|
72
|
+
export declare function calculateMaxBaseAssetAmountFillable(amm: AMM, orderDirection: PositionDirection): BN;
|
package/lib/math/amm.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
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;
|
|
3
|
+
exports.calculateMaxBaseAssetAmountFillable = exports.calculateQuoteAssetAmountSwapped = exports.calculateMaxBaseAssetAmountToTrade = exports.calculateTerminalPrice = exports.getSwapDirection = exports.calculateSwapOutput = exports.calculateSpreadReserves = exports.calculateSpread = exports.calculateSpreadBN = exports.calculateMaxSpread = exports.calculateEffectiveLeverage = exports.calculateInventoryScale = exports.calculateMarketOpenBidAsk = exports.calculateAmmReservesAfterSwap = exports.calculatePrice = exports.calculateBidAskPrice = exports.calculateUpdatedAMMSpreadReserves = exports.calculateUpdatedAMM = exports.calculateNewAmm = exports.calculateOptimalPegAndBudget = exports.calculatePegFromTargetPrice = void 0;
|
|
4
4
|
const anchor_1 = require("@project-serum/anchor");
|
|
5
5
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
6
6
|
const types_1 = require("../types");
|
|
@@ -8,15 +8,15 @@ const assert_1 = require("../assert/assert");
|
|
|
8
8
|
const __1 = require("..");
|
|
9
9
|
const repeg_1 = require("./repeg");
|
|
10
10
|
function calculatePegFromTargetPrice(targetPrice, baseAssetReserve, quoteAssetReserve) {
|
|
11
|
-
return targetPrice
|
|
11
|
+
return anchor_1.BN.max(targetPrice
|
|
12
12
|
.mul(baseAssetReserve)
|
|
13
13
|
.div(quoteAssetReserve)
|
|
14
14
|
.add(numericConstants_1.PRICE_DIV_PEG.div(new anchor_1.BN(2)))
|
|
15
|
-
.div(numericConstants_1.PRICE_DIV_PEG);
|
|
15
|
+
.div(numericConstants_1.PRICE_DIV_PEG), numericConstants_1.ONE);
|
|
16
16
|
}
|
|
17
17
|
exports.calculatePegFromTargetPrice = calculatePegFromTargetPrice;
|
|
18
18
|
function calculateOptimalPegAndBudget(amm, oraclePriceData) {
|
|
19
|
-
const
|
|
19
|
+
const reservePriceBefore = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
20
20
|
const targetPrice = oraclePriceData.price;
|
|
21
21
|
const newPeg = calculatePegFromTargetPrice(targetPrice, amm.baseAssetReserve, amm.quoteAssetReserve);
|
|
22
22
|
const prePegCost = (0, repeg_1.calculateRepegCost)(amm, newPeg);
|
|
@@ -29,14 +29,14 @@ function calculateOptimalPegAndBudget(amm, oraclePriceData) {
|
|
|
29
29
|
let newTargetPrice;
|
|
30
30
|
let newOptimalPeg;
|
|
31
31
|
let newBudget;
|
|
32
|
-
const targetPriceGap =
|
|
32
|
+
const targetPriceGap = reservePriceBefore.sub(targetPrice);
|
|
33
33
|
if (targetPriceGap.abs().gt(maxPriceSpread)) {
|
|
34
34
|
const markAdj = targetPriceGap.abs().sub(maxPriceSpread);
|
|
35
35
|
if (targetPriceGap.lt(new anchor_1.BN(0))) {
|
|
36
|
-
newTargetPrice =
|
|
36
|
+
newTargetPrice = reservePriceBefore.add(markAdj);
|
|
37
37
|
}
|
|
38
38
|
else {
|
|
39
|
-
newTargetPrice =
|
|
39
|
+
newTargetPrice = reservePriceBefore.sub(markAdj);
|
|
40
40
|
}
|
|
41
41
|
newOptimalPeg = calculatePegFromTargetPrice(newTargetPrice, amm.baseAssetReserve, amm.quoteAssetReserve);
|
|
42
42
|
newBudget = (0, repeg_1.calculateRepegCost)(amm, newOptimalPeg);
|
|
@@ -127,14 +127,14 @@ exports.calculateBidAskPrice = calculateBidAskPrice;
|
|
|
127
127
|
* @param baseAssetReserves
|
|
128
128
|
* @param quoteAssetReserves
|
|
129
129
|
* @param pegMultiplier
|
|
130
|
-
* @returns price : Precision
|
|
130
|
+
* @returns price : Precision PRICE_PRECISION
|
|
131
131
|
*/
|
|
132
132
|
function calculatePrice(baseAssetReserves, quoteAssetReserves, pegMultiplier) {
|
|
133
133
|
if (baseAssetReserves.abs().lte(numericConstants_1.ZERO)) {
|
|
134
134
|
return new anchor_1.BN(0);
|
|
135
135
|
}
|
|
136
136
|
return quoteAssetReserves
|
|
137
|
-
.mul(numericConstants_1.
|
|
137
|
+
.mul(numericConstants_1.PRICE_PRECISION)
|
|
138
138
|
.mul(pegMultiplier)
|
|
139
139
|
.div(numericConstants_1.PEG_PRECISION)
|
|
140
140
|
.div(baseAssetReserves);
|
|
@@ -165,15 +165,43 @@ function calculateAmmReservesAfterSwap(amm, inputAssetType, swapAmount, swapDire
|
|
|
165
165
|
return [newQuoteAssetReserve, newBaseAssetReserve];
|
|
166
166
|
}
|
|
167
167
|
exports.calculateAmmReservesAfterSwap = calculateAmmReservesAfterSwap;
|
|
168
|
-
function
|
|
168
|
+
function calculateMarketOpenBidAsk(baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve) {
|
|
169
|
+
// open orders
|
|
170
|
+
let openAsks;
|
|
171
|
+
if (maxBaseAssetReserve > baseAssetReserve) {
|
|
172
|
+
openAsks = maxBaseAssetReserve.sub(baseAssetReserve).mul(new anchor_1.BN(-1));
|
|
173
|
+
}
|
|
174
|
+
else {
|
|
175
|
+
openAsks = numericConstants_1.ZERO;
|
|
176
|
+
}
|
|
177
|
+
let openBids;
|
|
178
|
+
if (minBaseAssetReserve < baseAssetReserve) {
|
|
179
|
+
openBids = baseAssetReserve.sub(minBaseAssetReserve);
|
|
180
|
+
}
|
|
181
|
+
else {
|
|
182
|
+
openBids = numericConstants_1.ZERO;
|
|
183
|
+
}
|
|
184
|
+
return [openBids, openAsks];
|
|
185
|
+
}
|
|
186
|
+
exports.calculateMarketOpenBidAsk = calculateMarketOpenBidAsk;
|
|
187
|
+
function calculateInventoryScale(netBaseAssetAmount, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve) {
|
|
188
|
+
const maxScale = numericConstants_1.BID_ASK_SPREAD_PRECISION.mul(new anchor_1.BN(10));
|
|
189
|
+
// inventory skew
|
|
190
|
+
const [openBids, openAsks] = calculateMarketOpenBidAsk(baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve);
|
|
191
|
+
const minSideLiquidity = anchor_1.BN.max(new anchor_1.BN(1), anchor_1.BN.min(openBids.abs(), openAsks.abs()));
|
|
192
|
+
const inventoryScale = anchor_1.BN.min(maxScale, netBaseAssetAmount.mul(maxScale).div(minSideLiquidity).abs()).toNumber() / numericConstants_1.BID_ASK_SPREAD_PRECISION.toNumber();
|
|
193
|
+
return inventoryScale;
|
|
194
|
+
}
|
|
195
|
+
exports.calculateInventoryScale = calculateInventoryScale;
|
|
196
|
+
function calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, reservePrice, totalFeeMinusDistributions) {
|
|
169
197
|
// inventory skew
|
|
170
198
|
const netBaseAssetValue = quoteAssetReserve
|
|
171
199
|
.sub(terminalQuoteAssetReserve)
|
|
172
200
|
.mul(pegMultiplier)
|
|
173
201
|
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
174
202
|
const localBaseAssetValue = netBaseAssetAmount
|
|
175
|
-
.mul(
|
|
176
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.
|
|
203
|
+
.mul(reservePrice)
|
|
204
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.PRICE_PRECISION));
|
|
177
205
|
const effectiveLeverage = localBaseAssetValue.sub(netBaseAssetValue).toNumber() /
|
|
178
206
|
(Math.max(0, totalFeeMinusDistributions.toNumber()) + 1) +
|
|
179
207
|
1 / numericConstants_1.QUOTE_PRECISION.toNumber();
|
|
@@ -187,20 +215,30 @@ function calculateMaxSpread(marginRatioInitial) {
|
|
|
187
215
|
return maxTargetSpread;
|
|
188
216
|
}
|
|
189
217
|
exports.calculateMaxSpread = calculateMaxSpread;
|
|
190
|
-
function calculateSpreadBN(baseSpread,
|
|
218
|
+
function calculateSpreadBN(baseSpread, lastOracleReservePriceSpreadPct, lastOracleConfPct, maxSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, reservePrice, totalFeeMinusDistributions, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve) {
|
|
191
219
|
let longSpread = baseSpread / 2;
|
|
192
220
|
let shortSpread = baseSpread / 2;
|
|
193
|
-
if (
|
|
194
|
-
shortSpread = Math.max(shortSpread,
|
|
221
|
+
if (lastOracleReservePriceSpreadPct.gt(numericConstants_1.ZERO)) {
|
|
222
|
+
shortSpread = Math.max(shortSpread, lastOracleReservePriceSpreadPct.abs().toNumber() +
|
|
223
|
+
lastOracleConfPct.toNumber());
|
|
224
|
+
}
|
|
225
|
+
else if (lastOracleReservePriceSpreadPct.lt(numericConstants_1.ZERO)) {
|
|
226
|
+
longSpread = Math.max(longSpread, lastOracleReservePriceSpreadPct.abs().toNumber() +
|
|
227
|
+
lastOracleConfPct.toNumber());
|
|
228
|
+
}
|
|
229
|
+
const maxTargetSpread = Math.max(maxSpread, lastOracleReservePriceSpreadPct.abs().toNumber());
|
|
230
|
+
const MAX_BID_ASK_INVENTORY_SKEW_FACTOR = 10;
|
|
231
|
+
const inventoryScale = calculateInventoryScale(netBaseAssetAmount, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve);
|
|
232
|
+
const inventorySpreadScale = Math.min(MAX_BID_ASK_INVENTORY_SKEW_FACTOR, 1 + inventoryScale);
|
|
233
|
+
if (netBaseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
234
|
+
longSpread *= inventorySpreadScale;
|
|
195
235
|
}
|
|
196
|
-
else if (
|
|
197
|
-
|
|
236
|
+
else if (netBaseAssetAmount.lt(numericConstants_1.ZERO)) {
|
|
237
|
+
shortSpread *= inventorySpreadScale;
|
|
198
238
|
}
|
|
199
|
-
const
|
|
200
|
-
const MAX_INVENTORY_SKEW = 5;
|
|
201
|
-
const effectiveLeverage = calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions);
|
|
239
|
+
const effectiveLeverage = calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, reservePrice, totalFeeMinusDistributions);
|
|
202
240
|
if (totalFeeMinusDistributions.gt(numericConstants_1.ZERO)) {
|
|
203
|
-
const spreadScale = Math.min(
|
|
241
|
+
const spreadScale = Math.min(MAX_BID_ASK_INVENTORY_SKEW_FACTOR, 1 + effectiveLeverage);
|
|
204
242
|
if (netBaseAssetAmount.gt(numericConstants_1.ZERO)) {
|
|
205
243
|
longSpread *= spreadScale;
|
|
206
244
|
}
|
|
@@ -209,8 +247,8 @@ function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPc
|
|
|
209
247
|
}
|
|
210
248
|
}
|
|
211
249
|
else {
|
|
212
|
-
longSpread *=
|
|
213
|
-
shortSpread *=
|
|
250
|
+
longSpread *= MAX_BID_ASK_INVENTORY_SKEW_FACTOR;
|
|
251
|
+
shortSpread *= MAX_BID_ASK_INVENTORY_SKEW_FACTOR;
|
|
214
252
|
}
|
|
215
253
|
const totalSpread = longSpread + shortSpread;
|
|
216
254
|
if (totalSpread > maxTargetSpread) {
|
|
@@ -227,54 +265,26 @@ function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPc
|
|
|
227
265
|
}
|
|
228
266
|
exports.calculateSpreadBN = calculateSpreadBN;
|
|
229
267
|
function calculateSpread(amm, direction, oraclePriceData) {
|
|
230
|
-
let spread = amm.baseSpread / 2;
|
|
231
268
|
if (amm.baseSpread == 0 || amm.curveUpdateIntensity == 0) {
|
|
232
|
-
return
|
|
269
|
+
return amm.baseSpread / 2;
|
|
233
270
|
}
|
|
234
|
-
const
|
|
235
|
-
const targetPrice = (oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.price) ||
|
|
271
|
+
const reservePrice = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
272
|
+
const targetPrice = (oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.price) || reservePrice;
|
|
236
273
|
const confInterval = oraclePriceData.confidence || numericConstants_1.ZERO;
|
|
237
|
-
const targetMarkSpreadPct =
|
|
274
|
+
const targetMarkSpreadPct = reservePrice
|
|
238
275
|
.sub(targetPrice)
|
|
239
276
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
240
|
-
.div(
|
|
277
|
+
.div(reservePrice);
|
|
241
278
|
const confIntervalPct = confInterval
|
|
242
279
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
243
|
-
.div(
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
spread =
|
|
280
|
+
.div(reservePrice);
|
|
281
|
+
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.netBaseAssetAmount, reservePrice, amm.totalFeeMinusDistributions, amm.baseAssetReserve, amm.minBaseAssetReserve, amm.maxBaseAssetReserve);
|
|
282
|
+
let spread;
|
|
283
|
+
if ((0, types_1.isVariant)(direction, 'long')) {
|
|
284
|
+
spread = longSpread;
|
|
248
285
|
}
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
if ((amm.netBaseAssetAmount.gt(numericConstants_1.ZERO) && (0, types_1.isVariant)(direction, 'long')) ||
|
|
252
|
-
(amm.netBaseAssetAmount.lt(numericConstants_1.ZERO) && (0, types_1.isVariant)(direction, 'short')) ||
|
|
253
|
-
amm.totalFeeMinusDistributions.eq(numericConstants_1.ZERO)) {
|
|
254
|
-
const netCostBasis = amm.quoteAssetAmountLong.sub(amm.quoteAssetAmountShort);
|
|
255
|
-
const netBaseAssetValue = amm.quoteAssetReserve
|
|
256
|
-
.sub(amm.terminalQuoteAssetReserve)
|
|
257
|
-
.mul(amm.pegMultiplier)
|
|
258
|
-
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
259
|
-
const localBaseAssetValue = amm.netBaseAssetAmount
|
|
260
|
-
.mul(markPrice)
|
|
261
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.MARK_PRICE_PRECISION));
|
|
262
|
-
const netPnl = netBaseAssetValue.sub(netCostBasis);
|
|
263
|
-
const localPnl = localBaseAssetValue.sub(netCostBasis);
|
|
264
|
-
let effectiveLeverage = MAX_INVENTORY_SKEW;
|
|
265
|
-
if (amm.totalFeeMinusDistributions.gt(numericConstants_1.ZERO)) {
|
|
266
|
-
effectiveLeverage =
|
|
267
|
-
localPnl.sub(netPnl).toNumber() /
|
|
268
|
-
(amm.totalFeeMinusDistributions.toNumber() + 1);
|
|
269
|
-
}
|
|
270
|
-
let spreadScale = Math.min(MAX_INVENTORY_SKEW, 1 + effectiveLeverage);
|
|
271
|
-
const maxTargetSpread = numericConstants_1.BID_ASK_SPREAD_PRECISION.toNumber() / 50; // 2%
|
|
272
|
-
// cap the scale to attempt to only scale up to maxTargetSpread
|
|
273
|
-
// always let the oracle retreat methods go through 100%
|
|
274
|
-
if (spreadScale * spread > maxTargetSpread) {
|
|
275
|
-
spreadScale = Math.max(1.05, maxTargetSpread / spread);
|
|
276
|
-
}
|
|
277
|
-
spread *= spreadScale;
|
|
286
|
+
else {
|
|
287
|
+
spread = shortSpread;
|
|
278
288
|
}
|
|
279
289
|
return spread;
|
|
280
290
|
}
|
|
@@ -343,7 +353,7 @@ exports.getSwapDirection = getSwapDirection;
|
|
|
343
353
|
* Helper function calculating terminal price of amm
|
|
344
354
|
*
|
|
345
355
|
* @param market
|
|
346
|
-
* @returns cost : Precision
|
|
356
|
+
* @returns cost : Precision PRICE_PRECISION
|
|
347
357
|
*/
|
|
348
358
|
function calculateTerminalPrice(market) {
|
|
349
359
|
const directionToClose = market.amm.netBaseAssetAmount.gt(numericConstants_1.ZERO)
|
|
@@ -351,7 +361,7 @@ function calculateTerminalPrice(market) {
|
|
|
351
361
|
: types_1.PositionDirection.LONG;
|
|
352
362
|
const [newQuoteAssetReserve, newBaseAssetReserve] = calculateAmmReservesAfterSwap(market.amm, 'base', market.amm.netBaseAssetAmount.abs(), getSwapDirection('base', directionToClose));
|
|
353
363
|
const terminalPrice = newQuoteAssetReserve
|
|
354
|
-
.mul(numericConstants_1.
|
|
364
|
+
.mul(numericConstants_1.PRICE_PRECISION)
|
|
355
365
|
.mul(market.amm.pegMultiplier)
|
|
356
366
|
.div(numericConstants_1.PEG_PRECISION)
|
|
357
367
|
.div(newBaseAssetReserve);
|
|
@@ -361,7 +371,7 @@ exports.calculateTerminalPrice = calculateTerminalPrice;
|
|
|
361
371
|
function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, oraclePriceData) {
|
|
362
372
|
const invariant = amm.sqrtK.mul(amm.sqrtK);
|
|
363
373
|
const newBaseAssetReserveSquared = invariant
|
|
364
|
-
.mul(numericConstants_1.
|
|
374
|
+
.mul(numericConstants_1.PRICE_PRECISION)
|
|
365
375
|
.mul(amm.pegMultiplier)
|
|
366
376
|
.div(limit_price)
|
|
367
377
|
.div(numericConstants_1.PEG_PRECISION);
|
|
@@ -386,6 +396,9 @@ function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, oracleP
|
|
|
386
396
|
}
|
|
387
397
|
exports.calculateMaxBaseAssetAmountToTrade = calculateMaxBaseAssetAmountToTrade;
|
|
388
398
|
function calculateQuoteAssetAmountSwapped(quoteAssetReserves, pegMultiplier, swapDirection) {
|
|
399
|
+
if ((0, types_1.isVariant)(swapDirection, 'remove')) {
|
|
400
|
+
quoteAssetReserves = quoteAssetReserves.add(numericConstants_1.ONE);
|
|
401
|
+
}
|
|
389
402
|
let quoteAssetAmount = quoteAssetReserves
|
|
390
403
|
.mul(pegMultiplier)
|
|
391
404
|
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
@@ -395,3 +408,15 @@ function calculateQuoteAssetAmountSwapped(quoteAssetReserves, pegMultiplier, swa
|
|
|
395
408
|
return quoteAssetAmount;
|
|
396
409
|
}
|
|
397
410
|
exports.calculateQuoteAssetAmountSwapped = calculateQuoteAssetAmountSwapped;
|
|
411
|
+
function calculateMaxBaseAssetAmountFillable(amm, orderDirection) {
|
|
412
|
+
const maxFillSize = amm.baseAssetReserve.div(new anchor_1.BN(amm.maxBaseAssetAmountRatio));
|
|
413
|
+
let maxBaseAssetAmountOnSide;
|
|
414
|
+
if ((0, types_1.isVariant)(orderDirection, 'long')) {
|
|
415
|
+
maxBaseAssetAmountOnSide = anchor_1.BN.max(numericConstants_1.ZERO, amm.baseAssetReserve.sub(amm.minBaseAssetReserve));
|
|
416
|
+
}
|
|
417
|
+
else {
|
|
418
|
+
maxBaseAssetAmountOnSide = anchor_1.BN.max(numericConstants_1.ZERO, amm.maxBaseAssetReserve.sub(amm.baseAssetReserve));
|
|
419
|
+
}
|
|
420
|
+
return (0, __1.standardizeBaseAssetAmount)(anchor_1.BN.min(maxFillSize, maxBaseAssetAmountOnSide), amm.baseAssetAmountStepSize);
|
|
421
|
+
}
|
|
422
|
+
exports.calculateMaxBaseAssetAmountFillable = calculateMaxBaseAssetAmountFillable;
|
package/lib/math/auction.js
CHANGED
|
@@ -4,7 +4,10 @@ exports.getAuctionPrice = exports.isAuctionComplete = void 0;
|
|
|
4
4
|
const types_1 = require("../types");
|
|
5
5
|
const _1 = require("../.");
|
|
6
6
|
function isAuctionComplete(order, slot) {
|
|
7
|
-
|
|
7
|
+
if (order.auctionDuration === 0) {
|
|
8
|
+
return true;
|
|
9
|
+
}
|
|
10
|
+
return new _1.BN(slot).sub(order.slot).gt(new _1.BN(order.auctionDuration));
|
|
8
11
|
}
|
|
9
12
|
exports.isAuctionComplete = isAuctionComplete;
|
|
10
13
|
function getAuctionPrice(order, slot) {
|
package/lib/math/conversion.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.convertToNumber = void 0;
|
|
4
4
|
const numericConstants_1 = require("../constants/numericConstants");
|
|
5
|
-
const convertToNumber = (bigNumber, precision = numericConstants_1.
|
|
5
|
+
const convertToNumber = (bigNumber, precision = numericConstants_1.PRICE_PRECISION) => {
|
|
6
6
|
if (!bigNumber)
|
|
7
7
|
return 0;
|
|
8
8
|
return (bigNumber.div(precision).toNumber() +
|