@drift-labs/sdk 0.2.0-master.3 → 0.2.0-master.30
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 +34 -22
- package/lib/admin.js +182 -73
- package/lib/clearingHouse.d.ts +120 -42
- package/lib/clearingHouse.js +1505 -254
- package/lib/clearingHouseConfig.d.ts +4 -4
- package/lib/clearingHouseUser.d.ts +50 -38
- package/lib/clearingHouseUser.js +410 -190
- 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 +18 -12
- package/lib/constants/numericConstants.js +28 -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 +73 -0
- package/lib/dlob/DLOB.js +553 -0
- package/lib/dlob/DLOBNode.d.ts +52 -0
- package/lib/dlob/DLOBNode.js +82 -0
- package/lib/dlob/NodeList.d.ts +26 -0
- package/lib/dlob/NodeList.js +138 -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 +7 -2
- package/lib/events/types.js +5 -0
- package/lib/examples/makeTradeExample.js +20 -8
- package/lib/factory/bigNum.d.ts +1 -0
- package/lib/factory/bigNum.js +34 -10
- package/lib/idl/clearing_house.json +4313 -1433
- 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 +8 -5
- package/lib/math/amm.js +68 -46
- 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 +69 -7
- 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 +193 -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 +22 -29
- 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 +474 -123
- package/lib/types.js +99 -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 +9 -5
- 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 +333 -128
- package/src/assert/assert.js +9 -0
- package/src/clearingHouse.ts +2464 -458
- package/src/clearingHouseConfig.ts +4 -3
- package/src/clearingHouseUser.ts +747 -291
- package/src/clearingHouseUserStats.ts +75 -0
- package/src/clearingHouseUserStatsConfig.ts +18 -0
- package/src/config.ts +30 -31
- package/src/constants/numericConstants.ts +41 -25
- package/src/constants/{markets.ts → perpMarkets.ts} +10 -10
- package/src/constants/spotMarkets.ts +72 -0
- package/src/dlob/DLOB.ts +868 -0
- package/src/dlob/DLOBNode.ts +162 -0
- package/src/dlob/NodeList.ts +185 -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 +16 -1
- package/src/examples/makeTradeExample.js +157 -0
- package/src/examples/makeTradeExample.ts +32 -14
- package/src/factory/bigNum.ts +42 -13
- package/src/idl/clearing_house.json +4313 -1433
- package/src/idl/{mock_usdc_faucet.json → token_faucet.json} +46 -23
- package/src/index.ts +21 -6
- package/src/math/amm.ts +136 -66
- 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 +138 -12
- 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 +316 -0
- package/src/math/spotMarket.ts +9 -0
- package/src/math/spotPosition.ts +47 -0
- package/src/math/trade.ts +43 -49
- 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 +477 -125
- 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 +12 -3
- package/tests/dlob/helpers.ts +374 -0
- package/tests/dlob/test.ts +2865 -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 calculateMarketOpenBidAsk(baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): [BN, BN];
|
|
37
|
+
export declare function calculateInventoryScale(netBaseAssetAmount: BN, baseAssetReserve: BN, minBaseAssetReserve: BN, maxBaseAssetReserve: BN): number;
|
|
36
38
|
export declare function calculateEffectiveLeverage(baseSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, markPrice: BN, totalFeeMinusDistributions: BN): number;
|
|
37
39
|
export declare function calculateMaxSpread(marginRatioInitial: number): number;
|
|
38
|
-
export declare function calculateSpreadBN(baseSpread: number, lastOracleMarkSpreadPct: BN, lastOracleConfPct: BN, maxSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, markPrice: BN, totalFeeMinusDistributions: BN): [number, number];
|
|
40
|
+
export declare function calculateSpreadBN(baseSpread: number, lastOracleMarkSpreadPct: BN, lastOracleConfPct: BN, maxSpread: number, quoteAssetReserve: BN, terminalQuoteAssetReserve: BN, pegMultiplier: BN, netBaseAssetAmount: BN, markPrice: 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,11 +8,11 @@ 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) {
|
|
@@ -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,6 +165,36 @@ function calculateAmmReservesAfterSwap(amm, inputAssetType, swapAmount, swapDire
|
|
|
165
165
|
return [newQuoteAssetReserve, newBaseAssetReserve];
|
|
166
166
|
}
|
|
167
167
|
exports.calculateAmmReservesAfterSwap = calculateAmmReservesAfterSwap;
|
|
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
|
+
// inventory skew
|
|
189
|
+
const [openBids, openAsks] = calculateMarketOpenBidAsk(baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve);
|
|
190
|
+
const minSideLiquidity = anchor_1.BN.max(new anchor_1.BN(1), anchor_1.BN.min(openBids.abs(), openAsks.abs()));
|
|
191
|
+
const inventoryScale = anchor_1.BN.min(netBaseAssetAmount.abs(), minSideLiquidity)
|
|
192
|
+
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION.mul(new anchor_1.BN(10)))
|
|
193
|
+
.div(minSideLiquidity)
|
|
194
|
+
.toNumber() / numericConstants_1.BID_ASK_SPREAD_PRECISION.toNumber();
|
|
195
|
+
return inventoryScale;
|
|
196
|
+
}
|
|
197
|
+
exports.calculateInventoryScale = calculateInventoryScale;
|
|
168
198
|
function calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions) {
|
|
169
199
|
// inventory skew
|
|
170
200
|
const netBaseAssetValue = quoteAssetReserve
|
|
@@ -173,7 +203,7 @@ function calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuote
|
|
|
173
203
|
.div(numericConstants_1.AMM_TIMES_PEG_TO_QUOTE_PRECISION_RATIO);
|
|
174
204
|
const localBaseAssetValue = netBaseAssetAmount
|
|
175
205
|
.mul(markPrice)
|
|
176
|
-
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.
|
|
206
|
+
.div(numericConstants_1.AMM_TO_QUOTE_PRECISION_RATIO.mul(numericConstants_1.PRICE_PRECISION));
|
|
177
207
|
const effectiveLeverage = localBaseAssetValue.sub(netBaseAssetValue).toNumber() /
|
|
178
208
|
(Math.max(0, totalFeeMinusDistributions.toNumber()) + 1) +
|
|
179
209
|
1 / numericConstants_1.QUOTE_PRECISION.toNumber();
|
|
@@ -187,7 +217,7 @@ function calculateMaxSpread(marginRatioInitial) {
|
|
|
187
217
|
return maxTargetSpread;
|
|
188
218
|
}
|
|
189
219
|
exports.calculateMaxSpread = calculateMaxSpread;
|
|
190
|
-
function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPct, maxSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions) {
|
|
220
|
+
function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPct, maxSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve) {
|
|
191
221
|
let longSpread = baseSpread / 2;
|
|
192
222
|
let shortSpread = baseSpread / 2;
|
|
193
223
|
if (lastOracleMarkSpreadPct.gt(numericConstants_1.ZERO)) {
|
|
@@ -196,8 +226,16 @@ function calculateSpreadBN(baseSpread, lastOracleMarkSpreadPct, lastOracleConfPc
|
|
|
196
226
|
else if (lastOracleMarkSpreadPct.lt(numericConstants_1.ZERO)) {
|
|
197
227
|
longSpread = Math.max(longSpread, lastOracleMarkSpreadPct.abs().toNumber() + lastOracleConfPct.toNumber());
|
|
198
228
|
}
|
|
199
|
-
const maxTargetSpread = maxSpread;
|
|
229
|
+
const maxTargetSpread = Math.max(maxSpread, lastOracleMarkSpreadPct.abs().toNumber());
|
|
200
230
|
const MAX_INVENTORY_SKEW = 5;
|
|
231
|
+
const inventoryScale = calculateInventoryScale(netBaseAssetAmount, baseAssetReserve, minBaseAssetReserve, maxBaseAssetReserve);
|
|
232
|
+
const inventorySpreadScale = Math.min(MAX_INVENTORY_SKEW, 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
|
+
}
|
|
201
239
|
const effectiveLeverage = calculateEffectiveLeverage(baseSpread, quoteAssetReserve, terminalQuoteAssetReserve, pegMultiplier, netBaseAssetAmount, markPrice, totalFeeMinusDistributions);
|
|
202
240
|
if (totalFeeMinusDistributions.gt(numericConstants_1.ZERO)) {
|
|
203
241
|
const spreadScale = Math.min(MAX_INVENTORY_SKEW, 1 + effectiveLeverage);
|
|
@@ -227,9 +265,8 @@ 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
271
|
const markPrice = calculatePrice(amm.baseAssetReserve, amm.quoteAssetReserve, amm.pegMultiplier);
|
|
235
272
|
const targetPrice = (oraclePriceData === null || oraclePriceData === void 0 ? void 0 : oraclePriceData.price) || markPrice;
|
|
@@ -241,40 +278,13 @@ function calculateSpread(amm, direction, oraclePriceData) {
|
|
|
241
278
|
const confIntervalPct = confInterval
|
|
242
279
|
.mul(numericConstants_1.BID_ASK_SPREAD_PRECISION)
|
|
243
280
|
.div(markPrice);
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
spread =
|
|
281
|
+
const [longSpread, shortSpread] = calculateSpreadBN(amm.baseSpread, targetMarkSpreadPct, confIntervalPct, amm.maxSpread, amm.quoteAssetReserve, amm.terminalQuoteAssetReserve, amm.pegMultiplier, amm.netBaseAssetAmount, markPrice, 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);
|
|
@@ -395,3 +405,15 @@ function calculateQuoteAssetAmountSwapped(quoteAssetReserves, pegMultiplier, swa
|
|
|
395
405
|
return quoteAssetAmount;
|
|
396
406
|
}
|
|
397
407
|
exports.calculateQuoteAssetAmountSwapped = calculateQuoteAssetAmountSwapped;
|
|
408
|
+
function calculateMaxBaseAssetAmountFillable(amm, orderDirection) {
|
|
409
|
+
const maxFillSize = amm.baseAssetReserve.div(new anchor_1.BN(amm.maxBaseAssetAmountRatio));
|
|
410
|
+
let maxBaseAssetAmountOnSide;
|
|
411
|
+
if ((0, types_1.isVariant)(orderDirection, 'long')) {
|
|
412
|
+
maxBaseAssetAmountOnSide = anchor_1.BN.max(numericConstants_1.ZERO, amm.baseAssetReserve.sub(amm.minBaseAssetReserve));
|
|
413
|
+
}
|
|
414
|
+
else {
|
|
415
|
+
maxBaseAssetAmountOnSide = anchor_1.BN.max(numericConstants_1.ZERO, amm.maxBaseAssetReserve.sub(amm.baseAssetReserve));
|
|
416
|
+
}
|
|
417
|
+
return (0, __1.standardizeBaseAssetAmount)(anchor_1.BN.min(maxFillSize, maxBaseAssetAmountOnSide), amm.baseAssetAmountStepSize);
|
|
418
|
+
}
|
|
419
|
+
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() +
|
package/lib/math/funding.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference types="bn.js" />
|
|
2
2
|
import { BN } from '@project-serum/anchor';
|
|
3
|
-
import {
|
|
3
|
+
import { PerpMarketAccount } from '../types';
|
|
4
4
|
import { OraclePriceData } from '../oracles/types';
|
|
5
5
|
/**
|
|
6
6
|
*
|
|
@@ -9,7 +9,7 @@ import { OraclePriceData } from '../oracles/types';
|
|
|
9
9
|
* @param periodAdjustment
|
|
10
10
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
11
11
|
*/
|
|
12
|
-
export declare function calculateAllEstimatedFundingRate(market:
|
|
12
|
+
export declare function calculateAllEstimatedFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN, BN]>;
|
|
13
13
|
/**
|
|
14
14
|
*
|
|
15
15
|
* @param market
|
|
@@ -18,7 +18,7 @@ export declare function calculateAllEstimatedFundingRate(market: MarketAccount,
|
|
|
18
18
|
* @param estimationMethod
|
|
19
19
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
20
20
|
*/
|
|
21
|
-
export declare function calculateEstimatedFundingRate(market:
|
|
21
|
+
export declare function calculateEstimatedFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, periodAdjustment?: BN, estimationMethod?: 'interpolated' | 'lowerbound' | 'capped'): Promise<BN>;
|
|
22
22
|
/**
|
|
23
23
|
*
|
|
24
24
|
* @param market
|
|
@@ -26,7 +26,7 @@ export declare function calculateEstimatedFundingRate(market: MarketAccount, ora
|
|
|
26
26
|
* @param periodAdjustment
|
|
27
27
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
28
28
|
*/
|
|
29
|
-
export declare function calculateLongShortFundingRate(market:
|
|
29
|
+
export declare function calculateLongShortFundingRate(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN]>;
|
|
30
30
|
/**
|
|
31
31
|
*
|
|
32
32
|
* @param market
|
|
@@ -34,10 +34,10 @@ export declare function calculateLongShortFundingRate(market: MarketAccount, ora
|
|
|
34
34
|
* @param periodAdjustment
|
|
35
35
|
* @returns Estimated funding rate. : Precision //TODO-PRECISION
|
|
36
36
|
*/
|
|
37
|
-
export declare function calculateLongShortFundingRateAndLiveTwaps(market:
|
|
37
|
+
export declare function calculateLongShortFundingRateAndLiveTwaps(market: PerpMarketAccount, oraclePriceData?: OraclePriceData, periodAdjustment?: BN): Promise<[BN, BN, BN, BN]>;
|
|
38
38
|
/**
|
|
39
39
|
*
|
|
40
40
|
* @param market
|
|
41
41
|
* @returns Estimated fee pool size
|
|
42
42
|
*/
|
|
43
|
-
export declare function calculateFundingPool(market:
|
|
43
|
+
export declare function calculateFundingPool(market: PerpMarketAccount): BN;
|