@drift-labs/sdk 0.2.0-master.4 → 0.2.0-master.40
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 +27 -27
- package/lib/accounts/bulkAccountLoader.d.ts +2 -0
- package/lib/accounts/bulkAccountLoader.js +36 -29
- package/lib/accounts/bulkUserStatsSubscription.d.ts +7 -0
- package/lib/accounts/bulkUserStatsSubscription.js +21 -0
- package/lib/accounts/bulkUserSubscription.d.ts +2 -2
- 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 → pollingDriftClientAccountSubscriber.d.ts} +20 -25
- package/lib/accounts/{pollingClearingHouseAccountSubscriber.js → pollingDriftClientAccountSubscriber.js} +45 -49
- package/lib/accounts/{pollingOracleSubscriber.d.ts → pollingOracleAccountSubscriber.d.ts} +2 -2
- package/lib/accounts/{pollingOracleSubscriber.js → pollingOracleAccountSubscriber.js} +3 -3
- package/lib/accounts/pollingUserStatsAccountSubscriber.d.ts +27 -0
- package/lib/accounts/pollingUserStatsAccountSubscriber.js +113 -0
- package/lib/accounts/types.d.ts +26 -15
- package/lib/accounts/webSocketDriftClientAccountSubscriber.d.ts +49 -0
- package/lib/accounts/{webSocketClearingHouseAccountSubscriber.js → webSocketDriftClientAccountSubscriber.js} +47 -45
- 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/marketAddresses.js +1 -1
- package/lib/addresses/pda.d.ts +15 -9
- package/lib/addresses/pda.js +73 -35
- package/lib/adminClient.d.ts +65 -0
- package/lib/adminClient.js +637 -0
- package/lib/config.d.ts +9 -9
- package/lib/config.js +25 -21
- package/lib/constants/numericConstants.d.ts +30 -12
- package/lib/constants/numericConstants.js +41 -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 +82 -0
- package/lib/dlob/DLOB.js +696 -0
- package/lib/dlob/DLOBNode.d.ts +54 -0
- package/lib/dlob/DLOBNode.js +77 -0
- package/lib/dlob/NodeList.d.ts +27 -0
- package/lib/dlob/NodeList.js +144 -0
- package/lib/driftClient.d.ts +233 -0
- package/lib/driftClient.js +2096 -0
- package/lib/{clearingHouseConfig.d.ts → driftClientConfig.d.ts} +9 -9
- package/lib/{clearingHouseConfig.js → driftClientConfig.js} +0 -0
- package/lib/events/eventList.js +3 -0
- package/lib/events/eventSubscriber.d.ts +5 -2
- package/lib/events/eventSubscriber.js +25 -11
- package/lib/events/fetchLogs.d.ts +13 -2
- package/lib/events/fetchLogs.js +40 -12
- package/lib/events/pollingLogProvider.d.ts +2 -1
- package/lib/events/pollingLogProvider.js +7 -3
- package/lib/events/sort.js +8 -11
- package/lib/events/types.d.ts +9 -3
- package/lib/events/types.js +6 -0
- package/lib/events/webSocketLogProvider.js +1 -1
- package/lib/examples/makeTradeExample.js +30 -18
- package/lib/factory/bigNum.d.ts +8 -4
- package/lib/factory/bigNum.js +109 -19
- package/lib/idl/drift.json +8250 -0
- package/lib/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/lib/index.d.ts +30 -13
- package/lib/index.js +30 -13
- package/lib/math/amm.d.ts +9 -6
- package/lib/math/amm.js +91 -38
- package/lib/math/conversion.js +1 -1
- package/lib/math/exchangeStatus.d.ts +4 -0
- package/lib/math/exchangeStatus.js +18 -0
- package/lib/math/funding.d.ts +6 -6
- package/lib/math/funding.js +23 -21
- 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 +82 -0
- package/lib/math/market.d.ts +14 -9
- package/lib/math/market.js +70 -10
- package/lib/math/oracles.d.ts +4 -0
- package/lib/math/oracles.js +36 -8
- package/lib/math/orders.d.ts +14 -6
- package/lib/math/orders.js +90 -17
- package/lib/math/position.d.ts +27 -13
- package/lib/math/position.js +92 -36
- package/lib/math/repeg.js +17 -8
- 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 +27 -31
- 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 +4 -4
- package/lib/orderParams.js +12 -4
- package/lib/serum/serumFulfillmentConfigMap.d.ts +10 -0
- package/lib/serum/serumFulfillmentConfigMap.js +17 -0
- package/lib/serum/serumSubscriber.d.ts +27 -0
- package/lib/serum/serumSubscriber.js +56 -0
- package/lib/serum/types.d.ts +11 -0
- package/lib/{clearingHouseUserConfig.js → serum/types.js} +0 -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 +589 -196
- package/lib/types.js +108 -17
- package/lib/user.d.ts +226 -0
- package/lib/user.js +949 -0
- package/lib/userConfig.d.ts +14 -0
- package/lib/userConfig.js +2 -0
- package/lib/userMap/userMap.d.ts +41 -0
- package/lib/userMap/userMap.js +85 -0
- package/lib/userMap/userStatsMap.d.ts +19 -0
- package/lib/userMap/userStatsMap.js +68 -0
- package/lib/userName.d.ts +1 -0
- package/lib/userName.js +3 -2
- package/lib/userStats.d.ts +18 -0
- package/lib/userStats.js +49 -0
- package/lib/userStatsConfig.d.ts +14 -0
- package/lib/userStatsConfig.js +2 -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 +10 -3
- package/src/accounts/bulkAccountLoader.ts +44 -34
- package/src/accounts/bulkUserStatsSubscription.ts +33 -0
- package/src/accounts/bulkUserSubscription.ts +2 -3
- package/src/accounts/fetch.ts +27 -2
- package/src/accounts/{pollingClearingHouseAccountSubscriber.ts → pollingDriftClientAccountSubscriber.ts} +65 -75
- package/src/accounts/{pollingOracleSubscriber.ts → pollingOracleAccountSubscriber.ts} +2 -2
- package/src/accounts/pollingUserStatsAccountSubscriber.ts +172 -0
- package/src/accounts/types.ts +35 -15
- package/src/accounts/{webSocketClearingHouseAccountSubscriber.ts → webSocketDriftClientAccountSubscriber.ts} +78 -73
- package/src/accounts/webSocketUserStatsAccountSubsriber.ts +80 -0
- package/src/addresses/marketAddresses.ts +3 -4
- package/src/addresses/pda.ts +105 -33
- package/src/adminClient.ts +1207 -0
- package/src/config.ts +37 -31
- package/src/constants/numericConstants.ts +58 -24
- package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
- package/src/constants/spotMarkets.ts +73 -0
- package/src/dlob/DLOB.ts +1123 -0
- package/src/dlob/DLOBNode.ts +155 -0
- package/src/dlob/NodeList.ts +195 -0
- package/src/driftClient.ts +3564 -0
- package/src/{clearingHouseConfig.ts → driftClientConfig.ts} +9 -8
- package/src/events/eventList.ts +3 -0
- package/src/events/eventSubscriber.ts +36 -14
- package/src/events/fetchLogs.ts +55 -13
- package/src/events/pollingLogProvider.ts +11 -3
- package/src/events/sort.ts +11 -15
- package/src/events/types.ts +21 -2
- package/src/events/webSocketLogProvider.ts +1 -1
- package/src/examples/makeTradeExample.ts +44 -28
- package/src/factory/bigNum.ts +150 -22
- package/src/idl/drift.json +8250 -0
- package/src/idl/pyth.json +98 -2
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +30 -13
- package/src/math/amm.ts +161 -48
- package/src/math/conversion.ts +2 -2
- package/src/math/exchangeStatus.ts +31 -0
- package/src/math/funding.ts +41 -31
- package/src/math/insurance.ts +35 -0
- package/src/math/margin.ts +133 -0
- package/src/math/market.ts +143 -14
- package/src/math/oracles.ts +63 -9
- package/src/math/orders.ts +163 -26
- package/src/math/position.ts +136 -58
- package/src/math/repeg.ts +19 -9
- 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 +33 -37
- 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 +16 -8
- package/src/serum/serumFulfillmentConfigMap.ts +26 -0
- package/src/serum/serumSubscriber.ts +99 -0
- package/src/serum/types.ts +13 -0
- package/src/slot/SlotSubscriber.ts +11 -1
- package/src/{mockUSDCFaucet.ts → tokenFaucet.ts} +82 -70
- package/src/tx/retryTxSender.ts +16 -5
- package/src/tx/types.ts +2 -1
- package/src/tx/utils.ts +1 -1
- package/src/types.ts +572 -178
- package/src/user.ts +1582 -0
- package/src/{clearingHouseUserConfig.ts → userConfig.ts} +5 -5
- package/src/userMap/userMap.ts +124 -0
- package/src/userMap/userStatsMap.ts +108 -0
- package/src/userName.ts +2 -1
- package/src/userStats.ts +75 -0
- package/src/userStatsConfig.ts +18 -0
- package/src/util/computeUnits.ts +1 -1
- package/src/util/getTokenAddress.ts +18 -0
- package/tests/bn/test.ts +46 -11
- package/tests/dlob/helpers.ts +611 -0
- package/tests/dlob/test.ts +4588 -0
- package/lib/accounts/webSocketClearingHouseAccountSubscriber.d.ts +0 -49
- package/lib/admin.d.ts +0 -44
- package/lib/admin.js +0 -433
- package/lib/clearingHouse.d.ts +0 -133
- package/lib/clearingHouse.js +0 -931
- package/lib/clearingHouseUser.d.ts +0 -187
- package/lib/clearingHouseUser.js +0 -643
- package/lib/clearingHouseUserConfig.d.ts +0 -14
- 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/idl/clearing_house.json +0 -3998
- 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/admin.ts +0 -722
- package/src/clearingHouse.ts +0 -1451
- package/src/clearingHouseUser.ts +0 -989
- package/src/constants/banks.ts +0 -43
- package/src/idl/clearing_house.json +0 -3998
- 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 +0 -17
- 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,31 +1,39 @@
|
|
|
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 './
|
|
4
|
+
export * from './tokenFaucet';
|
|
5
5
|
export * from './oracles/types';
|
|
6
6
|
export * from './oracles/pythClient';
|
|
7
7
|
export * from './oracles/switchboardClient';
|
|
8
8
|
export * from './types';
|
|
9
|
-
export * from './constants/
|
|
9
|
+
export * from './constants/perpMarkets';
|
|
10
10
|
export * from './accounts/fetch';
|
|
11
|
-
export * from './accounts/
|
|
11
|
+
export * from './accounts/webSocketDriftClientAccountSubscriber';
|
|
12
12
|
export * from './accounts/bulkAccountLoader';
|
|
13
13
|
export * from './accounts/bulkUserSubscription';
|
|
14
|
-
export * from './accounts/
|
|
15
|
-
export * from './accounts/
|
|
14
|
+
export * from './accounts/bulkUserStatsSubscription';
|
|
15
|
+
export * from './accounts/pollingDriftClientAccountSubscriber';
|
|
16
|
+
export * from './accounts/pollingOracleAccountSubscriber';
|
|
16
17
|
export * from './accounts/pollingTokenAccountSubscriber';
|
|
18
|
+
export * from './accounts/pollingUserAccountSubscriber';
|
|
19
|
+
export * from './accounts/pollingUserStatsAccountSubscriber';
|
|
17
20
|
export * from './accounts/types';
|
|
18
21
|
export * from './addresses/pda';
|
|
19
|
-
export * from './
|
|
20
|
-
export * from './
|
|
21
|
-
export * from './
|
|
22
|
-
export * from './
|
|
22
|
+
export * from './adminClient';
|
|
23
|
+
export * from './user';
|
|
24
|
+
export * from './userConfig';
|
|
25
|
+
export * from './userStats';
|
|
26
|
+
export * from './userStatsConfig';
|
|
27
|
+
export * from './driftClient';
|
|
23
28
|
export * from './factory/oracleClient';
|
|
24
29
|
export * from './factory/bigNum';
|
|
25
30
|
export * from './events/types';
|
|
26
31
|
export * from './events/eventSubscriber';
|
|
32
|
+
export * from './events/fetchLogs';
|
|
27
33
|
export * from './math/auction';
|
|
34
|
+
export * from './math/spotMarket';
|
|
28
35
|
export * from './math/conversion';
|
|
36
|
+
export * from './math/exchangeStatus';
|
|
29
37
|
export * from './math/funding';
|
|
30
38
|
export * from './math/market';
|
|
31
39
|
export * from './math/position';
|
|
@@ -34,7 +42,8 @@ export * from './math/amm';
|
|
|
34
42
|
export * from './math/trade';
|
|
35
43
|
export * from './math/orders';
|
|
36
44
|
export * from './math/repeg';
|
|
37
|
-
export * from './
|
|
45
|
+
export * from './math/margin';
|
|
46
|
+
export * from './math/insurance';
|
|
38
47
|
export * from './orderParams';
|
|
39
48
|
export * from './slot/SlotSubscriber';
|
|
40
49
|
export * from './wallet';
|
|
@@ -42,10 +51,18 @@ export * from './types';
|
|
|
42
51
|
export * from './math/utils';
|
|
43
52
|
export * from './config';
|
|
44
53
|
export * from './constants/numericConstants';
|
|
54
|
+
export * from './serum/serumSubscriber';
|
|
55
|
+
export * from './serum/serumFulfillmentConfigMap';
|
|
45
56
|
export * from './tx/retryTxSender';
|
|
46
57
|
export * from './util/computeUnits';
|
|
47
58
|
export * from './util/tps';
|
|
48
|
-
export * from './
|
|
49
|
-
export * from './
|
|
50
|
-
export * from './
|
|
59
|
+
export * from './util/promiseTimeout';
|
|
60
|
+
export * from './math/spotBalance';
|
|
61
|
+
export * from './constants/spotMarkets';
|
|
62
|
+
export * from './driftClientConfig';
|
|
63
|
+
export * from './dlob/DLOB';
|
|
64
|
+
export * from './dlob/DLOBNode';
|
|
65
|
+
export * from './dlob/NodeList';
|
|
66
|
+
export * from './userMap/userMap';
|
|
67
|
+
export * from './userMap/userStatsMap';
|
|
51
68
|
export { BN, PublicKey, pyth };
|
package/lib/index.js
CHANGED
|
@@ -24,31 +24,39 @@ const web3_js_1 = require("@solana/web3.js");
|
|
|
24
24
|
Object.defineProperty(exports, "PublicKey", { enumerable: true, get: function () { return web3_js_1.PublicKey; } });
|
|
25
25
|
const client_1 = __importDefault(require("@pythnetwork/client"));
|
|
26
26
|
exports.pyth = client_1.default;
|
|
27
|
-
__exportStar(require("./
|
|
27
|
+
__exportStar(require("./tokenFaucet"), exports);
|
|
28
28
|
__exportStar(require("./oracles/types"), exports);
|
|
29
29
|
__exportStar(require("./oracles/pythClient"), exports);
|
|
30
30
|
__exportStar(require("./oracles/switchboardClient"), exports);
|
|
31
31
|
__exportStar(require("./types"), exports);
|
|
32
|
-
__exportStar(require("./constants/
|
|
32
|
+
__exportStar(require("./constants/perpMarkets"), exports);
|
|
33
33
|
__exportStar(require("./accounts/fetch"), exports);
|
|
34
|
-
__exportStar(require("./accounts/
|
|
34
|
+
__exportStar(require("./accounts/webSocketDriftClientAccountSubscriber"), exports);
|
|
35
35
|
__exportStar(require("./accounts/bulkAccountLoader"), exports);
|
|
36
36
|
__exportStar(require("./accounts/bulkUserSubscription"), exports);
|
|
37
|
-
__exportStar(require("./accounts/
|
|
38
|
-
__exportStar(require("./accounts/
|
|
37
|
+
__exportStar(require("./accounts/bulkUserStatsSubscription"), exports);
|
|
38
|
+
__exportStar(require("./accounts/pollingDriftClientAccountSubscriber"), exports);
|
|
39
|
+
__exportStar(require("./accounts/pollingOracleAccountSubscriber"), exports);
|
|
39
40
|
__exportStar(require("./accounts/pollingTokenAccountSubscriber"), exports);
|
|
41
|
+
__exportStar(require("./accounts/pollingUserAccountSubscriber"), exports);
|
|
42
|
+
__exportStar(require("./accounts/pollingUserStatsAccountSubscriber"), exports);
|
|
40
43
|
__exportStar(require("./accounts/types"), exports);
|
|
41
44
|
__exportStar(require("./addresses/pda"), exports);
|
|
42
|
-
__exportStar(require("./
|
|
43
|
-
__exportStar(require("./
|
|
44
|
-
__exportStar(require("./
|
|
45
|
-
__exportStar(require("./
|
|
45
|
+
__exportStar(require("./adminClient"), exports);
|
|
46
|
+
__exportStar(require("./user"), exports);
|
|
47
|
+
__exportStar(require("./userConfig"), exports);
|
|
48
|
+
__exportStar(require("./userStats"), exports);
|
|
49
|
+
__exportStar(require("./userStatsConfig"), exports);
|
|
50
|
+
__exportStar(require("./driftClient"), exports);
|
|
46
51
|
__exportStar(require("./factory/oracleClient"), exports);
|
|
47
52
|
__exportStar(require("./factory/bigNum"), exports);
|
|
48
53
|
__exportStar(require("./events/types"), exports);
|
|
49
54
|
__exportStar(require("./events/eventSubscriber"), exports);
|
|
55
|
+
__exportStar(require("./events/fetchLogs"), exports);
|
|
50
56
|
__exportStar(require("./math/auction"), exports);
|
|
57
|
+
__exportStar(require("./math/spotMarket"), exports);
|
|
51
58
|
__exportStar(require("./math/conversion"), exports);
|
|
59
|
+
__exportStar(require("./math/exchangeStatus"), exports);
|
|
52
60
|
__exportStar(require("./math/funding"), exports);
|
|
53
61
|
__exportStar(require("./math/market"), exports);
|
|
54
62
|
__exportStar(require("./math/position"), exports);
|
|
@@ -57,7 +65,8 @@ __exportStar(require("./math/amm"), exports);
|
|
|
57
65
|
__exportStar(require("./math/trade"), exports);
|
|
58
66
|
__exportStar(require("./math/orders"), exports);
|
|
59
67
|
__exportStar(require("./math/repeg"), exports);
|
|
60
|
-
__exportStar(require("./
|
|
68
|
+
__exportStar(require("./math/margin"), exports);
|
|
69
|
+
__exportStar(require("./math/insurance"), exports);
|
|
61
70
|
__exportStar(require("./orderParams"), exports);
|
|
62
71
|
__exportStar(require("./slot/SlotSubscriber"), exports);
|
|
63
72
|
__exportStar(require("./wallet"), exports);
|
|
@@ -65,9 +74,17 @@ __exportStar(require("./types"), exports);
|
|
|
65
74
|
__exportStar(require("./math/utils"), exports);
|
|
66
75
|
__exportStar(require("./config"), exports);
|
|
67
76
|
__exportStar(require("./constants/numericConstants"), exports);
|
|
77
|
+
__exportStar(require("./serum/serumSubscriber"), exports);
|
|
78
|
+
__exportStar(require("./serum/serumFulfillmentConfigMap"), exports);
|
|
68
79
|
__exportStar(require("./tx/retryTxSender"), exports);
|
|
69
80
|
__exportStar(require("./util/computeUnits"), exports);
|
|
70
81
|
__exportStar(require("./util/tps"), exports);
|
|
71
|
-
__exportStar(require("./
|
|
72
|
-
__exportStar(require("./
|
|
73
|
-
__exportStar(require("./
|
|
82
|
+
__exportStar(require("./util/promiseTimeout"), exports);
|
|
83
|
+
__exportStar(require("./math/spotBalance"), exports);
|
|
84
|
+
__exportStar(require("./constants/spotMarkets"), exports);
|
|
85
|
+
__exportStar(require("./driftClientConfig"), exports);
|
|
86
|
+
__exportStar(require("./dlob/DLOB"), exports);
|
|
87
|
+
__exportStar(require("./dlob/DLOBNode"), exports);
|
|
88
|
+
__exportStar(require("./dlob/NodeList"), exports);
|
|
89
|
+
__exportStar(require("./userMap/userMap"), exports);
|
|
90
|
+
__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);
|
|
@@ -62,10 +62,10 @@ function calculateNewAmm(amm, oraclePriceData) {
|
|
|
62
62
|
newAmm.sqrtK = newAmm.sqrtK.mul(pKNumer).div(pKDenom);
|
|
63
63
|
const invariant = newAmm.sqrtK.mul(newAmm.sqrtK);
|
|
64
64
|
newAmm.quoteAssetReserve = invariant.div(newAmm.baseAssetReserve);
|
|
65
|
-
const directionToClose = amm.
|
|
65
|
+
const directionToClose = amm.baseAssetAmountWithAmm.gt(numericConstants_1.ZERO)
|
|
66
66
|
? types_1.PositionDirection.SHORT
|
|
67
67
|
: types_1.PositionDirection.LONG;
|
|
68
|
-
const [newQuoteAssetReserve, _newBaseAssetReserve] = calculateAmmReservesAfterSwap(newAmm, 'base', amm.
|
|
68
|
+
const [newQuoteAssetReserve, _newBaseAssetReserve] = calculateAmmReservesAfterSwap(newAmm, 'base', amm.baseAssetAmountWithAmm.abs(), getSwapDirection('base', directionToClose));
|
|
69
69
|
newAmm.terminalQuoteAssetReserve = newQuoteAssetReserve;
|
|
70
70
|
newPeg = (0, repeg_1.calculateBudgetedPeg)(newAmm, prePegCost, targetPrice);
|
|
71
71
|
prePegCost = (0, repeg_1.calculateRepegCost)(newAmm, newPeg);
|
|
@@ -84,10 +84,10 @@ function calculateUpdatedAMM(amm, oraclePriceData) {
|
|
|
84
84
|
const invariant = newAmm.sqrtK.mul(newAmm.sqrtK);
|
|
85
85
|
newAmm.quoteAssetReserve = invariant.div(newAmm.baseAssetReserve);
|
|
86
86
|
newAmm.pegMultiplier = newPeg;
|
|
87
|
-
const directionToClose = amm.
|
|
87
|
+
const directionToClose = amm.baseAssetAmountWithAmm.gt(numericConstants_1.ZERO)
|
|
88
88
|
? types_1.PositionDirection.SHORT
|
|
89
89
|
: types_1.PositionDirection.LONG;
|
|
90
|
-
const [newQuoteAssetReserve, _newBaseAssetReserve] = calculateAmmReservesAfterSwap(newAmm, 'base', amm.
|
|
90
|
+
const [newQuoteAssetReserve, _newBaseAssetReserve] = calculateAmmReservesAfterSwap(newAmm, 'base', amm.baseAssetAmountWithAmm.abs(), getSwapDirection('base', directionToClose));
|
|
91
91
|
newAmm.terminalQuoteAssetReserve = newQuoteAssetReserve;
|
|
92
92
|
newAmm.totalFeeMinusDistributions =
|
|
93
93
|
newAmm.totalFeeMinusDistributions.sub(prepegCost);
|
|
@@ -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());
|
|
195
224
|
}
|
|
196
|
-
else if (
|
|
197
|
-
longSpread = Math.max(longSpread,
|
|
225
|
+
else if (lastOracleReservePriceSpreadPct.lt(numericConstants_1.ZERO)) {
|
|
226
|
+
longSpread = Math.max(longSpread, lastOracleReservePriceSpreadPct.abs().toNumber() +
|
|
227
|
+
lastOracleConfPct.toNumber());
|
|
198
228
|
}
|
|
199
|
-
const maxTargetSpread = maxSpread;
|
|
200
|
-
const
|
|
201
|
-
const
|
|
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;
|
|
235
|
+
}
|
|
236
|
+
else if (netBaseAssetAmount.lt(numericConstants_1.ZERO)) {
|
|
237
|
+
shortSpread *= inventorySpreadScale;
|
|
238
|
+
}
|
|
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) {
|
|
@@ -230,17 +268,17 @@ function calculateSpread(amm, direction, oraclePriceData) {
|
|
|
230
268
|
if (amm.baseSpread == 0 || amm.curveUpdateIntensity == 0) {
|
|
231
269
|
return amm.baseSpread / 2;
|
|
232
270
|
}
|
|
233
|
-
const
|
|
234
|
-
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;
|
|
235
273
|
const confInterval = oraclePriceData.confidence || numericConstants_1.ZERO;
|
|
236
|
-
const targetMarkSpreadPct =
|
|
274
|
+
const targetMarkSpreadPct = reservePrice
|
|
237
275
|
.sub(targetPrice)
|
|
238
276
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
239
|
-
.div(
|
|
277
|
+
.div(reservePrice);
|
|
240
278
|
const confIntervalPct = confInterval
|
|
241
279
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
242
|
-
.div(
|
|
243
|
-
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.
|
|
280
|
+
.div(reservePrice);
|
|
281
|
+
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.baseAssetAmountWithAmm, reservePrice, amm.totalFeeMinusDistributions, amm.baseAssetReserve, amm.minBaseAssetReserve, amm.maxBaseAssetReserve);
|
|
244
282
|
let spread;
|
|
245
283
|
if ((0, types_1.isVariant)(direction, 'long')) {
|
|
246
284
|
spread = longSpread;
|
|
@@ -315,15 +353,15 @@ exports.getSwapDirection = getSwapDirection;
|
|
|
315
353
|
* Helper function calculating terminal price of amm
|
|
316
354
|
*
|
|
317
355
|
* @param market
|
|
318
|
-
* @returns cost : Precision
|
|
356
|
+
* @returns cost : Precision PRICE_PRECISION
|
|
319
357
|
*/
|
|
320
358
|
function calculateTerminalPrice(market) {
|
|
321
|
-
const directionToClose = market.amm.
|
|
359
|
+
const directionToClose = market.amm.baseAssetAmountWithAmm.gt(numericConstants_1.ZERO)
|
|
322
360
|
? types_1.PositionDirection.SHORT
|
|
323
361
|
: types_1.PositionDirection.LONG;
|
|
324
|
-
const [newQuoteAssetReserve, newBaseAssetReserve] = calculateAmmReservesAfterSwap(market.amm, 'base', market.amm.
|
|
362
|
+
const [newQuoteAssetReserve, newBaseAssetReserve] = calculateAmmReservesAfterSwap(market.amm, 'base', market.amm.baseAssetAmountWithAmm.abs(), getSwapDirection('base', directionToClose));
|
|
325
363
|
const terminalPrice = newQuoteAssetReserve
|
|
326
|
-
.mul(numericConstants_1.
|
|
364
|
+
.mul(numericConstants_1.PRICE_PRECISION)
|
|
327
365
|
.mul(market.amm.pegMultiplier)
|
|
328
366
|
.div(numericConstants_1.PEG_PRECISION)
|
|
329
367
|
.div(newBaseAssetReserve);
|
|
@@ -333,7 +371,7 @@ exports.calculateTerminalPrice = calculateTerminalPrice;
|
|
|
333
371
|
function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, oraclePriceData) {
|
|
334
372
|
const invariant = amm.sqrtK.mul(amm.sqrtK);
|
|
335
373
|
const newBaseAssetReserveSquared = invariant
|
|
336
|
-
.mul(numericConstants_1.
|
|
374
|
+
.mul(numericConstants_1.PRICE_PRECISION)
|
|
337
375
|
.mul(amm.pegMultiplier)
|
|
338
376
|
.div(limit_price)
|
|
339
377
|
.div(numericConstants_1.PEG_PRECISION);
|
|
@@ -358,6 +396,9 @@ function calculateMaxBaseAssetAmountToTrade(amm, limit_price, direction, oracleP
|
|
|
358
396
|
}
|
|
359
397
|
exports.calculateMaxBaseAssetAmountToTrade = calculateMaxBaseAssetAmountToTrade;
|
|
360
398
|
function calculateQuoteAssetAmountSwapped(quoteAssetReserves, pegMultiplier, swapDirection) {
|
|
399
|
+
if ((0, types_1.isVariant)(swapDirection, 'remove')) {
|
|
400
|
+
quoteAssetReserves = quoteAssetReserves.add(numericConstants_1.ONE);
|
|
401
|
+
}
|
|
361
402
|
let quoteAssetAmount = quoteAssetReserves
|
|
362
403
|
.mul(pegMultiplier)
|
|
363
404
|
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
@@ -367,3 +408,15 @@ function calculateQuoteAssetAmountSwapped(quoteAssetReserves, pegMultiplier, swa
|
|
|
367
408
|
return quoteAssetAmount;
|
|
368
409
|
}
|
|
369
410
|
exports.calculateQuoteAssetAmountSwapped = calculateQuoteAssetAmountSwapped;
|
|
411
|
+
function calculateMaxBaseAssetAmountFillable(amm, orderDirection) {
|
|
412
|
+
const maxFillSize = amm.baseAssetReserve.div(new anchor_1.BN(amm.maxFillReserveFraction));
|
|
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.orderStepSize);
|
|
421
|
+
}
|
|
422
|
+
exports.calculateMaxBaseAssetAmountFillable = calculateMaxBaseAssetAmountFillable;
|
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() +
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { PerpMarketAccount, SpotMarketAccount, StateAccount } from '../types';
|
|
2
|
+
export declare function exchangePaused(state: StateAccount): boolean;
|
|
3
|
+
export declare function fillPaused(state: StateAccount, market: PerpMarketAccount | SpotMarketAccount): boolean;
|
|
4
|
+
export declare function ammPaused(state: StateAccount, market: PerpMarketAccount | SpotMarketAccount): boolean;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ammPaused = exports.fillPaused = exports.exchangePaused = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
function exchangePaused(state) {
|
|
6
|
+
return (0, types_1.isVariant)(state.exchangeStatus, 'paused');
|
|
7
|
+
}
|
|
8
|
+
exports.exchangePaused = exchangePaused;
|
|
9
|
+
function fillPaused(state, market) {
|
|
10
|
+
return ((0, types_1.isOneOfVariant)(state.exchangeStatus, ['paused', 'fillPaused']) ||
|
|
11
|
+
(0, types_1.isOneOfVariant)(market.status, ['paused', 'fillPaused']));
|
|
12
|
+
}
|
|
13
|
+
exports.fillPaused = fillPaused;
|
|
14
|
+
function ammPaused(state, market) {
|
|
15
|
+
return ((0, types_1.isOneOfVariant)(state.exchangeStatus, ['paused', 'ammPaused']) ||
|
|
16
|
+
(0, types_1.isOneOfVariant)(market.status, ['paused', 'ammPaused']));
|
|
17
|
+
}
|
|
18
|
+
exports.ammPaused = ammPaused;
|