@d8x/perpetuals-sdk 0.0.3 → 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 +7 -0
- package/dist/accountTrade.d.ts +3 -3
- package/dist/accountTrade.js +2 -2
- package/dist/brokerTool.d.ts +2 -2
- package/dist/brokerTool.js +1 -1
- package/dist/liquidityProviderTool.d.ts +1 -1
- package/dist/marketData.d.ts +18 -0
- package/dist/marketData.js +37 -0
- package/dist/nodeSDKTypes.d.ts +1 -0
- package/dist/nodeSDKTypes.js +3 -2
- package/dist/orderReferrerTool.d.ts +13 -1
- package/dist/orderReferrerTool.js +24 -3
- package/dist/perpetualDataHandler.js +1 -1
- package/dist/writeAccessHandler.d.ts +3 -3
- package/dist/writeAccessHandler.js +2 -3
- package/package.json +1 -1
- package/src/accountTrade.ts +5 -5
- package/src/brokerTool.ts +2 -5
- 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
package/dist/accountTrade.d.ts
CHANGED
|
@@ -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 {
|
|
25
|
+
* @returns {ContractTransaction} Contract Transaction (containing events).
|
|
26
26
|
*/
|
|
27
|
-
order(order: Order): Promise<
|
|
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).
|
|
@@ -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<
|
|
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>;
|
|
59
59
|
protected _cancelOrder(symbol: string, orderId: string, orderBookContract: ethers.Contract | null): Promise<string | undefined>;
|
|
60
60
|
/**
|
|
61
61
|
* Creates a signature
|
package/dist/accountTrade.js
CHANGED
|
@@ -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 {
|
|
56
|
+
* @returns {ContractTransaction} Contract Transaction (containing events).
|
|
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;
|
|
140
140
|
});
|
|
141
141
|
}
|
|
142
142
|
_cancelOrder(symbol, orderId, orderBookContract) {
|
package/dist/brokerTool.d.ts
CHANGED
|
@@ -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.
|
|
83
|
+
* @returns {ethers.ContractTransaction} ContractTransaction object.
|
|
84
84
|
*/
|
|
85
|
-
brokerDepositToDefaultFund(poolSymbolName: string, lots: number): Promise<ethers.
|
|
85
|
+
brokerDepositToDefaultFund(poolSymbolName: string, lots: number): Promise<ethers.ContractTransaction>;
|
|
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.
|
package/dist/brokerTool.js
CHANGED
|
@@ -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.
|
|
186
|
+
* @returns {ethers.ContractTransaction} ContractTransaction object.
|
|
187
187
|
*/
|
|
188
188
|
brokerDepositToDefaultFund(poolSymbolName, lots) {
|
|
189
189
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -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.
|
|
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).
|
package/dist/marketData.d.ts
CHANGED
|
@@ -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.
|
|
@@ -38,6 +43,19 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
38
43
|
* @returns {number} Price of index in given currency.
|
|
39
44
|
*/
|
|
40
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>;
|
|
41
59
|
/**
|
|
42
60
|
* Query smart contract to get user orders and convert to user friendly order format.
|
|
43
61
|
* @param {string} traderAddr Address of trader.
|
package/dist/marketData.js
CHANGED
|
@@ -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.
|
|
@@ -90,6 +100,33 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
90
100
|
return px == undefined ? undefined : (0, d8XMath_1.ABK64x64ToFloat)(px);
|
|
91
101
|
});
|
|
92
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
|
+
}
|
|
93
130
|
/**
|
|
94
131
|
* Query smart contract to get user orders and convert to user friendly order format.
|
|
95
132
|
* @param {string} traderAddr Address of trader.
|
package/dist/nodeSDKTypes.d.ts
CHANGED
|
@@ -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;
|
package/dist/nodeSDKTypes.js
CHANGED
|
@@ -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_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_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;
|
|
4
4
|
const ethers_1 = require("ethers");
|
|
5
5
|
exports.DEFAULT_CONFIG_TESTNET = "../config/defaultConfig.json";
|
|
6
6
|
exports.DEFAULT_CONFIG_MAINNET = "notthereyet";
|
|
@@ -11,7 +11,8 @@ 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 =
|
|
14
|
+
exports.ZERO_ADDRESS = ethers_1.constants.AddressZero;
|
|
15
|
+
exports.ZERO_ORDER_ID = ethers_1.constants.HashZero;
|
|
15
16
|
exports.ONE_64x64 = ethers_1.BigNumber.from("0x010000000000000000");
|
|
16
17
|
exports.MAX_64x64 = ethers_1.BigNumber.from("0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
|
|
17
18
|
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.
|
|
22
|
+
executeOrder(symbol: string, orderId: string, referrerAddr?: string): Promise<ethers.ContractTransaction>;
|
|
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,6 +32,13 @@ 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>;
|
|
35
42
|
/**
|
|
36
43
|
* Get a list of active conditional orders in the order book.
|
|
37
44
|
* This a read-only action and does not incur in gas costs.
|
|
@@ -41,5 +48,10 @@ export default class OrderReferrerTool extends WriteAccessHandler {
|
|
|
41
48
|
* @returns Array of orders and corresponding order IDs
|
|
42
49
|
*/
|
|
43
50
|
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
|
+
*/
|
|
44
56
|
isTradeable(order: Order): Promise<boolean>;
|
|
45
57
|
}
|
|
@@ -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
|
*/
|
|
@@ -72,6 +71,23 @@ class OrderReferrerTool extends writeAccessHandler_1.default {
|
|
|
72
71
|
return yield orderBookSC.numberOfOrderBookDigests();
|
|
73
72
|
});
|
|
74
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
|
+
}
|
|
75
91
|
/**
|
|
76
92
|
* Get a list of active conditional orders in the order book.
|
|
77
93
|
* This a read-only action and does not incur in gas costs.
|
|
@@ -86,7 +102,7 @@ class OrderReferrerTool extends writeAccessHandler_1.default {
|
|
|
86
102
|
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
87
103
|
}
|
|
88
104
|
if (typeof startAfter == "undefined") {
|
|
89
|
-
startAfter =
|
|
105
|
+
startAfter = nodeSDKTypes_1.ZERO_ORDER_ID;
|
|
90
106
|
}
|
|
91
107
|
const orderBookSC = this.getOrderBookContract(symbol);
|
|
92
108
|
let [orders, orderIds] = yield orderBookSC.pollLimitOrders(startAfter, numElements);
|
|
@@ -99,6 +115,11 @@ class OrderReferrerTool extends writeAccessHandler_1.default {
|
|
|
99
115
|
return [userFriendlyOrders, orderIds];
|
|
100
116
|
});
|
|
101
117
|
}
|
|
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
|
+
*/
|
|
102
123
|
isTradeable(order) {
|
|
103
124
|
return __awaiter(this, void 0, void 0, function* () {
|
|
104
125
|
if (this.proxyContract == null) {
|
|
@@ -108,7 +129,7 @@ class OrderReferrerTool extends writeAccessHandler_1.default {
|
|
|
108
129
|
throw Error("order does not have a limit price");
|
|
109
130
|
}
|
|
110
131
|
// check expiration date
|
|
111
|
-
if (order.deadline != undefined && order.deadline < Date.now()) {
|
|
132
|
+
if (order.deadline != undefined && order.deadline < Date.now() / 1000) {
|
|
112
133
|
return false;
|
|
113
134
|
}
|
|
114
135
|
// 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])));
|
|
232
|
+
return (0, d8XMath_1.ABK64x64ToFloat)(ammState[6].mul(nodeSDKTypes_1.ONE_64x64.add(ammState[8])).div(nodeSDKTypes_1.ONE_64x64));
|
|
233
233
|
});
|
|
234
234
|
}
|
|
235
235
|
/**
|
|
@@ -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
|
|
32
|
+
* @returns ContractTransaction
|
|
33
33
|
*/
|
|
34
|
-
setAllowance(symbol: string, amount?: number | undefined): Promise<
|
|
35
|
-
protected static _setAllowance(tokenAddr: string, proxyAddr: string, signer: ethers.Wallet, amount: BigNumber): Promise<
|
|
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>;
|
|
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
|
|
62
|
+
* @returns ContractTransaction
|
|
63
63
|
*/
|
|
64
64
|
setAllowance(symbol, amount = undefined) {
|
|
65
65
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -87,8 +87,7 @@ 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
|
-
|
|
91
|
-
return tx.hash;
|
|
90
|
+
return tx;
|
|
92
91
|
});
|
|
93
92
|
}
|
|
94
93
|
}
|
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
|
}
|
|
@@ -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
|
}
|