@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.
package/src/marketData.ts CHANGED
@@ -16,10 +16,29 @@ import PerpetualDataHandler from "./perpetualDataHandler";
16
16
  import { SmartContractOrder, Order } from "./nodeSDKTypes";
17
17
 
18
18
  /**
19
+ * Functions to access market data (e.g., information on open orders, information on products that can be traded).
19
20
  * This class requires no private key and is blockchain read-only.
20
21
  * No gas required for the queries here.
21
22
  */
22
23
  export default class MarketData extends PerpetualDataHandler {
24
+ /**
25
+ * Constructor
26
+ * @param {NodeSDKConfig} config Configuration object, see
27
+ * PerpetualDataHandler.readSDKConfig.
28
+ * @example
29
+ * import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
30
+ * async function main() {
31
+ * console.log(MarketData);
32
+ * // load configuration for testnet
33
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
34
+ * // MarketData (read only, no authentication needed)
35
+ * let mktData = new MarketData(config);
36
+ * // Create a proxy instance to access the blockchain
37
+ * await mktData.createProxyInstance();
38
+ * }
39
+ * main();
40
+ *
41
+ */
23
42
  public constructor(config: NodeSDKConfig) {
24
43
  super(config);
25
44
  }
@@ -31,6 +50,20 @@ export default class MarketData extends PerpetualDataHandler {
31
50
 
32
51
  /**
33
52
  * Get contract instance. Useful for event listening.
53
+ * @example
54
+ * import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
55
+ * async function main() {
56
+ * console.log(MarketData);
57
+ * // setup
58
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
59
+ * let mktData = new MarketData(config);
60
+ * await mktData.createProxyInstance();
61
+ * // Get contract instance
62
+ * let proxy = await mktData.getReadOnlyProxyInstance();
63
+ * console.log(proxy);
64
+ * }
65
+ * main();
66
+ *
34
67
  * @returns read-only proxy instance
35
68
  */
36
69
  public getReadOnlyProxyInstance(): ethers.Contract {
@@ -42,6 +75,20 @@ export default class MarketData extends PerpetualDataHandler {
42
75
 
43
76
  /**
44
77
  * Information about the products traded in the exchange.
78
+ * @example
79
+ * import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
80
+ * async function main() {
81
+ * console.log(MarketData);
82
+ * // setup
83
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
84
+ * let mktData = new MarketData(config);
85
+ * await mktData.createProxyInstance();
86
+ * // Get exchange info
87
+ * let info = await mktData.exchangeInfo();
88
+ * console.log(info);
89
+ * }
90
+ * main();
91
+ *
45
92
  * @returns {ExchangeInfo} Array of static data for all the pools and perpetuals in the system.
46
93
  */
47
94
  public async exchangeInfo(): Promise<ExchangeInfo> {
@@ -55,6 +102,29 @@ export default class MarketData extends PerpetualDataHandler {
55
102
  * All open orders for a trader-address and a symbol.
56
103
  * @param {string} traderAddr Address of the trader for which we get the open orders.
57
104
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
105
+ * @example
106
+ * // Setup
107
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
108
+ * let mktData = new MarketData(config);
109
+ * await mktData.createProxyInstance();
110
+ * // Get all open orders for a trader/symbol
111
+ * let opOrder = await mktData.openOrders("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
112
+ * "ETH-USD-MATIC");
113
+ * @example
114
+ * import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
115
+ * async function main() {
116
+ * console.log(MarketData);
117
+ * // setup
118
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
119
+ * let mktData = new MarketData(config);
120
+ * await mktData.createProxyInstance();
121
+ * // Get all open orders for a trader/symbol
122
+ * let opOrder = await mktData.openOrders("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
123
+ * "ETH-USD-MATIC");
124
+ * console.log(opOrder);
125
+ * }
126
+ * main();
127
+ *
58
128
  * @returns {Array<Array<Order>, Array<string>>} Array of open orders and corresponding order-ids.
59
129
  */
60
130
  public async openOrders(traderAddr: string, symbol: string): Promise<{ orders: Order[]; orderIds: string[] }> {
@@ -62,15 +132,38 @@ export default class MarketData extends PerpetualDataHandler {
62
132
  let orderBookContract = this.getOrderBookContract(symbol);
63
133
  let [orders, digests] = await Promise.all([
64
134
  this.openOrdersOnOrderBook(traderAddr, orderBookContract),
65
- this.orderIdsOfTrader(traderAddr, orderBookContract),
135
+ MarketData.orderIdsOfTrader(traderAddr, orderBookContract),
66
136
  ]);
67
137
  return { orders: orders, orderIds: digests };
68
138
  }
69
139
 
70
140
  /**
71
- * Information about the position open by a given trader in a given perpetual contract.
141
+ * Information about the positions open by a given trader in a given perpetual contract.
72
142
  * @param {string} traderAddr Address of the trader for which we get the position risk.
73
143
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
144
+ * @example
145
+ * // Setup
146
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
147
+ * let mktData = new MarketData(config);
148
+ * await mktData.createProxyInstance();
149
+ * // Get position risk info
150
+ * let posRisk = await mktData.positionRisk("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
151
+ * "ETH-USD-MATIC");
152
+ * @example
153
+ * import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
154
+ * async function main() {
155
+ * console.log(MarketData);
156
+ * // setup
157
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
158
+ * let mktData = new MarketData(config);
159
+ * await mktData.createProxyInstance();
160
+ * // Get position risk info
161
+ * let posRisk = await mktData.positionRisk("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
162
+ * "ETH-USD-MATIC");
163
+ * console.log(posRisk);
164
+ * }
165
+ * main();
166
+ *
74
167
  * @returns {MarginAccount}
75
168
  */
76
169
  public async positionRisk(traderAddr: string, symbol: string): Promise<MarginAccount> {
@@ -90,6 +183,20 @@ export default class MarketData extends PerpetualDataHandler {
90
183
  * Uses the Oracle(s) in the exchange to get the latest price of a given index in a given currency, if a route exists.
91
184
  * @param {string} base Index name, e.g. ETH.
92
185
  * @param {string} quote Quote currency, e.g. USD.
186
+ * @example
187
+ * import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
188
+ * async function main() {
189
+ * console.log(MarketData);
190
+ * // setup
191
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
192
+ * let mktData = new MarketData(config);
193
+ * await mktData.createProxyInstance();
194
+ * // get oracle price
195
+ * let price = await mktData.getOraclePrice("ETH", "USD");
196
+ * console.log(price);
197
+ * }
198
+ * main();
199
+ *
93
200
  * @returns {number} Price of index in given currency.
94
201
  */
95
202
  public async getOraclePrice(base: string, quote: string): Promise<number | undefined> {
@@ -103,6 +210,20 @@ export default class MarketData extends PerpetualDataHandler {
103
210
  /**
104
211
  * Get the current mark price
105
212
  * @param symbol symbol of the form ETH-USD-MATIC
213
+ * @example
214
+ * import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
215
+ * async function main() {
216
+ * console.log(MarketData);
217
+ * // setup
218
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
219
+ * let mktData = new MarketData(config);
220
+ * await mktData.createProxyInstance();
221
+ * // get mark price
222
+ * let price = await mktData.getMarkPrice("ETH-USD-MATIC");
223
+ * console.log(price);
224
+ * }
225
+ * main();
226
+ *
106
227
  * @returns mark price
107
228
  */
108
229
  public async getMarkPrice(symbol: string): Promise<number> {
@@ -116,6 +237,20 @@ export default class MarketData extends PerpetualDataHandler {
116
237
  * get the current price for a given quantity
117
238
  * @param symbol symbol of the form ETH-USD-MATIC
118
239
  * @param quantity quantity to be traded, negative if short
240
+ * @example
241
+ * import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
242
+ * async function main() {
243
+ * console.log(MarketData);
244
+ * // setup
245
+ * const config = PerpetualDataHandler.readSDKConfig("testnet");
246
+ * let mktData = new MarketData(config);
247
+ * await mktData.createProxyInstance();
248
+ * // get perpetual price
249
+ * let price = await mktData.getPerpetualPrice("ETH-USD-MATIC", 1);
250
+ * console.log(price);
251
+ * }
252
+ * main();
253
+ *
119
254
  * @returns price (number)
120
255
  */
121
256
  public async getPerpetualPrice(symbol: string, quantity: number): Promise<number> {
@@ -151,12 +286,12 @@ export default class MarketData extends PerpetualDataHandler {
151
286
 
152
287
  /**
153
288
  *
154
- * @param traderAddr address of the trader
155
- * @param orderBookContract instance of order book contract
156
- * @returns array of order-id's
289
+ * @param traderAddr Address of the trader
290
+ * @param orderBookContract Instance of order book contract
291
+ * @returns Array of order-id's
157
292
  * @ignore
158
293
  */
159
- protected async orderIdsOfTrader(traderAddr: string, orderBookContract: ethers.Contract): Promise<string[]> {
294
+ public static async orderIdsOfTrader(traderAddr: string, orderBookContract: ethers.Contract): Promise<string[]> {
160
295
  let digestsRaw: string[] = await orderBookContract.limitDigestsOfTrader(traderAddr, 0, 15);
161
296
  let k: number = 0;
162
297
  let digests: string[] = [];
@@ -9,6 +9,11 @@ export const COLLATERAL_CURRENCY_QUOTE = 0;
9
9
  export const COLLATERAL_CURRENCY_BASE = 1;
10
10
  export const COLLATERAL_CURRENCY_QUANTO = 2;
11
11
  export const PERP_STATE_STR = ["INVALID", "INITIALIZING", "NORMAL", "EMERGENCY", "CLEARED"];
12
+ export const PerpetualStateINVALID = 0;
13
+ export const PerpetualStateINITIALIZING = 1;
14
+ export const PerpetualStateNORMAL = 2;
15
+ export const PerpetualStateEMERGENCY = 3;
16
+ export const PerpetualStateCLEARED = 4;
12
17
  export const ZERO_ADDRESS = constants.AddressZero;
13
18
  export const ZERO_ORDER_ID = constants.HashZero;
14
19
 
@@ -3,12 +3,17 @@ import { BUY_SIDE, NodeSDKConfig, Order, SELL_SIDE, ZERO_ADDRESS, ZERO_ORDER_ID
3
3
  import { ethers } from "ethers";
4
4
 
5
5
  /**
6
- * Methods to execute existing orders from the limit order book.
6
+ * Functions to execute existing conditional orders from the limit order book. This class
7
+ * requires a private key and executes smart-contract interactions that require
8
+ * gas-payments.
7
9
  */
8
10
  export default class OrderReferrerTool extends WriteAccessHandler {
9
11
  /**
10
12
  * Constructor.
11
- * @param {NodeSDKConfig} config Configuration object.
13
+ * @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.readSDKConfig.
14
+ * @example
15
+ * const config = PerpetualDataHandler.readSDKConfig("testnet")
16
+ *
12
17
  * @param {string} privateKey Private key of the wallet that executes the conditional orders.
13
18
  */
14
19
  public constructor(config: NodeSDKConfig, privateKey: string) {
@@ -16,7 +21,7 @@ export default class OrderReferrerTool extends WriteAccessHandler {
16
21
  }
17
22
 
18
23
  /**
19
- * Executes an order by symbol and ID. This action interacts with the blockchain and incurs in gas costs.
24
+ * Executes an order by symbol and ID. This action interacts with the blockchain and incurs gas costs.
20
25
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
21
26
  * @param {string} orderId ID of the order to be executed.
22
27
  * @param {string=} referrerAddr Address of the wallet to be credited for executing the order,
@@ -29,6 +29,8 @@ import {
29
29
  DEFAULT_CONFIG_TESTNET_NAME,
30
30
  DEFAULT_CONFIG_TESTNET,
31
31
  ONE_64x64,
32
+ PerpetualStateINITIALIZING,
33
+ PerpetualStateINVALID,
32
34
  } from "./nodeSDKTypes";
33
35
  import { fromBytes4HexString, to4Chars, combineFlags, containsFlag } from "./utils";
34
36
  import {
@@ -130,6 +132,10 @@ export default class PerpetualDataHandler {
130
132
 
131
133
  for (let k = 0; k < perpetualIDs.length; k++) {
132
134
  let perp = await proxyContract.getPerpetual(perpetualIDs[k]);
135
+ if (perp.state == PerpetualStateINVALID || perp.state == PerpetualStateINITIALIZING) {
136
+ // only active perpetuals
137
+ continue;
138
+ }
133
139
  let base = fromBytes4HexString(perp.S2BaseCCY);
134
140
  let quote = fromBytes4HexString(perp.S2QuoteCCY);
135
141
  let base3 = fromBytes4HexString(perp.S3BaseCCY);
@@ -475,7 +481,7 @@ export default class PerpetualDataHandler {
475
481
  fLimitPrice = floatToABK64x64(order.limitPrice);
476
482
  }
477
483
 
478
- let iDeadline = order.deadline == undefined ? Date.now() + ORDER_MAX_DURATION_SEC : order.deadline;
484
+ let iDeadline = order.deadline == undefined ? Date.now() / 1000 + ORDER_MAX_DURATION_SEC : order.deadline;
479
485
  let fTriggerPrice = order.stopPrice == undefined ? BigNumber.from(0) : floatToABK64x64(order.stopPrice);
480
486
  if (order.reduceOnly != undefined && order.reduceOnly == true) {
481
487
  }
@@ -491,8 +497,8 @@ export default class PerpetualDataHandler {
491
497
  fLimitPrice: fLimitPrice,
492
498
  fTriggerPrice: fTriggerPrice,
493
499
  fLeverage: order.leverage == undefined ? BigNumber.from(0) : floatToABK64x64(order.leverage),
494
- iDeadline: BigNumber.from(iDeadline),
495
- createdTimestamp: BigNumber.from(order.timestamp),
500
+ iDeadline: BigNumber.from(Math.round(iDeadline)),
501
+ createdTimestamp: BigNumber.from(Math.round(order.timestamp)),
496
502
  };
497
503
  return smOrder;
498
504
  }