@d8x/perpetuals-sdk 0.0.2 → 0.0.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 CHANGED
@@ -15,3 +15,10 @@ Node TypeScript SDK for D8X Perpetual Futures
15
15
  ### Test
16
16
 
17
17
  `npm test`
18
+
19
+ ### NPM Package Deployment
20
+
21
+ `npm run build`
22
+ `npm login`
23
+ increase version via `npm version patch`
24
+ `npm publish`
@@ -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 {string} Transaction hash.
25
+ * @returns {ContractTransaction} Contract Transaction (containing events).
26
26
  */
27
- order(order: Order): Promise<string | undefined>;
27
+ order(order: Order): Promise<ethers.ContractTransaction>;
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).
@@ -35,6 +35,13 @@ export default class AccountTrade extends WriteAccessHandler {
35
35
  * @returns Exchange fee, in decimals (i.e. 0.1% is 0.001).
36
36
  */
37
37
  queryExchangeFee(poolSymbolName: string, brokerAddr?: string): Promise<number>;
38
+ /**
39
+ * Exponentially weighted EMA of the total trading volume of all trades performed by this trader.
40
+ * The weights are chosen so that in average this coincides with the 30 day volume.
41
+ * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
42
+ * @returns {number} Current trading volume for this trader, in USD.
43
+ */
44
+ getCurrentTraderVolume(poolSymbolName: string): Promise<number>;
38
45
  /**
39
46
  * Static order function
40
47
  * @param order order type (not SmartContractOrder but Order)
@@ -48,7 +55,7 @@ export default class AccountTrade extends WriteAccessHandler {
48
55
  * @returns transaction hash
49
56
  * @ignore
50
57
  */
51
- _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>;
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>;
52
59
  protected _cancelOrder(symbol: string, orderId: string, orderBookContract: ethers.Contract | null): Promise<string | undefined>;
53
60
  /**
54
61
  * Creates a signature
@@ -13,6 +13,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
13
13
  };
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
15
  const ethers_1 = require("ethers");
16
+ const d8XMath_1 = require("./d8XMath");
16
17
  const nodeSDKTypes_1 = require("./nodeSDKTypes");
17
18
  const writeAccessHandler_1 = __importDefault(require("./writeAccessHandler"));
18
19
  /**
@@ -52,7 +53,7 @@ class AccountTrade extends writeAccessHandler_1.default {
52
53
  /**
53
54
  * Submits an order to the exchange.
54
55
  * @param {Order} order Order struct.
55
- * @returns {string} Transaction hash.
56
+ * @returns {ContractTransaction} Contract Transaction (containing events).
56
57
  */
57
58
  order(order) {
58
59
  return __awaiter(this, void 0, void 0, function* () {
@@ -88,6 +89,22 @@ class AccountTrade extends writeAccessHandler_1.default {
88
89
  return feeTbps / 100000;
89
90
  });
90
91
  }
92
+ /**
93
+ * Exponentially weighted EMA of the total trading volume of all trades performed by this trader.
94
+ * The weights are chosen so that in average this coincides with the 30 day volume.
95
+ * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
96
+ * @returns {number} Current trading volume for this trader, in USD.
97
+ */
98
+ getCurrentTraderVolume(poolSymbolName) {
99
+ return __awaiter(this, void 0, void 0, function* () {
100
+ if (this.proxyContract == null || this.signer == null) {
101
+ throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
102
+ }
103
+ let poolId = writeAccessHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
104
+ let volume = yield this.proxyContract.getCurrentTraderVolume(poolId, this.traderAddr);
105
+ return (0, d8XMath_1.ABK64x64ToFloat)(volume);
106
+ });
107
+ }
91
108
  /**
92
109
  * Static order function
93
110
  * @param order order type (not SmartContractOrder but Order)
@@ -119,7 +136,7 @@ class AccountTrade extends writeAccessHandler_1.default {
119
136
  let signature = yield this._createSignature(scOrder, chainId, true, signer, proxyContract.address);
120
137
  tx = yield orderBookContract.createLimitOrder(scOrder, signature, { gasLimit: gasLimit });
121
138
  }
122
- return tx.hash;
139
+ return tx;
123
140
  });
124
141
  }
125
142
  _cancelOrder(symbol, orderId, orderBookContract) {
@@ -11,35 +11,20 @@ export default class BrokerTool extends WriteAccessHandler {
11
11
  * @param {string} privateKey Private key of a broker.
12
12
  */
13
13
  constructor(config: NodeSDKConfig, privateKey: string);
14
- getBrokerInducedFee(poolSymbolName: string): Promise<number>;
15
- /**
16
- * Total amount of collateral currency a broker has to deposit into the default fund to purchase one lot.
17
- * This is equivalent to the price of a lot expressed in a given pool's currency (e.g. MATIC, USDC, etc).
18
- * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
19
- * @returns Broker lot size in a given pool's currency, e.g. in MATIC for poolSymbolName MATIC.
20
- */
21
- getLotSize(poolSymbolName: string): Promise<number>;
22
- /**
23
- * Provides information on how many lots a broker purchased for a given pool.
24
- * This is relevant to determine the broker's fee tier.
25
- * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
26
- * @returns Number of lots purchased by this broker.
27
- */
28
- getBrokerDesignation(poolSymbolName: string): Promise<number>;
29
14
  /**
30
- * Deposit lots to the default fund of a given pool.
15
+ * Determine the exchange fee based on lots, traded volume, and D8X balance of this broker.
16
+ * This is the final exchange fee paid by the broker.
31
17
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
32
- * @param lots Number of lots to deposit into this pool.
33
- * @returns Transaction object.
18
+ * @returns {number} Exchange fee for this broker, in decimals (i.e. 0.1% is 0.001)
34
19
  */
35
- brokerDepositToDefaultFund(poolSymbolName: string, lots: number): Promise<ethers.providers.TransactionResponse>;
20
+ getBrokerInducedFee(poolSymbolName: string): Promise<number>;
36
21
  /**
37
22
  * Determine the exchange fee based on lots purchased by this broker.
38
23
  * The final exchange fee paid by the broker is equal to
39
24
  * maximum(brokerTool.getFeeForBrokerDesignation(poolSymbolName), brokerTool.getFeeForBrokerVolume(poolSymbolName), brokerTool.getFeeForBrokerStake())
40
25
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
41
- * @param {number} lots Optional, designation to use if different from this broker's.
42
- * @returns Fee based solely on this broker's designation, in decimals (i.e. 0.1% is 0.001).
26
+ * @param {number=} lots Optional, designation to use if different from this broker's.
27
+ * @returns {number} Fee based solely on this broker's designation, in decimals (i.e. 0.1% is 0.001).
43
28
  */
44
29
  getFeeForBrokerDesignation(poolSymbolName: string, lots?: number): Promise<number>;
45
30
  /**
@@ -47,15 +32,15 @@ export default class BrokerTool extends WriteAccessHandler {
47
32
  * The final exchange fee paid by the broker is equal to
48
33
  * maximum(brokerTool.getFeeForBrokerDesignation(poolSymbolName), brokerTool.getFeeForBrokerVolume(poolSymbolName), brokerTool.getFeeForBrokerStake())
49
34
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
50
- * @returns Fee based solely on a broker's traded volume in the corresponding pool, in decimals (i.e. 0.1% is 0.001).
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).
51
36
  */
52
- getFeeForBrokerVolume(poolSymbolName: string): Promise<number | undefined>;
37
+ getFeeForBrokerVolume(poolSymbolName: string): Promise<number>;
53
38
  /**
54
39
  * Determine the exchange fee based on the current D8X balance in a broker's wallet.
55
40
  * The final exchange fee paid by the broker is equal to
56
41
  * maximum(brokerTool.getFeeForBrokerDesignation(symbol, lots), brokerTool.getFeeForBrokerVolume(symbol), brokerTool.getFeeForBrokerStake)
57
42
  * @param {string=} brokerAddr Address of the broker in question, if different from the one calling this function.
58
- * @returns Fee based solely on a broker's D8X balance, in decimals (i.e. 0.1% is 0.001).
43
+ * @returns {number} Fee based solely on a broker's D8X balance, in decimals (i.e. 0.1% is 0.001).
59
44
  */
60
45
  getFeeForBrokerStake(brokerAddr?: string): Promise<number>;
61
46
  /**
@@ -64,18 +49,47 @@ export default class BrokerTool extends WriteAccessHandler {
64
49
  * and it takes into account whether the order given here has been signed by a broker or not.
65
50
  * Use this, for instance, to verify that the fee to be charged for a given order is as expected,
66
51
  * before and after signing it with brokerTool.signOrder.
52
+ * This fee is equal or lower than the broker induced fee, provided the order is properly signed.
67
53
  * @param {Order} order Order for which to determine the exchange fee. Not necessarily signed by this broker.
68
54
  * @param {string} traderAddr Address of the trader for whom to determine the fee.
69
- * @returns Fee in decimals (i.e. 0.1% is 0.001).
55
+ * @returns {number} Fee in decimals (i.e. 0.1% is 0.001).
70
56
  */
71
57
  determineExchangeFee(order: Order, traderAddr: string): Promise<number>;
58
+ /**
59
+ * Exponentially weighted EMA of the total trading volume of all trades performed under this broker.
60
+ * The weights are chosen so that in average this coincides with the 30 day volume.
61
+ * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
62
+ * @returns {number} Current trading volume for this broker, in USD.
63
+ */
64
+ getCurrentBrokerVolume(poolSymbolName: string): Promise<number>;
65
+ /**
66
+ * Total amount of collateral currency a broker has to deposit into the default fund to purchase one lot.
67
+ * This is equivalent to the price of a lot expressed in a given pool's currency (e.g. MATIC, USDC, etc).
68
+ * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
69
+ * @returns {number} Broker lot size in a given pool's currency, e.g. in MATIC for poolSymbolName MATIC.
70
+ */
71
+ getLotSize(poolSymbolName: string): Promise<number>;
72
+ /**
73
+ * Provides information on how many lots a broker purchased for a given pool.
74
+ * This is relevant to determine the broker's fee tier.
75
+ * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
76
+ * @returns {number} Number of lots purchased by this broker.
77
+ */
78
+ getBrokerDesignation(poolSymbolName: string): Promise<number>;
79
+ /**
80
+ * Deposit lots to the default fund of a given pool.
81
+ * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
82
+ * @param {number} lots Number of lots to deposit into this pool.
83
+ * @returns {ethers.ContractTransaction} ContractTransaction object.
84
+ */
85
+ brokerDepositToDefaultFund(poolSymbolName: string, lots: number): Promise<ethers.ContractTransaction>;
72
86
  /**
73
87
  * Adds this broker's signature to an order so that it can be submitted by an approved trader.
74
88
  * @param {Order} order Order to sign.
75
89
  * @param {string} traderAddr Address of trader submitting the order.
76
90
  * @param {number} feeDecimals Fee that this broker is approving for the trader.
77
91
  * @param {number} deadline Deadline for the order to be executed.
78
- * @returns An order signed by this broker, which can be submitted directly with AccountTrade.order.
92
+ * @returns {Order} An order signed by this broker, which can be submitted directly with AccountTrade.order.
79
93
  */
80
94
  signOrder(order: Order, traderAddr: string, brokerFee: number, deadline: number): Promise<Order>;
81
95
  /**
@@ -85,16 +99,16 @@ export default class BrokerTool extends WriteAccessHandler {
85
99
  * @param {string} symbol Perpetual that this trader will be trading, of the form ETH-USD-MATIC.
86
100
  * @param {number} brokerFee Broker fee for this trader, in decimals (i.e. 0.1% is 0.001).
87
101
  * @param {number} deadline Deadline for the order to be executed.
88
- * @returns Broker signature approving this trader's fee, symbol, and deadline.
102
+ * @returns {string} Broker signature approving this trader's fee, symbol, and deadline.
89
103
  * @ignore
90
104
  */
91
105
  createSignatureForTrader(traderAddr: string, symbol: string, brokerFee: number, deadline: number): Promise<string>;
92
106
  static _signOrder(symbol: string, brokerFeeTbps: number, traderAddr: string, iDeadline: BigNumber, signer: ethers.Wallet, chainId: number, proxyAddress: string, symbolToPerpStaticInfo: Map<string, PerpetualStaticInfo>): Promise<string>;
93
107
  /**
94
108
  * Transfer ownership of a broker's status to a new wallet.
95
- * @param poolSymbolName Symbol refers to the pool (e.g., MATIC for the MATIC-pool)
96
- * @param newAddress The address this broker wants to use from now on.
97
- * @returns ethers transaction object
109
+ * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
110
+ * @param {string} newAddress The address this broker wants to use from now on.
111
+ * @returns {ethers.providers.TransactionResponse} ethers transaction object
98
112
  */
99
113
  transferOwnership(poolSymbolName: string, newAddress: string): Promise<ethers.providers.TransactionResponse>;
100
114
  }
@@ -29,63 +29,21 @@ class BrokerTool extends writeAccessHandler_1.default {
29
29
  constructor(config, privateKey) {
30
30
  super(config, privateKey);
31
31
  }
32
- getBrokerInducedFee(poolSymbolName) {
33
- return __awaiter(this, void 0, void 0, function* () {
34
- if (this.proxyContract == null || this.signer == null) {
35
- throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
36
- }
37
- let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
38
- let feeTbps = yield this.proxyContract.getBrokerInducedFee(poolId, this.traderAddr);
39
- return feeTbps / 100000;
40
- });
41
- }
42
- /**
43
- * Total amount of collateral currency a broker has to deposit into the default fund to purchase one lot.
44
- * This is equivalent to the price of a lot expressed in a given pool's currency (e.g. MATIC, USDC, etc).
45
- * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
46
- * @returns Broker lot size in a given pool's currency, e.g. in MATIC for poolSymbolName MATIC.
47
- */
48
- getLotSize(poolSymbolName) {
49
- return __awaiter(this, void 0, void 0, function* () {
50
- if (this.proxyContract == null) {
51
- throw Error("no proxy contract initialized. Use createProxyInstance().");
52
- }
53
- let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
54
- let pool = yield this.proxyContract.getLiquidityPool(poolId);
55
- let lot = (0, d8XMath_1.ABK64x64ToFloat)(pool.fBrokerCollateralLotSize);
56
- return lot;
57
- });
58
- }
32
+ // Fee getters
59
33
  /**
60
- * Provides information on how many lots a broker purchased for a given pool.
61
- * This is relevant to determine the broker's fee tier.
62
- * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
63
- * @returns Number of lots purchased by this broker.
64
- */
65
- getBrokerDesignation(poolSymbolName) {
66
- return __awaiter(this, void 0, void 0, function* () {
67
- if (this.proxyContract == null || this.signer == null) {
68
- throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
69
- }
70
- let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
71
- let designation = yield this.proxyContract.getBrokerDesignation(poolId, this.traderAddr);
72
- return designation;
73
- });
74
- }
75
- /**
76
- * Deposit lots to the default fund of a given pool.
34
+ * 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.
77
36
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
78
- * @param lots Number of lots to deposit into this pool.
79
- * @returns Transaction object.
37
+ * @returns {number} Exchange fee for this broker, in decimals (i.e. 0.1% is 0.001)
80
38
  */
81
- brokerDepositToDefaultFund(poolSymbolName, lots) {
39
+ getBrokerInducedFee(poolSymbolName) {
82
40
  return __awaiter(this, void 0, void 0, function* () {
83
41
  if (this.proxyContract == null || this.signer == null) {
84
42
  throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
85
43
  }
86
44
  let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
87
- let tx = yield this.proxyContract.brokerDepositToDefaultFund(poolId, lots, { gasLimit: this.gasLimit });
88
- return tx;
45
+ let feeTbps = yield this.proxyContract.getBrokerInducedFee(poolId, this.traderAddr);
46
+ return feeTbps / 100000;
89
47
  });
90
48
  }
91
49
  /**
@@ -93,8 +51,8 @@ class BrokerTool extends writeAccessHandler_1.default {
93
51
  * The final exchange fee paid by the broker is equal to
94
52
  * maximum(brokerTool.getFeeForBrokerDesignation(poolSymbolName), brokerTool.getFeeForBrokerVolume(poolSymbolName), brokerTool.getFeeForBrokerStake())
95
53
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
96
- * @param {number} lots Optional, designation to use if different from this broker's.
97
- * @returns Fee based solely on this broker's designation, in decimals (i.e. 0.1% is 0.001).
54
+ * @param {number=} lots Optional, designation to use if different from this broker's.
55
+ * @returns {number} Fee based solely on this broker's designation, in decimals (i.e. 0.1% is 0.001).
98
56
  */
99
57
  getFeeForBrokerDesignation(poolSymbolName, lots) {
100
58
  return __awaiter(this, void 0, void 0, function* () {
@@ -118,7 +76,7 @@ class BrokerTool extends writeAccessHandler_1.default {
118
76
  * The final exchange fee paid by the broker is equal to
119
77
  * maximum(brokerTool.getFeeForBrokerDesignation(poolSymbolName), brokerTool.getFeeForBrokerVolume(poolSymbolName), brokerTool.getFeeForBrokerStake())
120
78
  * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
121
- * @returns Fee based solely on a broker's traded volume in the corresponding pool, in decimals (i.e. 0.1% is 0.001).
79
+ * @returns {number} Fee based solely on a broker's traded volume in the corresponding pool, in decimals (i.e. 0.1% is 0.001).
122
80
  */
123
81
  getFeeForBrokerVolume(poolSymbolName) {
124
82
  return __awaiter(this, void 0, void 0, function* () {
@@ -135,7 +93,7 @@ class BrokerTool extends writeAccessHandler_1.default {
135
93
  * The final exchange fee paid by the broker is equal to
136
94
  * maximum(brokerTool.getFeeForBrokerDesignation(symbol, lots), brokerTool.getFeeForBrokerVolume(symbol), brokerTool.getFeeForBrokerStake)
137
95
  * @param {string=} brokerAddr Address of the broker in question, if different from the one calling this function.
138
- * @returns Fee based solely on a broker's D8X balance, in decimals (i.e. 0.1% is 0.001).
96
+ * @returns {number} Fee based solely on a broker's D8X balance, in decimals (i.e. 0.1% is 0.001).
139
97
  */
140
98
  getFeeForBrokerStake(brokerAddr) {
141
99
  return __awaiter(this, void 0, void 0, function* () {
@@ -155,9 +113,10 @@ class BrokerTool extends writeAccessHandler_1.default {
155
113
  * and it takes into account whether the order given here has been signed by a broker or not.
156
114
  * Use this, for instance, to verify that the fee to be charged for a given order is as expected,
157
115
  * before and after signing it with brokerTool.signOrder.
116
+ * This fee is equal or lower than the broker induced fee, provided the order is properly signed.
158
117
  * @param {Order} order Order for which to determine the exchange fee. Not necessarily signed by this broker.
159
118
  * @param {string} traderAddr Address of the trader for whom to determine the fee.
160
- * @returns Fee in decimals (i.e. 0.1% is 0.001).
119
+ * @returns {number} Fee in decimals (i.e. 0.1% is 0.001).
161
120
  */
162
121
  determineExchangeFee(order, traderAddr) {
163
122
  return __awaiter(this, void 0, void 0, function* () {
@@ -169,13 +128,81 @@ class BrokerTool extends writeAccessHandler_1.default {
169
128
  return feeTbps / 100000;
170
129
  });
171
130
  }
131
+ // Volume
132
+ /**
133
+ * Exponentially weighted EMA of the total trading volume of all trades performed under this broker.
134
+ * The weights are chosen so that in average this coincides with the 30 day volume.
135
+ * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
136
+ * @returns {number} Current trading volume for this broker, in USD.
137
+ */
138
+ getCurrentBrokerVolume(poolSymbolName) {
139
+ return __awaiter(this, void 0, void 0, function* () {
140
+ if (this.proxyContract == null || this.signer == null) {
141
+ throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
142
+ }
143
+ let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
144
+ let volume = yield this.proxyContract.getCurrentBrokerVolume(poolId, this.traderAddr);
145
+ return (0, d8XMath_1.ABK64x64ToFloat)(volume);
146
+ });
147
+ }
148
+ // Lots
149
+ /**
150
+ * Total amount of collateral currency a broker has to deposit into the default fund to purchase one lot.
151
+ * This is equivalent to the price of a lot expressed in a given pool's currency (e.g. MATIC, USDC, etc).
152
+ * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
153
+ * @returns {number} Broker lot size in a given pool's currency, e.g. in MATIC for poolSymbolName MATIC.
154
+ */
155
+ getLotSize(poolSymbolName) {
156
+ return __awaiter(this, void 0, void 0, function* () {
157
+ if (this.proxyContract == null) {
158
+ throw Error("no proxy contract initialized. Use createProxyInstance().");
159
+ }
160
+ let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
161
+ let pool = yield this.proxyContract.getLiquidityPool(poolId);
162
+ let lot = (0, d8XMath_1.ABK64x64ToFloat)(pool.fBrokerCollateralLotSize);
163
+ return lot;
164
+ });
165
+ }
166
+ /**
167
+ * Provides information on how many lots a broker purchased for a given pool.
168
+ * This is relevant to determine the broker's fee tier.
169
+ * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
170
+ * @returns {number} Number of lots purchased by this broker.
171
+ */
172
+ getBrokerDesignation(poolSymbolName) {
173
+ return __awaiter(this, void 0, void 0, function* () {
174
+ if (this.proxyContract == null || this.signer == null) {
175
+ throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
176
+ }
177
+ let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
178
+ let designation = yield this.proxyContract.getBrokerDesignation(poolId, this.traderAddr);
179
+ return designation;
180
+ });
181
+ }
182
+ /**
183
+ * Deposit lots to the default fund of a given pool.
184
+ * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
185
+ * @param {number} lots Number of lots to deposit into this pool.
186
+ * @returns {ethers.ContractTransaction} ContractTransaction object.
187
+ */
188
+ brokerDepositToDefaultFund(poolSymbolName, lots) {
189
+ return __awaiter(this, void 0, void 0, function* () {
190
+ if (this.proxyContract == null || this.signer == null) {
191
+ throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
192
+ }
193
+ let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
194
+ let tx = yield this.proxyContract.brokerDepositToDefaultFund(poolId, lots, { gasLimit: this.gasLimit });
195
+ return tx;
196
+ });
197
+ }
198
+ // Signatures
172
199
  /**
173
200
  * Adds this broker's signature to an order so that it can be submitted by an approved trader.
174
201
  * @param {Order} order Order to sign.
175
202
  * @param {string} traderAddr Address of trader submitting the order.
176
203
  * @param {number} feeDecimals Fee that this broker is approving for the trader.
177
204
  * @param {number} deadline Deadline for the order to be executed.
178
- * @returns An order signed by this broker, which can be submitted directly with AccountTrade.order.
205
+ * @returns {Order} An order signed by this broker, which can be submitted directly with AccountTrade.order.
179
206
  */
180
207
  signOrder(order, traderAddr, brokerFee, deadline) {
181
208
  return __awaiter(this, void 0, void 0, function* () {
@@ -196,7 +223,7 @@ class BrokerTool extends writeAccessHandler_1.default {
196
223
  * @param {string} symbol Perpetual that this trader will be trading, of the form ETH-USD-MATIC.
197
224
  * @param {number} brokerFee Broker fee for this trader, in decimals (i.e. 0.1% is 0.001).
198
225
  * @param {number} deadline Deadline for the order to be executed.
199
- * @returns Broker signature approving this trader's fee, symbol, and deadline.
226
+ * @returns {string} Broker signature approving this trader's fee, symbol, and deadline.
200
227
  * @ignore
201
228
  */
202
229
  createSignatureForTrader(traderAddr, symbol, brokerFee, deadline) {
@@ -224,11 +251,12 @@ class BrokerTool extends writeAccessHandler_1.default {
224
251
  return yield signer.signMessage(digestBuffer);
225
252
  });
226
253
  }
254
+ // Transfer ownership
227
255
  /**
228
256
  * Transfer ownership of a broker's status to a new wallet.
229
- * @param poolSymbolName Symbol refers to the pool (e.g., MATIC for the MATIC-pool)
230
- * @param newAddress The address this broker wants to use from now on.
231
- * @returns ethers transaction object
257
+ * @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
258
+ * @param {string} newAddress The address this broker wants to use from now on.
259
+ * @returns {ethers.providers.TransactionResponse} ethers transaction object
232
260
  */
233
261
  transferOwnership(poolSymbolName, newAddress) {
234
262
  return __awaiter(this, void 0, void 0, function* () {
@@ -35,4 +35,24 @@ export default class LiquidatorTool extends WriteAccessHandler {
35
35
  * @ignore
36
36
  */
37
37
  _liquidateByAMM(perpetualId: number, liquidatorAddr: string, traderAddr: string, gasLimit: number): Promise<number>;
38
+ /**
39
+ * Total number of active accounts for this symbol, i.e. accounts with positions that are currently open.
40
+ * @param {string} symbol Symbol of the form ETH-USD-MATIC.
41
+ * @returns {number} Number of active accounts.
42
+ */
43
+ countActivePerpAccounts(symbol: string): Promise<number>;
44
+ /**
45
+ * Get addresses of active accounts by chunks.
46
+ * @param {string} symbol Symbol of the form ETH-USD-MATIC.
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.
50
+ */
51
+ getActiveAccountsByChunks(symbol: string, from: number, to: number): Promise<string[]>;
52
+ /**
53
+ * Addresses for all the active accounts in this perpetual symbol.
54
+ * @param {string} symbol Symbol of the form ETH-USD-MATIC.
55
+ * @returns {string[]} Array of addresses.
56
+ */
57
+ getAllActiveAccounts(symbol: string): Promise<string[]>;
38
58
  }
@@ -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.isMaintenanceMarginSafe(perpID, traderAddr);
63
+ return yield this.proxyContract.isTraderMaintenanceMarginSafe(perpID, traderAddr);
64
64
  });
65
65
  }
66
66
  /**
@@ -79,5 +79,47 @@ class LiquidatorTool extends writeAccessHandler_1.default {
79
79
  return (0, d8XMath_1.ABK64x64ToFloat)(fAmount);
80
80
  });
81
81
  }
82
+ /**
83
+ * Total number of active accounts for this symbol, i.e. accounts with positions that are currently open.
84
+ * @param {string} symbol Symbol of the form ETH-USD-MATIC.
85
+ * @returns {number} Number of active accounts.
86
+ */
87
+ countActivePerpAccounts(symbol) {
88
+ return __awaiter(this, void 0, void 0, function* () {
89
+ if (this.proxyContract == null) {
90
+ throw Error("no proxy contract initialized. Use createProxyInstance().");
91
+ }
92
+ let perpID = LiquidatorTool.symbolToPerpetualId(symbol, this.symbolToPerpStaticInfo);
93
+ return yield this.proxyContract.countActivePerpAccounts(perpID);
94
+ });
95
+ }
96
+ /**
97
+ * Get addresses of active accounts by chunks.
98
+ * @param {string} symbol Symbol of the form ETH-USD-MATIC.
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.
102
+ */
103
+ getActiveAccountsByChunks(symbol, from, to) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ if (this.proxyContract == null) {
106
+ throw Error("no proxy contract initialized. Use createProxyInstance().");
107
+ }
108
+ let perpID = LiquidatorTool.symbolToPerpetualId(symbol, this.symbolToPerpStaticInfo);
109
+ return yield this.proxyContract.getActivePerpAccountsByChunks(perpID, from, to);
110
+ });
111
+ }
112
+ /**
113
+ * Addresses for all the active accounts in this perpetual symbol.
114
+ * @param {string} symbol Symbol of the form ETH-USD-MATIC.
115
+ * @returns {string[]} Array of addresses.
116
+ */
117
+ getAllActiveAccounts(symbol) {
118
+ return __awaiter(this, void 0, void 0, function* () {
119
+ // checks are done inside the intermediate functions
120
+ let totalAccounts = yield this.countActivePerpAccounts(symbol);
121
+ return yield this.getActiveAccountsByChunks(symbol, 0, totalAccounts);
122
+ });
123
+ }
82
124
  }
83
125
  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.providers.TransactionResponse>;
31
+ addLiquidity(poolSymbolName: string, amountCC: number): Promise<ethers.ContractTransaction>;
32
32
  /**
33
33
  * Remove liquidity from the pool.
34
34
  * @param {string} poolSymbolName Name of pool symbol (e.g. MATIC).
@@ -9,6 +9,11 @@ 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;
12
17
  /**
13
18
  * Information about the products traded in the exchange.
14
19
  * @returns {ExchangeInfo} Array of static data for all the pools and perpetuals in the system.
@@ -31,6 +36,26 @@ export default class MarketData extends PerpetualDataHandler {
31
36
  * @returns {MarginAccount}
32
37
  */
33
38
  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>;
34
59
  /**
35
60
  * Query smart contract to get user orders and convert to user friendly order format.
36
61
  * @param {string} traderAddr Address of trader.
@@ -31,6 +31,16 @@ 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
+ }
34
44
  /**
35
45
  * Information about the products traded in the exchange.
36
46
  * @returns {ExchangeInfo} Array of static data for all the pools and perpetuals in the system.
@@ -75,6 +85,48 @@ class MarketData extends perpetualDataHandler_1.default {
75
85
  return mgnAcct;
76
86
  });
77
87
  }
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
+ }
78
130
  /**
79
131
  * Query smart contract to get user orders and convert to user friendly order format.
80
132
  * @param {string} traderAddr Address of trader.
@@ -9,6 +9,7 @@ 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";
12
13
  export declare const ONE_64x64: BigNumber;
13
14
  export declare const MAX_64x64: BigNumber;
14
15
  export declare const MAX_UINT_256: BigNumber;