@d8x/perpetuals-sdk 0.0.44 → 0.0.46
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 +12 -0
- package/abi/MockTokenSwap.json +186 -0
- package/config/defaultConfig.json +36 -10
- package/config/mockSwap.json +4 -0
- package/config/oldConfig.json +2 -1
- package/config/priceFeedConfig.json +18 -0
- package/dist/accountTrade.d.ts +1 -0
- package/dist/accountTrade.js +31 -7
- package/dist/d8XMath.d.ts +6 -0
- package/dist/d8XMath.js +19 -1
- package/dist/liquidatorTool.d.ts +6 -4
- package/dist/liquidatorTool.js +13 -7
- package/dist/marketData.d.ts +6 -11
- package/dist/marketData.js +59 -41
- package/dist/nodeSDKTypes.d.ts +33 -2
- package/dist/nodeSDKTypes.js +3 -18
- package/dist/orderReferrerTool.d.ts +17 -7
- package/dist/orderReferrerTool.js +43 -13
- package/dist/perpetualDataHandler.d.ts +46 -6
- package/dist/perpetualDataHandler.js +143 -16
- package/dist/perpetualEventHandler.d.ts +0 -3
- package/dist/perpetualEventHandler.js +0 -3
- package/dist/priceFeeds.d.ts +115 -0
- package/dist/priceFeeds.js +373 -0
- package/dist/triangulator.d.ts +27 -0
- package/dist/triangulator.js +107 -0
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -2
- package/src/accountTrade.ts +31 -8
- package/src/d8XMath.ts +47 -22
- package/src/index.ts +2 -0
- package/src/liquidatorTool.ts +28 -8
- package/src/marketData.ts +99 -61
- package/src/nodeSDKTypes.ts +30 -3
- package/src/orderReferrerTool.ts +56 -13
- package/src/perpetualDataHandler.ts +149 -21
- package/src/perpetualEventHandler.ts +0 -3
- package/src/priceFeeds.ts +371 -0
- package/src/triangulator.ts +105 -0
- package/src/version.ts +1 -1
package/README.md
CHANGED
|
@@ -12,6 +12,18 @@ Node TypeScript SDK for D8X Perpetual Futures
|
|
|
12
12
|
- clone this repo
|
|
13
13
|
- open directory of repo and type `yarn` to install packages
|
|
14
14
|
|
|
15
|
+
#### Optional
|
|
16
|
+
- To execute orders and liquidate traders, we recommend to run your own Pyth-price service
|
|
17
|
+
- Efficient execution requires the endpoint `api/latest_vaas_px` which is available
|
|
18
|
+
in D8X's [fork](https://github.com/D8-X/pyth-crosschain-d8x.git) of [Pyth's repo](https://github.com/pyth-network/pyth-crosschain.git)
|
|
19
|
+
- Follow the instructions on [Wormhole Spy](https://github.com/pyth-network/pyth-crosschain/tree/main/price_service/server), specifically
|
|
20
|
+
- Copy `.env.example` into `.env` and edit as described in the Readme
|
|
21
|
+
- For the D8X fork (with `api/latest_vaas_px`) to run, you need to build the image as described in the last [paragraph of the Readme here](https://github.com/D8-X/pyth-crosschain-d8x/tree/main/price_service/server).
|
|
22
|
+
Ensure your own price server is enabled in docker-compose:
|
|
23
|
+
```
|
|
24
|
+
# image: public.ecr.aws/pyth-network/xc-server:v3.0.0
|
|
25
|
+
image: pyth_price_server
|
|
26
|
+
```
|
|
15
27
|
### Test
|
|
16
28
|
|
|
17
29
|
`yarn test`
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"inputs": [
|
|
4
|
+
{
|
|
5
|
+
"internalType": "address",
|
|
6
|
+
"name": "_mockTokenAddr",
|
|
7
|
+
"type": "address"
|
|
8
|
+
},
|
|
9
|
+
{
|
|
10
|
+
"internalType": "uint256",
|
|
11
|
+
"name": "_d18MaticToMockConversion",
|
|
12
|
+
"type": "uint256"
|
|
13
|
+
}
|
|
14
|
+
],
|
|
15
|
+
"stateMutability": "nonpayable",
|
|
16
|
+
"type": "constructor"
|
|
17
|
+
},
|
|
18
|
+
{
|
|
19
|
+
"anonymous": false,
|
|
20
|
+
"inputs": [
|
|
21
|
+
{
|
|
22
|
+
"indexed": false,
|
|
23
|
+
"internalType": "uint256",
|
|
24
|
+
"name": "amount",
|
|
25
|
+
"type": "uint256"
|
|
26
|
+
}
|
|
27
|
+
],
|
|
28
|
+
"name": "BalanceWithdrawn",
|
|
29
|
+
"type": "event"
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
"anonymous": false,
|
|
33
|
+
"inputs": [
|
|
34
|
+
{
|
|
35
|
+
"indexed": false,
|
|
36
|
+
"internalType": "uint256",
|
|
37
|
+
"name": "amount",
|
|
38
|
+
"type": "uint256"
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
"indexed": false,
|
|
42
|
+
"internalType": "address",
|
|
43
|
+
"name": "sender",
|
|
44
|
+
"type": "address"
|
|
45
|
+
}
|
|
46
|
+
],
|
|
47
|
+
"name": "MockTokensReceived",
|
|
48
|
+
"type": "event"
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
"anonymous": false,
|
|
52
|
+
"inputs": [
|
|
53
|
+
{
|
|
54
|
+
"indexed": false,
|
|
55
|
+
"internalType": "uint256",
|
|
56
|
+
"name": "amountIn",
|
|
57
|
+
"type": "uint256"
|
|
58
|
+
},
|
|
59
|
+
{
|
|
60
|
+
"indexed": false,
|
|
61
|
+
"internalType": "uint256",
|
|
62
|
+
"name": "amountOut",
|
|
63
|
+
"type": "uint256"
|
|
64
|
+
},
|
|
65
|
+
{
|
|
66
|
+
"indexed": false,
|
|
67
|
+
"internalType": "uint256",
|
|
68
|
+
"name": "poolBalance",
|
|
69
|
+
"type": "uint256"
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
"indexed": false,
|
|
73
|
+
"internalType": "address",
|
|
74
|
+
"name": "sender",
|
|
75
|
+
"type": "address"
|
|
76
|
+
}
|
|
77
|
+
],
|
|
78
|
+
"name": "MockTokensSwapped",
|
|
79
|
+
"type": "event"
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
"anonymous": false,
|
|
83
|
+
"inputs": [
|
|
84
|
+
{
|
|
85
|
+
"indexed": true,
|
|
86
|
+
"internalType": "address",
|
|
87
|
+
"name": "previousOwner",
|
|
88
|
+
"type": "address"
|
|
89
|
+
},
|
|
90
|
+
{
|
|
91
|
+
"indexed": true,
|
|
92
|
+
"internalType": "address",
|
|
93
|
+
"name": "newOwner",
|
|
94
|
+
"type": "address"
|
|
95
|
+
}
|
|
96
|
+
],
|
|
97
|
+
"name": "OwnershipTransferred",
|
|
98
|
+
"type": "event"
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
"inputs": [],
|
|
102
|
+
"name": "d18MaticToMockConversion",
|
|
103
|
+
"outputs": [
|
|
104
|
+
{
|
|
105
|
+
"internalType": "uint256",
|
|
106
|
+
"name": "",
|
|
107
|
+
"type": "uint256"
|
|
108
|
+
}
|
|
109
|
+
],
|
|
110
|
+
"stateMutability": "view",
|
|
111
|
+
"type": "function"
|
|
112
|
+
},
|
|
113
|
+
{
|
|
114
|
+
"inputs": [
|
|
115
|
+
{
|
|
116
|
+
"internalType": "uint256",
|
|
117
|
+
"name": "_amount",
|
|
118
|
+
"type": "uint256"
|
|
119
|
+
}
|
|
120
|
+
],
|
|
121
|
+
"name": "depositMockToken",
|
|
122
|
+
"outputs": [],
|
|
123
|
+
"stateMutability": "nonpayable",
|
|
124
|
+
"type": "function"
|
|
125
|
+
},
|
|
126
|
+
{
|
|
127
|
+
"inputs": [],
|
|
128
|
+
"name": "mockTokenAddr",
|
|
129
|
+
"outputs": [
|
|
130
|
+
{
|
|
131
|
+
"internalType": "address",
|
|
132
|
+
"name": "",
|
|
133
|
+
"type": "address"
|
|
134
|
+
}
|
|
135
|
+
],
|
|
136
|
+
"stateMutability": "view",
|
|
137
|
+
"type": "function"
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
"inputs": [],
|
|
141
|
+
"name": "owner",
|
|
142
|
+
"outputs": [
|
|
143
|
+
{
|
|
144
|
+
"internalType": "address",
|
|
145
|
+
"name": "",
|
|
146
|
+
"type": "address"
|
|
147
|
+
}
|
|
148
|
+
],
|
|
149
|
+
"stateMutability": "view",
|
|
150
|
+
"type": "function"
|
|
151
|
+
},
|
|
152
|
+
{
|
|
153
|
+
"inputs": [],
|
|
154
|
+
"name": "renounceOwnership",
|
|
155
|
+
"outputs": [],
|
|
156
|
+
"stateMutability": "nonpayable",
|
|
157
|
+
"type": "function"
|
|
158
|
+
},
|
|
159
|
+
{
|
|
160
|
+
"inputs": [],
|
|
161
|
+
"name": "swapToMockToken",
|
|
162
|
+
"outputs": [],
|
|
163
|
+
"stateMutability": "payable",
|
|
164
|
+
"type": "function"
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
"inputs": [
|
|
168
|
+
{
|
|
169
|
+
"internalType": "address",
|
|
170
|
+
"name": "newOwner",
|
|
171
|
+
"type": "address"
|
|
172
|
+
}
|
|
173
|
+
],
|
|
174
|
+
"name": "transferOwnership",
|
|
175
|
+
"outputs": [],
|
|
176
|
+
"stateMutability": "nonpayable",
|
|
177
|
+
"type": "function"
|
|
178
|
+
},
|
|
179
|
+
{
|
|
180
|
+
"inputs": [],
|
|
181
|
+
"name": "withdraw",
|
|
182
|
+
"outputs": [],
|
|
183
|
+
"stateMutability": "nonpayable",
|
|
184
|
+
"type": "function"
|
|
185
|
+
}
|
|
186
|
+
]
|
|
@@ -1,10 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"name": "testnet",
|
|
4
|
+
"version": 1.5,
|
|
5
|
+
"proxyAddr": "0xaB6AD25eE5CA28E7d7b16A33A33897aE34BF9e67",
|
|
6
|
+
"nodeURL": "https://polygon-mumbai.blockpi.network/v1/rpc/public",
|
|
7
|
+
"proxyABILocation": "../abi/IPerpetualManager.json",
|
|
8
|
+
"limitOrderBookFactoryAddr": "0xe95422bf27C62F3b9ae19d6032aC58c1bd13C71E",
|
|
9
|
+
"limitOrderBookFactoryABILocation": "../abi/LimitOrderBookFactory.json",
|
|
10
|
+
"limitOrderBookABILocation": "../abi/LimitOrderBook.json",
|
|
11
|
+
"symbolListLocation": "../config/symbolList.json"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"name": "mainnet",
|
|
15
|
+
"version": 0,
|
|
16
|
+
"proxyAddr": "",
|
|
17
|
+
"nodeURL": "",
|
|
18
|
+
"proxyABILocation": "../abi/IPerpetualManager.json",
|
|
19
|
+
"limitOrderBookFactoryAddr": "",
|
|
20
|
+
"limitOrderBookFactoryABILocation": "../abi/LimitOrderBookFactory.json",
|
|
21
|
+
"limitOrderBookABILocation": "../abi/LimitOrderBook.json",
|
|
22
|
+
"symbolListLocation": "../config/symbolList.json"
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
"name": "central-park",
|
|
26
|
+
"version": 3.0,
|
|
27
|
+
"proxyAddr": "0x4F1C7EE9E881Cc735f39Ca36c649ba9E930B4E4e",
|
|
28
|
+
"nodeURL": "https://polygon-mumbai.blockpi.network/v1/rpc/public",
|
|
29
|
+
"priceFeedConfigNetwork": "testnet",
|
|
30
|
+
"proxyABILocation": "../abi/central-park/IPerpetualManager.json",
|
|
31
|
+
"limitOrderBookFactoryAddr": "0xf8a4B731C3663A411F96b35a1989D4cD3C59912d",
|
|
32
|
+
"limitOrderBookFactoryABILocation": "../abi/central-park/LimitOrderBookFactory.json",
|
|
33
|
+
"limitOrderBookABILocation": "../abi/central-park/LimitOrderBook.json",
|
|
34
|
+
"symbolListLocation": "../config/symbolList.json"
|
|
35
|
+
}
|
|
36
|
+
]
|
package/config/oldConfig.json
CHANGED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"network": "testnet",
|
|
4
|
+
"ids":
|
|
5
|
+
[
|
|
6
|
+
{"symbol":"BTC-USD", "id": "0xf9c0172ba10dfa4d19088d94f5bf61d3b54d5bd7483a322a982e1373ee8ea31b", "type": "pyth", "origin": "Crypto.BTC/USD"},
|
|
7
|
+
{"symbol":"ETH-USD", "id": "0xca80ba6dc32e08d06f1aa886011eed1d77c77be9eb761cc10d72b7d0a2fd57a6", "type": "pyth", "origin": "Crypto.ETH/USD"},
|
|
8
|
+
{"symbol":"MATIC-USD", "id": "0xd2c2c1f2bba8e0964f9589e060c2ee97f5e19057267ac3284caef3bd50bd2cb5", "type": "pyth", "origin": "Crypto.MATIC/USD"},
|
|
9
|
+
{"symbol":"USDC-USD", "id": "0x41f3625971ca2ed2263e78573fe5ce23e13d2558ed3f2e47ab0f84fb9e7ae722", "type": "pyth", "origin": "Crypto.USDC/USD"},
|
|
10
|
+
{"symbol":"XAU-USD", "id": "0x30a19158f5a54c0adf8fb7560627343f22a1bc852b89d56be1accdc5dbf96d0e", "type": "pyth", "origin": "Metal.XAU/USD"},
|
|
11
|
+
{"symbol":"USD-CHF", "id": "0x796d24444ff50728b58e94b1f53dc3a406b2f1ba9d0d0b91d4406c37491a6feb", "type": "pyth", "origin": "FX.USD/CHF"},
|
|
12
|
+
{"symbol":"GBP-USD", "id": "0xbcbdc2755bd74a2065f9d3283c2b8acbd898e473bdb90a6764b3dbd467c56ecd", "type": "pyth", "origin": "FX.GBP/USD"},
|
|
13
|
+
{"symbol":"EUR-USD", "id": "0xc1b12769f6633798d45adfd62bfc70114839232e2949b01fb3d3f927d2606154", "type": "pyth", "origin": "FX.EUR/USD"},
|
|
14
|
+
{"symbol":"USD-JPY", "id": "0x20a938f54b68f1f2ef18ea0328f6dd0747f8ea11486d22b021e83a900be89776", "type": "pyth", "origin": "FX.USD/JPY"}
|
|
15
|
+
],
|
|
16
|
+
"endpoints": [{"type": "pyth", "endpoint": "https://pyth.testnet.quantena.tech/api"}]
|
|
17
|
+
}
|
|
18
|
+
]
|
package/dist/accountTrade.d.ts
CHANGED
|
@@ -218,4 +218,5 @@ export default class AccountTrade extends WriteAccessHandler {
|
|
|
218
218
|
* @param {number} amount How much collateral to remove, in units of collateral currency, e.g. MATIC
|
|
219
219
|
*/
|
|
220
220
|
removeCollateral(symbol: string, amount: number): Promise<ethers.ContractTransaction>;
|
|
221
|
+
swapForMockToken(symbol: string, amountToPay: string): Promise<any>;
|
|
221
222
|
}
|
package/dist/accountTrade.js
CHANGED
|
@@ -12,6 +12,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
12
12
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const ethers_1 = require("ethers");
|
|
15
16
|
const d8XMath_1 = require("./d8XMath");
|
|
16
17
|
const marketData_1 = __importDefault(require("./marketData"));
|
|
17
18
|
const nodeSDKTypes_1 = require("./nodeSDKTypes");
|
|
@@ -80,11 +81,6 @@ class AccountTrade extends writeAccessHandler_1.default {
|
|
|
80
81
|
return yield this._cancelOrder(symbol, orderId, orderBookContract);
|
|
81
82
|
});
|
|
82
83
|
}
|
|
83
|
-
/*
|
|
84
|
-
TODO: -deposit (margin into account)
|
|
85
|
-
-withdraw margin withdraw(uint24 _iPerpetualId, int128 _fAmount)
|
|
86
|
-
|
|
87
|
-
*/
|
|
88
84
|
/**
|
|
89
85
|
* Submits an order to the exchange.
|
|
90
86
|
* @param {Order} order Order structure. As a minimum the structure needs to
|
|
@@ -320,7 +316,11 @@ class AccountTrade extends writeAccessHandler_1.default {
|
|
|
320
316
|
}
|
|
321
317
|
let perpId = this.getPerpIdFromSymbol(symbol);
|
|
322
318
|
let fAmountCC = (0, d8XMath_1.floatToABK64x64)(amount);
|
|
323
|
-
|
|
319
|
+
let priceFeedData = yield this.fetchLatestFeedPriceInfo(symbol);
|
|
320
|
+
return yield this.proxyContract.deposit(perpId, fAmountCC, priceFeedData.priceFeedVaas, priceFeedData.timestamps, {
|
|
321
|
+
gasLimit: this.gasLimit,
|
|
322
|
+
value: this.PRICE_UPDATE_FEE_GWEI * priceFeedData.priceFeedVaas.length,
|
|
323
|
+
});
|
|
324
324
|
});
|
|
325
325
|
}
|
|
326
326
|
/**
|
|
@@ -335,7 +335,31 @@ class AccountTrade extends writeAccessHandler_1.default {
|
|
|
335
335
|
}
|
|
336
336
|
let perpId = this.getPerpIdFromSymbol(symbol);
|
|
337
337
|
let fAmountCC = (0, d8XMath_1.floatToABK64x64)(amount);
|
|
338
|
-
|
|
338
|
+
let priceFeedData = yield this.fetchLatestFeedPriceInfo(symbol);
|
|
339
|
+
return yield this.proxyContract.withdraw(perpId, fAmountCC, priceFeedData.priceFeedVaas, priceFeedData.timestamps, {
|
|
340
|
+
gasLimit: this.gasLimit,
|
|
341
|
+
value: this.PRICE_UPDATE_FEE_GWEI * priceFeedData.priceFeedVaas.length,
|
|
342
|
+
});
|
|
343
|
+
});
|
|
344
|
+
}
|
|
345
|
+
swapForMockToken(symbol, amountToPay) {
|
|
346
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
347
|
+
if (this.signer == null) {
|
|
348
|
+
throw Error("no wallet initialized. Use createProxyInstance().");
|
|
349
|
+
}
|
|
350
|
+
let tokenAddress = this.getMarginTokenFromSymbol(symbol);
|
|
351
|
+
if (tokenAddress == undefined) {
|
|
352
|
+
throw Error("symbols not found");
|
|
353
|
+
}
|
|
354
|
+
let tokenToSwap = new Map(Object.entries(require("../config/mockSwap.json")));
|
|
355
|
+
let swapAddress = tokenToSwap.get(tokenAddress);
|
|
356
|
+
if (swapAddress == undefined) {
|
|
357
|
+
throw Error("No swap contract found for symbol.");
|
|
358
|
+
}
|
|
359
|
+
let contract = new ethers_1.ethers.Contract(swapAddress, nodeSDKTypes_1.MOCK_TOKEN_SWAP_ABI, this.signer.provider);
|
|
360
|
+
return yield contract.swapToMockToken({
|
|
361
|
+
value: ethers_1.ethers.utils.parseEther(amountToPay),
|
|
362
|
+
});
|
|
339
363
|
});
|
|
340
364
|
}
|
|
341
365
|
}
|
package/dist/d8XMath.d.ts
CHANGED
|
@@ -9,6 +9,12 @@ import { BigNumber } from "ethers";
|
|
|
9
9
|
* @returns {number} x/2^64 in number-format (float)
|
|
10
10
|
*/
|
|
11
11
|
export declare function ABK64x64ToFloat(x: BigNumber | number): number;
|
|
12
|
+
/**
|
|
13
|
+
*
|
|
14
|
+
* @param {BigNumber} x BigNumber in Dec-N format
|
|
15
|
+
* @returns {number} x as a float (number)
|
|
16
|
+
*/
|
|
17
|
+
export declare function decNToFloat(x: BigNumber, numDec: number): number;
|
|
12
18
|
/**
|
|
13
19
|
*
|
|
14
20
|
* @param {BigNumber} x BigNumber in Dec18 format
|
package/dist/d8XMath.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getDepositAmountForLvgTrade = exports.getNewPositionLeverage = exports.getMaxSignedPositionSize = exports.getMarginRequiredForLeveragedTrade = exports.calculateLiquidationPriceCollateralQuote = exports.calculateLiquidationPriceCollateralQuanto = exports.calculateLiquidationPriceCollateralBase = exports.div64x64 = exports.mul64x64 = exports.floatToDec18 = exports.floatToABK64x64 = exports.dec18ToFloat = exports.ABK64x64ToFloat = void 0;
|
|
3
|
+
exports.getDepositAmountForLvgTrade = exports.getNewPositionLeverage = exports.getMaxSignedPositionSize = exports.getMarginRequiredForLeveragedTrade = exports.calculateLiquidationPriceCollateralQuote = exports.calculateLiquidationPriceCollateralQuanto = exports.calculateLiquidationPriceCollateralBase = exports.div64x64 = exports.mul64x64 = exports.floatToDec18 = exports.floatToABK64x64 = exports.dec18ToFloat = exports.decNToFloat = exports.ABK64x64ToFloat = void 0;
|
|
4
4
|
const ethers_1 = require("ethers");
|
|
5
5
|
const nodeSDKTypes_1 = require("./nodeSDKTypes");
|
|
6
6
|
/**
|
|
@@ -29,6 +29,24 @@ function ABK64x64ToFloat(x) {
|
|
|
29
29
|
return parseFloat(NumberStr) * s;
|
|
30
30
|
}
|
|
31
31
|
exports.ABK64x64ToFloat = ABK64x64ToFloat;
|
|
32
|
+
/**
|
|
33
|
+
*
|
|
34
|
+
* @param {BigNumber} x BigNumber in Dec-N format
|
|
35
|
+
* @returns {number} x as a float (number)
|
|
36
|
+
*/
|
|
37
|
+
function decNToFloat(x, numDec) {
|
|
38
|
+
//x: BigNumber in DecN format to float
|
|
39
|
+
const DECIMALS = ethers_1.BigNumber.from(10).pow(ethers_1.BigNumber.from(numDec));
|
|
40
|
+
let s = x.lt(0) ? -1 : 1;
|
|
41
|
+
x = x.mul(s);
|
|
42
|
+
let xInt = x.div(DECIMALS);
|
|
43
|
+
let xDec = x.sub(xInt.mul(DECIMALS));
|
|
44
|
+
let k = numDec - xDec.toString().length;
|
|
45
|
+
let sPad = "0".repeat(k);
|
|
46
|
+
let NumberStr = xInt.toString() + "." + sPad + xDec.toString();
|
|
47
|
+
return parseFloat(NumberStr) * s;
|
|
48
|
+
}
|
|
49
|
+
exports.decNToFloat = decNToFloat;
|
|
32
50
|
/**
|
|
33
51
|
*
|
|
34
52
|
* @param {BigNumber} x BigNumber in Dec18 format
|
package/dist/liquidatorTool.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import WriteAccessHandler from "./writeAccessHandler";
|
|
2
|
-
import { NodeSDKConfig } from "./nodeSDKTypes";
|
|
2
|
+
import { NodeSDKConfig, PriceFeedSubmission } from "./nodeSDKTypes";
|
|
3
3
|
import { ethers } from "ethers";
|
|
4
4
|
/**
|
|
5
5
|
* Functions to liquidate traders. This class requires a private key
|
|
@@ -33,6 +33,7 @@ export default class LiquidatorTool extends WriteAccessHandler {
|
|
|
33
33
|
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
34
34
|
* @param {string} traderAddr Address of the trader to be liquidated.
|
|
35
35
|
* @param {string=} liquidatorAddr Address to be credited if the liquidation succeeds.
|
|
36
|
+
* @param {PriceFeedSubmission} priceFeedData optional. VAA and timestamps for oracle. If not provided will query from REST API.
|
|
36
37
|
* Defaults to the wallet used to execute the liquidation.
|
|
37
38
|
* @example
|
|
38
39
|
* import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
@@ -52,7 +53,7 @@ export default class LiquidatorTool extends WriteAccessHandler {
|
|
|
52
53
|
*
|
|
53
54
|
* @returns Transaction object.
|
|
54
55
|
*/
|
|
55
|
-
liquidateTrader(symbol: string, traderAddr: string, liquidatorAddr?: string): Promise<ethers.ContractTransaction>;
|
|
56
|
+
liquidateTrader(symbol: string, traderAddr: string, liquidatorAddr?: string, priceFeedData?: PriceFeedSubmission): Promise<ethers.ContractTransaction>;
|
|
56
57
|
/**
|
|
57
58
|
* Check if the collateral of a trader is above the maintenance margin ("maintenance margin safe").
|
|
58
59
|
* If not, the position can be liquidated.
|
|
@@ -83,10 +84,11 @@ export default class LiquidatorTool extends WriteAccessHandler {
|
|
|
83
84
|
* @param perpetualId Perpetual id.
|
|
84
85
|
* @param liquidatorAddr Address to be credited for the liquidation.
|
|
85
86
|
* @param traderAddr Address of the trader to be liquidated.
|
|
86
|
-
* @param
|
|
87
|
+
* @param priceFeedData contains VAA and timestamps required
|
|
88
|
+
* @param options E.g., Gas limit, fee.
|
|
87
89
|
* @ignore
|
|
88
90
|
*/
|
|
89
|
-
_liquidateByAMM(perpetualId: number, liquidatorAddr: string, traderAddr: string,
|
|
91
|
+
_liquidateByAMM(perpetualId: number, liquidatorAddr: string, traderAddr: string, priceFeedData: PriceFeedSubmission, options: object): Promise<any>;
|
|
90
92
|
/**
|
|
91
93
|
* Total number of active accounts for this symbol, i.e. accounts with positions that are currently open.
|
|
92
94
|
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
package/dist/liquidatorTool.js
CHANGED
|
@@ -47,6 +47,7 @@ class LiquidatorTool extends writeAccessHandler_1.default {
|
|
|
47
47
|
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
48
48
|
* @param {string} traderAddr Address of the trader to be liquidated.
|
|
49
49
|
* @param {string=} liquidatorAddr Address to be credited if the liquidation succeeds.
|
|
50
|
+
* @param {PriceFeedSubmission} priceFeedData optional. VAA and timestamps for oracle. If not provided will query from REST API.
|
|
50
51
|
* Defaults to the wallet used to execute the liquidation.
|
|
51
52
|
* @example
|
|
52
53
|
* import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
@@ -66,7 +67,7 @@ class LiquidatorTool extends writeAccessHandler_1.default {
|
|
|
66
67
|
*
|
|
67
68
|
* @returns Transaction object.
|
|
68
69
|
*/
|
|
69
|
-
liquidateTrader(symbol, traderAddr, liquidatorAddr = "") {
|
|
70
|
+
liquidateTrader(symbol, traderAddr, liquidatorAddr = "", priceFeedData) {
|
|
70
71
|
return __awaiter(this, void 0, void 0, function* () {
|
|
71
72
|
// this operation spends gas, so signer is required
|
|
72
73
|
if (this.proxyContract == null || this.signer == null) {
|
|
@@ -77,7 +78,13 @@ class LiquidatorTool extends writeAccessHandler_1.default {
|
|
|
77
78
|
liquidatorAddr = this.traderAddr;
|
|
78
79
|
}
|
|
79
80
|
let perpID = LiquidatorTool.symbolToPerpetualId(symbol, this.symbolToPerpStaticInfo);
|
|
80
|
-
|
|
81
|
+
if (priceFeedData == undefined) {
|
|
82
|
+
priceFeedData = yield this.fetchLatestFeedPriceInfo(symbol);
|
|
83
|
+
}
|
|
84
|
+
return yield this._liquidateByAMM(perpID, liquidatorAddr, traderAddr, priceFeedData, {
|
|
85
|
+
gasLimit: this.gasLimit,
|
|
86
|
+
value: this.PRICE_UPDATE_FEE_GWEI * priceFeedData.priceFeedVaas.length,
|
|
87
|
+
});
|
|
81
88
|
});
|
|
82
89
|
}
|
|
83
90
|
/**
|
|
@@ -124,14 +131,13 @@ class LiquidatorTool extends writeAccessHandler_1.default {
|
|
|
124
131
|
* @param perpetualId Perpetual id.
|
|
125
132
|
* @param liquidatorAddr Address to be credited for the liquidation.
|
|
126
133
|
* @param traderAddr Address of the trader to be liquidated.
|
|
127
|
-
* @param
|
|
134
|
+
* @param priceFeedData contains VAA and timestamps required
|
|
135
|
+
* @param options E.g., Gas limit, fee.
|
|
128
136
|
* @ignore
|
|
129
137
|
*/
|
|
130
|
-
_liquidateByAMM(perpetualId, liquidatorAddr, traderAddr,
|
|
138
|
+
_liquidateByAMM(perpetualId, liquidatorAddr, traderAddr, priceFeedData, options) {
|
|
131
139
|
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
-
return yield this.proxyContract.liquidateByAMM(perpetualId, liquidatorAddr, traderAddr,
|
|
133
|
-
gasLimit: gasLimit,
|
|
134
|
-
});
|
|
140
|
+
return yield this.proxyContract.liquidateByAMM(perpetualId, liquidatorAddr, traderAddr, priceFeedData.priceFeedVaas, priceFeedData.timestamps, options);
|
|
135
141
|
});
|
|
136
142
|
}
|
|
137
143
|
/**
|
package/dist/marketData.d.ts
CHANGED
|
@@ -2,6 +2,7 @@ import { ethers } from "ethers";
|
|
|
2
2
|
import "./nodeSDKTypes";
|
|
3
3
|
import { ExchangeInfo, MarginAccount, NodeSDKConfig, Order, PerpetualState, PerpetualStaticInfo, PoolStaticInfo, SmartContractOrder } from "./nodeSDKTypes";
|
|
4
4
|
import PerpetualDataHandler from "./perpetualDataHandler";
|
|
5
|
+
import PriceFeeds from "./priceFeeds";
|
|
5
6
|
/**
|
|
6
7
|
* Functions to access market data (e.g., information on open orders, information on products that can be traded).
|
|
7
8
|
* This class requires no private key and is blockchain read-only.
|
|
@@ -148,12 +149,6 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
148
149
|
*/
|
|
149
150
|
positionRiskOnCollateralAction(deltaCollateral: number, currentPositionRisk: MarginAccount): Promise<MarginAccount>;
|
|
150
151
|
protected static _positionRiskOnAccountAction(symbol: string, tradeAmount: number, marginDeposit: number, tradeLeverage: number | undefined, keepPositionLvg: boolean | undefined, tradePrice: number, feeRate: number, perpetualState: PerpetualState, currentPositionRisk: MarginAccount, symbolToPerpStaticInfo: Map<string, PerpetualStaticInfo>): MarginAccount;
|
|
151
|
-
/**
|
|
152
|
-
* Gets the pool index (in exchangeInfo) corresponding to a given symbol.
|
|
153
|
-
* @param symbol Symbol of the form ETH-USD-MATIC
|
|
154
|
-
* @returns Pool index
|
|
155
|
-
*/
|
|
156
|
-
getPoolIndexFromSymbol(symbol: string): number;
|
|
157
152
|
/**
|
|
158
153
|
* Gets the wallet balance in the collateral currency corresponding to a given perpetual symbol.
|
|
159
154
|
* @param address Address to check
|
|
@@ -211,7 +206,7 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
211
206
|
*
|
|
212
207
|
* @returns mark price
|
|
213
208
|
*/
|
|
214
|
-
getMarkPrice(symbol: string): Promise<number>;
|
|
209
|
+
getMarkPrice(symbol: string, indexPrices?: [number, number]): Promise<number>;
|
|
215
210
|
/**
|
|
216
211
|
* get the current price for a given quantity
|
|
217
212
|
* @param symbol symbol of the form ETH-USD-MATIC
|
|
@@ -232,13 +227,14 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
232
227
|
*
|
|
233
228
|
* @returns price (number)
|
|
234
229
|
*/
|
|
235
|
-
getPerpetualPrice(symbol: string, quantity: number): Promise<number>;
|
|
230
|
+
getPerpetualPrice(symbol: string, quantity: number, indexPrices?: [number, number]): Promise<number>;
|
|
236
231
|
/**
|
|
237
232
|
* Query recent perpetual state from blockchain
|
|
238
233
|
* @param symbol symbol of the form ETH-USD-MATIC
|
|
234
|
+
* @param indexPrices S2 and S3 prices/isMarketOpen if not provided fetch via REST API
|
|
239
235
|
* @returns PerpetualState reference
|
|
240
236
|
*/
|
|
241
|
-
getPerpetualState(symbol: string): Promise<PerpetualState>;
|
|
237
|
+
getPerpetualState(symbol: string, indexPriceInfo?: [number, number, boolean, boolean]): Promise<PerpetualState>;
|
|
242
238
|
/**
|
|
243
239
|
* Query perpetual static info.
|
|
244
240
|
* This information is queried once at createProxyInstance-time and remains static after that.
|
|
@@ -283,6 +279,5 @@ export default class MarketData extends PerpetualDataHandler {
|
|
|
283
279
|
*/
|
|
284
280
|
static orderIdsOfTrader(traderAddr: string, orderBookContract: ethers.Contract): Promise<string[]>;
|
|
285
281
|
getAvailableMargin(traderAddr: string, symbol: string): Promise<number>;
|
|
286
|
-
|
|
287
|
-
static _exchangeInfo(_proxyContract: ethers.Contract, _poolStaticInfos: Array<PoolStaticInfo>, _symbolList: Map<string, string>): Promise<ExchangeInfo>;
|
|
282
|
+
static _exchangeInfo(_proxyContract: ethers.Contract, _poolStaticInfos: Array<PoolStaticInfo>, _symbolToPerpStaticInfo: Map<string, PerpetualStaticInfo>, _symbolList: Map<string, string>, _priceFeedGetter: PriceFeeds): Promise<ExchangeInfo>;
|
|
288
283
|
}
|