@d8x/perpetuals-sdk 0.1.3 → 0.1.4
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 +0 -1
- package/abi/Multicall.json +86 -0
- package/abi/central-park/IPerpetualManager.json +7 -2
- package/abi/central-park/LimitOrderBook.json +130 -2
- package/config/defaultConfig.json +2 -2
- package/dist/liquidatorTool.d.ts +2 -1
- package/dist/liquidatorTool.js +8 -2
- package/dist/marketData.d.ts +8 -4
- package/dist/marketData.js +178 -97
- package/dist/perpetualDataHandler.d.ts +3 -2
- package/dist/perpetualDataHandler.js +5 -8
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +1 -1
- package/src/liquidatorTool.ts +8 -2
- package/src/marketData.ts +225 -166
- package/src/perpetualDataHandler.ts +6 -7
- package/src/version.ts +1 -1
package/README.md
CHANGED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"constant": true,
|
|
4
|
+
"inputs": [],
|
|
5
|
+
"name": "getCurrentBlockTimestamp",
|
|
6
|
+
"outputs": [{ "name": "timestamp", "type": "uint256" }],
|
|
7
|
+
"payable": false,
|
|
8
|
+
"stateMutability": "view",
|
|
9
|
+
"type": "function"
|
|
10
|
+
},
|
|
11
|
+
{
|
|
12
|
+
"constant": false,
|
|
13
|
+
"inputs": [
|
|
14
|
+
{
|
|
15
|
+
"components": [
|
|
16
|
+
{ "name": "target", "type": "address" },
|
|
17
|
+
{ "name": "callData", "type": "bytes" }
|
|
18
|
+
],
|
|
19
|
+
"name": "calls",
|
|
20
|
+
"type": "tuple[]"
|
|
21
|
+
}
|
|
22
|
+
],
|
|
23
|
+
"name": "aggregate",
|
|
24
|
+
"outputs": [
|
|
25
|
+
{ "name": "blockNumber", "type": "uint256" },
|
|
26
|
+
{ "name": "returnData", "type": "bytes[]" }
|
|
27
|
+
],
|
|
28
|
+
"payable": false,
|
|
29
|
+
"stateMutability": "nonpayable",
|
|
30
|
+
"type": "function"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"constant": true,
|
|
34
|
+
"inputs": [],
|
|
35
|
+
"name": "getLastBlockHash",
|
|
36
|
+
"outputs": [{ "name": "blockHash", "type": "bytes32" }],
|
|
37
|
+
"payable": false,
|
|
38
|
+
"stateMutability": "view",
|
|
39
|
+
"type": "function"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"constant": true,
|
|
43
|
+
"inputs": [{ "name": "addr", "type": "address" }],
|
|
44
|
+
"name": "getEthBalance",
|
|
45
|
+
"outputs": [{ "name": "balance", "type": "uint256" }],
|
|
46
|
+
"payable": false,
|
|
47
|
+
"stateMutability": "view",
|
|
48
|
+
"type": "function"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"constant": true,
|
|
52
|
+
"inputs": [],
|
|
53
|
+
"name": "getCurrentBlockDifficulty",
|
|
54
|
+
"outputs": [{ "name": "difficulty", "type": "uint256" }],
|
|
55
|
+
"payable": false,
|
|
56
|
+
"stateMutability": "view",
|
|
57
|
+
"type": "function"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"constant": true,
|
|
61
|
+
"inputs": [],
|
|
62
|
+
"name": "getCurrentBlockGasLimit",
|
|
63
|
+
"outputs": [{ "name": "gaslimit", "type": "uint256" }],
|
|
64
|
+
"payable": false,
|
|
65
|
+
"stateMutability": "view",
|
|
66
|
+
"type": "function"
|
|
67
|
+
},
|
|
68
|
+
{
|
|
69
|
+
"constant": true,
|
|
70
|
+
"inputs": [],
|
|
71
|
+
"name": "getCurrentBlockCoinbase",
|
|
72
|
+
"outputs": [{ "name": "coinbase", "type": "address" }],
|
|
73
|
+
"payable": false,
|
|
74
|
+
"stateMutability": "view",
|
|
75
|
+
"type": "function"
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
"constant": true,
|
|
79
|
+
"inputs": [{ "name": "blockNumber", "type": "uint256" }],
|
|
80
|
+
"name": "getBlockHash",
|
|
81
|
+
"outputs": [{ "name": "blockHash", "type": "bytes32" }],
|
|
82
|
+
"payable": false,
|
|
83
|
+
"stateMutability": "view",
|
|
84
|
+
"type": "function"
|
|
85
|
+
}
|
|
86
|
+
]
|
|
@@ -3705,14 +3705,19 @@
|
|
|
3705
3705
|
"internalType": "address",
|
|
3706
3706
|
"name": "_traderAddress",
|
|
3707
3707
|
"type": "address"
|
|
3708
|
+
},
|
|
3709
|
+
{
|
|
3710
|
+
"internalType": "int128[2]",
|
|
3711
|
+
"name": "_fIndexPrice",
|
|
3712
|
+
"type": "int128[2]"
|
|
3708
3713
|
}
|
|
3709
3714
|
],
|
|
3710
3715
|
"name": "getTraderState",
|
|
3711
3716
|
"outputs": [
|
|
3712
3717
|
{
|
|
3713
|
-
"internalType": "int128[
|
|
3718
|
+
"internalType": "int128[11]",
|
|
3714
3719
|
"name": "",
|
|
3715
|
-
"type": "int128[
|
|
3720
|
+
"type": "int128[11]"
|
|
3716
3721
|
}
|
|
3717
3722
|
],
|
|
3718
3723
|
"stateMutability": "view",
|
|
@@ -225,11 +225,21 @@
|
|
|
225
225
|
"internalType": "bytes",
|
|
226
226
|
"name": "_signature",
|
|
227
227
|
"type": "bytes"
|
|
228
|
+
},
|
|
229
|
+
{
|
|
230
|
+
"internalType": "bytes[]",
|
|
231
|
+
"name": "_updateData",
|
|
232
|
+
"type": "bytes[]"
|
|
233
|
+
},
|
|
234
|
+
{
|
|
235
|
+
"internalType": "uint64[]",
|
|
236
|
+
"name": "_publishTimes",
|
|
237
|
+
"type": "uint64[]"
|
|
228
238
|
}
|
|
229
239
|
],
|
|
230
240
|
"name": "cancelOrder",
|
|
231
241
|
"outputs": [],
|
|
232
|
-
"stateMutability": "
|
|
242
|
+
"stateMutability": "payable",
|
|
233
243
|
"type": "function"
|
|
234
244
|
},
|
|
235
245
|
{
|
|
@@ -284,6 +294,34 @@
|
|
|
284
294
|
"stateMutability": "payable",
|
|
285
295
|
"type": "function"
|
|
286
296
|
},
|
|
297
|
+
{
|
|
298
|
+
"inputs": [
|
|
299
|
+
{
|
|
300
|
+
"internalType": "bytes32[]",
|
|
301
|
+
"name": "_digests",
|
|
302
|
+
"type": "bytes32[]"
|
|
303
|
+
},
|
|
304
|
+
{
|
|
305
|
+
"internalType": "address",
|
|
306
|
+
"name": "_referrerAddr",
|
|
307
|
+
"type": "address"
|
|
308
|
+
},
|
|
309
|
+
{
|
|
310
|
+
"internalType": "bytes[]",
|
|
311
|
+
"name": "_updateData",
|
|
312
|
+
"type": "bytes[]"
|
|
313
|
+
},
|
|
314
|
+
{
|
|
315
|
+
"internalType": "uint64[]",
|
|
316
|
+
"name": "_publishTimes",
|
|
317
|
+
"type": "uint64[]"
|
|
318
|
+
}
|
|
319
|
+
],
|
|
320
|
+
"name": "executeOrders",
|
|
321
|
+
"outputs": [],
|
|
322
|
+
"stateMutability": "payable",
|
|
323
|
+
"type": "function"
|
|
324
|
+
},
|
|
287
325
|
{
|
|
288
326
|
"inputs": [
|
|
289
327
|
{
|
|
@@ -872,7 +910,7 @@
|
|
|
872
910
|
},
|
|
873
911
|
{
|
|
874
912
|
"internalType": "bytes",
|
|
875
|
-
"name": "
|
|
913
|
+
"name": "_signature",
|
|
876
914
|
"type": "bytes"
|
|
877
915
|
}
|
|
878
916
|
],
|
|
@@ -881,6 +919,96 @@
|
|
|
881
919
|
"stateMutability": "nonpayable",
|
|
882
920
|
"type": "function"
|
|
883
921
|
},
|
|
922
|
+
{
|
|
923
|
+
"inputs": [
|
|
924
|
+
{
|
|
925
|
+
"components": [
|
|
926
|
+
{
|
|
927
|
+
"internalType": "uint32",
|
|
928
|
+
"name": "flags",
|
|
929
|
+
"type": "uint32"
|
|
930
|
+
},
|
|
931
|
+
{
|
|
932
|
+
"internalType": "uint24",
|
|
933
|
+
"name": "iPerpetualId",
|
|
934
|
+
"type": "uint24"
|
|
935
|
+
},
|
|
936
|
+
{
|
|
937
|
+
"internalType": "uint16",
|
|
938
|
+
"name": "brokerFeeTbps",
|
|
939
|
+
"type": "uint16"
|
|
940
|
+
},
|
|
941
|
+
{
|
|
942
|
+
"internalType": "address",
|
|
943
|
+
"name": "traderAddr",
|
|
944
|
+
"type": "address"
|
|
945
|
+
},
|
|
946
|
+
{
|
|
947
|
+
"internalType": "address",
|
|
948
|
+
"name": "brokerAddr",
|
|
949
|
+
"type": "address"
|
|
950
|
+
},
|
|
951
|
+
{
|
|
952
|
+
"internalType": "address",
|
|
953
|
+
"name": "referrerAddr",
|
|
954
|
+
"type": "address"
|
|
955
|
+
},
|
|
956
|
+
{
|
|
957
|
+
"internalType": "bytes",
|
|
958
|
+
"name": "brokerSignature",
|
|
959
|
+
"type": "bytes"
|
|
960
|
+
},
|
|
961
|
+
{
|
|
962
|
+
"internalType": "int128",
|
|
963
|
+
"name": "fAmount",
|
|
964
|
+
"type": "int128"
|
|
965
|
+
},
|
|
966
|
+
{
|
|
967
|
+
"internalType": "int128",
|
|
968
|
+
"name": "fLimitPrice",
|
|
969
|
+
"type": "int128"
|
|
970
|
+
},
|
|
971
|
+
{
|
|
972
|
+
"internalType": "int128",
|
|
973
|
+
"name": "fTriggerPrice",
|
|
974
|
+
"type": "int128"
|
|
975
|
+
},
|
|
976
|
+
{
|
|
977
|
+
"internalType": "int128",
|
|
978
|
+
"name": "fLeverage",
|
|
979
|
+
"type": "int128"
|
|
980
|
+
},
|
|
981
|
+
{
|
|
982
|
+
"internalType": "uint64",
|
|
983
|
+
"name": "iDeadline",
|
|
984
|
+
"type": "uint64"
|
|
985
|
+
},
|
|
986
|
+
{
|
|
987
|
+
"internalType": "uint64",
|
|
988
|
+
"name": "createdTimestamp",
|
|
989
|
+
"type": "uint64"
|
|
990
|
+
},
|
|
991
|
+
{
|
|
992
|
+
"internalType": "uint64",
|
|
993
|
+
"name": "submittedBlock",
|
|
994
|
+
"type": "uint64"
|
|
995
|
+
}
|
|
996
|
+
],
|
|
997
|
+
"internalType": "struct IPerpetualOrder.Order[]",
|
|
998
|
+
"name": "_orders",
|
|
999
|
+
"type": "tuple[]"
|
|
1000
|
+
},
|
|
1001
|
+
{
|
|
1002
|
+
"internalType": "bytes[]",
|
|
1003
|
+
"name": "_signatures",
|
|
1004
|
+
"type": "bytes[]"
|
|
1005
|
+
}
|
|
1006
|
+
],
|
|
1007
|
+
"name": "postOrders",
|
|
1008
|
+
"outputs": [],
|
|
1009
|
+
"stateMutability": "nonpayable",
|
|
1010
|
+
"type": "function"
|
|
1011
|
+
},
|
|
884
1012
|
{
|
|
885
1013
|
"inputs": [
|
|
886
1014
|
{
|
|
@@ -23,12 +23,12 @@
|
|
|
23
23
|
},
|
|
24
24
|
{
|
|
25
25
|
"name": "central-park",
|
|
26
|
-
"version":
|
|
26
|
+
"version": 4.0,
|
|
27
27
|
"proxyAddr": "0x4F1C7EE9E881Cc735f39Ca36c649ba9E930B4E4e",
|
|
28
28
|
"nodeURL": "https://polygon-mumbai.blockpi.network/v1/rpc/public",
|
|
29
29
|
"priceFeedConfigNetwork": "testnet",
|
|
30
30
|
"proxyABILocation": "../abi/central-park/IPerpetualManager.json",
|
|
31
|
-
"limitOrderBookFactoryAddr": "
|
|
31
|
+
"limitOrderBookFactoryAddr": "0xeC71488Ca228506C93CDaEd4C00Ef65ee7E6A4F1",
|
|
32
32
|
"limitOrderBookFactoryABILocation": "../abi/central-park/LimitOrderBookFactory.json",
|
|
33
33
|
"limitOrderBookABILocation": "../abi/central-park/LimitOrderBook.json",
|
|
34
34
|
"symbolListLocation": "../config/symbolList.json"
|
package/dist/liquidatorTool.d.ts
CHANGED
|
@@ -59,6 +59,7 @@ export default class LiquidatorTool extends WriteAccessHandler {
|
|
|
59
59
|
* If not, the position can be liquidated.
|
|
60
60
|
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
61
61
|
* @param {string} traderAddr Address of the trader whose position you want to assess.
|
|
62
|
+
* @param {[number,number]} indexPrices optional, index price S2/S3 for which we test
|
|
62
63
|
* @example
|
|
63
64
|
* import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
64
65
|
* async function main() {
|
|
@@ -78,7 +79,7 @@ export default class LiquidatorTool extends WriteAccessHandler {
|
|
|
78
79
|
* @returns {boolean} True if the trader is maintenance margin safe in the perpetual.
|
|
79
80
|
* False means that the trader's position can be liquidated.
|
|
80
81
|
*/
|
|
81
|
-
isMaintenanceMarginSafe(symbol: string, traderAddr: string): Promise<boolean>;
|
|
82
|
+
isMaintenanceMarginSafe(symbol: string, traderAddr: string, indexPrices?: [number, number]): Promise<boolean>;
|
|
82
83
|
/**
|
|
83
84
|
*
|
|
84
85
|
* @param perpetualId Perpetual id.
|
package/dist/liquidatorTool.js
CHANGED
|
@@ -92,6 +92,7 @@ class LiquidatorTool extends writeAccessHandler_1.default {
|
|
|
92
92
|
* If not, the position can be liquidated.
|
|
93
93
|
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
94
94
|
* @param {string} traderAddr Address of the trader whose position you want to assess.
|
|
95
|
+
* @param {[number,number]} indexPrices optional, index price S2/S3 for which we test
|
|
95
96
|
* @example
|
|
96
97
|
* import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
97
98
|
* async function main() {
|
|
@@ -111,14 +112,19 @@ class LiquidatorTool extends writeAccessHandler_1.default {
|
|
|
111
112
|
* @returns {boolean} True if the trader is maintenance margin safe in the perpetual.
|
|
112
113
|
* False means that the trader's position can be liquidated.
|
|
113
114
|
*/
|
|
114
|
-
isMaintenanceMarginSafe(symbol, traderAddr) {
|
|
115
|
+
isMaintenanceMarginSafe(symbol, traderAddr, indexPrices) {
|
|
115
116
|
return __awaiter(this, void 0, void 0, function* () {
|
|
116
117
|
if (this.proxyContract == null) {
|
|
117
118
|
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
118
119
|
}
|
|
119
120
|
const idx_notional = 4;
|
|
120
121
|
let perpID = LiquidatorTool.symbolToPerpetualId(symbol, this.symbolToPerpStaticInfo);
|
|
121
|
-
|
|
122
|
+
if (indexPrices == undefined) {
|
|
123
|
+
// fetch from API
|
|
124
|
+
let obj = yield this.priceFeedGetter.fetchPricesForPerpetual(symbol);
|
|
125
|
+
indexPrices = [obj.idxPrices[0], obj.idxPrices[1]];
|
|
126
|
+
}
|
|
127
|
+
let traderState = yield this.proxyContract.getTraderState(perpID, traderAddr, indexPrices);
|
|
122
128
|
if (traderState[idx_notional] == 0) {
|
|
123
129
|
// trader does not have open position
|
|
124
130
|
return false;
|
package/dist/marketData.d.ts
CHANGED
|
@@ -136,10 +136,14 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
136
136
|
* Estimates what the position risk will be if a given order is executed.
|
|
137
137
|
* @param traderAddr Address of trader
|
|
138
138
|
* @param order Order to be submitted
|
|
139
|
-
* @param
|
|
139
|
+
* @param account Position risk before trade
|
|
140
|
+
* @param indexPriceInfo Index prices and market status (open/closed)
|
|
140
141
|
* @returns {MarginAccount} Position risk after trade
|
|
141
142
|
*/
|
|
142
|
-
positionRiskOnTrade(traderAddr: string, order: Order,
|
|
143
|
+
positionRiskOnTrade(traderAddr: string, order: Order, account?: MarginAccount, indexPriceInfo?: [number, number, boolean, boolean]): Promise<{
|
|
144
|
+
newPositionRisk: MarginAccount;
|
|
145
|
+
orderCost: number;
|
|
146
|
+
}>;
|
|
143
147
|
/**
|
|
144
148
|
* Estimates what the position risk will be if given amount of collateral is added/removed from the account.
|
|
145
149
|
* @param traderAddr Address of trader
|
|
@@ -147,8 +151,8 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
147
151
|
* @param currentPositionRisk Position risk before
|
|
148
152
|
* @returns {MarginAccount} Position risk after
|
|
149
153
|
*/
|
|
150
|
-
positionRiskOnCollateralAction(deltaCollateral: number,
|
|
151
|
-
protected static
|
|
154
|
+
positionRiskOnCollateralAction(deltaCollateral: number, account: MarginAccount, indexPriceInfo?: [number, number, boolean, boolean]): Promise<MarginAccount>;
|
|
155
|
+
protected static _getLiquidationParams(symbol: string, lockedInQC: number, signedPositionBC: number, marginCashCC: number, markPrice: number, collToQuoteConversion: number, symbolToPerpStaticInfo: Map<string, PerpetualStaticInfo>): [number, number | undefined, number];
|
|
152
156
|
/**
|
|
153
157
|
* Gets the wallet balance in the collateral currency corresponding to a given perpetual symbol.
|
|
154
158
|
* @param address Address to check
|