@d8x/perpetuals-sdk 0.0.3 → 0.0.5
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 +7 -0
- package/config/defaultConfig.json +1 -1
- package/dist/brokerTool.d.ts +1 -1
- package/dist/liquidatorTool.d.ts +2 -2
- package/dist/liquidatorTool.js +5 -5
- package/dist/liquiditatorTool.d.ts +14 -0
- package/dist/liquiditatorTool.js +21 -0
- package/dist/marketData.d.ts +0 -7
- package/dist/marketData.js +0 -15
- package/dist/orderReferrerTool.d.ts +1 -1
- package/dist/orderReferrerTool.js +2 -3
- package/dist/perpetualDataHandler.js +6 -10
- package/package.json +1 -1
- package/src/accountTrade.ts +5 -5
- package/src/brokerTool.ts +2 -5
- package/src/index.ts +2 -0
- package/src/liquidityProviderTool.ts +1 -1
- package/src/marketData.ts +41 -0
- package/src/nodeSDKTypes.ts +3 -2
- package/src/orderReferrerTool.ts +26 -5
- package/src/perpetualDataHandler.ts +1 -1
- package/src/writeAccessHandler.ts +7 -5
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "v1.1",
|
|
3
|
-
"proxyAddr": "
|
|
3
|
+
"proxyAddr": "0x51b05809eF1E84BB963D84ff95FB5E625d88152d",
|
|
4
4
|
"nodeURL": "https://rpc-mumbai.maticvigil.com/",
|
|
5
5
|
"proxyABILocation": "../abi/IPerpetualManager.json",
|
|
6
6
|
"limitOrderBookFactoryAddr": "0xd63Faf780687795EBac85ce248a45E457752f021",
|
package/dist/brokerTool.d.ts
CHANGED
|
@@ -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
|
package/dist/liquidatorTool.d.ts
CHANGED
|
@@ -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
|
|
49
|
-
* @returns {string[]} Array of addresses
|
|
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
|
/**
|
package/dist/liquidatorTool.js
CHANGED
|
@@ -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.
|
|
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
|
|
101
|
-
* @returns {string[]} Array of addresses
|
|
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
|
|
121
|
-
return yield this.getActiveAccountsByChunks(symbol, 0,
|
|
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;
|
package/dist/marketData.d.ts
CHANGED
|
@@ -31,13 +31,6 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
31
31
|
* @returns {MarginAccount}
|
|
32
32
|
*/
|
|
33
33
|
positionRisk(traderAddr: string, symbol: string): Promise<MarginAccount>;
|
|
34
|
-
/**
|
|
35
|
-
* Uses the Oracle(s) in the exchange to get the latest price of a given index in a given currency, if a route exists.
|
|
36
|
-
* @param {string} base Index name, e.g. ETH.
|
|
37
|
-
* @param {string} quote Quote currency, e.g. USD.
|
|
38
|
-
* @returns {number} Price of index in given currency.
|
|
39
|
-
*/
|
|
40
|
-
getOraclePrice(base: string, quote: string): Promise<number | undefined>;
|
|
41
34
|
/**
|
|
42
35
|
* Query smart contract to get user orders and convert to user friendly order format.
|
|
43
36
|
* @param {string} traderAddr Address of trader.
|
package/dist/marketData.js
CHANGED
|
@@ -75,21 +75,6 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
75
75
|
return mgnAcct;
|
|
76
76
|
});
|
|
77
77
|
}
|
|
78
|
-
/**
|
|
79
|
-
* Uses the Oracle(s) in the exchange to get the latest price of a given index in a given currency, if a route exists.
|
|
80
|
-
* @param {string} base Index name, e.g. ETH.
|
|
81
|
-
* @param {string} quote Quote currency, e.g. USD.
|
|
82
|
-
* @returns {number} Price of index in given currency.
|
|
83
|
-
*/
|
|
84
|
-
getOraclePrice(base, quote) {
|
|
85
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
86
|
-
if (this.proxyContract == null) {
|
|
87
|
-
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
88
|
-
}
|
|
89
|
-
let px = yield this.proxyContract.getOraclePrice([(0, utils_1.toBytes4)(base), (0, utils_1.toBytes4)(quote)]);
|
|
90
|
-
return px == undefined ? undefined : (0, d8XMath_1.ABK64x64ToFloat)(px);
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
78
|
/**
|
|
94
79
|
* Query smart contract to get user orders and convert to user friendly order format.
|
|
95
80
|
* @param {string} traderAddr Address of trader.
|
|
@@ -38,7 +38,7 @@ export default class OrderReferrerTool extends WriteAccessHandler {
|
|
|
38
38
|
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
39
39
|
* @param {number} numElements Maximum number of orders to poll.
|
|
40
40
|
* @param {string=} startAfter Optional order ID from where to start polling. Defaults to the first order.
|
|
41
|
-
* @returns Array of orders and corresponding order IDs
|
|
41
|
+
* @returns {[Order[], string[]]} Array of orders and corresponding order IDs
|
|
42
42
|
*/
|
|
43
43
|
pollLimitOrders(symbol: string, numElements: number, startAfter?: string): Promise<[Order[], string[]]>;
|
|
44
44
|
isTradeable(order: Order): Promise<boolean>;
|
|
@@ -14,7 +14,6 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
const writeAccessHandler_1 = __importDefault(require("./writeAccessHandler"));
|
|
16
16
|
const nodeSDKTypes_1 = require("./nodeSDKTypes");
|
|
17
|
-
const ethers_1 = require("ethers");
|
|
18
17
|
/**
|
|
19
18
|
* Methods to execute existing orders from the limit order book.
|
|
20
19
|
*/
|
|
@@ -78,7 +77,7 @@ class OrderReferrerTool extends writeAccessHandler_1.default {
|
|
|
78
77
|
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
79
78
|
* @param {number} numElements Maximum number of orders to poll.
|
|
80
79
|
* @param {string=} startAfter Optional order ID from where to start polling. Defaults to the first order.
|
|
81
|
-
* @returns Array of orders and corresponding order IDs
|
|
80
|
+
* @returns {[Order[], string[]]} Array of orders and corresponding order IDs
|
|
82
81
|
*/
|
|
83
82
|
pollLimitOrders(symbol, numElements, startAfter) {
|
|
84
83
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -86,7 +85,7 @@ class OrderReferrerTool extends writeAccessHandler_1.default {
|
|
|
86
85
|
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
87
86
|
}
|
|
88
87
|
if (typeof startAfter == "undefined") {
|
|
89
|
-
startAfter =
|
|
88
|
+
startAfter = nodeSDKTypes_1.ZERO_ADDRESS;
|
|
90
89
|
}
|
|
91
90
|
const orderBookSC = this.getOrderBookContract(symbol);
|
|
92
91
|
let [orders, orderIds] = yield orderBookSC.pollLimitOrders(startAfter, numElements);
|
|
@@ -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
|
|
313
|
+
static _getByValue(map, searchValue) {
|
|
314
314
|
for (let [key, value] of map.entries()) {
|
|
315
|
-
if (value
|
|
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
|
|
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:
|
|
352
|
-
brokerAddr: order.brokerAddr
|
|
353
|
-
brokerSignature: 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),
|
package/package.json
CHANGED
package/src/accountTrade.ts
CHANGED
|
@@ -49,9 +49,9 @@ export default class AccountTrade extends WriteAccessHandler {
|
|
|
49
49
|
/**
|
|
50
50
|
* Submits an order to the exchange.
|
|
51
51
|
* @param {Order} order Order struct.
|
|
52
|
-
* @returns {
|
|
52
|
+
* @returns {ContractTransaction} Contract Transaction (containing events).
|
|
53
53
|
*/
|
|
54
|
-
public async order(order: Order): Promise<
|
|
54
|
+
public async order(order: Order): Promise<ethers.ContractTransaction> {
|
|
55
55
|
if (this.proxyContract == null || this.signer == null) {
|
|
56
56
|
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
57
57
|
}
|
|
@@ -129,11 +129,11 @@ export default class AccountTrade extends WriteAccessHandler {
|
|
|
129
129
|
chainId: number,
|
|
130
130
|
signer: ethers.Wallet,
|
|
131
131
|
gasLimit: number
|
|
132
|
-
): Promise<
|
|
132
|
+
): Promise<ethers.ContractTransaction> {
|
|
133
133
|
let scOrder = AccountTrade.toSmartContractOrder(order, traderAddr, symbolToPerpetualMap);
|
|
134
134
|
// if we are here, we have a clean order
|
|
135
135
|
// decide whether to send order to Limit Order Book or AMM based on order type
|
|
136
|
-
let tx: ethers.
|
|
136
|
+
let tx: ethers.ContractTransaction;
|
|
137
137
|
if (order.type == ORDER_TYPE_MARKET) {
|
|
138
138
|
// send market order
|
|
139
139
|
tx = await proxyContract.trade(scOrder, { gasLimit: gasLimit });
|
|
@@ -145,7 +145,7 @@ export default class AccountTrade extends WriteAccessHandler {
|
|
|
145
145
|
let signature = await this._createSignature(scOrder, chainId, true, signer, proxyContract.address);
|
|
146
146
|
tx = await orderBookContract.createLimitOrder(scOrder, signature, { gasLimit: gasLimit });
|
|
147
147
|
}
|
|
148
|
-
return tx
|
|
148
|
+
return tx;
|
|
149
149
|
}
|
|
150
150
|
|
|
151
151
|
protected async _cancelOrder(
|
package/src/brokerTool.ts
CHANGED
|
@@ -165,12 +165,9 @@ export default class BrokerTool extends WriteAccessHandler {
|
|
|
165
165
|
* Deposit lots to the default fund of a given pool.
|
|
166
166
|
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
167
167
|
* @param {number} lots Number of lots to deposit into this pool.
|
|
168
|
-
* @returns {ethers.
|
|
168
|
+
* @returns {ethers.ContractTransaction} ContractTransaction object.
|
|
169
169
|
*/
|
|
170
|
-
public async brokerDepositToDefaultFund(
|
|
171
|
-
poolSymbolName: string,
|
|
172
|
-
lots: number
|
|
173
|
-
): Promise<ethers.providers.TransactionResponse> {
|
|
170
|
+
public async brokerDepositToDefaultFund(poolSymbolName: string, lots: number): Promise<ethers.ContractTransaction> {
|
|
174
171
|
if (this.proxyContract == null || this.signer == null) {
|
|
175
172
|
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
176
173
|
}
|
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
|
};
|
|
@@ -59,7 +59,7 @@ export default class LiquidityProviderTool extends WriteAccessHandler {
|
|
|
59
59
|
* @param {number} amountCC Amount in pool-collateral currency
|
|
60
60
|
* @return Transaction object
|
|
61
61
|
*/
|
|
62
|
-
public async addLiquidity(poolSymbolName: string, amountCC: number): Promise<ethers.
|
|
62
|
+
public async addLiquidity(poolSymbolName: string, amountCC: number): Promise<ethers.ContractTransaction> {
|
|
63
63
|
if (this.proxyContract == null || this.signer == null) {
|
|
64
64
|
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
65
65
|
}
|
package/src/marketData.ts
CHANGED
|
@@ -29,6 +29,17 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
29
29
|
await this.initContractsAndData(this.provider);
|
|
30
30
|
}
|
|
31
31
|
|
|
32
|
+
/**
|
|
33
|
+
* Get contract instance. Useful for event listening.
|
|
34
|
+
* @returns read-only proxy instance
|
|
35
|
+
*/
|
|
36
|
+
public getReadOnlyProxyInstance(): ethers.Contract {
|
|
37
|
+
if (this.proxyContract == null) {
|
|
38
|
+
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
39
|
+
}
|
|
40
|
+
return this.proxyContract;
|
|
41
|
+
}
|
|
42
|
+
|
|
32
43
|
/**
|
|
33
44
|
* Information about the products traded in the exchange.
|
|
34
45
|
* @returns {ExchangeInfo} Array of static data for all the pools and perpetuals in the system.
|
|
@@ -89,6 +100,36 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
89
100
|
return px == undefined ? undefined : ABK64x64ToFloat(px);
|
|
90
101
|
}
|
|
91
102
|
|
|
103
|
+
/**
|
|
104
|
+
* Get the current mark price
|
|
105
|
+
* @param symbol symbol of the form ETH-USD-MATIC
|
|
106
|
+
* @returns mark price
|
|
107
|
+
*/
|
|
108
|
+
public async getMarkPrice(symbol: string): Promise<number> {
|
|
109
|
+
if (this.proxyContract == null) {
|
|
110
|
+
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
111
|
+
}
|
|
112
|
+
return await PerpetualDataHandler._queryPerpetualMarkPrice(symbol, this.symbolToPerpStaticInfo, this.proxyContract);
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* get the current price for a given quantity
|
|
117
|
+
* @param symbol symbol of the form ETH-USD-MATIC
|
|
118
|
+
* @param quantity quantity to be traded, negative if short
|
|
119
|
+
* @returns price (number)
|
|
120
|
+
*/
|
|
121
|
+
public async getPerpetualPrice(symbol: string, quantity: number): Promise<number> {
|
|
122
|
+
if (this.proxyContract == null) {
|
|
123
|
+
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
124
|
+
}
|
|
125
|
+
return await PerpetualDataHandler._queryPerpetualPrice(
|
|
126
|
+
symbol,
|
|
127
|
+
quantity,
|
|
128
|
+
this.symbolToPerpStaticInfo,
|
|
129
|
+
this.proxyContract
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
|
|
92
133
|
/**
|
|
93
134
|
* Query smart contract to get user orders and convert to user friendly order format.
|
|
94
135
|
* @param {string} traderAddr Address of trader.
|
package/src/nodeSDKTypes.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BytesLike, BigNumber, BigNumberish } from "ethers";
|
|
1
|
+
import { BytesLike, BigNumber, BigNumberish, constants } from "ethers";
|
|
2
2
|
export const DEFAULT_CONFIG_TESTNET = "../config/defaultConfig.json";
|
|
3
3
|
export const DEFAULT_CONFIG_MAINNET = "notthereyet";
|
|
4
4
|
export const DEFAULT_CONFIG_TESTNET_NAME = "testnet";
|
|
@@ -9,7 +9,8 @@ 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 ZERO_ADDRESS =
|
|
12
|
+
export const ZERO_ADDRESS = constants.AddressZero;
|
|
13
|
+
export const ZERO_ORDER_ID = constants.HashZero;
|
|
13
14
|
|
|
14
15
|
export const ONE_64x64 = BigNumber.from("0x010000000000000000");
|
|
15
16
|
export const MAX_64x64 = BigNumber.from("0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
|
package/src/orderReferrerTool.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import WriteAccessHandler from "./writeAccessHandler";
|
|
2
|
-
import { BUY_SIDE, NodeSDKConfig, Order, SELL_SIDE, ZERO_ADDRESS } from "./nodeSDKTypes";
|
|
2
|
+
import { BUY_SIDE, NodeSDKConfig, Order, SELL_SIDE, ZERO_ADDRESS, ZERO_ORDER_ID } from "./nodeSDKTypes";
|
|
3
3
|
import { ethers } from "ethers";
|
|
4
4
|
|
|
5
5
|
/**
|
|
@@ -27,7 +27,7 @@ export default class OrderReferrerTool extends WriteAccessHandler {
|
|
|
27
27
|
symbol: string,
|
|
28
28
|
orderId: string,
|
|
29
29
|
referrerAddr?: string
|
|
30
|
-
): Promise<ethers.
|
|
30
|
+
): Promise<ethers.ContractTransaction> {
|
|
31
31
|
if (this.proxyContract == null || this.signer == null) {
|
|
32
32
|
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
33
33
|
}
|
|
@@ -61,6 +61,22 @@ export default class OrderReferrerTool extends WriteAccessHandler {
|
|
|
61
61
|
return await orderBookSC.numberOfOrderBookDigests();
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Get order from the digest (=id)
|
|
66
|
+
* @param symbol symbol of order book, e.g. ETH-USD-MATIC
|
|
67
|
+
* @param digest digest of the order (=order ID)
|
|
68
|
+
* @returns order or undefined
|
|
69
|
+
*/
|
|
70
|
+
public async getOrderById(symbol: string, id: string): Promise<Order | undefined> {
|
|
71
|
+
let ob = await this.getOrderBookContract(symbol);
|
|
72
|
+
let smartContractOrder = await ob.orderOfDigest(id);
|
|
73
|
+
if (smartContractOrder.traderAddr == ZERO_ADDRESS) {
|
|
74
|
+
return undefined;
|
|
75
|
+
}
|
|
76
|
+
let order = OrderReferrerTool.fromSmartContractOrder(smartContractOrder, this.symbolToPerpStaticInfo);
|
|
77
|
+
return order;
|
|
78
|
+
}
|
|
79
|
+
|
|
64
80
|
/**
|
|
65
81
|
* Get a list of active conditional orders in the order book.
|
|
66
82
|
* This a read-only action and does not incur in gas costs.
|
|
@@ -74,7 +90,7 @@ export default class OrderReferrerTool extends WriteAccessHandler {
|
|
|
74
90
|
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
75
91
|
}
|
|
76
92
|
if (typeof startAfter == "undefined") {
|
|
77
|
-
startAfter =
|
|
93
|
+
startAfter = ZERO_ORDER_ID;
|
|
78
94
|
}
|
|
79
95
|
const orderBookSC = this.getOrderBookContract(symbol);
|
|
80
96
|
let [orders, orderIds] = await orderBookSC.pollLimitOrders(startAfter, numElements);
|
|
@@ -87,7 +103,12 @@ export default class OrderReferrerTool extends WriteAccessHandler {
|
|
|
87
103
|
return [userFriendlyOrders, orderIds];
|
|
88
104
|
}
|
|
89
105
|
|
|
90
|
-
|
|
106
|
+
/**
|
|
107
|
+
* Check if a conditional order can be executed
|
|
108
|
+
* @param order order structure
|
|
109
|
+
* @returns true if order can be executed for the current state of the perpetuals
|
|
110
|
+
*/
|
|
111
|
+
public async isTradeable(order: Order): Promise<boolean> {
|
|
91
112
|
if (this.proxyContract == null) {
|
|
92
113
|
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
93
114
|
}
|
|
@@ -95,7 +116,7 @@ export default class OrderReferrerTool extends WriteAccessHandler {
|
|
|
95
116
|
throw Error("order does not have a limit price");
|
|
96
117
|
}
|
|
97
118
|
// check expiration date
|
|
98
|
-
if (order.deadline != undefined && order.deadline < Date.now()) {
|
|
119
|
+
if (order.deadline != undefined && order.deadline < Date.now() / 1000) {
|
|
99
120
|
return false;
|
|
100
121
|
}
|
|
101
122
|
// check limit price
|
|
@@ -301,7 +301,7 @@ export default class PerpetualDataHandler {
|
|
|
301
301
|
): Promise<number> {
|
|
302
302
|
let perpId = PerpetualDataHandler.symbolToPerpetualId(symbol, symbolToPerpStaticInfo);
|
|
303
303
|
let ammState = await _proxyContract.getAMMState(perpId);
|
|
304
|
-
return ABK64x64ToFloat(ammState[6].mul(ONE_64x64.add(ammState[8])));
|
|
304
|
+
return ABK64x64ToFloat(ammState[6].mul(ONE_64x64.add(ammState[8])).div(ONE_64x64));
|
|
305
305
|
}
|
|
306
306
|
|
|
307
307
|
/**
|
|
@@ -47,9 +47,12 @@ export default class WriteAccessHandler extends PerpetualDataHandler {
|
|
|
47
47
|
* Set allowance for ar margin token (e.g., MATIC, ETH, USDC)
|
|
48
48
|
* @param symbol token in 'long-form' such as MATIC, symbol also fine (ETH-USD-MATIC)
|
|
49
49
|
* @param amount optional, amount to approve if not 'infinity'
|
|
50
|
-
* @returns
|
|
50
|
+
* @returns ContractTransaction
|
|
51
51
|
*/
|
|
52
|
-
public async setAllowance(
|
|
52
|
+
public async setAllowance(
|
|
53
|
+
symbol: string,
|
|
54
|
+
amount: number | undefined = undefined
|
|
55
|
+
): Promise<ethers.ContractTransaction> {
|
|
53
56
|
//extract margin-currency name
|
|
54
57
|
let symbolarr = symbol.split("-");
|
|
55
58
|
symbol = symbol.length == 3 ? symbolarr[2] : symbolarr[0];
|
|
@@ -74,10 +77,9 @@ export default class WriteAccessHandler extends PerpetualDataHandler {
|
|
|
74
77
|
proxyAddr: string,
|
|
75
78
|
signer: ethers.Wallet,
|
|
76
79
|
amount: BigNumber
|
|
77
|
-
): Promise<
|
|
80
|
+
): Promise<ethers.ContractTransaction> {
|
|
78
81
|
const marginToken: ethers.Contract = new ethers.Contract(tokenAddr, ERC20_ABI, signer);
|
|
79
82
|
let tx = await marginToken.approve(proxyAddr, amount);
|
|
80
|
-
|
|
81
|
-
return tx.hash;
|
|
83
|
+
return tx;
|
|
82
84
|
}
|
|
83
85
|
}
|