@d8x/perpetuals-sdk 0.0.25 → 0.0.27
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/dist/apiInterface.d.ts
CHANGED
|
@@ -2,9 +2,19 @@ import { ethers } from "ethers";
|
|
|
2
2
|
import MarketData from "./marketData";
|
|
3
3
|
import { NodeSDKConfig, SmartContractOrder, Order } from "./nodeSDKTypes";
|
|
4
4
|
import TraderDigests from "./traderDigests";
|
|
5
|
+
/**
|
|
6
|
+
* Interface that can be used by front-end that wraps all private functions
|
|
7
|
+
* so that signatures can be handled in frontend via wallet
|
|
8
|
+
* @extends MarketData
|
|
9
|
+
*/
|
|
5
10
|
export default class APIInterface extends MarketData {
|
|
6
11
|
protected chainId: number;
|
|
7
12
|
protected digestTool: TraderDigests;
|
|
13
|
+
/**
|
|
14
|
+
* Constructor
|
|
15
|
+
* @param {NodeSDKConfig} config Configuration object, see
|
|
16
|
+
* PerpetualDataHandler.readSDKConfig.
|
|
17
|
+
*/
|
|
8
18
|
constructor(config: NodeSDKConfig);
|
|
9
19
|
/**
|
|
10
20
|
* Initialize the marketData-Class with this function
|
package/dist/apiInterface.js
CHANGED
|
@@ -15,11 +15,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
const marketData_1 = __importDefault(require("./marketData"));
|
|
16
16
|
const perpetualDataHandler_1 = __importDefault(require("./perpetualDataHandler"));
|
|
17
17
|
const traderDigests_1 = __importDefault(require("./traderDigests"));
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
*/
|
|
18
|
+
/**
|
|
19
|
+
* Interface that can be used by front-end that wraps all private functions
|
|
20
|
+
* so that signatures can be handled in frontend via wallet
|
|
21
|
+
* @extends MarketData
|
|
22
|
+
*/
|
|
23
23
|
class APIInterface extends marketData_1.default {
|
|
24
24
|
// accTrade.order(order)
|
|
25
25
|
// cancelOrder(symbol: string, orderId: string)
|
|
@@ -27,6 +27,11 @@ class APIInterface extends marketData_1.default {
|
|
|
27
27
|
// accTrade.queryExchangeFee("MATIC")
|
|
28
28
|
// accTrade.getCurrentTraderVolume("MATIC")
|
|
29
29
|
// accTrade.getOrderIds("MATIC-USD-MATIC")
|
|
30
|
+
/**
|
|
31
|
+
* Constructor
|
|
32
|
+
* @param {NodeSDKConfig} config Configuration object, see
|
|
33
|
+
* PerpetualDataHandler.readSDKConfig.
|
|
34
|
+
*/
|
|
30
35
|
constructor(config) {
|
|
31
36
|
super(config);
|
|
32
37
|
this.chainId = 0;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ethers } from "ethers";
|
|
2
|
+
import MarketData from "./marketData";
|
|
3
|
+
import { NodeSDKConfig, SmartContractOrder, Order } from "./nodeSDKTypes";
|
|
4
|
+
import TraderDigests from "./traderDigests";
|
|
5
|
+
/**
|
|
6
|
+
* Interface that can be used by front-end that wraps all private functions
|
|
7
|
+
* so that signatures can be handled in frontend via wallet
|
|
8
|
+
* @extends MarketData
|
|
9
|
+
*/
|
|
10
|
+
export default class TraderInterface extends MarketData {
|
|
11
|
+
protected chainId: number;
|
|
12
|
+
protected digestTool: TraderDigests;
|
|
13
|
+
/**
|
|
14
|
+
* Constructor
|
|
15
|
+
* @param {NodeSDKConfig} config Configuration object, see
|
|
16
|
+
* PerpetualDataHandler.readSDKConfig.
|
|
17
|
+
*/
|
|
18
|
+
constructor(config: NodeSDKConfig);
|
|
19
|
+
/**
|
|
20
|
+
* Initialize the marketData-Class with this function
|
|
21
|
+
* to create instance of D8X perpetual contract and gather information
|
|
22
|
+
* about perpetual currencies
|
|
23
|
+
* @param provider optional provider
|
|
24
|
+
*/
|
|
25
|
+
createProxyInstance(provider?: ethers.providers.JsonRpcProvider): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* Get digest to cancel an order. Digest needs to be signed and submitted via
|
|
28
|
+
* orderBookContract.cancelOrder(orderId, signature);
|
|
29
|
+
* @param symbol
|
|
30
|
+
* @param orderId
|
|
31
|
+
* @returns tuple of digest which the trader needs to sign and address of order book contract
|
|
32
|
+
*/
|
|
33
|
+
cancelOrderDigest(symbol: string, orderId: string): Promise<{
|
|
34
|
+
digest: string;
|
|
35
|
+
OBContractAddr: string;
|
|
36
|
+
}>;
|
|
37
|
+
/**
|
|
38
|
+
* Create smart contract order and digest that the trader signs.
|
|
39
|
+
* await orderBookContract.postOrder(scOrder, signature, { gasLimit: gasLimit });
|
|
40
|
+
* Order must contain broker fee and broker address if there is supposed to be a broker.
|
|
41
|
+
* @param order order struct
|
|
42
|
+
* @param traderAddr address of the trader
|
|
43
|
+
* @returns tuple of digest that the trader has to sign, order book address, and smart contract order
|
|
44
|
+
*/
|
|
45
|
+
orderDigest(order: Order, traderAddr: string): Promise<{
|
|
46
|
+
digest: string;
|
|
47
|
+
OBAddr: string;
|
|
48
|
+
SCOrder: SmartContractOrder;
|
|
49
|
+
}>;
|
|
50
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const marketData_1 = __importDefault(require("./marketData"));
|
|
16
|
+
const perpetualDataHandler_1 = __importDefault(require("./perpetualDataHandler"));
|
|
17
|
+
const traderDigests_1 = __importDefault(require("./traderDigests"));
|
|
18
|
+
/**
|
|
19
|
+
* Interface that can be used by front-end that wraps all private functions
|
|
20
|
+
* so that signatures can be handled in frontend via wallet
|
|
21
|
+
* @extends MarketData
|
|
22
|
+
*/
|
|
23
|
+
class TraderInterface extends marketData_1.default {
|
|
24
|
+
// accTrade.order(order)
|
|
25
|
+
// cancelOrder(symbol: string, orderId: string)
|
|
26
|
+
// accTrade.setAllowance
|
|
27
|
+
// accTrade.queryExchangeFee("MATIC")
|
|
28
|
+
// accTrade.getCurrentTraderVolume("MATIC")
|
|
29
|
+
// accTrade.getOrderIds("MATIC-USD-MATIC")
|
|
30
|
+
/**
|
|
31
|
+
* Constructor
|
|
32
|
+
* @param {NodeSDKConfig} config Configuration object, see
|
|
33
|
+
* PerpetualDataHandler.readSDKConfig.
|
|
34
|
+
*/
|
|
35
|
+
constructor(config) {
|
|
36
|
+
super(config);
|
|
37
|
+
this.chainId = 0;
|
|
38
|
+
this.digestTool = new traderDigests_1.default();
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Initialize the marketData-Class with this function
|
|
42
|
+
* to create instance of D8X perpetual contract and gather information
|
|
43
|
+
* about perpetual currencies
|
|
44
|
+
* @param provider optional provider
|
|
45
|
+
*/
|
|
46
|
+
createProxyInstance(provider) {
|
|
47
|
+
const _super = Object.create(null, {
|
|
48
|
+
createProxyInstance: { get: () => super.createProxyInstance }
|
|
49
|
+
});
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
yield _super.createProxyInstance.call(this, provider);
|
|
52
|
+
this.chainId = (yield this.provider.getNetwork()).chainId;
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Get digest to cancel an order. Digest needs to be signed and submitted via
|
|
57
|
+
* orderBookContract.cancelOrder(orderId, signature);
|
|
58
|
+
* @param symbol
|
|
59
|
+
* @param orderId
|
|
60
|
+
* @returns tuple of digest which the trader needs to sign and address of order book contract
|
|
61
|
+
*/
|
|
62
|
+
cancelOrderDigest(symbol, orderId) {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
if (this.proxyContract == null) {
|
|
65
|
+
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
66
|
+
}
|
|
67
|
+
let orderBookContract = this.getOrderBookContract(symbol);
|
|
68
|
+
let scOrder = yield orderBookContract.orderOfDigest(orderId);
|
|
69
|
+
let digest = yield this.digestTool.createDigest(scOrder, this.chainId, false, this.proxyAddr);
|
|
70
|
+
return { digest: digest, OBContractAddr: orderBookContract.address };
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Create smart contract order and digest that the trader signs.
|
|
75
|
+
* await orderBookContract.postOrder(scOrder, signature, { gasLimit: gasLimit });
|
|
76
|
+
* Order must contain broker fee and broker address if there is supposed to be a broker.
|
|
77
|
+
* @param order order struct
|
|
78
|
+
* @param traderAddr address of the trader
|
|
79
|
+
* @returns tuple of digest that the trader has to sign, order book address, and smart contract order
|
|
80
|
+
*/
|
|
81
|
+
orderDigest(order, traderAddr) {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
if (this.proxyContract == null) {
|
|
84
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
85
|
+
}
|
|
86
|
+
let minSize = perpetualDataHandler_1.default._getMinimalPositionSize(order.symbol, this.symbolToPerpStaticInfo);
|
|
87
|
+
if (Math.abs(order.quantity) < minSize) {
|
|
88
|
+
throw Error("order size too small");
|
|
89
|
+
}
|
|
90
|
+
let orderBookContract = this.getOrderBookContract(order.symbol);
|
|
91
|
+
let scOrder = TraderInterface.toSmartContractOrder(order, traderAddr, this.symbolToPerpStaticInfo);
|
|
92
|
+
let digest = yield this.digestTool.createDigest(scOrder, this.chainId, true, this.proxyContract.address);
|
|
93
|
+
return { digest: digest, OBAddr: orderBookContract.address, SCOrder: scOrder };
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
exports.default = TraderInterface;
|
package/package.json
CHANGED
|
@@ -3,13 +3,13 @@ import MarketData from "./marketData";
|
|
|
3
3
|
import PerpetualDataHandler from "./perpetualDataHandler";
|
|
4
4
|
import { NodeSDKConfig, SmartContractOrder, Order } from "./nodeSDKTypes";
|
|
5
5
|
import TraderDigests from "./traderDigests";
|
|
6
|
-
/*
|
|
7
|
-
interface that can be used by front-end that wraps all private functions
|
|
8
|
-
so that signatures can be handled in frontend via wallet
|
|
9
6
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Interface that can be used by front-end that wraps all private functions
|
|
9
|
+
* so that signatures can be handled in frontend via wallet
|
|
10
|
+
* @extends MarketData
|
|
11
|
+
*/
|
|
12
|
+
export default class TraderInterface extends MarketData {
|
|
13
13
|
protected chainId: number = 0;
|
|
14
14
|
protected digestTool: TraderDigests;
|
|
15
15
|
|
|
@@ -19,7 +19,13 @@ export default class APIInterface extends MarketData {
|
|
|
19
19
|
// accTrade.queryExchangeFee("MATIC")
|
|
20
20
|
// accTrade.getCurrentTraderVolume("MATIC")
|
|
21
21
|
// accTrade.getOrderIds("MATIC-USD-MATIC")
|
|
22
|
-
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Constructor
|
|
25
|
+
* @param {NodeSDKConfig} config Configuration object, see
|
|
26
|
+
* PerpetualDataHandler.readSDKConfig.
|
|
27
|
+
*/
|
|
28
|
+
public constructor(config: NodeSDKConfig) {
|
|
23
29
|
super(config);
|
|
24
30
|
this.digestTool = new TraderDigests();
|
|
25
31
|
}
|
|
@@ -72,7 +78,7 @@ export default class APIInterface extends MarketData {
|
|
|
72
78
|
throw Error("order size too small");
|
|
73
79
|
}
|
|
74
80
|
let orderBookContract: ethers.Contract = this.getOrderBookContract(order.symbol);
|
|
75
|
-
let scOrder =
|
|
81
|
+
let scOrder = TraderInterface.toSmartContractOrder(order, traderAddr, this.symbolToPerpStaticInfo);
|
|
76
82
|
let digest = await this.digestTool.createDigest(scOrder, this.chainId, true, this.proxyContract.address);
|
|
77
83
|
return { digest: digest, OBAddr: orderBookContract.address, SCOrder: scOrder };
|
|
78
84
|
}
|