@d8x/perpetuals-sdk 0.0.4 → 0.0.6

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.
@@ -1,4 +1,66 @@
1
1
  [
2
+ {
3
+ "anonymous": false,
4
+ "inputs": [
5
+ {
6
+ "indexed": true,
7
+ "internalType": "uint8",
8
+ "name": "poolId",
9
+ "type": "uint8"
10
+ },
11
+ {
12
+ "indexed": false,
13
+ "internalType": "address",
14
+ "name": "oldOwner",
15
+ "type": "address"
16
+ },
17
+ {
18
+ "indexed": false,
19
+ "internalType": "address",
20
+ "name": "newOwner",
21
+ "type": "address"
22
+ },
23
+ {
24
+ "indexed": false,
25
+ "internalType": "uint32",
26
+ "name": "numLots",
27
+ "type": "uint32"
28
+ }
29
+ ],
30
+ "name": "BrokerLotsTransferred",
31
+ "type": "event"
32
+ },
33
+ {
34
+ "anonymous": false,
35
+ "inputs": [
36
+ {
37
+ "indexed": true,
38
+ "internalType": "uint8",
39
+ "name": "poolId",
40
+ "type": "uint8"
41
+ },
42
+ {
43
+ "indexed": false,
44
+ "internalType": "address",
45
+ "name": "oldOwner",
46
+ "type": "address"
47
+ },
48
+ {
49
+ "indexed": false,
50
+ "internalType": "address",
51
+ "name": "newOwner",
52
+ "type": "address"
53
+ },
54
+ {
55
+ "indexed": false,
56
+ "internalType": "int128",
57
+ "name": "fVolume",
58
+ "type": "int128"
59
+ }
60
+ ],
61
+ "name": "BrokerVolumeTransferred",
62
+ "type": "event"
63
+ },
2
64
  {
3
65
  "anonymous": false,
4
66
  "inputs": [
@@ -365,6 +427,63 @@
365
427
  "name": "SetBrokerCollateralLotSize",
366
428
  "type": "event"
367
429
  },
430
+ {
431
+ "anonymous": false,
432
+ "inputs": [
433
+ {
434
+ "indexed": false,
435
+ "internalType": "uint32[]",
436
+ "name": "designations",
437
+ "type": "uint32[]"
438
+ },
439
+ {
440
+ "indexed": false,
441
+ "internalType": "uint16[]",
442
+ "name": "fees",
443
+ "type": "uint16[]"
444
+ }
445
+ ],
446
+ "name": "SetBrokerDesignations",
447
+ "type": "event"
448
+ },
449
+ {
450
+ "anonymous": false,
451
+ "inputs": [
452
+ {
453
+ "indexed": false,
454
+ "internalType": "uint256[]",
455
+ "name": "tiers",
456
+ "type": "uint256[]"
457
+ },
458
+ {
459
+ "indexed": false,
460
+ "internalType": "uint16[]",
461
+ "name": "feesTbps",
462
+ "type": "uint16[]"
463
+ }
464
+ ],
465
+ "name": "SetBrokerTiers",
466
+ "type": "event"
467
+ },
468
+ {
469
+ "anonymous": false,
470
+ "inputs": [
471
+ {
472
+ "indexed": false,
473
+ "internalType": "uint256[]",
474
+ "name": "tiers",
475
+ "type": "uint256[]"
476
+ },
477
+ {
478
+ "indexed": false,
479
+ "internalType": "uint16[]",
480
+ "name": "feesTbps",
481
+ "type": "uint16[]"
482
+ }
483
+ ],
484
+ "name": "SetBrokerVolumeTiers",
485
+ "type": "event"
486
+ },
368
487
  {
369
488
  "anonymous": false,
370
489
  "inputs": [
@@ -604,6 +723,57 @@
604
723
  "name": "SetTargetPoolSizeUpdateTime",
605
724
  "type": "event"
606
725
  },
726
+ {
727
+ "anonymous": false,
728
+ "inputs": [
729
+ {
730
+ "indexed": false,
731
+ "internalType": "uint256[]",
732
+ "name": "tiers",
733
+ "type": "uint256[]"
734
+ },
735
+ {
736
+ "indexed": false,
737
+ "internalType": "uint16[]",
738
+ "name": "feesTbps",
739
+ "type": "uint16[]"
740
+ }
741
+ ],
742
+ "name": "SetTraderTiers",
743
+ "type": "event"
744
+ },
745
+ {
746
+ "anonymous": false,
747
+ "inputs": [
748
+ {
749
+ "indexed": false,
750
+ "internalType": "uint256[]",
751
+ "name": "tiers",
752
+ "type": "uint256[]"
753
+ },
754
+ {
755
+ "indexed": false,
756
+ "internalType": "uint16[]",
757
+ "name": "feesTbps",
758
+ "type": "uint16[]"
759
+ }
760
+ ],
761
+ "name": "SetTraderVolumeTiers",
762
+ "type": "event"
763
+ },
764
+ {
765
+ "anonymous": false,
766
+ "inputs": [
767
+ {
768
+ "indexed": false,
769
+ "internalType": "address",
770
+ "name": "tokenAddr",
771
+ "type": "address"
772
+ }
773
+ ],
774
+ "name": "SetUtilityToken",
775
+ "type": "event"
776
+ },
607
777
  {
608
778
  "anonymous": false,
609
779
  "inputs": [
@@ -2334,6 +2504,54 @@
2334
2504
  "stateMutability": "view",
2335
2505
  "type": "function"
2336
2506
  },
2507
+ {
2508
+ "inputs": [
2509
+ {
2510
+ "internalType": "uint8",
2511
+ "name": "_poolId",
2512
+ "type": "uint8"
2513
+ },
2514
+ {
2515
+ "internalType": "address",
2516
+ "name": "_brokerAddr",
2517
+ "type": "address"
2518
+ }
2519
+ ],
2520
+ "name": "getCurrentBrokerVolume",
2521
+ "outputs": [
2522
+ {
2523
+ "internalType": "int128",
2524
+ "name": "",
2525
+ "type": "int128"
2526
+ }
2527
+ ],
2528
+ "stateMutability": "view",
2529
+ "type": "function"
2530
+ },
2531
+ {
2532
+ "inputs": [
2533
+ {
2534
+ "internalType": "uint8",
2535
+ "name": "_poolId",
2536
+ "type": "uint8"
2537
+ },
2538
+ {
2539
+ "internalType": "address",
2540
+ "name": "_traderAddr",
2541
+ "type": "address"
2542
+ }
2543
+ ],
2544
+ "name": "getCurrentTraderVolume",
2545
+ "outputs": [
2546
+ {
2547
+ "internalType": "int128",
2548
+ "name": "",
2549
+ "type": "int128"
2550
+ }
2551
+ ],
2552
+ "stateMutability": "view",
2553
+ "type": "function"
2554
+ },
2337
2555
  {
2338
2556
  "inputs": [
2339
2557
  {
@@ -3699,6 +3917,30 @@
3699
3917
  "stateMutability": "view",
3700
3918
  "type": "function"
3701
3919
  },
3920
+ {
3921
+ "inputs": [
3922
+ {
3923
+ "internalType": "uint24",
3924
+ "name": "_iPerpetualId",
3925
+ "type": "uint24"
3926
+ },
3927
+ {
3928
+ "internalType": "int128",
3929
+ "name": "_fTradeAmountBC",
3930
+ "type": "int128"
3931
+ }
3932
+ ],
3933
+ "name": "queryPerpetualPrice",
3934
+ "outputs": [
3935
+ {
3936
+ "internalType": "int128",
3937
+ "name": "",
3938
+ "type": "int128"
3939
+ }
3940
+ ],
3941
+ "stateMutability": "view",
3942
+ "type": "function"
3943
+ },
3702
3944
  {
3703
3945
  "inputs": [
3704
3946
  {
@@ -3931,6 +4173,29 @@
3931
4173
  "stateMutability": "nonpayable",
3932
4174
  "type": "function"
3933
4175
  },
4176
+ {
4177
+ "inputs": [
4178
+ {
4179
+ "internalType": "uint8",
4180
+ "name": "_poolId",
4181
+ "type": "uint8"
4182
+ },
4183
+ {
4184
+ "internalType": "address",
4185
+ "name": "_brokerAddr",
4186
+ "type": "address"
4187
+ },
4188
+ {
4189
+ "internalType": "uint16",
4190
+ "name": "_feeTbps",
4191
+ "type": "uint16"
4192
+ }
4193
+ ],
4194
+ "name": "setInitialVolumeForFee",
4195
+ "outputs": [],
4196
+ "stateMutability": "nonpayable",
4197
+ "type": "function"
4198
+ },
3934
4199
  {
3935
4200
  "inputs": [
3936
4201
  {
@@ -4450,6 +4715,47 @@
4450
4715
  "stateMutability": "nonpayable",
4451
4716
  "type": "function"
4452
4717
  },
4718
+ {
4719
+ "inputs": [
4720
+ {
4721
+ "internalType": "uint8",
4722
+ "name": "_poolId",
4723
+ "type": "uint8"
4724
+ },
4725
+ {
4726
+ "internalType": "address",
4727
+ "name": "_transferToAddr",
4728
+ "type": "address"
4729
+ },
4730
+ {
4731
+ "internalType": "uint32",
4732
+ "name": "_lots",
4733
+ "type": "uint32"
4734
+ }
4735
+ ],
4736
+ "name": "transferBrokerLots",
4737
+ "outputs": [],
4738
+ "stateMutability": "nonpayable",
4739
+ "type": "function"
4740
+ },
4741
+ {
4742
+ "inputs": [
4743
+ {
4744
+ "internalType": "uint8",
4745
+ "name": "_poolId",
4746
+ "type": "uint8"
4747
+ },
4748
+ {
4749
+ "internalType": "address",
4750
+ "name": "_transferToAddr",
4751
+ "type": "address"
4752
+ }
4753
+ ],
4754
+ "name": "transferBrokerOwnership",
4755
+ "outputs": [],
4756
+ "stateMutability": "nonpayable",
4757
+ "type": "function"
4758
+ },
4453
4759
  {
4454
4760
  "inputs": [
4455
4761
  {
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "v1.1",
3
- "proxyAddr": "0x55F4041DAea27c6F380d6E221d63EF300a293Ee5",
3
+ "proxyAddr": "0x51b05809eF1E84BB963D84ff95FB5E625d88152d",
4
4
  "nodeURL": "https://rpc-mumbai.maticvigil.com/",
5
5
  "proxyABILocation": "../abi/IPerpetualManager.json",
6
- "limitOrderBookFactoryAddr": "0xd63Faf780687795EBac85ce248a45E457752f021",
6
+ "limitOrderBookFactoryAddr": "0x92B6967Ea9b6A0375013649dBd3d0CD9DceeEf4d",
7
7
  "limitOrderBookFactoryABILocation": "../abi/LimitOrderBookFactory.json",
8
8
  "limitOrderBookABILocation": "../abi/LimitOrderBook.json"
9
9
  }
@@ -22,9 +22,9 @@ export default class AccountTrade extends WriteAccessHandler {
22
22
  /**
23
23
  * Submits an order to the exchange.
24
24
  * @param {Order} order Order struct.
25
- * @returns {ContractTransaction} Contract Transaction (containing events).
25
+ * @returns {string} Transaction hash.
26
26
  */
27
- order(order: Order): Promise<ethers.ContractTransaction>;
27
+ order(order: Order): Promise<string | undefined>;
28
28
  /**
29
29
  * Fee charged by the exchange for trading any perpetual on a given pool.
30
30
  * It accounts for the current trader's fee tier (based on the trader's D8X balance and trading volume).
@@ -55,7 +55,7 @@ export default class AccountTrade extends WriteAccessHandler {
55
55
  * @returns transaction hash
56
56
  * @ignore
57
57
  */
58
- _order(order: Order, traderAddr: string, symbolToPerpetualMap: Map<string, PerpetualStaticInfo>, proxyContract: ethers.Contract, orderBookContract: ethers.Contract | null, chainId: number, signer: ethers.Wallet, gasLimit: number): Promise<ethers.ContractTransaction>;
58
+ _order(order: Order, traderAddr: string, symbolToPerpetualMap: Map<string, PerpetualStaticInfo>, proxyContract: ethers.Contract, orderBookContract: ethers.Contract | null, chainId: number, signer: ethers.Wallet, gasLimit: number): Promise<string | undefined>;
59
59
  protected _cancelOrder(symbol: string, orderId: string, orderBookContract: ethers.Contract | null): Promise<string | undefined>;
60
60
  /**
61
61
  * Creates a signature
@@ -53,7 +53,7 @@ class AccountTrade extends writeAccessHandler_1.default {
53
53
  /**
54
54
  * Submits an order to the exchange.
55
55
  * @param {Order} order Order struct.
56
- * @returns {ContractTransaction} Contract Transaction (containing events).
56
+ * @returns {string} Transaction hash.
57
57
  */
58
58
  order(order) {
59
59
  return __awaiter(this, void 0, void 0, function* () {
@@ -136,7 +136,7 @@ class AccountTrade extends writeAccessHandler_1.default {
136
136
  let signature = yield this._createSignature(scOrder, chainId, true, signer, proxyContract.address);
137
137
  tx = yield orderBookContract.createLimitOrder(scOrder, signature, { gasLimit: gasLimit });
138
138
  }
139
- return tx;
139
+ return tx.hash;
140
140
  });
141
141
  }
142
142
  _cancelOrder(symbol, orderId, orderBookContract) {
@@ -34,7 +34,7 @@ export default class BrokerTool extends WriteAccessHandler {
34
34
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
35
35
  * @returns {number} Fee based solely on a broker's traded volume in the corresponding pool, in decimals (i.e. 0.1% is 0.001).
36
36
  */
37
- getFeeForBrokerVolume(poolSymbolName: string): Promise<number>;
37
+ getFeeForBrokerVolume(poolSymbolName: string): Promise<number | undefined>;
38
38
  /**
39
39
  * Determine the exchange fee based on the current D8X balance in a broker's wallet.
40
40
  * The final exchange fee paid by the broker is equal to
@@ -80,9 +80,9 @@ export default class BrokerTool extends WriteAccessHandler {
80
80
  * Deposit lots to the default fund of a given pool.
81
81
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
82
82
  * @param {number} lots Number of lots to deposit into this pool.
83
- * @returns {ethers.ContractTransaction} ContractTransaction object.
83
+ * @returns {ethers.providers.TransactionResponse} Transaction object.
84
84
  */
85
- brokerDepositToDefaultFund(poolSymbolName: string, lots: number): Promise<ethers.ContractTransaction>;
85
+ brokerDepositToDefaultFund(poolSymbolName: string, lots: number): Promise<ethers.providers.TransactionResponse>;
86
86
  /**
87
87
  * Adds this broker's signature to an order so that it can be submitted by an approved trader.
88
88
  * @param {Order} order Order to sign.
@@ -183,7 +183,7 @@ class BrokerTool extends writeAccessHandler_1.default {
183
183
  * Deposit lots to the default fund of a given pool.
184
184
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
185
185
  * @param {number} lots Number of lots to deposit into this pool.
186
- * @returns {ethers.ContractTransaction} ContractTransaction object.
186
+ * @returns {ethers.providers.TransactionResponse} Transaction object.
187
187
  */
188
188
  brokerDepositToDefaultFund(poolSymbolName, lots) {
189
189
  return __awaiter(this, void 0, void 0, function* () {
@@ -45,8 +45,8 @@ export default class LiquidatorTool extends WriteAccessHandler {
45
45
  * Get addresses of active accounts by chunks.
46
46
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
47
47
  * @param {number} from From which account we start counting (0-indexed).
48
- * @param {number} to Until which account we count, non inclusive.
49
- * @returns {string[]} Array of addresses at locations 'from', 'from'+1 ,..., 'to'-1.
48
+ * @param {number} to Until which account we count.
49
+ * @returns {string[]} Array of addresses.
50
50
  */
51
51
  getActiveAccountsByChunks(symbol: string, from: number, to: number): Promise<string[]>;
52
52
  /**
@@ -60,7 +60,7 @@ class LiquidatorTool extends writeAccessHandler_1.default {
60
60
  throw Error("no proxy contract initialized. Use createProxyInstance().");
61
61
  }
62
62
  let perpID = LiquidatorTool.symbolToPerpetualId(symbol, this.symbolToPerpStaticInfo);
63
- return yield this.proxyContract.isTraderMaintenanceMarginSafe(perpID, traderAddr);
63
+ return yield this.proxyContract.isMaintenanceMarginSafe(perpID, traderAddr);
64
64
  });
65
65
  }
66
66
  /**
@@ -97,8 +97,8 @@ class LiquidatorTool extends writeAccessHandler_1.default {
97
97
  * Get addresses of active accounts by chunks.
98
98
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
99
99
  * @param {number} from From which account we start counting (0-indexed).
100
- * @param {number} to Until which account we count, non inclusive.
101
- * @returns {string[]} Array of addresses at locations 'from', 'from'+1 ,..., 'to'-1.
100
+ * @param {number} to Until which account we count.
101
+ * @returns {string[]} Array of addresses.
102
102
  */
103
103
  getActiveAccountsByChunks(symbol, from, to) {
104
104
  return __awaiter(this, void 0, void 0, function* () {
@@ -117,8 +117,8 @@ class LiquidatorTool extends writeAccessHandler_1.default {
117
117
  getAllActiveAccounts(symbol) {
118
118
  return __awaiter(this, void 0, void 0, function* () {
119
119
  // checks are done inside the intermediate functions
120
- let totalAccounts = yield this.countActivePerpAccounts(symbol);
121
- return yield this.getActiveAccountsByChunks(symbol, 0, totalAccounts);
120
+ let totalAccoutns = yield this.countActivePerpAccounts(symbol);
121
+ return yield this.getActiveAccountsByChunks(symbol, 0, totalAccoutns - 1);
122
122
  });
123
123
  }
124
124
  }
@@ -0,0 +1,14 @@
1
+ import WriteAccessHandler from "./writeAccessHandler";
2
+ import { NodeSDKConfig } from "./nodeSDKTypes";
3
+ /**
4
+ * LiquidatorTool
5
+ * Methods to liquidate traders
6
+ */
7
+ export default class LiquidatorTool extends WriteAccessHandler {
8
+ /**
9
+ * Constructor
10
+ * @param config configuration
11
+ * @param privateKey private key of account that trades
12
+ */
13
+ constructor(config: NodeSDKConfig, privateKey: string);
14
+ }
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ const writeAccessHandler_1 = __importDefault(require("./writeAccessHandler"));
7
+ /**
8
+ * LiquidatorTool
9
+ * Methods to liquidate traders
10
+ */
11
+ class LiquidatorTool extends writeAccessHandler_1.default {
12
+ /**
13
+ * Constructor
14
+ * @param config configuration
15
+ * @param privateKey private key of account that trades
16
+ */
17
+ constructor(config, privateKey) {
18
+ super(config, privateKey);
19
+ }
20
+ }
21
+ exports.default = LiquidatorTool;
@@ -28,7 +28,7 @@ export default class LiquidityProviderTool extends WriteAccessHandler {
28
28
  * @param {number} amountCC Amount in pool-collateral currency
29
29
  * @return Transaction object
30
30
  */
31
- addLiquidity(poolSymbolName: string, amountCC: number): Promise<ethers.ContractTransaction>;
31
+ addLiquidity(poolSymbolName: string, amountCC: number): Promise<ethers.providers.TransactionResponse>;
32
32
  /**
33
33
  * Remove liquidity from the pool.
34
34
  * @param {string} poolSymbolName Name of pool symbol (e.g. MATIC).
@@ -9,11 +9,6 @@ import { Order } from "./nodeSDKTypes";
9
9
  export default class MarketData extends PerpetualDataHandler {
10
10
  constructor(config: NodeSDKConfig);
11
11
  createProxyInstance(): Promise<void>;
12
- /**
13
- * Get contract instance. Useful for event listening.
14
- * @returns read-only proxy instance
15
- */
16
- getReadOnlyProxyInstance(): ethers.Contract;
17
12
  /**
18
13
  * Information about the products traded in the exchange.
19
14
  * @returns {ExchangeInfo} Array of static data for all the pools and perpetuals in the system.
@@ -36,26 +31,6 @@ export default class MarketData extends PerpetualDataHandler {
36
31
  * @returns {MarginAccount}
37
32
  */
38
33
  positionRisk(traderAddr: string, symbol: string): Promise<MarginAccount>;
39
- /**
40
- * Uses the Oracle(s) in the exchange to get the latest price of a given index in a given currency, if a route exists.
41
- * @param {string} base Index name, e.g. ETH.
42
- * @param {string} quote Quote currency, e.g. USD.
43
- * @returns {number} Price of index in given currency.
44
- */
45
- getOraclePrice(base: string, quote: string): Promise<number | undefined>;
46
- /**
47
- * Get the current mark price
48
- * @param symbol symbol of the form ETH-USD-MATIC
49
- * @returns mark price
50
- */
51
- getMarkPrice(symbol: string): Promise<number>;
52
- /**
53
- * get the current price for a given quantity
54
- * @param symbol symbol of the form ETH-USD-MATIC
55
- * @param quantity quantity to be traded, negative if short
56
- * @returns price (number)
57
- */
58
- getPerpetualPrice(symbol: string, quantity: number): Promise<number>;
59
34
  /**
60
35
  * Query smart contract to get user orders and convert to user friendly order format.
61
36
  * @param {string} traderAddr Address of trader.
@@ -31,16 +31,6 @@ class MarketData extends perpetualDataHandler_1.default {
31
31
  yield this.initContractsAndData(this.provider);
32
32
  });
33
33
  }
34
- /**
35
- * Get contract instance. Useful for event listening.
36
- * @returns read-only proxy instance
37
- */
38
- getReadOnlyProxyInstance() {
39
- if (this.proxyContract == null) {
40
- throw Error("no proxy contract initialized. Use createProxyInstance().");
41
- }
42
- return this.proxyContract;
43
- }
44
34
  /**
45
35
  * Information about the products traded in the exchange.
46
36
  * @returns {ExchangeInfo} Array of static data for all the pools and perpetuals in the system.
@@ -85,48 +75,6 @@ class MarketData extends perpetualDataHandler_1.default {
85
75
  return mgnAcct;
86
76
  });
87
77
  }
88
- /**
89
- * Uses the Oracle(s) in the exchange to get the latest price of a given index in a given currency, if a route exists.
90
- * @param {string} base Index name, e.g. ETH.
91
- * @param {string} quote Quote currency, e.g. USD.
92
- * @returns {number} Price of index in given currency.
93
- */
94
- getOraclePrice(base, quote) {
95
- return __awaiter(this, void 0, void 0, function* () {
96
- if (this.proxyContract == null) {
97
- throw Error("no proxy contract initialized. Use createProxyInstance().");
98
- }
99
- let px = yield this.proxyContract.getOraclePrice([(0, utils_1.toBytes4)(base), (0, utils_1.toBytes4)(quote)]);
100
- return px == undefined ? undefined : (0, d8XMath_1.ABK64x64ToFloat)(px);
101
- });
102
- }
103
- /**
104
- * Get the current mark price
105
- * @param symbol symbol of the form ETH-USD-MATIC
106
- * @returns mark price
107
- */
108
- getMarkPrice(symbol) {
109
- return __awaiter(this, void 0, void 0, function* () {
110
- if (this.proxyContract == null) {
111
- throw Error("no proxy contract initialized. Use createProxyInstance().");
112
- }
113
- return yield perpetualDataHandler_1.default._queryPerpetualMarkPrice(symbol, this.symbolToPerpStaticInfo, this.proxyContract);
114
- });
115
- }
116
- /**
117
- * get the current price for a given quantity
118
- * @param symbol symbol of the form ETH-USD-MATIC
119
- * @param quantity quantity to be traded, negative if short
120
- * @returns price (number)
121
- */
122
- getPerpetualPrice(symbol, quantity) {
123
- return __awaiter(this, void 0, void 0, function* () {
124
- if (this.proxyContract == null) {
125
- throw Error("no proxy contract initialized. Use createProxyInstance().");
126
- }
127
- return yield perpetualDataHandler_1.default._queryPerpetualPrice(symbol, quantity, this.symbolToPerpStaticInfo, this.proxyContract);
128
- });
129
- }
130
78
  /**
131
79
  * Query smart contract to get user orders and convert to user friendly order format.
132
80
  * @param {string} traderAddr Address of trader.
@@ -9,7 +9,6 @@ export declare const COLLATERAL_CURRENCY_BASE = 1;
9
9
  export declare const COLLATERAL_CURRENCY_QUANTO = 2;
10
10
  export declare const PERP_STATE_STR: string[];
11
11
  export declare const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
12
- export declare const ZERO_ORDER_ID = "0x0000000000000000000000000000000000000000000000000000000000000000";
13
12
  export declare const ONE_64x64: BigNumber;
14
13
  export declare const MAX_64x64: BigNumber;
15
14
  export declare const MAX_UINT_256: BigNumber;
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.CollaterlCCY = exports.CLOSED_SIDE = exports.SELL_SIDE = exports.BUY_SIDE = exports.ORDER_TYPE_STOP_LIMIT = exports.ORDER_TYPE_STOP_MARKET = exports.ORDER_TYPE_MARKET = exports.ORDER_TYPE_LIMIT = exports.MASK_KEEP_POS_LEVERAGE = exports.MASK_STOP_ORDER = exports.MASK_MARKET_ORDER = exports.MASK_LIMIT_ORDER = exports.MASK_CLOSE_ONLY = exports.ORDER_MAX_DURATION_SEC = exports.DECIMALS = exports.MAX_UINT_256 = exports.MAX_64x64 = exports.ONE_64x64 = exports.ZERO_ORDER_ID = exports.ZERO_ADDRESS = exports.PERP_STATE_STR = exports.COLLATERAL_CURRENCY_QUANTO = exports.COLLATERAL_CURRENCY_BASE = exports.COLLATERAL_CURRENCY_QUOTE = exports.ERC20_ABI = exports.DEFAULT_CONFIG_MAINNET_NAME = exports.DEFAULT_CONFIG_TESTNET_NAME = exports.DEFAULT_CONFIG_MAINNET = exports.DEFAULT_CONFIG_TESTNET = void 0;
3
+ exports.CollaterlCCY = exports.CLOSED_SIDE = exports.SELL_SIDE = exports.BUY_SIDE = exports.ORDER_TYPE_STOP_LIMIT = exports.ORDER_TYPE_STOP_MARKET = exports.ORDER_TYPE_MARKET = exports.ORDER_TYPE_LIMIT = exports.MASK_KEEP_POS_LEVERAGE = exports.MASK_STOP_ORDER = exports.MASK_MARKET_ORDER = exports.MASK_LIMIT_ORDER = exports.MASK_CLOSE_ONLY = exports.ORDER_MAX_DURATION_SEC = exports.DECIMALS = exports.MAX_UINT_256 = exports.MAX_64x64 = exports.ONE_64x64 = exports.ZERO_ADDRESS = exports.PERP_STATE_STR = exports.COLLATERAL_CURRENCY_QUANTO = exports.COLLATERAL_CURRENCY_BASE = exports.COLLATERAL_CURRENCY_QUOTE = exports.ERC20_ABI = exports.DEFAULT_CONFIG_MAINNET_NAME = exports.DEFAULT_CONFIG_TESTNET_NAME = exports.DEFAULT_CONFIG_MAINNET = exports.DEFAULT_CONFIG_TESTNET = void 0;
4
4
  const ethers_1 = require("ethers");
5
5
  exports.DEFAULT_CONFIG_TESTNET = "../config/defaultConfig.json";
6
6
  exports.DEFAULT_CONFIG_MAINNET = "notthereyet";
@@ -11,8 +11,7 @@ exports.COLLATERAL_CURRENCY_QUOTE = 0;
11
11
  exports.COLLATERAL_CURRENCY_BASE = 1;
12
12
  exports.COLLATERAL_CURRENCY_QUANTO = 2;
13
13
  exports.PERP_STATE_STR = ["INVALID", "INITIALIZING", "NORMAL", "EMERGENCY", "CLEARED"];
14
- exports.ZERO_ADDRESS = ethers_1.constants.AddressZero;
15
- exports.ZERO_ORDER_ID = ethers_1.constants.HashZero;
14
+ exports.ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
16
15
  exports.ONE_64x64 = ethers_1.BigNumber.from("0x010000000000000000");
17
16
  exports.MAX_64x64 = ethers_1.BigNumber.from("0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
18
17
  exports.MAX_UINT_256 = ethers_1.BigNumber.from(2).pow(256).sub(ethers_1.BigNumber.from(1));
@@ -19,7 +19,7 @@ export default class OrderReferrerTool extends WriteAccessHandler {
19
19
  * if different from the one submitting this transaction.
20
20
  * @returns Transaction object.
21
21
  */
22
- executeOrder(symbol: string, orderId: string, referrerAddr?: string): Promise<ethers.ContractTransaction>;
22
+ executeOrder(symbol: string, orderId: string, referrerAddr?: string): Promise<ethers.providers.TransactionResponse>;
23
23
  /**
24
24
  * All the orders in the order book for a given symbol that are currently open.
25
25
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
@@ -32,26 +32,14 @@ export default class OrderReferrerTool extends WriteAccessHandler {
32
32
  * @returns {number} Number of open orders.
33
33
  */
34
34
  numberOfOpenOrders(symbol: string): Promise<number>;
35
- /**
36
- * Get order from the digest (=id)
37
- * @param symbol symbol of order book, e.g. ETH-USD-MATIC
38
- * @param digest digest of the order (=order ID)
39
- * @returns order or undefined
40
- */
41
- getOrderById(symbol: string, id: string): Promise<Order | undefined>;
42
35
  /**
43
36
  * Get a list of active conditional orders in the order book.
44
37
  * This a read-only action and does not incur in gas costs.
45
38
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
46
39
  * @param {number} numElements Maximum number of orders to poll.
47
40
  * @param {string=} startAfter Optional order ID from where to start polling. Defaults to the first order.
48
- * @returns Array of orders and corresponding order IDs
41
+ * @returns {[Order[], string[]]} Array of orders and corresponding order IDs
49
42
  */
50
43
  pollLimitOrders(symbol: string, numElements: number, startAfter?: string): Promise<[Order[], string[]]>;
51
- /**
52
- * Check if a conditional order can be executed
53
- * @param order order structure
54
- * @returns true if order can be executed for the current state of the perpetuals
55
- */
56
44
  isTradeable(order: Order): Promise<boolean>;
57
45
  }
@@ -71,30 +71,13 @@ class OrderReferrerTool extends writeAccessHandler_1.default {
71
71
  return yield orderBookSC.numberOfOrderBookDigests();
72
72
  });
73
73
  }
74
- /**
75
- * Get order from the digest (=id)
76
- * @param symbol symbol of order book, e.g. ETH-USD-MATIC
77
- * @param digest digest of the order (=order ID)
78
- * @returns order or undefined
79
- */
80
- getOrderById(symbol, id) {
81
- return __awaiter(this, void 0, void 0, function* () {
82
- let ob = yield this.getOrderBookContract(symbol);
83
- let smartContractOrder = yield ob.orderOfDigest(id);
84
- if (smartContractOrder.traderAddr == nodeSDKTypes_1.ZERO_ADDRESS) {
85
- return undefined;
86
- }
87
- let order = OrderReferrerTool.fromSmartContractOrder(smartContractOrder, this.symbolToPerpStaticInfo);
88
- return order;
89
- });
90
- }
91
74
  /**
92
75
  * Get a list of active conditional orders in the order book.
93
76
  * This a read-only action and does not incur in gas costs.
94
77
  * @param {string} symbol Symbol of the form ETH-USD-MATIC.
95
78
  * @param {number} numElements Maximum number of orders to poll.
96
79
  * @param {string=} startAfter Optional order ID from where to start polling. Defaults to the first order.
97
- * @returns Array of orders and corresponding order IDs
80
+ * @returns {[Order[], string[]]} Array of orders and corresponding order IDs
98
81
  */
99
82
  pollLimitOrders(symbol, numElements, startAfter) {
100
83
  return __awaiter(this, void 0, void 0, function* () {
@@ -102,7 +85,7 @@ class OrderReferrerTool extends writeAccessHandler_1.default {
102
85
  throw Error("no proxy contract initialized. Use createProxyInstance().");
103
86
  }
104
87
  if (typeof startAfter == "undefined") {
105
- startAfter = nodeSDKTypes_1.ZERO_ORDER_ID;
88
+ startAfter = nodeSDKTypes_1.ZERO_ADDRESS;
106
89
  }
107
90
  const orderBookSC = this.getOrderBookContract(symbol);
108
91
  let [orders, orderIds] = yield orderBookSC.pollLimitOrders(startAfter, numElements);
@@ -115,11 +98,6 @@ class OrderReferrerTool extends writeAccessHandler_1.default {
115
98
  return [userFriendlyOrders, orderIds];
116
99
  });
117
100
  }
118
- /**
119
- * Check if a conditional order can be executed
120
- * @param order order structure
121
- * @returns true if order can be executed for the current state of the perpetuals
122
- */
123
101
  isTradeable(order) {
124
102
  return __awaiter(this, void 0, void 0, function* () {
125
103
  if (this.proxyContract == null) {
@@ -129,7 +107,7 @@ class OrderReferrerTool extends writeAccessHandler_1.default {
129
107
  throw Error("order does not have a limit price");
130
108
  }
131
109
  // check expiration date
132
- if (order.deadline != undefined && order.deadline < Date.now() / 1000) {
110
+ if (order.deadline != undefined && order.deadline < Date.now()) {
133
111
  return false;
134
112
  }
135
113
  // check limit price
@@ -229,7 +229,7 @@ class PerpetualDataHandler {
229
229
  return __awaiter(this, void 0, void 0, function* () {
230
230
  let perpId = PerpetualDataHandler.symbolToPerpetualId(symbol, symbolToPerpStaticInfo);
231
231
  let ammState = yield _proxyContract.getAMMState(perpId);
232
- return (0, d8XMath_1.ABK64x64ToFloat)(ammState[6].mul(nodeSDKTypes_1.ONE_64x64.add(ammState[8])).div(nodeSDKTypes_1.ONE_64x64));
232
+ return (0, d8XMath_1.ABK64x64ToFloat)(ammState[6].mul(nodeSDKTypes_1.ONE_64x64.add(ammState[8])));
233
233
  });
234
234
  }
235
235
  /**
@@ -310,20 +310,16 @@ class PerpetualDataHandler {
310
310
  // concatenate and find perpetual Id in map
311
311
  return symbols[0] + "-" + symbols[1] + "-" + symbols[2];
312
312
  }
313
- static _getByValue(map, searchValue, valueField) {
313
+ static _getByValue(map, searchValue) {
314
314
  for (let [key, value] of map.entries()) {
315
- if (value[valueField] === searchValue) {
315
+ if (value === searchValue) {
316
316
  return key;
317
317
  }
318
318
  }
319
- return undefined;
320
319
  }
321
320
  static fromSmartContractOrder(order, symbolToPerpInfoMap) {
322
321
  // find symbol of perpetual id
323
- let symbol = PerpetualDataHandler._getByValue(symbolToPerpInfoMap, order.iPerpetualId, "id");
324
- if (symbol == undefined) {
325
- throw Error(`Perpetual id ${order.iPerpetualId} not found. Check with marketData.exchangeInfo().`);
326
- }
322
+ let symbol = PerpetualDataHandler._getByValue(symbolToPerpInfoMap, order.iPerpetualId);
327
323
  let side = order.fAmount > 0 ? nodeSDKTypes_1.BUY_SIDE : nodeSDKTypes_1.SELL_SIDE;
328
324
  let limitPrice, stopPrice;
329
325
  let fLimitPrice = ethers_1.BigNumber.from(order.fLimitPrice);
@@ -348,9 +344,9 @@ class PerpetualDataHandler {
348
344
  reduceOnly: (0, utils_1.containsFlag)(ethers_1.BigNumber.from(order.flags), nodeSDKTypes_1.MASK_CLOSE_ONLY),
349
345
  limitPrice: limitPrice,
350
346
  keepPositionLvg: (0, utils_1.containsFlag)(ethers_1.BigNumber.from(order.flags), nodeSDKTypes_1.MASK_KEEP_POS_LEVERAGE),
351
- brokerFeeTbps: order.brokerFeeTbps == 0 ? undefined : Number(order.brokerFeeTbps),
352
- brokerAddr: order.brokerAddr == nodeSDKTypes_1.ZERO_ADDRESS ? undefined : order.brokerAddr,
353
- brokerSignature: order.brokerSignature == "0x" ? undefined : order.brokerSignature,
347
+ brokerFeeTbps: Number(order.brokerFeeTbps),
348
+ brokerAddr: order.brokerAddr,
349
+ brokerSignature: order.brokerSignature,
354
350
  stopPrice: stopPrice,
355
351
  leverage: (0, d8XMath_1.ABK64x64ToFloat)(ethers_1.BigNumber.from(order.fLeverage)),
356
352
  deadline: Number(order.iDeadline),
@@ -29,8 +29,8 @@ export default class WriteAccessHandler extends PerpetualDataHandler {
29
29
  * Set allowance for ar margin token (e.g., MATIC, ETH, USDC)
30
30
  * @param symbol token in 'long-form' such as MATIC, symbol also fine (ETH-USD-MATIC)
31
31
  * @param amount optional, amount to approve if not 'infinity'
32
- * @returns ContractTransaction
32
+ * @returns transaction hash
33
33
  */
34
- setAllowance(symbol: string, amount?: number | undefined): Promise<ethers.ContractTransaction>;
35
- protected static _setAllowance(tokenAddr: string, proxyAddr: string, signer: ethers.Wallet, amount: BigNumber): Promise<ethers.ContractTransaction>;
34
+ setAllowance(symbol: string, amount?: number | undefined): Promise<string>;
35
+ protected static _setAllowance(tokenAddr: string, proxyAddr: string, signer: ethers.Wallet, amount: BigNumber): Promise<string>;
36
36
  }
@@ -59,7 +59,7 @@ class WriteAccessHandler extends perpetualDataHandler_1.default {
59
59
  * Set allowance for ar margin token (e.g., MATIC, ETH, USDC)
60
60
  * @param symbol token in 'long-form' such as MATIC, symbol also fine (ETH-USD-MATIC)
61
61
  * @param amount optional, amount to approve if not 'infinity'
62
- * @returns ContractTransaction
62
+ * @returns transaction hash
63
63
  */
64
64
  setAllowance(symbol, amount = undefined) {
65
65
  return __awaiter(this, void 0, void 0, function* () {
@@ -87,7 +87,8 @@ class WriteAccessHandler extends perpetualDataHandler_1.default {
87
87
  return __awaiter(this, void 0, void 0, function* () {
88
88
  const marginToken = new ethers_1.ethers.Contract(tokenAddr, nodeSDKTypes_1.ERC20_ABI, signer);
89
89
  let tx = yield marginToken.approve(proxyAddr, amount);
90
- return tx;
90
+ yield tx.wait();
91
+ return tx.hash;
91
92
  });
92
93
  }
93
94
  }
package/package.json CHANGED
@@ -27,7 +27,7 @@
27
27
  },
28
28
  "name": "@d8x/perpetuals-sdk",
29
29
  "description": "Node TypeScript SDK for D8X Perpetual Futures",
30
- "version": "0.0.4",
30
+ "version": "0.0.6",
31
31
  "main": "./dist/index.js",
32
32
  "types": "./dist/index.d.ts",
33
33
  "directories": {
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@ 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
  // import {
9
10
  // NodeSDKConfig,
10
11
  // MarginAccount,
@@ -29,4 +30,5 @@ export {
29
30
  OrderReferrerTool,
30
31
  PerpetualDataHandler,
31
32
  WriteAccessHandler,
33
+ LiquidatorTool,
32
34
  };