@d8x/perpetuals-sdk 0.0.8 → 0.0.9
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/config/defaultConfig.json +1 -1
- package/dist/accountTrade.d.ts +62 -2
- package/dist/accountTrade.js +71 -2
- package/dist/brokerTool.d.ts +196 -10
- package/dist/brokerTool.js +196 -10
- package/dist/liquidatorTool.d.ts +99 -4
- package/dist/liquidatorTool.js +99 -4
- package/dist/liquidityProviderTool.d.ts +68 -7
- package/dist/liquidityProviderTool.js +68 -7
- package/dist/marketData.d.ts +140 -5
- package/dist/marketData.js +141 -6
- package/dist/nodeSDKTypes.d.ts +5 -0
- package/dist/nodeSDKTypes.js +6 -1
- package/dist/orderReferrerTool.d.ts +8 -3
- package/dist/orderReferrerTool.js +8 -3
- package/dist/perpetualDataHandler.js +7 -3
- package/package.json +1 -1
- package/src/accountTrade.ts +70 -2
- package/src/brokerTool.ts +196 -10
- package/src/liquidatorTool.ts +99 -4
- package/src/liquidityProviderTool.ts +68 -7
- package/src/marketData.ts +141 -6
- package/src/nodeSDKTypes.ts +5 -0
- package/src/orderReferrerTool.ts +8 -3
- package/src/perpetualDataHandler.ts +9 -3
|
@@ -2,20 +2,51 @@ import { ethers } from "ethers";
|
|
|
2
2
|
import WriteAccessHandler from "./writeAccessHandler";
|
|
3
3
|
import { NodeSDKConfig } from "./nodeSDKTypes";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
5
|
+
* Functions to provide liquidity. This class requires a private key and executes
|
|
6
|
+
* smart-contract interactions that require gas-payments.
|
|
6
7
|
*/
|
|
7
8
|
export default class LiquidityProviderTool extends WriteAccessHandler {
|
|
8
9
|
/**
|
|
9
10
|
* Constructor
|
|
10
|
-
* @param config
|
|
11
|
+
* @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.
|
|
12
|
+
* readSDKConfig.
|
|
13
|
+
* @example
|
|
14
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
15
|
+
* async function main() {
|
|
16
|
+
* console.log(LiquidityProviderTool);
|
|
17
|
+
* // load configuration for testnet
|
|
18
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
19
|
+
* // LiquidityProviderTool (authentication required, PK is an environment variable with a private key)
|
|
20
|
+
* const pk: string = <string>process.env.PK;
|
|
21
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
22
|
+
* // Create a proxy instance to access the blockchain
|
|
23
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
24
|
+
* }
|
|
25
|
+
* main();
|
|
26
|
+
*
|
|
11
27
|
* @param privateKey private key of account that trades
|
|
12
28
|
*/
|
|
13
29
|
constructor(config: NodeSDKConfig, privateKey: string);
|
|
14
30
|
/**
|
|
15
|
-
* Value of the share tokens for this liquidity provider
|
|
31
|
+
* Value of the pool share tokens for this liquidity provider
|
|
16
32
|
* in poolSymbol-currency (e.g. MATIC, USDC).
|
|
17
33
|
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC).
|
|
18
|
-
* @
|
|
34
|
+
* @example
|
|
35
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
36
|
+
* async function main() {
|
|
37
|
+
* console.log(LiquidityProviderTool);
|
|
38
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
39
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
40
|
+
* const pk: string = <string>process.env.PK;
|
|
41
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
42
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
43
|
+
* // get value of pool share token
|
|
44
|
+
* let shareToken = await lqudtProviderTool.getParticipationValue("MATIC");
|
|
45
|
+
* console.log(shareToken);
|
|
46
|
+
* }
|
|
47
|
+
* main();
|
|
48
|
+
*
|
|
49
|
+
* @return Value in poolSymbol-currency (e.g. MATIC, USDC), balance of pool share tokens, and share token symbol.
|
|
19
50
|
*/
|
|
20
51
|
getParticipationValue(poolSymbolName: string): Promise<{
|
|
21
52
|
value: number;
|
|
@@ -24,15 +55,45 @@ export default class LiquidityProviderTool extends WriteAccessHandler {
|
|
|
24
55
|
}>;
|
|
25
56
|
/**
|
|
26
57
|
* Add liquidity to the PnL participant fund. The address gets pool shares in return.
|
|
27
|
-
* @param {string}
|
|
58
|
+
* @param {string} poolSymbolName Name of pool symbol (e.g. MATIC)
|
|
28
59
|
* @param {number} amountCC Amount in pool-collateral currency
|
|
60
|
+
* @example
|
|
61
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
62
|
+
* async function main() {
|
|
63
|
+
* console.log(LiquidityProviderTool);
|
|
64
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
65
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
66
|
+
* const pk: string = <string>process.env.PK;
|
|
67
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
68
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
69
|
+
* // add liquidity
|
|
70
|
+
* let respAddLiquidity = await lqudtProviderTool.addLiquidity("MATIC", 0.1);
|
|
71
|
+
* console.log(respAddLiquidity);
|
|
72
|
+
* }
|
|
73
|
+
* main();
|
|
74
|
+
*
|
|
29
75
|
* @return Transaction object
|
|
30
76
|
*/
|
|
31
77
|
addLiquidity(poolSymbolName: string, amountCC: number): Promise<ethers.ContractTransaction>;
|
|
32
78
|
/**
|
|
33
|
-
* Remove liquidity from the pool.
|
|
79
|
+
* Remove liquidity from the pool. The address loses pool shares in return.
|
|
34
80
|
* @param {string} poolSymbolName Name of pool symbol (e.g. MATIC).
|
|
35
|
-
* @param {string} amountPoolShares Amount in pool-
|
|
81
|
+
* @param {string} amountPoolShares Amount in pool-shares, removes everything if > available amount.
|
|
82
|
+
* @example
|
|
83
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
84
|
+
* async function main() {
|
|
85
|
+
* console.log(LiquidityProviderTool);
|
|
86
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
87
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
88
|
+
* const pk: string = <string>process.env.PK;
|
|
89
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
90
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
91
|
+
* // remove liquidity
|
|
92
|
+
* let respRemoveLiquidity = await lqudtProviderTool.removeLiquidity("MATIC", 0.1);
|
|
93
|
+
* console.log(respRemoveLiquidity);
|
|
94
|
+
* }
|
|
95
|
+
* main();
|
|
96
|
+
*
|
|
36
97
|
* @return Transaction object.
|
|
37
98
|
*/
|
|
38
99
|
removeLiquidity(poolSymbolName: string, amountPoolShares: number): Promise<ethers.providers.TransactionResponse>;
|
|
@@ -18,22 +18,53 @@ const nodeSDKTypes_1 = require("./nodeSDKTypes");
|
|
|
18
18
|
const perpetualDataHandler_1 = __importDefault(require("./perpetualDataHandler"));
|
|
19
19
|
const d8XMath_1 = require("./d8XMath");
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
21
|
+
* Functions to provide liquidity. This class requires a private key and executes
|
|
22
|
+
* smart-contract interactions that require gas-payments.
|
|
22
23
|
*/
|
|
23
24
|
class LiquidityProviderTool extends writeAccessHandler_1.default {
|
|
24
25
|
/**
|
|
25
26
|
* Constructor
|
|
26
|
-
* @param config
|
|
27
|
+
* @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.
|
|
28
|
+
* readSDKConfig.
|
|
29
|
+
* @example
|
|
30
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
31
|
+
* async function main() {
|
|
32
|
+
* console.log(LiquidityProviderTool);
|
|
33
|
+
* // load configuration for testnet
|
|
34
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
35
|
+
* // LiquidityProviderTool (authentication required, PK is an environment variable with a private key)
|
|
36
|
+
* const pk: string = <string>process.env.PK;
|
|
37
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
38
|
+
* // Create a proxy instance to access the blockchain
|
|
39
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
40
|
+
* }
|
|
41
|
+
* main();
|
|
42
|
+
*
|
|
27
43
|
* @param privateKey private key of account that trades
|
|
28
44
|
*/
|
|
29
45
|
constructor(config, privateKey) {
|
|
30
46
|
super(config, privateKey);
|
|
31
47
|
}
|
|
32
48
|
/**
|
|
33
|
-
* Value of the share tokens for this liquidity provider
|
|
49
|
+
* Value of the pool share tokens for this liquidity provider
|
|
34
50
|
* in poolSymbol-currency (e.g. MATIC, USDC).
|
|
35
51
|
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC).
|
|
36
|
-
* @
|
|
52
|
+
* @example
|
|
53
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
54
|
+
* async function main() {
|
|
55
|
+
* console.log(LiquidityProviderTool);
|
|
56
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
57
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
58
|
+
* const pk: string = <string>process.env.PK;
|
|
59
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
60
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
61
|
+
* // get value of pool share token
|
|
62
|
+
* let shareToken = await lqudtProviderTool.getParticipationValue("MATIC");
|
|
63
|
+
* console.log(shareToken);
|
|
64
|
+
* }
|
|
65
|
+
* main();
|
|
66
|
+
*
|
|
67
|
+
* @return Value in poolSymbol-currency (e.g. MATIC, USDC), balance of pool share tokens, and share token symbol.
|
|
37
68
|
*/
|
|
38
69
|
getParticipationValue(poolSymbolName) {
|
|
39
70
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -62,8 +93,23 @@ class LiquidityProviderTool extends writeAccessHandler_1.default {
|
|
|
62
93
|
}
|
|
63
94
|
/**
|
|
64
95
|
* Add liquidity to the PnL participant fund. The address gets pool shares in return.
|
|
65
|
-
* @param {string}
|
|
96
|
+
* @param {string} poolSymbolName Name of pool symbol (e.g. MATIC)
|
|
66
97
|
* @param {number} amountCC Amount in pool-collateral currency
|
|
98
|
+
* @example
|
|
99
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
100
|
+
* async function main() {
|
|
101
|
+
* console.log(LiquidityProviderTool);
|
|
102
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
103
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
104
|
+
* const pk: string = <string>process.env.PK;
|
|
105
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
106
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
107
|
+
* // add liquidity
|
|
108
|
+
* let respAddLiquidity = await lqudtProviderTool.addLiquidity("MATIC", 0.1);
|
|
109
|
+
* console.log(respAddLiquidity);
|
|
110
|
+
* }
|
|
111
|
+
* main();
|
|
112
|
+
*
|
|
67
113
|
* @return Transaction object
|
|
68
114
|
*/
|
|
69
115
|
addLiquidity(poolSymbolName, amountCC) {
|
|
@@ -79,9 +125,24 @@ class LiquidityProviderTool extends writeAccessHandler_1.default {
|
|
|
79
125
|
});
|
|
80
126
|
}
|
|
81
127
|
/**
|
|
82
|
-
* Remove liquidity from the pool.
|
|
128
|
+
* Remove liquidity from the pool. The address loses pool shares in return.
|
|
83
129
|
* @param {string} poolSymbolName Name of pool symbol (e.g. MATIC).
|
|
84
|
-
* @param {string} amountPoolShares Amount in pool-
|
|
130
|
+
* @param {string} amountPoolShares Amount in pool-shares, removes everything if > available amount.
|
|
131
|
+
* @example
|
|
132
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
133
|
+
* async function main() {
|
|
134
|
+
* console.log(LiquidityProviderTool);
|
|
135
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
136
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
137
|
+
* const pk: string = <string>process.env.PK;
|
|
138
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
139
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
140
|
+
* // remove liquidity
|
|
141
|
+
* let respRemoveLiquidity = await lqudtProviderTool.removeLiquidity("MATIC", 0.1);
|
|
142
|
+
* console.log(respRemoveLiquidity);
|
|
143
|
+
* }
|
|
144
|
+
* main();
|
|
145
|
+
*
|
|
85
146
|
* @return Transaction object.
|
|
86
147
|
*/
|
|
87
148
|
removeLiquidity(poolSymbolName, amountPoolShares) {
|
package/dist/marketData.d.ts
CHANGED
|
@@ -3,19 +3,66 @@ import { ethers } from "ethers";
|
|
|
3
3
|
import PerpetualDataHandler from "./perpetualDataHandler";
|
|
4
4
|
import { Order } from "./nodeSDKTypes";
|
|
5
5
|
/**
|
|
6
|
+
* Functions to access market data (e.g., information on open orders, information on products that can be traded).
|
|
6
7
|
* This class requires no private key and is blockchain read-only.
|
|
7
8
|
* No gas required for the queries here.
|
|
8
9
|
*/
|
|
9
10
|
export default class MarketData extends PerpetualDataHandler {
|
|
11
|
+
/**
|
|
12
|
+
* Constructor
|
|
13
|
+
* @param {NodeSDKConfig} config Configuration object, see
|
|
14
|
+
* PerpetualDataHandler.readSDKConfig.
|
|
15
|
+
* @example
|
|
16
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
17
|
+
* async function main() {
|
|
18
|
+
* console.log(MarketData);
|
|
19
|
+
* // load configuration for testnet
|
|
20
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
21
|
+
* // MarketData (read only, no authentication needed)
|
|
22
|
+
* let mktData = new MarketData(config);
|
|
23
|
+
* // Create a proxy instance to access the blockchain
|
|
24
|
+
* await mktData.createProxyInstance();
|
|
25
|
+
* }
|
|
26
|
+
* main();
|
|
27
|
+
*
|
|
28
|
+
*/
|
|
10
29
|
constructor(config: NodeSDKConfig);
|
|
11
30
|
createProxyInstance(): Promise<void>;
|
|
12
31
|
/**
|
|
13
32
|
* Get contract instance. Useful for event listening.
|
|
33
|
+
* @example
|
|
34
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
35
|
+
* async function main() {
|
|
36
|
+
* console.log(MarketData);
|
|
37
|
+
* // setup
|
|
38
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
39
|
+
* let mktData = new MarketData(config);
|
|
40
|
+
* await mktData.createProxyInstance();
|
|
41
|
+
* // Get contract instance
|
|
42
|
+
* let proxy = await mktData.getReadOnlyProxyInstance();
|
|
43
|
+
* console.log(proxy);
|
|
44
|
+
* }
|
|
45
|
+
* main();
|
|
46
|
+
*
|
|
14
47
|
* @returns read-only proxy instance
|
|
15
48
|
*/
|
|
16
49
|
getReadOnlyProxyInstance(): ethers.Contract;
|
|
17
50
|
/**
|
|
18
51
|
* Information about the products traded in the exchange.
|
|
52
|
+
* @example
|
|
53
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
54
|
+
* async function main() {
|
|
55
|
+
* console.log(MarketData);
|
|
56
|
+
* // setup
|
|
57
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
58
|
+
* let mktData = new MarketData(config);
|
|
59
|
+
* await mktData.createProxyInstance();
|
|
60
|
+
* // Get exchange info
|
|
61
|
+
* let info = await mktData.exchangeInfo();
|
|
62
|
+
* console.log(info);
|
|
63
|
+
* }
|
|
64
|
+
* main();
|
|
65
|
+
*
|
|
19
66
|
* @returns {ExchangeInfo} Array of static data for all the pools and perpetuals in the system.
|
|
20
67
|
*/
|
|
21
68
|
exchangeInfo(): Promise<ExchangeInfo>;
|
|
@@ -23,6 +70,29 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
23
70
|
* All open orders for a trader-address and a symbol.
|
|
24
71
|
* @param {string} traderAddr Address of the trader for which we get the open orders.
|
|
25
72
|
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
73
|
+
* @example
|
|
74
|
+
* // Setup
|
|
75
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
76
|
+
* let mktData = new MarketData(config);
|
|
77
|
+
* await mktData.createProxyInstance();
|
|
78
|
+
* // Get all open orders for a trader/symbol
|
|
79
|
+
* let opOrder = await mktData.openOrders("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
|
|
80
|
+
* "ETH-USD-MATIC");
|
|
81
|
+
* @example
|
|
82
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
83
|
+
* async function main() {
|
|
84
|
+
* console.log(MarketData);
|
|
85
|
+
* // setup
|
|
86
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
87
|
+
* let mktData = new MarketData(config);
|
|
88
|
+
* await mktData.createProxyInstance();
|
|
89
|
+
* // Get all open orders for a trader/symbol
|
|
90
|
+
* let opOrder = await mktData.openOrders("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
|
|
91
|
+
* "ETH-USD-MATIC");
|
|
92
|
+
* console.log(opOrder);
|
|
93
|
+
* }
|
|
94
|
+
* main();
|
|
95
|
+
*
|
|
26
96
|
* @returns {Array<Array<Order>, Array<string>>} Array of open orders and corresponding order-ids.
|
|
27
97
|
*/
|
|
28
98
|
openOrders(traderAddr: string, symbol: string): Promise<{
|
|
@@ -30,9 +100,32 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
30
100
|
orderIds: string[];
|
|
31
101
|
}>;
|
|
32
102
|
/**
|
|
33
|
-
* Information about the
|
|
103
|
+
* Information about the positions open by a given trader in a given perpetual contract.
|
|
34
104
|
* @param {string} traderAddr Address of the trader for which we get the position risk.
|
|
35
105
|
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
106
|
+
* @example
|
|
107
|
+
* // Setup
|
|
108
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
109
|
+
* let mktData = new MarketData(config);
|
|
110
|
+
* await mktData.createProxyInstance();
|
|
111
|
+
* // Get position risk info
|
|
112
|
+
* let posRisk = await mktData.positionRisk("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
|
|
113
|
+
* "ETH-USD-MATIC");
|
|
114
|
+
* @example
|
|
115
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
116
|
+
* async function main() {
|
|
117
|
+
* console.log(MarketData);
|
|
118
|
+
* // setup
|
|
119
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
120
|
+
* let mktData = new MarketData(config);
|
|
121
|
+
* await mktData.createProxyInstance();
|
|
122
|
+
* // Get position risk info
|
|
123
|
+
* let posRisk = await mktData.positionRisk("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
|
|
124
|
+
* "ETH-USD-MATIC");
|
|
125
|
+
* console.log(posRisk);
|
|
126
|
+
* }
|
|
127
|
+
* main();
|
|
128
|
+
*
|
|
36
129
|
* @returns {MarginAccount}
|
|
37
130
|
*/
|
|
38
131
|
positionRisk(traderAddr: string, symbol: string): Promise<MarginAccount>;
|
|
@@ -40,12 +133,40 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
40
133
|
* 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
134
|
* @param {string} base Index name, e.g. ETH.
|
|
42
135
|
* @param {string} quote Quote currency, e.g. USD.
|
|
136
|
+
* @example
|
|
137
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
138
|
+
* async function main() {
|
|
139
|
+
* console.log(MarketData);
|
|
140
|
+
* // setup
|
|
141
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
142
|
+
* let mktData = new MarketData(config);
|
|
143
|
+
* await mktData.createProxyInstance();
|
|
144
|
+
* // get oracle price
|
|
145
|
+
* let price = await mktData.getOraclePrice("ETH", "USD");
|
|
146
|
+
* console.log(price);
|
|
147
|
+
* }
|
|
148
|
+
* main();
|
|
149
|
+
*
|
|
43
150
|
* @returns {number} Price of index in given currency.
|
|
44
151
|
*/
|
|
45
152
|
getOraclePrice(base: string, quote: string): Promise<number | undefined>;
|
|
46
153
|
/**
|
|
47
154
|
* Get the current mark price
|
|
48
155
|
* @param symbol symbol of the form ETH-USD-MATIC
|
|
156
|
+
* @example
|
|
157
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
158
|
+
* async function main() {
|
|
159
|
+
* console.log(MarketData);
|
|
160
|
+
* // setup
|
|
161
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
162
|
+
* let mktData = new MarketData(config);
|
|
163
|
+
* await mktData.createProxyInstance();
|
|
164
|
+
* // get mark price
|
|
165
|
+
* let price = await mktData.getMarkPrice("ETH-USD-MATIC");
|
|
166
|
+
* console.log(price);
|
|
167
|
+
* }
|
|
168
|
+
* main();
|
|
169
|
+
*
|
|
49
170
|
* @returns mark price
|
|
50
171
|
*/
|
|
51
172
|
getMarkPrice(symbol: string): Promise<number>;
|
|
@@ -53,6 +174,20 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
53
174
|
* get the current price for a given quantity
|
|
54
175
|
* @param symbol symbol of the form ETH-USD-MATIC
|
|
55
176
|
* @param quantity quantity to be traded, negative if short
|
|
177
|
+
* @example
|
|
178
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
179
|
+
* async function main() {
|
|
180
|
+
* console.log(MarketData);
|
|
181
|
+
* // setup
|
|
182
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
183
|
+
* let mktData = new MarketData(config);
|
|
184
|
+
* await mktData.createProxyInstance();
|
|
185
|
+
* // get perpetual price
|
|
186
|
+
* let price = await mktData.getPerpetualPrice("ETH-USD-MATIC", 1);
|
|
187
|
+
* console.log(price);
|
|
188
|
+
* }
|
|
189
|
+
* main();
|
|
190
|
+
*
|
|
56
191
|
* @returns price (number)
|
|
57
192
|
*/
|
|
58
193
|
getPerpetualPrice(symbol: string, quantity: number): Promise<number>;
|
|
@@ -66,11 +201,11 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
66
201
|
protected openOrdersOnOrderBook(traderAddr: string, orderBookContract: ethers.Contract): Promise<Order[]>;
|
|
67
202
|
/**
|
|
68
203
|
*
|
|
69
|
-
* @param traderAddr
|
|
70
|
-
* @param orderBookContract
|
|
71
|
-
* @returns
|
|
204
|
+
* @param traderAddr Address of the trader
|
|
205
|
+
* @param orderBookContract Instance of order book contract
|
|
206
|
+
* @returns Array of order-id's
|
|
72
207
|
* @ignore
|
|
73
208
|
*/
|
|
74
|
-
|
|
209
|
+
static orderIdsOfTrader(traderAddr: string, orderBookContract: ethers.Contract): Promise<string[]>;
|
|
75
210
|
static _exchangeInfo(_proxyContract: ethers.Contract): Promise<ExchangeInfo>;
|
|
76
211
|
}
|
package/dist/marketData.js
CHANGED
|
@@ -18,10 +18,29 @@ const d8XMath_1 = require("./d8XMath");
|
|
|
18
18
|
const utils_1 = require("./utils");
|
|
19
19
|
const perpetualDataHandler_1 = __importDefault(require("./perpetualDataHandler"));
|
|
20
20
|
/**
|
|
21
|
+
* Functions to access market data (e.g., information on open orders, information on products that can be traded).
|
|
21
22
|
* This class requires no private key and is blockchain read-only.
|
|
22
23
|
* No gas required for the queries here.
|
|
23
24
|
*/
|
|
24
25
|
class MarketData extends perpetualDataHandler_1.default {
|
|
26
|
+
/**
|
|
27
|
+
* Constructor
|
|
28
|
+
* @param {NodeSDKConfig} config Configuration object, see
|
|
29
|
+
* PerpetualDataHandler.readSDKConfig.
|
|
30
|
+
* @example
|
|
31
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
32
|
+
* async function main() {
|
|
33
|
+
* console.log(MarketData);
|
|
34
|
+
* // load configuration for testnet
|
|
35
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
36
|
+
* // MarketData (read only, no authentication needed)
|
|
37
|
+
* let mktData = new MarketData(config);
|
|
38
|
+
* // Create a proxy instance to access the blockchain
|
|
39
|
+
* await mktData.createProxyInstance();
|
|
40
|
+
* }
|
|
41
|
+
* main();
|
|
42
|
+
*
|
|
43
|
+
*/
|
|
25
44
|
constructor(config) {
|
|
26
45
|
super(config);
|
|
27
46
|
}
|
|
@@ -33,6 +52,20 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
33
52
|
}
|
|
34
53
|
/**
|
|
35
54
|
* Get contract instance. Useful for event listening.
|
|
55
|
+
* @example
|
|
56
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
57
|
+
* async function main() {
|
|
58
|
+
* console.log(MarketData);
|
|
59
|
+
* // setup
|
|
60
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
61
|
+
* let mktData = new MarketData(config);
|
|
62
|
+
* await mktData.createProxyInstance();
|
|
63
|
+
* // Get contract instance
|
|
64
|
+
* let proxy = await mktData.getReadOnlyProxyInstance();
|
|
65
|
+
* console.log(proxy);
|
|
66
|
+
* }
|
|
67
|
+
* main();
|
|
68
|
+
*
|
|
36
69
|
* @returns read-only proxy instance
|
|
37
70
|
*/
|
|
38
71
|
getReadOnlyProxyInstance() {
|
|
@@ -43,6 +76,20 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
43
76
|
}
|
|
44
77
|
/**
|
|
45
78
|
* Information about the products traded in the exchange.
|
|
79
|
+
* @example
|
|
80
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
81
|
+
* async function main() {
|
|
82
|
+
* console.log(MarketData);
|
|
83
|
+
* // setup
|
|
84
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
85
|
+
* let mktData = new MarketData(config);
|
|
86
|
+
* await mktData.createProxyInstance();
|
|
87
|
+
* // Get exchange info
|
|
88
|
+
* let info = await mktData.exchangeInfo();
|
|
89
|
+
* console.log(info);
|
|
90
|
+
* }
|
|
91
|
+
* main();
|
|
92
|
+
*
|
|
46
93
|
* @returns {ExchangeInfo} Array of static data for all the pools and perpetuals in the system.
|
|
47
94
|
*/
|
|
48
95
|
exchangeInfo() {
|
|
@@ -57,6 +104,29 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
57
104
|
* All open orders for a trader-address and a symbol.
|
|
58
105
|
* @param {string} traderAddr Address of the trader for which we get the open orders.
|
|
59
106
|
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
107
|
+
* @example
|
|
108
|
+
* // Setup
|
|
109
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
110
|
+
* let mktData = new MarketData(config);
|
|
111
|
+
* await mktData.createProxyInstance();
|
|
112
|
+
* // Get all open orders for a trader/symbol
|
|
113
|
+
* let opOrder = await mktData.openOrders("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
|
|
114
|
+
* "ETH-USD-MATIC");
|
|
115
|
+
* @example
|
|
116
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
117
|
+
* async function main() {
|
|
118
|
+
* console.log(MarketData);
|
|
119
|
+
* // setup
|
|
120
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
121
|
+
* let mktData = new MarketData(config);
|
|
122
|
+
* await mktData.createProxyInstance();
|
|
123
|
+
* // Get all open orders for a trader/symbol
|
|
124
|
+
* let opOrder = await mktData.openOrders("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
|
|
125
|
+
* "ETH-USD-MATIC");
|
|
126
|
+
* console.log(opOrder);
|
|
127
|
+
* }
|
|
128
|
+
* main();
|
|
129
|
+
*
|
|
60
130
|
* @returns {Array<Array<Order>, Array<string>>} Array of open orders and corresponding order-ids.
|
|
61
131
|
*/
|
|
62
132
|
openOrders(traderAddr, symbol) {
|
|
@@ -65,15 +135,38 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
65
135
|
let orderBookContract = this.getOrderBookContract(symbol);
|
|
66
136
|
let [orders, digests] = yield Promise.all([
|
|
67
137
|
this.openOrdersOnOrderBook(traderAddr, orderBookContract),
|
|
68
|
-
|
|
138
|
+
MarketData.orderIdsOfTrader(traderAddr, orderBookContract),
|
|
69
139
|
]);
|
|
70
140
|
return { orders: orders, orderIds: digests };
|
|
71
141
|
});
|
|
72
142
|
}
|
|
73
143
|
/**
|
|
74
|
-
* Information about the
|
|
144
|
+
* Information about the positions open by a given trader in a given perpetual contract.
|
|
75
145
|
* @param {string} traderAddr Address of the trader for which we get the position risk.
|
|
76
146
|
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
147
|
+
* @example
|
|
148
|
+
* // Setup
|
|
149
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
150
|
+
* let mktData = new MarketData(config);
|
|
151
|
+
* await mktData.createProxyInstance();
|
|
152
|
+
* // Get position risk info
|
|
153
|
+
* let posRisk = await mktData.positionRisk("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
|
|
154
|
+
* "ETH-USD-MATIC");
|
|
155
|
+
* @example
|
|
156
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
157
|
+
* async function main() {
|
|
158
|
+
* console.log(MarketData);
|
|
159
|
+
* // setup
|
|
160
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
161
|
+
* let mktData = new MarketData(config);
|
|
162
|
+
* await mktData.createProxyInstance();
|
|
163
|
+
* // Get position risk info
|
|
164
|
+
* let posRisk = await mktData.positionRisk("0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B",
|
|
165
|
+
* "ETH-USD-MATIC");
|
|
166
|
+
* console.log(posRisk);
|
|
167
|
+
* }
|
|
168
|
+
* main();
|
|
169
|
+
*
|
|
77
170
|
* @returns {MarginAccount}
|
|
78
171
|
*/
|
|
79
172
|
positionRisk(traderAddr, symbol) {
|
|
@@ -89,6 +182,20 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
89
182
|
* 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
183
|
* @param {string} base Index name, e.g. ETH.
|
|
91
184
|
* @param {string} quote Quote currency, e.g. USD.
|
|
185
|
+
* @example
|
|
186
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
187
|
+
* async function main() {
|
|
188
|
+
* console.log(MarketData);
|
|
189
|
+
* // setup
|
|
190
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
191
|
+
* let mktData = new MarketData(config);
|
|
192
|
+
* await mktData.createProxyInstance();
|
|
193
|
+
* // get oracle price
|
|
194
|
+
* let price = await mktData.getOraclePrice("ETH", "USD");
|
|
195
|
+
* console.log(price);
|
|
196
|
+
* }
|
|
197
|
+
* main();
|
|
198
|
+
*
|
|
92
199
|
* @returns {number} Price of index in given currency.
|
|
93
200
|
*/
|
|
94
201
|
getOraclePrice(base, quote) {
|
|
@@ -103,6 +210,20 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
103
210
|
/**
|
|
104
211
|
* Get the current mark price
|
|
105
212
|
* @param symbol symbol of the form ETH-USD-MATIC
|
|
213
|
+
* @example
|
|
214
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
215
|
+
* async function main() {
|
|
216
|
+
* console.log(MarketData);
|
|
217
|
+
* // setup
|
|
218
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
219
|
+
* let mktData = new MarketData(config);
|
|
220
|
+
* await mktData.createProxyInstance();
|
|
221
|
+
* // get mark price
|
|
222
|
+
* let price = await mktData.getMarkPrice("ETH-USD-MATIC");
|
|
223
|
+
* console.log(price);
|
|
224
|
+
* }
|
|
225
|
+
* main();
|
|
226
|
+
*
|
|
106
227
|
* @returns mark price
|
|
107
228
|
*/
|
|
108
229
|
getMarkPrice(symbol) {
|
|
@@ -117,6 +238,20 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
117
238
|
* get the current price for a given quantity
|
|
118
239
|
* @param symbol symbol of the form ETH-USD-MATIC
|
|
119
240
|
* @param quantity quantity to be traded, negative if short
|
|
241
|
+
* @example
|
|
242
|
+
* import { MarketData, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
243
|
+
* async function main() {
|
|
244
|
+
* console.log(MarketData);
|
|
245
|
+
* // setup
|
|
246
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
247
|
+
* let mktData = new MarketData(config);
|
|
248
|
+
* await mktData.createProxyInstance();
|
|
249
|
+
* // get perpetual price
|
|
250
|
+
* let price = await mktData.getPerpetualPrice("ETH-USD-MATIC", 1);
|
|
251
|
+
* console.log(price);
|
|
252
|
+
* }
|
|
253
|
+
* main();
|
|
254
|
+
*
|
|
120
255
|
* @returns price (number)
|
|
121
256
|
*/
|
|
122
257
|
getPerpetualPrice(symbol, quantity) {
|
|
@@ -149,12 +284,12 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
149
284
|
}
|
|
150
285
|
/**
|
|
151
286
|
*
|
|
152
|
-
* @param traderAddr
|
|
153
|
-
* @param orderBookContract
|
|
154
|
-
* @returns
|
|
287
|
+
* @param traderAddr Address of the trader
|
|
288
|
+
* @param orderBookContract Instance of order book contract
|
|
289
|
+
* @returns Array of order-id's
|
|
155
290
|
* @ignore
|
|
156
291
|
*/
|
|
157
|
-
orderIdsOfTrader(traderAddr, orderBookContract) {
|
|
292
|
+
static orderIdsOfTrader(traderAddr, orderBookContract) {
|
|
158
293
|
return __awaiter(this, void 0, void 0, function* () {
|
|
159
294
|
let digestsRaw = yield orderBookContract.limitDigestsOfTrader(traderAddr, 0, 15);
|
|
160
295
|
let k = 0;
|
package/dist/nodeSDKTypes.d.ts
CHANGED
|
@@ -8,6 +8,11 @@ export declare const COLLATERAL_CURRENCY_QUOTE = 0;
|
|
|
8
8
|
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
|
+
export declare const PerpetualStateINVALID = 0;
|
|
12
|
+
export declare const PerpetualStateINITIALIZING = 1;
|
|
13
|
+
export declare const PerpetualStateNORMAL = 2;
|
|
14
|
+
export declare const PerpetualStateEMERGENCY = 3;
|
|
15
|
+
export declare const PerpetualStateCLEARED = 4;
|
|
11
16
|
export declare const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
12
17
|
export declare const ZERO_ORDER_ID = "0x0000000000000000000000000000000000000000000000000000000000000000";
|
|
13
18
|
export declare const ONE_64x64: 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_ORDER_ID = exports.ZERO_ADDRESS = exports.PERP_STATE_STR = exports.COLLATERAL_CURRENCY_QUANTO = exports.COLLATERAL_CURRENCY_BASE = exports.COLLATERAL_CURRENCY_QUOTE = exports.ERC20_ABI = exports.DEFAULT_CONFIG_MAINNET_NAME = exports.DEFAULT_CONFIG_TESTNET_NAME = exports.DEFAULT_CONFIG_MAINNET = exports.DEFAULT_CONFIG_TESTNET = void 0;
|
|
3
|
+
exports.CollaterlCCY = exports.CLOSED_SIDE = exports.SELL_SIDE = exports.BUY_SIDE = exports.ORDER_TYPE_STOP_LIMIT = exports.ORDER_TYPE_STOP_MARKET = exports.ORDER_TYPE_MARKET = exports.ORDER_TYPE_LIMIT = exports.MASK_KEEP_POS_LEVERAGE = exports.MASK_STOP_ORDER = exports.MASK_MARKET_ORDER = exports.MASK_LIMIT_ORDER = exports.MASK_CLOSE_ONLY = exports.ORDER_MAX_DURATION_SEC = exports.DECIMALS = exports.MAX_UINT_256 = exports.MAX_64x64 = exports.ONE_64x64 = exports.ZERO_ORDER_ID = exports.ZERO_ADDRESS = exports.PerpetualStateCLEARED = exports.PerpetualStateEMERGENCY = exports.PerpetualStateNORMAL = exports.PerpetualStateINITIALIZING = exports.PerpetualStateINVALID = 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,6 +11,11 @@ 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.PerpetualStateINVALID = 0;
|
|
15
|
+
exports.PerpetualStateINITIALIZING = 1;
|
|
16
|
+
exports.PerpetualStateNORMAL = 2;
|
|
17
|
+
exports.PerpetualStateEMERGENCY = 3;
|
|
18
|
+
exports.PerpetualStateCLEARED = 4;
|
|
14
19
|
exports.ZERO_ADDRESS = ethers_1.constants.AddressZero;
|
|
15
20
|
exports.ZERO_ORDER_ID = ethers_1.constants.HashZero;
|
|
16
21
|
exports.ONE_64x64 = ethers_1.BigNumber.from("0x010000000000000000");
|