@d8x/perpetuals-sdk 0.0.7 → 0.0.9

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.
@@ -19,11 +19,28 @@ const ethers_1 = require("ethers");
19
19
  const accountTrade_1 = __importDefault(require("./accountTrade"));
20
20
  /**
21
21
  * Functions for brokers to determine fees, deposit lots, and sign-up traders.
22
+ * This class requires a private key and executes smart-contract interactions that
23
+ * require gas-payments.
22
24
  */
23
25
  class BrokerTool extends writeAccessHandler_1.default {
24
26
  /**
25
27
  * Constructor
26
- * @param {NodeSDKConfig} config Configuration object.
28
+ * @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.
29
+ * readSDKConfig.
30
+ * @example
31
+ * import { BrokerTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
32
+ * async function main() {
33
+ * console.log(BrokerTool);
34
+ * // load configuration for testnet
35
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
36
+ * // BrokerTool (authentication required, PK is an environment variable with a private key)
37
+ * const pk: string = <string>process.env.PK;
38
+ * let brokTool = new BrokerTool(config, pk);
39
+ * // Create a proxy instance to access the blockchain
40
+ * await brokTool.createProxyInstance();
41
+ * }
42
+ * main();
43
+ *
27
44
  * @param {string} privateKey Private key of a broker.
28
45
  */
29
46
  constructor(config, privateKey) {
@@ -32,8 +49,23 @@ class BrokerTool extends writeAccessHandler_1.default {
32
49
  // Fee getters
33
50
  /**
34
51
  * Determine the exchange fee based on lots, traded volume, and D8X balance of this broker.
35
- * This is the final exchange fee paid by the broker.
52
+ * This is the final exchange fee that this broker can offer to traders that trade through him.
36
53
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
54
+ * @example
55
+ * import { BrokerTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
56
+ * async function main() {
57
+ * console.log(BrokerTool);
58
+ * // Setup (authentication required, PK is an environment variable with a private key)
59
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
60
+ * const pk: string = <string>process.env.PK;
61
+ * let brokTool = new BrokerTool(config, pk);
62
+ * await brokTool.createProxyInstance();
63
+ * // get broker induced fee
64
+ * let brokFee = await brokTool.getBrokerInducedFee("MATIC");
65
+ * console.log(brokFee);
66
+ * }
67
+ * main();
68
+ *
37
69
  * @returns {number} Exchange fee for this broker, in decimals (i.e. 0.1% is 0.001)
38
70
  */
39
71
  getBrokerInducedFee(poolSymbolName) {
@@ -48,10 +80,25 @@ class BrokerTool extends writeAccessHandler_1.default {
48
80
  }
49
81
  /**
50
82
  * Determine the exchange fee based on lots purchased by this broker.
51
- * The final exchange fee paid by the broker is equal to
83
+ * The final exchange fee that this broker can offer to traders that trade through him is equal to
52
84
  * maximum(brokerTool.getFeeForBrokerDesignation(poolSymbolName), brokerTool.getFeeForBrokerVolume(poolSymbolName), brokerTool.getFeeForBrokerStake())
53
85
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
54
86
  * @param {number=} lots Optional, designation to use if different from this broker's.
87
+ * @example
88
+ * import { BrokerTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
89
+ * async function main() {
90
+ * console.log(BrokerTool);
91
+ * // Setup (authentication required, PK is an environment variable with a private key)
92
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
93
+ * const pk: string = <string>process.env.PK;
94
+ * let brokTool = new BrokerTool(config, pk);
95
+ * await brokTool.createProxyInstance();
96
+ * // get broker fee induced by lots
97
+ * let brokFeeLots = await brokTool.getFeeForBrokerDesignation("MATIC");
98
+ * console.log(brokFeeLots);
99
+ * }
100
+ * main();
101
+ *
55
102
  * @returns {number} Fee based solely on this broker's designation, in decimals (i.e. 0.1% is 0.001).
56
103
  */
57
104
  getFeeForBrokerDesignation(poolSymbolName, lots) {
@@ -73,9 +120,24 @@ class BrokerTool extends writeAccessHandler_1.default {
73
120
  }
74
121
  /**
75
122
  * Determine the exchange fee based on volume traded under this broker.
76
- * The final exchange fee paid by the broker is equal to
123
+ * The final exchange fee that this broker can offer to traders that trade through him is equal to
77
124
  * maximum(brokerTool.getFeeForBrokerDesignation(poolSymbolName), brokerTool.getFeeForBrokerVolume(poolSymbolName), brokerTool.getFeeForBrokerStake())
78
125
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
126
+ * @example
127
+ * import { BrokerTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
128
+ * async function main() {
129
+ * console.log(BrokerTool);
130
+ * // Setup (authentication required, PK is an environment variable with a private key)
131
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
132
+ * const pk: string = <string>process.env.PK;
133
+ * let brokTool = new BrokerTool(config, pk);
134
+ * await brokTool.createProxyInstance();
135
+ * // get broker fee induced by volume
136
+ * let brokFeeVol = await brokTool.getFeeForBrokerVolume("MATIC");
137
+ * console.log(brokFeeVol);
138
+ * }
139
+ * main();
140
+ *
79
141
  * @returns {number} Fee based solely on a broker's traded volume in the corresponding pool, in decimals (i.e. 0.1% is 0.001).
80
142
  */
81
143
  getFeeForBrokerVolume(poolSymbolName) {
@@ -90,9 +152,24 @@ class BrokerTool extends writeAccessHandler_1.default {
90
152
  }
91
153
  /**
92
154
  * Determine the exchange fee based on the current D8X balance in a broker's wallet.
93
- * The final exchange fee paid by the broker is equal to
155
+ * The final exchange fee that this broker can offer to traders that trade through him is equal to
94
156
  * maximum(brokerTool.getFeeForBrokerDesignation(symbol, lots), brokerTool.getFeeForBrokerVolume(symbol), brokerTool.getFeeForBrokerStake)
95
157
  * @param {string=} brokerAddr Address of the broker in question, if different from the one calling this function.
158
+ * @example
159
+ * import { BrokerTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
160
+ * async function main() {
161
+ * console.log(BrokerTool);
162
+ * // Setup (authentication required, PK is an environment variable with a private key)
163
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
164
+ * const pk: string = <string>process.env.PK;
165
+ * let brokTool = new BrokerTool(config, pk);
166
+ * await brokTool.createProxyInstance();
167
+ * // get broker fee induced by staked d8x
168
+ * let brokFeeStake = await brokTool.getFeeForBrokerStake("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B");
169
+ * console.log(brokFeeStake);
170
+ * }
171
+ * main();
172
+ *
96
173
  * @returns {number} Fee based solely on a broker's D8X balance, in decimals (i.e. 0.1% is 0.001).
97
174
  */
98
175
  getFeeForBrokerStake(brokerAddr) {
@@ -114,7 +191,30 @@ class BrokerTool extends writeAccessHandler_1.default {
114
191
  * Use this, for instance, to verify that the fee to be charged for a given order is as expected,
115
192
  * before and after signing it with brokerTool.signOrder.
116
193
  * This fee is equal or lower than the broker induced fee, provided the order is properly signed.
117
- * @param {Order} order Order for which to determine the exchange fee. Not necessarily signed by this broker.
194
+ * @param {Order} order Order structure. As a minimum the structure needs to
195
+ * specify symbol, side, type and quantity.
196
+ * @example
197
+ * import { BrokerTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
198
+ * async function main() {
199
+ * console.log(BrokerTool);
200
+ * // Setup (authentication required, PK is an environment variable with a private key)
201
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
202
+ * const pk: string = <string>process.env.PK;
203
+ * let brokTool = new BrokerTool(config, pk);
204
+ * await brokTool.createProxyInstance();
205
+ * // get exchange fee based on an order and trader
206
+ * let order = {symbol: "ETH-USD-MATIC",
207
+ * side: "BUY",
208
+ * type: "MARKET",
209
+ * quantity: 1,
210
+ * timestamp: Date.now()
211
+ * };
212
+ * let exchFee = await brokTool.determineExchangeFee(order,
213
+ * "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B");
214
+ * console.log(exchFee);
215
+ * }
216
+ * main();
217
+ *
118
218
  * @param {string} traderAddr Address of the trader for whom to determine the fee.
119
219
  * @returns {number} Fee in decimals (i.e. 0.1% is 0.001).
120
220
  */
@@ -133,6 +233,21 @@ class BrokerTool extends writeAccessHandler_1.default {
133
233
  * Exponentially weighted EMA of the total trading volume of all trades performed under this broker.
134
234
  * The weights are chosen so that in average this coincides with the 30 day volume.
135
235
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
236
+ * @example
237
+ * import { BrokerTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
238
+ * async function main() {
239
+ * console.log(BrokerTool);
240
+ * // Setup (authentication required, PK is an environment variable with a private key)
241
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
242
+ * const pk: string = <string>process.env.PK;
243
+ * let brokTool = new BrokerTool(config, pk);
244
+ * await brokTool.createProxyInstance();
245
+ * // get 30 day volume for broker
246
+ * let brokVolume = await brokTool.getCurrentBrokerVolume("MATIC");
247
+ * console.log(brokVolume);
248
+ * }
249
+ * main();
250
+ *
136
251
  * @returns {number} Current trading volume for this broker, in USD.
137
252
  */
138
253
  getCurrentBrokerVolume(poolSymbolName) {
@@ -150,6 +265,21 @@ class BrokerTool extends writeAccessHandler_1.default {
150
265
  * Total amount of collateral currency a broker has to deposit into the default fund to purchase one lot.
151
266
  * This is equivalent to the price of a lot expressed in a given pool's currency (e.g. MATIC, USDC, etc).
152
267
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
268
+ * @example
269
+ * import { BrokerTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
270
+ * async function main() {
271
+ * console.log(BrokerTool);
272
+ * // Setup (authentication required, PK is an environment variable with a private key)
273
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
274
+ * const pk: string = <string>process.env.PK;
275
+ * let brokTool = new BrokerTool(config, pk);
276
+ * await brokTool.createProxyInstance();
277
+ * // get lot price
278
+ * let brokLotSize = await brokTool.getLotSize("MATIC");
279
+ * console.log(brokLotSize);
280
+ * }
281
+ * main();
282
+ *
153
283
  * @returns {number} Broker lot size in a given pool's currency, e.g. in MATIC for poolSymbolName MATIC.
154
284
  */
155
285
  getLotSize(poolSymbolName) {
@@ -167,6 +297,21 @@ class BrokerTool extends writeAccessHandler_1.default {
167
297
  * Provides information on how many lots a broker purchased for a given pool.
168
298
  * This is relevant to determine the broker's fee tier.
169
299
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
300
+ * @example
301
+ * import { BrokerTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
302
+ * async function main() {
303
+ * console.log(BrokerTool);
304
+ * // Setup (authentication required, PK is an environment variable with a private key)
305
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
306
+ * const pk: string = <string>process.env.PK;
307
+ * let brokTool = new BrokerTool(config, pk);
308
+ * await brokTool.createProxyInstance();
309
+ * // get broker designation
310
+ * let brokDesignation = await brokTool.getBrokerDesignation("MATIC");
311
+ * console.log(brokDesignation);
312
+ * }
313
+ * main();
314
+ *
170
315
  * @returns {number} Number of lots purchased by this broker.
171
316
  */
172
317
  getBrokerDesignation(poolSymbolName) {
@@ -183,7 +328,22 @@ class BrokerTool extends writeAccessHandler_1.default {
183
328
  * Deposit lots to the default fund of a given pool.
184
329
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
185
330
  * @param {number} lots Number of lots to deposit into this pool.
186
- * @returns {ethers.providers.TransactionResponse} Transaction object.
331
+ * @example
332
+ * import { BrokerTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
333
+ * async function main() {
334
+ * console.log(BrokerTool);
335
+ * // Setup (authentication required, PK is an environment variable with a private key)
336
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
337
+ * const pk: string = <string>process.env.PK;
338
+ * let brokTool = new BrokerTool(config, pk);
339
+ * await brokTool.createProxyInstance();
340
+ * // deposit to default fund
341
+ * let respDeposit = await brokTool.brokerDepositToDefaultFund("MATIC",1);
342
+ * console.log(respDeposit);
343
+ * }
344
+ * main();
345
+ *
346
+ * @returns {ethers.ContractTransaction} ContractTransaction object.
187
347
  */
188
348
  brokerDepositToDefaultFund(poolSymbolName, lots) {
189
349
  return __awaiter(this, void 0, void 0, function* () {
@@ -197,11 +357,35 @@ class BrokerTool extends writeAccessHandler_1.default {
197
357
  }
198
358
  // Signatures
199
359
  /**
200
- * Adds this broker's signature to an order so that it can be submitted by an approved trader.
360
+ * Adds this broker's signature to an order. An order signed by a broker is considered
361
+ * to be routed through this broker and benefits from the broker's fee conditions.
201
362
  * @param {Order} order Order to sign.
202
363
  * @param {string} traderAddr Address of trader submitting the order.
203
- * @param {number} feeDecimals Fee that this broker is approving for the trader.
204
- * @param {number} deadline Deadline for the order to be executed.
364
+ * @param {number} feeDecimals Fee that this broker imposes on this order.
365
+ * The fee is sent to the broker's wallet. Fee should be specified in decimals, e.g., 0.0001 equals 1bps.
366
+ * @param {number} deadline Deadline for the order to be executed. Specify deadline as a unix timestamp
367
+ * @example
368
+ * import { BrokerTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
369
+ * async function main() {
370
+ * console.log(BrokerTool);
371
+ * // Setup (authentication required, PK is an environment variable with a private key)
372
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
373
+ * const pk: string = <string>process.env.PK;
374
+ * let brokTool = new BrokerTool(config, pk);
375
+ * await brokTool.createProxyInstance();
376
+ * // sign order
377
+ * let order = {symbol: "ETH-USD-MATIC",
378
+ * side: "BUY",
379
+ * type: "MARKET",
380
+ * quantity: 1,
381
+ * timestamp: Date.now()
382
+ * };
383
+ * let signedOrder = await brokTool.signOrder(order, "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
384
+ * 0.0001, 1669723339);
385
+ * console.log(signedOrder);
386
+ * }
387
+ * main();
388
+ *
205
389
  * @returns {Order} An order signed by this broker, which can be submitted directly with AccountTrade.order.
206
390
  */
207
391
  signOrder(order, traderAddr, brokerFee, deadline) {
@@ -253,7 +437,9 @@ class BrokerTool extends writeAccessHandler_1.default {
253
437
  }
254
438
  // Transfer ownership
255
439
  /**
256
- * Transfer ownership of a broker's status to a new wallet.
440
+ * Transfer ownership of a broker's status to a new wallet. This function transfers the values related to
441
+ * (i) trading volume and (ii) deposited lots to newAddress. The broker needs in addition to manually transfer
442
+ * his D8X holdings to newAddress. Until this transfer is completed, the broker will not have his current designation reflected at newAddress.
257
443
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
258
444
  * @param {string} newAddress The address this broker wants to use from now on.
259
445
  * @returns {ethers.providers.TransactionResponse} ethers transaction object
package/dist/index.d.ts CHANGED
@@ -5,7 +5,8 @@ import MarketData from "./marketData";
5
5
  import OrderReferrerTool from "./orderReferrerTool";
6
6
  import PerpetualDataHandler from "./perpetualDataHandler";
7
7
  import WriteAccessHandler from "./writeAccessHandler";
8
+ import LiquidatorTool from "./liquidatorTool";
8
9
  export * from "./nodeSDKTypes";
9
10
  export * from "./utils";
10
11
  export * from "./d8XMath";
11
- export { AccountTrade, BrokerTool, LiquidityProviderTool, MarketData, OrderReferrerTool, PerpetualDataHandler, WriteAccessHandler, };
12
+ export { AccountTrade, BrokerTool, LiquidityProviderTool, MarketData, OrderReferrerTool, PerpetualDataHandler, WriteAccessHandler, LiquidatorTool, };
package/dist/index.js CHANGED
@@ -17,7 +17,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
17
17
  return (mod && mod.__esModule) ? mod : { "default": mod };
18
18
  };
19
19
  Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.WriteAccessHandler = exports.PerpetualDataHandler = exports.OrderReferrerTool = exports.MarketData = exports.LiquidityProviderTool = exports.BrokerTool = exports.AccountTrade = void 0;
20
+ exports.LiquidatorTool = exports.WriteAccessHandler = exports.PerpetualDataHandler = exports.OrderReferrerTool = exports.MarketData = exports.LiquidityProviderTool = exports.BrokerTool = exports.AccountTrade = void 0;
21
21
  const accountTrade_1 = __importDefault(require("./accountTrade"));
22
22
  exports.AccountTrade = accountTrade_1.default;
23
23
  const brokerTool_1 = __importDefault(require("./brokerTool"));
@@ -32,6 +32,8 @@ const perpetualDataHandler_1 = __importDefault(require("./perpetualDataHandler")
32
32
  exports.PerpetualDataHandler = perpetualDataHandler_1.default;
33
33
  const writeAccessHandler_1 = __importDefault(require("./writeAccessHandler"));
34
34
  exports.WriteAccessHandler = writeAccessHandler_1.default;
35
+ const liquidatorTool_1 = __importDefault(require("./liquidatorTool"));
36
+ exports.LiquidatorTool = liquidatorTool_1.default;
35
37
  // import {
36
38
  // NodeSDKConfig,
37
39
  // MarginAccount,
@@ -1,12 +1,28 @@
1
1
  import WriteAccessHandler from "./writeAccessHandler";
2
2
  import { NodeSDKConfig } from "./nodeSDKTypes";
3
3
  /**
4
- * Methods to liquidate traders.
4
+ * Functions to liquidate traders. This class requires a private key
5
+ * and executes smart-contract interactions that require gas-payments.
5
6
  */
6
7
  export default class LiquidatorTool extends WriteAccessHandler {
7
8
  /**
8
9
  * Constructs a LiquidatorTool instance for a given configuration and private key.
9
- * @param {NodeSDKConfig} config Configuration object.
10
+ * @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.
11
+ * readSDKConfig.
12
+ * @example
13
+ * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
14
+ * async function main() {
15
+ * console.log(LiquidatorTool);
16
+ * // load configuration for testnet
17
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
18
+ * // LiquidatorTool (authentication required, PK is an environment variable with a private key)
19
+ * const pk: string = <string>process.env.PK;
20
+ * let lqudtrTool = new LiquidatorTool(config, pk);
21
+ * // Create a proxy instance to access the blockchain
22
+ * await lqudtrTool.createProxyInstance();
23
+ * }
24
+ * main();
25
+ *
10
26
  * @param {string} privateKey Private key of account that liquidates.
11
27
  */
12
28
  constructor(config: NodeSDKConfig, privateKey: string);
@@ -16,14 +32,48 @@ export default class LiquidatorTool extends WriteAccessHandler {
16
32
  * @param {string} traderAddr Address of the trader to be liquidated.
17
33
  * @param {string=} liquidatorAddr Address to be credited if the liquidation succeeds.
18
34
  * Defaults to the wallet used to execute the liquidation.
35
+ * @example
36
+ * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
37
+ * async function main() {
38
+ * console.log(LiquidatorTool);
39
+ * // Setup (authentication required, PK is an environment variable with a private key)
40
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
41
+ * const pk: string = <string>process.env.PK;
42
+ * let lqudtrTool = new LiquidatorTool(config, pk);
43
+ * await lqudtrTool.createProxyInstance();
44
+ * // liquidate trader
45
+ * let liqAmount = await lqudtrTool.liquidateTrader("ETH-USD-MATIC",
46
+ * "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B");
47
+ * console.log(liqAmount);
48
+ * }
49
+ * main();
50
+ *
19
51
  * @returns {number} Liquidated amount.
20
52
  */
21
53
  liquidateTrader(symbol: string, traderAddr: string, liquidatorAddr?: string): Promise<number>;
22
54
  /**
23
- * Check if a trader is maintenance margin safe - if not, it can be liquidated.
55
+ * Check if the collateral of a trader is above the maintenance margin ("maintenance margin safe").
56
+ * If not, the position can be liquidated.
24
57
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
25
- * @param {string} traderAddr Address of the trader whose position we want to assess.
58
+ * @param {string} traderAddr Address of the trader whose position you want to assess.
59
+ * @example
60
+ * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
61
+ * async function main() {
62
+ * console.log(LiquidatorTool);
63
+ * // Setup (authentication required, PK is an environment variable with a private key)
64
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
65
+ * const pk: string = <string>process.env.PK;
66
+ * let lqudtrTool = new LiquidatorTool(config, pk);
67
+ * await lqudtrTool.createProxyInstance();
68
+ * // check if trader can be liquidated
69
+ * let safe = await lqudtrTool.isMaintenanceMarginSafe("ETH-USD-MATIC",
70
+ * "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B");
71
+ * console.log(safe);
72
+ * }
73
+ * main();
74
+ *
26
75
  * @returns {boolean} True if the trader is maintenance margin safe in the perpetual.
76
+ * False means that the trader's position can be liquidated.
27
77
  */
28
78
  isMaintenanceMarginSafe(symbol: string, traderAddr: string): Promise<boolean>;
29
79
  /**
@@ -38,6 +88,21 @@ export default class LiquidatorTool extends WriteAccessHandler {
38
88
  /**
39
89
  * Total number of active accounts for this symbol, i.e. accounts with positions that are currently open.
40
90
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
91
+ * @example
92
+ * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
93
+ * async function main() {
94
+ * console.log(LiquidatorTool);
95
+ * // Setup (authentication required, PK is an environment variable with a private key)
96
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
97
+ * const pk: string = <string>process.env.PK;
98
+ * let lqudtrTool = new LiquidatorTool(config, pk);
99
+ * await lqudtrTool.createProxyInstance();
100
+ * // get number of active accounts
101
+ * let accounts = await lqudtrTool.countActivePerpAccounts("ETH-USD-MATIC");
102
+ * console.log(accounts);
103
+ * }
104
+ * main();
105
+ *
41
106
  * @returns {number} Number of active accounts.
42
107
  */
43
108
  countActivePerpAccounts(symbol: string): Promise<number>;
@@ -45,13 +110,43 @@ export default class LiquidatorTool extends WriteAccessHandler {
45
110
  * Get addresses of active accounts by chunks.
46
111
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
47
112
  * @param {number} from From which account we start counting (0-indexed).
48
- * @param {number} to Until which account we count.
49
- * @returns {string[]} Array of addresses.
113
+ * @param {number} to Until which account we count, non inclusive.
114
+ * @example
115
+ * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
116
+ * async function main() {
117
+ * console.log(LiquidatorTool);
118
+ * // Setup (authentication required, PK is an environment variable with a private key)
119
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
120
+ * const pk: string = <string>process.env.PK;
121
+ * let lqudtrTool = new LiquidatorTool(config, pk);
122
+ * await lqudtrTool.createProxyInstance();
123
+ * // get all active accounts in chunks
124
+ * let accounts = await lqudtrTool.getActiveAccountsByChunks("ETH-USD-MATIC", 0, 4);
125
+ * console.log(accounts);
126
+ * }
127
+ * main();
128
+ *
129
+ * @returns {string[]} Array of addresses at locations 'from', 'from'+1 ,..., 'to'-1.
50
130
  */
51
131
  getActiveAccountsByChunks(symbol: string, from: number, to: number): Promise<string[]>;
52
132
  /**
53
133
  * Addresses for all the active accounts in this perpetual symbol.
54
134
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
135
+ * @example
136
+ * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
137
+ * async function main() {
138
+ * console.log(LiquidatorTool);
139
+ * // Setup (authentication required, PK is an environment variable with a private key)
140
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
141
+ * const pk: string = <string>process.env.PK;
142
+ * let lqudtrTool = new LiquidatorTool(config, pk);
143
+ * await lqudtrTool.createProxyInstance();
144
+ * // get all active accounts
145
+ * let accounts = await lqudtrTool.getAllActiveAccounts("ETH-USD-MATIC");
146
+ * console.log(accounts);
147
+ * }
148
+ * main();
149
+ *
55
150
  * @returns {string[]} Array of addresses.
56
151
  */
57
152
  getAllActiveAccounts(symbol: string): Promise<string[]>;
@@ -15,12 +15,28 @@ Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const writeAccessHandler_1 = __importDefault(require("./writeAccessHandler"));
16
16
  const d8XMath_1 = require("./d8XMath");
17
17
  /**
18
- * Methods to liquidate traders.
18
+ * Functions to liquidate traders. This class requires a private key
19
+ * and executes smart-contract interactions that require gas-payments.
19
20
  */
20
21
  class LiquidatorTool extends writeAccessHandler_1.default {
21
22
  /**
22
23
  * Constructs a LiquidatorTool instance for a given configuration and private key.
23
- * @param {NodeSDKConfig} config Configuration object.
24
+ * @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.
25
+ * readSDKConfig.
26
+ * @example
27
+ * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
28
+ * async function main() {
29
+ * console.log(LiquidatorTool);
30
+ * // load configuration for testnet
31
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
32
+ * // LiquidatorTool (authentication required, PK is an environment variable with a private key)
33
+ * const pk: string = <string>process.env.PK;
34
+ * let lqudtrTool = new LiquidatorTool(config, pk);
35
+ * // Create a proxy instance to access the blockchain
36
+ * await lqudtrTool.createProxyInstance();
37
+ * }
38
+ * main();
39
+ *
24
40
  * @param {string} privateKey Private key of account that liquidates.
25
41
  */
26
42
  constructor(config, privateKey) {
@@ -32,6 +48,22 @@ class LiquidatorTool extends writeAccessHandler_1.default {
32
48
  * @param {string} traderAddr Address of the trader to be liquidated.
33
49
  * @param {string=} liquidatorAddr Address to be credited if the liquidation succeeds.
34
50
  * Defaults to the wallet used to execute the liquidation.
51
+ * @example
52
+ * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
53
+ * async function main() {
54
+ * console.log(LiquidatorTool);
55
+ * // Setup (authentication required, PK is an environment variable with a private key)
56
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
57
+ * const pk: string = <string>process.env.PK;
58
+ * let lqudtrTool = new LiquidatorTool(config, pk);
59
+ * await lqudtrTool.createProxyInstance();
60
+ * // liquidate trader
61
+ * let liqAmount = await lqudtrTool.liquidateTrader("ETH-USD-MATIC",
62
+ * "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B");
63
+ * console.log(liqAmount);
64
+ * }
65
+ * main();
66
+ *
35
67
  * @returns {number} Liquidated amount.
36
68
  */
37
69
  liquidateTrader(symbol, traderAddr, liquidatorAddr = "") {
@@ -49,10 +81,28 @@ class LiquidatorTool extends writeAccessHandler_1.default {
49
81
  });
50
82
  }
51
83
  /**
52
- * Check if a trader is maintenance margin safe - if not, it can be liquidated.
84
+ * Check if the collateral of a trader is above the maintenance margin ("maintenance margin safe").
85
+ * If not, the position can be liquidated.
53
86
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
54
- * @param {string} traderAddr Address of the trader whose position we want to assess.
87
+ * @param {string} traderAddr Address of the trader whose position you want to assess.
88
+ * @example
89
+ * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
90
+ * async function main() {
91
+ * console.log(LiquidatorTool);
92
+ * // Setup (authentication required, PK is an environment variable with a private key)
93
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
94
+ * const pk: string = <string>process.env.PK;
95
+ * let lqudtrTool = new LiquidatorTool(config, pk);
96
+ * await lqudtrTool.createProxyInstance();
97
+ * // check if trader can be liquidated
98
+ * let safe = await lqudtrTool.isMaintenanceMarginSafe("ETH-USD-MATIC",
99
+ * "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B");
100
+ * console.log(safe);
101
+ * }
102
+ * main();
103
+ *
55
104
  * @returns {boolean} True if the trader is maintenance margin safe in the perpetual.
105
+ * False means that the trader's position can be liquidated.
56
106
  */
57
107
  isMaintenanceMarginSafe(symbol, traderAddr) {
58
108
  return __awaiter(this, void 0, void 0, function* () {
@@ -60,7 +110,7 @@ class LiquidatorTool extends writeAccessHandler_1.default {
60
110
  throw Error("no proxy contract initialized. Use createProxyInstance().");
61
111
  }
62
112
  let perpID = LiquidatorTool.symbolToPerpetualId(symbol, this.symbolToPerpStaticInfo);
63
- return yield this.proxyContract.isMaintenanceMarginSafe(perpID, traderAddr);
113
+ return yield this.proxyContract.isTraderMaintenanceMarginSafe(perpID, traderAddr);
64
114
  });
65
115
  }
66
116
  /**
@@ -82,6 +132,21 @@ class LiquidatorTool extends writeAccessHandler_1.default {
82
132
  /**
83
133
  * Total number of active accounts for this symbol, i.e. accounts with positions that are currently open.
84
134
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
135
+ * @example
136
+ * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
137
+ * async function main() {
138
+ * console.log(LiquidatorTool);
139
+ * // Setup (authentication required, PK is an environment variable with a private key)
140
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
141
+ * const pk: string = <string>process.env.PK;
142
+ * let lqudtrTool = new LiquidatorTool(config, pk);
143
+ * await lqudtrTool.createProxyInstance();
144
+ * // get number of active accounts
145
+ * let accounts = await lqudtrTool.countActivePerpAccounts("ETH-USD-MATIC");
146
+ * console.log(accounts);
147
+ * }
148
+ * main();
149
+ *
85
150
  * @returns {number} Number of active accounts.
86
151
  */
87
152
  countActivePerpAccounts(symbol) {
@@ -97,8 +162,23 @@ class LiquidatorTool extends writeAccessHandler_1.default {
97
162
  * Get addresses of active accounts by chunks.
98
163
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
99
164
  * @param {number} from From which account we start counting (0-indexed).
100
- * @param {number} to Until which account we count.
101
- * @returns {string[]} Array of addresses.
165
+ * @param {number} to Until which account we count, non inclusive.
166
+ * @example
167
+ * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
168
+ * async function main() {
169
+ * console.log(LiquidatorTool);
170
+ * // Setup (authentication required, PK is an environment variable with a private key)
171
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
172
+ * const pk: string = <string>process.env.PK;
173
+ * let lqudtrTool = new LiquidatorTool(config, pk);
174
+ * await lqudtrTool.createProxyInstance();
175
+ * // get all active accounts in chunks
176
+ * let accounts = await lqudtrTool.getActiveAccountsByChunks("ETH-USD-MATIC", 0, 4);
177
+ * console.log(accounts);
178
+ * }
179
+ * main();
180
+ *
181
+ * @returns {string[]} Array of addresses at locations 'from', 'from'+1 ,..., 'to'-1.
102
182
  */
103
183
  getActiveAccountsByChunks(symbol, from, to) {
104
184
  return __awaiter(this, void 0, void 0, function* () {
@@ -112,13 +192,28 @@ class LiquidatorTool extends writeAccessHandler_1.default {
112
192
  /**
113
193
  * Addresses for all the active accounts in this perpetual symbol.
114
194
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
195
+ * @example
196
+ * import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
197
+ * async function main() {
198
+ * console.log(LiquidatorTool);
199
+ * // Setup (authentication required, PK is an environment variable with a private key)
200
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
201
+ * const pk: string = <string>process.env.PK;
202
+ * let lqudtrTool = new LiquidatorTool(config, pk);
203
+ * await lqudtrTool.createProxyInstance();
204
+ * // get all active accounts
205
+ * let accounts = await lqudtrTool.getAllActiveAccounts("ETH-USD-MATIC");
206
+ * console.log(accounts);
207
+ * }
208
+ * main();
209
+ *
115
210
  * @returns {string[]} Array of addresses.
116
211
  */
117
212
  getAllActiveAccounts(symbol) {
118
213
  return __awaiter(this, void 0, void 0, function* () {
119
214
  // checks are done inside the intermediate functions
120
- let totalAccoutns = yield this.countActivePerpAccounts(symbol);
121
- return yield this.getActiveAccountsByChunks(symbol, 0, totalAccoutns - 1);
215
+ let totalAccounts = yield this.countActivePerpAccounts(symbol);
216
+ return yield this.getActiveAccountsByChunks(symbol, 0, totalAccounts);
122
217
  });
123
218
  }
124
219
  }