@d8x/perpetuals-sdk 0.0.7 → 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 +65 -5
- package/dist/accountTrade.js +73 -4
- package/dist/brokerTool.d.ts +199 -13
- package/dist/brokerTool.js +197 -11
- package/dist/index.d.ts +2 -1
- package/dist/index.js +3 -1
- package/dist/liquidatorTool.d.ts +101 -6
- package/dist/liquidatorTool.js +104 -9
- package/dist/liquidityProviderTool.d.ts +69 -8
- package/dist/liquidityProviderTool.js +68 -7
- package/dist/marketData.d.ts +165 -5
- package/dist/marketData.js +193 -6
- package/dist/nodeSDKTypes.d.ts +6 -0
- package/dist/nodeSDKTypes.js +8 -2
- package/dist/orderReferrerTool.d.ts +22 -5
- package/dist/orderReferrerTool.js +33 -6
- package/dist/perpetualDataHandler.js +18 -10
- package/dist/writeAccessHandler.d.ts +3 -3
- package/dist/writeAccessHandler.js +2 -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
|
-
addLiquidity(poolSymbolName: string, amountCC: number): Promise<ethers.
|
|
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,14 +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>;
|
|
31
|
+
/**
|
|
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
|
+
*
|
|
47
|
+
* @returns read-only proxy instance
|
|
48
|
+
*/
|
|
49
|
+
getReadOnlyProxyInstance(): ethers.Contract;
|
|
12
50
|
/**
|
|
13
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
|
+
*
|
|
14
66
|
* @returns {ExchangeInfo} Array of static data for all the pools and perpetuals in the system.
|
|
15
67
|
*/
|
|
16
68
|
exchangeInfo(): Promise<ExchangeInfo>;
|
|
@@ -18,6 +70,29 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
18
70
|
* All open orders for a trader-address and a symbol.
|
|
19
71
|
* @param {string} traderAddr Address of the trader for which we get the open orders.
|
|
20
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
|
+
*
|
|
21
96
|
* @returns {Array<Array<Order>, Array<string>>} Array of open orders and corresponding order-ids.
|
|
22
97
|
*/
|
|
23
98
|
openOrders(traderAddr: string, symbol: string): Promise<{
|
|
@@ -25,12 +100,97 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
25
100
|
orderIds: string[];
|
|
26
101
|
}>;
|
|
27
102
|
/**
|
|
28
|
-
* Information about the
|
|
103
|
+
* Information about the positions open by a given trader in a given perpetual contract.
|
|
29
104
|
* @param {string} traderAddr Address of the trader for which we get the position risk.
|
|
30
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
|
+
*
|
|
31
129
|
* @returns {MarginAccount}
|
|
32
130
|
*/
|
|
33
131
|
positionRisk(traderAddr: string, symbol: string): Promise<MarginAccount>;
|
|
132
|
+
/**
|
|
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.
|
|
134
|
+
* @param {string} base Index name, e.g. ETH.
|
|
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
|
+
*
|
|
150
|
+
* @returns {number} Price of index in given currency.
|
|
151
|
+
*/
|
|
152
|
+
getOraclePrice(base: string, quote: string): Promise<number | undefined>;
|
|
153
|
+
/**
|
|
154
|
+
* Get the current mark price
|
|
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
|
+
*
|
|
170
|
+
* @returns mark price
|
|
171
|
+
*/
|
|
172
|
+
getMarkPrice(symbol: string): Promise<number>;
|
|
173
|
+
/**
|
|
174
|
+
* get the current price for a given quantity
|
|
175
|
+
* @param symbol symbol of the form ETH-USD-MATIC
|
|
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
|
+
*
|
|
191
|
+
* @returns price (number)
|
|
192
|
+
*/
|
|
193
|
+
getPerpetualPrice(symbol: string, quantity: number): Promise<number>;
|
|
34
194
|
/**
|
|
35
195
|
* Query smart contract to get user orders and convert to user friendly order format.
|
|
36
196
|
* @param {string} traderAddr Address of trader.
|
|
@@ -41,11 +201,11 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
41
201
|
protected openOrdersOnOrderBook(traderAddr: string, orderBookContract: ethers.Contract): Promise<Order[]>;
|
|
42
202
|
/**
|
|
43
203
|
*
|
|
44
|
-
* @param traderAddr
|
|
45
|
-
* @param orderBookContract
|
|
46
|
-
* @returns
|
|
204
|
+
* @param traderAddr Address of the trader
|
|
205
|
+
* @param orderBookContract Instance of order book contract
|
|
206
|
+
* @returns Array of order-id's
|
|
47
207
|
* @ignore
|
|
48
208
|
*/
|
|
49
|
-
|
|
209
|
+
static orderIdsOfTrader(traderAddr: string, orderBookContract: ethers.Contract): Promise<string[]>;
|
|
50
210
|
static _exchangeInfo(_proxyContract: ethers.Contract): Promise<ExchangeInfo>;
|
|
51
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
|
}
|
|
@@ -31,8 +50,46 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
31
50
|
yield this.initContractsAndData(this.provider);
|
|
32
51
|
});
|
|
33
52
|
}
|
|
53
|
+
/**
|
|
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
|
+
*
|
|
69
|
+
* @returns read-only proxy instance
|
|
70
|
+
*/
|
|
71
|
+
getReadOnlyProxyInstance() {
|
|
72
|
+
if (this.proxyContract == null) {
|
|
73
|
+
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
74
|
+
}
|
|
75
|
+
return this.proxyContract;
|
|
76
|
+
}
|
|
34
77
|
/**
|
|
35
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
|
+
*
|
|
36
93
|
* @returns {ExchangeInfo} Array of static data for all the pools and perpetuals in the system.
|
|
37
94
|
*/
|
|
38
95
|
exchangeInfo() {
|
|
@@ -47,6 +104,29 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
47
104
|
* All open orders for a trader-address and a symbol.
|
|
48
105
|
* @param {string} traderAddr Address of the trader for which we get the open orders.
|
|
49
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
|
+
*
|
|
50
130
|
* @returns {Array<Array<Order>, Array<string>>} Array of open orders and corresponding order-ids.
|
|
51
131
|
*/
|
|
52
132
|
openOrders(traderAddr, symbol) {
|
|
@@ -55,15 +135,38 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
55
135
|
let orderBookContract = this.getOrderBookContract(symbol);
|
|
56
136
|
let [orders, digests] = yield Promise.all([
|
|
57
137
|
this.openOrdersOnOrderBook(traderAddr, orderBookContract),
|
|
58
|
-
|
|
138
|
+
MarketData.orderIdsOfTrader(traderAddr, orderBookContract),
|
|
59
139
|
]);
|
|
60
140
|
return { orders: orders, orderIds: digests };
|
|
61
141
|
});
|
|
62
142
|
}
|
|
63
143
|
/**
|
|
64
|
-
* Information about the
|
|
144
|
+
* Information about the positions open by a given trader in a given perpetual contract.
|
|
65
145
|
* @param {string} traderAddr Address of the trader for which we get the position risk.
|
|
66
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
|
+
*
|
|
67
170
|
* @returns {MarginAccount}
|
|
68
171
|
*/
|
|
69
172
|
positionRisk(traderAddr, symbol) {
|
|
@@ -75,6 +178,90 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
75
178
|
return mgnAcct;
|
|
76
179
|
});
|
|
77
180
|
}
|
|
181
|
+
/**
|
|
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.
|
|
183
|
+
* @param {string} base Index name, e.g. ETH.
|
|
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
|
+
*
|
|
199
|
+
* @returns {number} Price of index in given currency.
|
|
200
|
+
*/
|
|
201
|
+
getOraclePrice(base, quote) {
|
|
202
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
203
|
+
if (this.proxyContract == null) {
|
|
204
|
+
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
205
|
+
}
|
|
206
|
+
let px = yield this.proxyContract.getOraclePrice([(0, utils_1.toBytes4)(base), (0, utils_1.toBytes4)(quote)]);
|
|
207
|
+
return px == undefined ? undefined : (0, d8XMath_1.ABK64x64ToFloat)(px);
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Get the current mark price
|
|
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
|
+
*
|
|
227
|
+
* @returns mark price
|
|
228
|
+
*/
|
|
229
|
+
getMarkPrice(symbol) {
|
|
230
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
231
|
+
if (this.proxyContract == null) {
|
|
232
|
+
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
233
|
+
}
|
|
234
|
+
return yield perpetualDataHandler_1.default._queryPerpetualMarkPrice(symbol, this.symbolToPerpStaticInfo, this.proxyContract);
|
|
235
|
+
});
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* get the current price for a given quantity
|
|
239
|
+
* @param symbol symbol of the form ETH-USD-MATIC
|
|
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
|
+
*
|
|
255
|
+
* @returns price (number)
|
|
256
|
+
*/
|
|
257
|
+
getPerpetualPrice(symbol, quantity) {
|
|
258
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
259
|
+
if (this.proxyContract == null) {
|
|
260
|
+
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
261
|
+
}
|
|
262
|
+
return yield perpetualDataHandler_1.default._queryPerpetualPrice(symbol, quantity, this.symbolToPerpStaticInfo, this.proxyContract);
|
|
263
|
+
});
|
|
264
|
+
}
|
|
78
265
|
/**
|
|
79
266
|
* Query smart contract to get user orders and convert to user friendly order format.
|
|
80
267
|
* @param {string} traderAddr Address of trader.
|
|
@@ -97,12 +284,12 @@ class MarketData extends perpetualDataHandler_1.default {
|
|
|
97
284
|
}
|
|
98
285
|
/**
|
|
99
286
|
*
|
|
100
|
-
* @param traderAddr
|
|
101
|
-
* @param orderBookContract
|
|
102
|
-
* @returns
|
|
287
|
+
* @param traderAddr Address of the trader
|
|
288
|
+
* @param orderBookContract Instance of order book contract
|
|
289
|
+
* @returns Array of order-id's
|
|
103
290
|
* @ignore
|
|
104
291
|
*/
|
|
105
|
-
orderIdsOfTrader(traderAddr, orderBookContract) {
|
|
292
|
+
static orderIdsOfTrader(traderAddr, orderBookContract) {
|
|
106
293
|
return __awaiter(this, void 0, void 0, function* () {
|
|
107
294
|
let digestsRaw = yield orderBookContract.limitDigestsOfTrader(traderAddr, 0, 15);
|
|
108
295
|
let k = 0;
|