@d8x/perpetuals-sdk 0.1.12 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/abi/testnet/IPerpetualManager.json +5215 -0
- package/dist/cjs/abi/testnet/LimitOrderBook.json +1075 -0
- package/dist/cjs/abi/zkevmTestnet/IPerpetualManager.json +5215 -0
- package/dist/cjs/abi/zkevmTestnet/LimitOrderBook.json +1075 -0
- package/dist/cjs/abi/zkevmTestnet/LimitOrderBookFactory.json +135 -0
- package/dist/cjs/accountTrade.js +441 -0
- package/dist/cjs/accountTrade.js.map +1 -0
- package/dist/{src → cjs}/brokerTool.js +31 -84
- package/dist/cjs/brokerTool.js.map +1 -0
- package/dist/cjs/config/defaultConfig.json +47 -0
- package/dist/cjs/config/mockSwap.json +4 -0
- package/dist/cjs/config/priceFeedConfig.json +104 -0
- package/dist/cjs/config/symbolList.json +13 -0
- package/dist/cjs/d8XMath.js.map +1 -0
- package/dist/cjs/index.js +29 -0
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/liquidatorTool.js +287 -0
- package/dist/cjs/liquidatorTool.js.map +1 -0
- package/dist/{src → cjs}/liquidityProviderTool.js +12 -65
- package/dist/cjs/liquidityProviderTool.js.map +1 -0
- package/dist/{src → cjs}/marketData.js +52 -134
- package/dist/cjs/marketData.js.map +1 -0
- package/dist/{src → cjs}/nodeSDKTypes.d.ts +5 -5
- package/dist/cjs/nodeSDKTypes.js +64 -0
- package/dist/cjs/nodeSDKTypes.js.map +1 -0
- package/dist/{src → cjs}/orderReferrerTool.d.ts +12 -5
- package/dist/{src → cjs}/orderReferrerTool.js +114 -112
- package/dist/cjs/orderReferrerTool.js.map +1 -0
- package/dist/{src → cjs}/perpetualDataHandler.d.ts +1 -1
- package/dist/{src → cjs}/perpetualDataHandler.js +47 -109
- package/dist/cjs/perpetualDataHandler.js.map +1 -0
- package/dist/{src → cjs}/perpetualEventHandler.d.ts +3 -3
- package/dist/{src → cjs}/perpetualEventHandler.js +12 -74
- package/dist/cjs/perpetualEventHandler.js.map +1 -0
- package/dist/cjs/priceFeeds.js +466 -0
- package/dist/cjs/priceFeeds.js.map +1 -0
- package/dist/{src → cjs}/traderDigests.js +7 -43
- package/dist/cjs/traderDigests.js.map +1 -0
- package/dist/{src → cjs}/traderInterface.js +13 -66
- package/dist/cjs/traderInterface.js.map +1 -0
- package/dist/{src → cjs}/triangulator.js +2 -17
- package/dist/cjs/triangulator.js.map +1 -0
- package/dist/{src → cjs}/utils.js +3 -29
- package/dist/cjs/utils.js.map +1 -0
- package/dist/cjs/version.d.ts +1 -0
- package/dist/{src → cjs}/version.js +1 -1
- package/dist/cjs/version.js.map +1 -0
- package/dist/{src → cjs}/writeAccessHandler.js +12 -65
- package/dist/cjs/writeAccessHandler.js.map +1 -0
- package/dist/esm/abi/ERC20.json +288 -0
- package/dist/esm/abi/MockTokenSwap.json +186 -0
- package/dist/{abi/testnet → esm/abi/central-park}/IPerpetualManager.json +404 -214
- package/dist/{abi/testnet → esm/abi/central-park}/LimitOrderBook.json +197 -15
- package/dist/esm/abi/central-park/LimitOrderBookFactory.json +135 -0
- package/dist/esm/abi/testnet/IPerpetualManager.json +5215 -0
- package/dist/esm/abi/testnet/LimitOrderBook.json +1075 -0
- package/dist/esm/abi/testnet/LimitOrderBookFactory.json +135 -0
- package/dist/esm/abi/zkevmTestnet/IPerpetualManager.json +5215 -0
- package/dist/esm/abi/zkevmTestnet/LimitOrderBook.json +1075 -0
- package/dist/esm/abi/zkevmTestnet/LimitOrderBookFactory.json +135 -0
- package/dist/esm/accountTrade.d.ts +221 -0
- package/dist/{src → esm}/accountTrade.js +22 -93
- package/dist/esm/accountTrade.js.map +1 -0
- package/dist/esm/brokerTool.d.ts +318 -0
- package/dist/esm/brokerTool.js +572 -0
- package/dist/esm/brokerTool.js.map +1 -0
- package/dist/esm/config/defaultConfig.json +47 -0
- package/dist/esm/config/mockSwap.json +4 -0
- package/dist/esm/config/priceFeedConfig.json +104 -0
- package/dist/esm/config/symbolList.json +13 -0
- package/dist/esm/d8XMath.d.ts +122 -0
- package/dist/esm/d8XMath.js +247 -0
- package/dist/esm/d8XMath.js.map +1 -0
- package/{src/index.ts → dist/esm/index.d.ts} +1 -15
- package/dist/esm/index.js +16 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/liquidatorTool.d.ts +158 -0
- package/dist/{src → esm}/liquidatorTool.js +10 -65
- package/dist/esm/liquidatorTool.js.map +1 -0
- package/dist/esm/liquidityProviderTool.d.ts +126 -0
- package/dist/esm/liquidityProviderTool.js +218 -0
- package/dist/esm/liquidityProviderTool.js.map +1 -0
- package/dist/esm/marketData.d.ts +309 -0
- package/dist/esm/marketData.js +1007 -0
- package/dist/esm/marketData.js.map +1 -0
- package/dist/esm/nodeSDKTypes.d.ts +266 -0
- package/dist/esm/nodeSDKTypes.js +60 -0
- package/dist/esm/nodeSDKTypes.js.map +1 -0
- package/dist/esm/orderReferrerTool.d.ts +196 -0
- package/dist/esm/orderReferrerTool.js +491 -0
- package/dist/esm/orderReferrerTool.js.map +1 -0
- package/dist/esm/perpetualDataHandler.d.ts +220 -0
- package/dist/esm/perpetualDataHandler.js +1060 -0
- package/dist/esm/perpetualDataHandler.js.map +1 -0
- package/dist/esm/perpetualEventHandler.d.ts +179 -0
- package/dist/esm/perpetualEventHandler.js +435 -0
- package/dist/esm/perpetualEventHandler.js.map +1 -0
- package/dist/esm/priceFeeds.d.ts +115 -0
- package/dist/{src → esm}/priceFeeds.js +16 -83
- package/dist/esm/priceFeeds.js.map +1 -0
- package/dist/esm/traderDigests.d.ts +21 -0
- package/dist/esm/traderDigests.js +80 -0
- package/dist/esm/traderDigests.js.map +1 -0
- package/dist/esm/traderInterface.d.ts +79 -0
- package/dist/esm/traderInterface.js +196 -0
- package/dist/esm/traderInterface.js.map +1 -0
- package/dist/esm/triangulator.d.ts +27 -0
- package/dist/esm/triangulator.js +110 -0
- package/dist/esm/triangulator.js.map +1 -0
- package/dist/esm/utils.d.ts +59 -0
- package/dist/esm/utils.js +138 -0
- package/dist/esm/utils.js.map +1 -0
- package/dist/esm/version.d.ts +1 -0
- package/dist/esm/version.js +2 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/esm/writeAccessHandler.d.ts +50 -0
- package/dist/esm/writeAccessHandler.js +157 -0
- package/dist/esm/writeAccessHandler.js.map +1 -0
- package/package.json +16 -26
- package/dist/bundle.js +0 -36793
- package/dist/config/defaultConfig.json +0 -47
- package/dist/config/mockSwap.json +0 -4
- package/dist/config/priceFeedConfig.json +0 -104
- package/dist/config/symbolList.json +0 -13
- package/dist/src/index.js +0 -45
- package/dist/src/nodeSDKTypes.js +0 -115
- package/dist/src/version.d.ts +0 -1
- package/module.d.ts +0 -1
- package/src/accountTrade.ts +0 -392
- package/src/brokerTool.ts +0 -507
- package/src/d8XMath.ts +0 -319
- package/src/liquidatorTool.ts +0 -258
- package/src/liquidityProviderTool.ts +0 -186
- package/src/marketData.ts +0 -946
- package/src/nodeSDKTypes.ts +0 -293
- package/src/orderReferrerTool.ts +0 -389
- package/src/perpetualDataHandler.ts +0 -1061
- package/src/perpetualEventHandler.ts +0 -455
- package/src/priceFeeds.ts +0 -381
- package/src/traderDigests.ts +0 -91
- package/src/traderInterface.ts +0 -159
- package/src/triangulator.ts +0 -105
- package/src/utils.ts +0 -134
- package/src/version.ts +0 -1
- package/src/writeAccessHandler.ts +0 -127
- /package/dist/{abi → cjs/abi}/ERC20.json +0 -0
- /package/dist/{abi → cjs/abi}/MockTokenSwap.json +0 -0
- /package/dist/{abi → cjs/abi}/central-park/IPerpetualManager.json +0 -0
- /package/dist/{abi → cjs/abi}/central-park/LimitOrderBook.json +0 -0
- /package/dist/{abi → cjs/abi}/central-park/LimitOrderBookFactory.json +0 -0
- /package/dist/{abi → cjs/abi}/testnet/LimitOrderBookFactory.json +0 -0
- /package/dist/{src → cjs}/accountTrade.d.ts +0 -0
- /package/dist/{src → cjs}/brokerTool.d.ts +0 -0
- /package/dist/{src → cjs}/d8XMath.d.ts +0 -0
- /package/dist/{src → cjs}/d8XMath.js +0 -0
- /package/dist/{src → cjs}/index.d.ts +0 -0
- /package/dist/{src → cjs}/liquidatorTool.d.ts +0 -0
- /package/dist/{src → cjs}/liquidityProviderTool.d.ts +0 -0
- /package/dist/{src → cjs}/marketData.d.ts +0 -0
- /package/dist/{src → cjs}/priceFeeds.d.ts +0 -0
- /package/dist/{src → cjs}/traderDigests.d.ts +0 -0
- /package/dist/{src → cjs}/traderInterface.d.ts +0 -0
- /package/dist/{src → cjs}/triangulator.d.ts +0 -0
- /package/dist/{src → cjs}/utils.d.ts +0 -0
- /package/dist/{src → cjs}/writeAccessHandler.d.ts +0 -0
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import WriteAccessHandler from "./writeAccessHandler";
|
|
2
|
+
import { NodeSDKConfig, PriceFeedSubmission } from "./nodeSDKTypes";
|
|
3
|
+
import { ethers } from "ethers";
|
|
4
|
+
/**
|
|
5
|
+
* Functions to liquidate traders. This class requires a private key
|
|
6
|
+
* and executes smart-contract interactions that require gas-payments.
|
|
7
|
+
* @extends WriteAccessHandler
|
|
8
|
+
*/
|
|
9
|
+
export default class LiquidatorTool extends WriteAccessHandler {
|
|
10
|
+
/**
|
|
11
|
+
* Constructs a LiquidatorTool instance for a given configuration and private key.
|
|
12
|
+
* @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.
|
|
13
|
+
* readSDKConfig.
|
|
14
|
+
* @example
|
|
15
|
+
* import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
16
|
+
* async function main() {
|
|
17
|
+
* console.log(LiquidatorTool);
|
|
18
|
+
* // load configuration for testnet
|
|
19
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
20
|
+
* // LiquidatorTool (authentication required, PK is an environment variable with a private key)
|
|
21
|
+
* const pk: string = <string>process.env.PK;
|
|
22
|
+
* let lqudtrTool = new LiquidatorTool(config, pk);
|
|
23
|
+
* // Create a proxy instance to access the blockchain
|
|
24
|
+
* await lqudtrTool.createProxyInstance();
|
|
25
|
+
* }
|
|
26
|
+
* main();
|
|
27
|
+
*
|
|
28
|
+
* @param {string} privateKey Private key of account that liquidates.
|
|
29
|
+
*/
|
|
30
|
+
constructor(config: NodeSDKConfig, privateKey: string);
|
|
31
|
+
/**
|
|
32
|
+
* Liquidate a trader.
|
|
33
|
+
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
34
|
+
* @param {string} traderAddr Address of the trader to be liquidated.
|
|
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.
|
|
37
|
+
* Defaults to the wallet used to execute the liquidation.
|
|
38
|
+
* @example
|
|
39
|
+
* import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
40
|
+
* async function main() {
|
|
41
|
+
* console.log(LiquidatorTool);
|
|
42
|
+
* // Setup (authentication required, PK is an environment variable with a private key)
|
|
43
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
44
|
+
* const pk: string = <string>process.env.PK;
|
|
45
|
+
* let lqudtrTool = new LiquidatorTool(config, pk);
|
|
46
|
+
* await lqudtrTool.createProxyInstance();
|
|
47
|
+
* // liquidate trader
|
|
48
|
+
* let liqAmount = await lqudtrTool.liquidateTrader("ETH-USD-MATIC",
|
|
49
|
+
* "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B");
|
|
50
|
+
* console.log(liqAmount);
|
|
51
|
+
* }
|
|
52
|
+
* main();
|
|
53
|
+
*
|
|
54
|
+
* @returns Transaction object.
|
|
55
|
+
*/
|
|
56
|
+
liquidateTrader(symbol: string, traderAddr: string, liquidatorAddr?: string, priceFeedData?: PriceFeedSubmission): Promise<ethers.ContractTransaction>;
|
|
57
|
+
/**
|
|
58
|
+
* Check if the collateral of a trader is above the maintenance margin ("maintenance margin safe").
|
|
59
|
+
* If not, the position can be liquidated.
|
|
60
|
+
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
61
|
+
* @param {string} traderAddr Address of the trader whose position you want to assess.
|
|
62
|
+
* @param {number[]} indexPrices optional, index price S2/S3 for which we test
|
|
63
|
+
* @example
|
|
64
|
+
* import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
65
|
+
* async function main() {
|
|
66
|
+
* console.log(LiquidatorTool);
|
|
67
|
+
* // Setup (authentication required, PK is an environment variable with a private key)
|
|
68
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
69
|
+
* const pk: string = <string>process.env.PK;
|
|
70
|
+
* let lqudtrTool = new LiquidatorTool(config, pk);
|
|
71
|
+
* await lqudtrTool.createProxyInstance();
|
|
72
|
+
* // check if trader can be liquidated
|
|
73
|
+
* let safe = await lqudtrTool.isMaintenanceMarginSafe("ETH-USD-MATIC",
|
|
74
|
+
* "0xAb5801a7D398351b8bE11C439e05C5B3259aeC9B");
|
|
75
|
+
* console.log(safe);
|
|
76
|
+
* }
|
|
77
|
+
* main();
|
|
78
|
+
*
|
|
79
|
+
* @returns {boolean} True if the trader is maintenance margin safe in the perpetual.
|
|
80
|
+
* False means that the trader's position can be liquidated.
|
|
81
|
+
*/
|
|
82
|
+
isMaintenanceMarginSafe(symbol: string, traderAddr: string, indexPrices?: [number, number]): Promise<boolean>;
|
|
83
|
+
/**
|
|
84
|
+
*
|
|
85
|
+
* @param perpetualId Perpetual id.
|
|
86
|
+
* @param liquidatorAddr Address to be credited for the liquidation.
|
|
87
|
+
* @param traderAddr Address of the trader to be liquidated.
|
|
88
|
+
* @param priceFeedData contains VAA and timestamps required
|
|
89
|
+
* @param options E.g., Gas limit, fee.
|
|
90
|
+
* @ignore
|
|
91
|
+
*/
|
|
92
|
+
_liquidateByAMM(perpetualId: number, liquidatorAddr: string, traderAddr: string, priceFeedData: PriceFeedSubmission, options: object): Promise<any>;
|
|
93
|
+
/**
|
|
94
|
+
* Total number of active accounts for this symbol, i.e. accounts with positions that are currently open.
|
|
95
|
+
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
96
|
+
* @example
|
|
97
|
+
* import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
98
|
+
* async function main() {
|
|
99
|
+
* console.log(LiquidatorTool);
|
|
100
|
+
* // Setup (authentication required, PK is an environment variable with a private key)
|
|
101
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
102
|
+
* const pk: string = <string>process.env.PK;
|
|
103
|
+
* let lqudtrTool = new LiquidatorTool(config, pk);
|
|
104
|
+
* await lqudtrTool.createProxyInstance();
|
|
105
|
+
* // get number of active accounts
|
|
106
|
+
* let accounts = await lqudtrTool.countActivePerpAccounts("ETH-USD-MATIC");
|
|
107
|
+
* console.log(accounts);
|
|
108
|
+
* }
|
|
109
|
+
* main();
|
|
110
|
+
*
|
|
111
|
+
* @returns {number} Number of active accounts.
|
|
112
|
+
*/
|
|
113
|
+
countActivePerpAccounts(symbol: string): Promise<number>;
|
|
114
|
+
/**
|
|
115
|
+
* Get addresses of active accounts by chunks.
|
|
116
|
+
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
117
|
+
* @param {number} from From which account we start counting (0-indexed).
|
|
118
|
+
* @param {number} to Until which account we count, non inclusive.
|
|
119
|
+
* @example
|
|
120
|
+
* import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
121
|
+
* async function main() {
|
|
122
|
+
* console.log(LiquidatorTool);
|
|
123
|
+
* // Setup (authentication required, PK is an environment variable with a private key)
|
|
124
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
125
|
+
* const pk: string = <string>process.env.PK;
|
|
126
|
+
* let lqudtrTool = new LiquidatorTool(config, pk);
|
|
127
|
+
* await lqudtrTool.createProxyInstance();
|
|
128
|
+
* // get all active accounts in chunks
|
|
129
|
+
* let accounts = await lqudtrTool.getActiveAccountsByChunks("ETH-USD-MATIC", 0, 4);
|
|
130
|
+
* console.log(accounts);
|
|
131
|
+
* }
|
|
132
|
+
* main();
|
|
133
|
+
*
|
|
134
|
+
* @returns {string[]} Array of addresses at locations 'from', 'from'+1 ,..., 'to'-1.
|
|
135
|
+
*/
|
|
136
|
+
getActiveAccountsByChunks(symbol: string, from: number, to: number): Promise<string[]>;
|
|
137
|
+
/**
|
|
138
|
+
* Addresses for all the active accounts in this perpetual symbol.
|
|
139
|
+
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
140
|
+
* @example
|
|
141
|
+
* import { LiquidatorTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
142
|
+
* async function main() {
|
|
143
|
+
* console.log(LiquidatorTool);
|
|
144
|
+
* // Setup (authentication required, PK is an environment variable with a private key)
|
|
145
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
146
|
+
* const pk: string = <string>process.env.PK;
|
|
147
|
+
* let lqudtrTool = new LiquidatorTool(config, pk);
|
|
148
|
+
* await lqudtrTool.createProxyInstance();
|
|
149
|
+
* // get all active accounts
|
|
150
|
+
* let accounts = await lqudtrTool.getAllActiveAccounts("ETH-USD-MATIC");
|
|
151
|
+
* console.log(accounts);
|
|
152
|
+
* }
|
|
153
|
+
* main();
|
|
154
|
+
*
|
|
155
|
+
* @returns {string[]} Array of addresses.
|
|
156
|
+
*/
|
|
157
|
+
getAllActiveAccounts(symbol: string): Promise<string[]>;
|
|
158
|
+
}
|
|
@@ -1,61 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
extendStatics = Object.setPrototypeOf ||
|
|
5
|
-
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
6
|
-
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
7
|
-
return extendStatics(d, b);
|
|
8
|
-
};
|
|
9
|
-
return function (d, b) {
|
|
10
|
-
if (typeof b !== "function" && b !== null)
|
|
11
|
-
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
12
|
-
extendStatics(d, b);
|
|
13
|
-
function __() { this.constructor = d; }
|
|
14
|
-
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
15
|
-
};
|
|
16
|
-
})();
|
|
17
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
18
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
19
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
20
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
21
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
22
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
23
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
24
|
-
});
|
|
25
|
-
};
|
|
26
|
-
var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
27
|
-
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
|
|
28
|
-
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
|
|
29
|
-
function verb(n) { return function (v) { return step([n, v]); }; }
|
|
30
|
-
function step(op) {
|
|
31
|
-
if (f) throw new TypeError("Generator is already executing.");
|
|
32
|
-
while (g && (g = 0, op[0] && (_ = 0)), _) try {
|
|
33
|
-
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
|
|
34
|
-
if (y = 0, t) op = [op[0] & 2, t.value];
|
|
35
|
-
switch (op[0]) {
|
|
36
|
-
case 0: case 1: t = op; break;
|
|
37
|
-
case 4: _.label++; return { value: op[1], done: false };
|
|
38
|
-
case 5: _.label++; y = op[1]; op = [0]; continue;
|
|
39
|
-
case 7: op = _.ops.pop(); _.trys.pop(); continue;
|
|
40
|
-
default:
|
|
41
|
-
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
|
|
42
|
-
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
|
|
43
|
-
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
|
|
44
|
-
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
|
|
45
|
-
if (t[2]) _.ops.pop();
|
|
46
|
-
_.trys.pop(); continue;
|
|
47
|
-
}
|
|
48
|
-
op = body.call(thisArg, _);
|
|
49
|
-
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
|
|
50
|
-
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
51
|
-
}
|
|
52
|
-
};
|
|
53
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
54
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
55
|
-
};
|
|
56
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
57
|
-
var writeAccessHandler_1 = __importDefault(require("./writeAccessHandler"));
|
|
58
|
-
var d8XMath_1 = require("./d8XMath");
|
|
1
|
+
import { __awaiter, __extends, __generator } from "tslib";
|
|
2
|
+
import WriteAccessHandler from "./writeAccessHandler";
|
|
3
|
+
import { ABK64x64ToFloat, floatToABK64x64 } from "./d8XMath";
|
|
59
4
|
/**
|
|
60
5
|
* Functions to liquidate traders. This class requires a private key
|
|
61
6
|
* and executes smart-contract interactions that require gas-payments.
|
|
@@ -183,7 +128,7 @@ var LiquidatorTool = /** @class */ (function (_super) {
|
|
|
183
128
|
obj = _a.sent();
|
|
184
129
|
indexPrices = [obj.idxPrices[0], obj.idxPrices[1]];
|
|
185
130
|
_a.label = 2;
|
|
186
|
-
case 2: return [4 /*yield*/, this.proxyContract.getTraderState(perpID, traderAddr, indexPrices.map(function (x) { return
|
|
131
|
+
case 2: return [4 /*yield*/, this.proxyContract.getTraderState(perpID, traderAddr, indexPrices.map(function (x) { return floatToABK64x64(x); }))];
|
|
187
132
|
case 3:
|
|
188
133
|
traderState = _a.sent();
|
|
189
134
|
if (traderState[idx_notional] == 0) {
|
|
@@ -194,10 +139,10 @@ var LiquidatorTool = /** @class */ (function (_super) {
|
|
|
194
139
|
idx_marginAccountPositionBC = 4;
|
|
195
140
|
idx_collateralToQuoteConversion = 9;
|
|
196
141
|
idx_marginBalance = 0;
|
|
197
|
-
maintMgnRate =
|
|
198
|
-
pos =
|
|
199
|
-
marginbalance =
|
|
200
|
-
coll2quote =
|
|
142
|
+
maintMgnRate = ABK64x64ToFloat(traderState[idx_maintenanceMgnRate]);
|
|
143
|
+
pos = ABK64x64ToFloat(traderState[idx_marginAccountPositionBC]);
|
|
144
|
+
marginbalance = ABK64x64ToFloat(traderState[idx_marginBalance]);
|
|
145
|
+
coll2quote = ABK64x64ToFloat(traderState[idx_collateralToQuoteConversion]);
|
|
201
146
|
base2collateral = indexPrices[0] / coll2quote;
|
|
202
147
|
threshold = Math.abs(pos * base2collateral * maintMgnRate);
|
|
203
148
|
return [2 /*return*/, marginbalance >= threshold];
|
|
@@ -335,6 +280,6 @@ var LiquidatorTool = /** @class */ (function (_super) {
|
|
|
335
280
|
});
|
|
336
281
|
};
|
|
337
282
|
return LiquidatorTool;
|
|
338
|
-
}(
|
|
339
|
-
|
|
283
|
+
}(WriteAccessHandler));
|
|
284
|
+
export default LiquidatorTool;
|
|
340
285
|
//# sourceMappingURL=liquidatorTool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"liquidatorTool.js","sourceRoot":"","sources":["../../src/liquidatorTool.ts"],"names":[],"mappings":";AAAA,OAAO,kBAAkB,MAAM,sBAAsB,CAAC;AAGtD,OAA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,IAAI,EAAE,EAAE,CAAC,EAAA;4BAA/E,sBAAO,SAAwE,EAAC;;;;KACjF;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACU,6CAAoB,GAAjC,UAAkC,MAAc;;;;;4BAE1B,qBAAM,IAAI,CAAC,uBAAuB,CAAC,MAAM,CAAC,EAAA;;wBAA1D,aAAa,GAAG,SAA0C;wBACvD,qBAAM,IAAI,CAAC,yBAAyB,CAAC,MAAM,EAAE,CAAC,EAAE,aAAa,CAAC,EAAA;4BAArE,sBAAO,SAA8D,EAAC;;;;KACvE;IACH,qBAAC;AAAD,CAAC,AAvPD,CAA4C,kBAAkB,GAuP7D"}
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
import { ethers } from "ethers";
|
|
2
|
+
import { NodeSDKConfig } from "./nodeSDKTypes";
|
|
3
|
+
import WriteAccessHandler from "./writeAccessHandler";
|
|
4
|
+
/**
|
|
5
|
+
* Functions to provide liquidity. This class requires a private key and executes
|
|
6
|
+
* smart-contract interactions that require gas-payments.
|
|
7
|
+
* @extends WriteAccessHandler
|
|
8
|
+
*/
|
|
9
|
+
export default class LiquidityProviderTool extends WriteAccessHandler {
|
|
10
|
+
/**
|
|
11
|
+
* Constructor
|
|
12
|
+
* @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.
|
|
13
|
+
* readSDKConfig.
|
|
14
|
+
* @example
|
|
15
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
16
|
+
* async function main() {
|
|
17
|
+
* console.log(LiquidityProviderTool);
|
|
18
|
+
* // load configuration for testnet
|
|
19
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
20
|
+
* // LiquidityProviderTool (authentication required, PK is an environment variable with a private key)
|
|
21
|
+
* const pk: string = <string>process.env.PK;
|
|
22
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
23
|
+
* // Create a proxy instance to access the blockchain
|
|
24
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
25
|
+
* }
|
|
26
|
+
* main();
|
|
27
|
+
*
|
|
28
|
+
* @param privateKey private key of account that trades
|
|
29
|
+
*/
|
|
30
|
+
constructor(config: NodeSDKConfig, privateKey: string);
|
|
31
|
+
/**
|
|
32
|
+
* Value of the pool share tokens for this liquidity provider
|
|
33
|
+
* in poolSymbol-currency (e.g. MATIC, USDC).
|
|
34
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC).
|
|
35
|
+
* @example
|
|
36
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
37
|
+
* async function main() {
|
|
38
|
+
* console.log(LiquidityProviderTool);
|
|
39
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
40
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
41
|
+
* const pk: string = <string>process.env.PK;
|
|
42
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
43
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
44
|
+
* // get value of pool share token
|
|
45
|
+
* let shareToken = await lqudtProviderTool.getParticipationValue("MATIC");
|
|
46
|
+
* console.log(shareToken);
|
|
47
|
+
* }
|
|
48
|
+
* main();
|
|
49
|
+
*
|
|
50
|
+
* @return Value in poolSymbol-currency (e.g. MATIC, USDC), balance of pool share tokens, and share token symbol.
|
|
51
|
+
*/
|
|
52
|
+
getParticipationValue(poolSymbolName: string): Promise<{
|
|
53
|
+
value: number;
|
|
54
|
+
shareTokenBalance: number;
|
|
55
|
+
poolShareToken: string;
|
|
56
|
+
}>;
|
|
57
|
+
/**
|
|
58
|
+
* Add liquidity to the PnL participant fund. The address gets pool shares in return.
|
|
59
|
+
* @param {string} poolSymbolName Name of pool symbol (e.g. MATIC)
|
|
60
|
+
* @param {number} amountCC Amount in pool-collateral currency
|
|
61
|
+
* @example
|
|
62
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
63
|
+
* async function main() {
|
|
64
|
+
* console.log(LiquidityProviderTool);
|
|
65
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
66
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
67
|
+
* const pk: string = <string>process.env.PK;
|
|
68
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
69
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
70
|
+
* // add liquidity
|
|
71
|
+
* await lqudtProviderTool.setAllowance("MATIC");
|
|
72
|
+
* let respAddLiquidity = await lqudtProviderTool.addLiquidity("MATIC", 0.1);
|
|
73
|
+
* console.log(respAddLiquidity);
|
|
74
|
+
* }
|
|
75
|
+
* main();
|
|
76
|
+
*
|
|
77
|
+
* @return Transaction object
|
|
78
|
+
*/
|
|
79
|
+
addLiquidity(poolSymbolName: string, amountCC: number): Promise<ethers.ContractTransaction>;
|
|
80
|
+
/**
|
|
81
|
+
* Initiates a liquidity withdrawal from the pool
|
|
82
|
+
* It triggers a time-delayed unlocking of the given number of pool shares.
|
|
83
|
+
* The amount of pool shares to be unlocked is fixed by this call, but not their value in pool currency.
|
|
84
|
+
* @param {string} poolSymbolName Name of pool symbol (e.g. MATIC).
|
|
85
|
+
* @param {string} amountPoolShares Amount in pool-shares, removes everything if > available amount.
|
|
86
|
+
* @example
|
|
87
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
88
|
+
* async function main() {
|
|
89
|
+
* console.log(LiquidityProviderTool);
|
|
90
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
91
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
92
|
+
* const pk: string = <string>process.env.PK;
|
|
93
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
94
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
95
|
+
* // initiate withdrawal
|
|
96
|
+
* let respRemoveLiquidity = await lqudtProviderTool.initiateLiquidityWithdrawal("MATIC", 0.1);
|
|
97
|
+
* console.log(respRemoveLiquidity);
|
|
98
|
+
* }
|
|
99
|
+
* main();
|
|
100
|
+
*
|
|
101
|
+
* @return Transaction object.
|
|
102
|
+
*/
|
|
103
|
+
initiateLiquidityWithdrawal(poolSymbolName: string, amountPoolShares: number): Promise<ethers.providers.TransactionResponse>;
|
|
104
|
+
/**
|
|
105
|
+
* Withdraws as much liquidity as there is available after a call to initiateLiquidityWithdrawal.
|
|
106
|
+
* The address loses pool shares in return.
|
|
107
|
+
* @param poolSymbolName
|
|
108
|
+
* @example
|
|
109
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
110
|
+
* async function main() {
|
|
111
|
+
* console.log(LiquidityProviderTool);
|
|
112
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
113
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
114
|
+
* const pk: string = <string>process.env.PK;
|
|
115
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
116
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
117
|
+
* // remove liquidity
|
|
118
|
+
* let respRemoveLiquidity = await lqudtProviderTool.executeLiquidityWithdrawal("MATIC", 0.1);
|
|
119
|
+
* console.log(respRemoveLiquidity);
|
|
120
|
+
* }
|
|
121
|
+
* main();
|
|
122
|
+
*
|
|
123
|
+
* @returns Transaction object.
|
|
124
|
+
*/
|
|
125
|
+
executeLiquidityWithdrawal(poolSymbolName: string): Promise<ethers.providers.TransactionResponse>;
|
|
126
|
+
}
|
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
import { __awaiter, __extends, __generator } from "tslib";
|
|
2
|
+
import { ethers } from "ethers";
|
|
3
|
+
import { dec18ToFloat, floatToDec18 } from "./d8XMath";
|
|
4
|
+
import { ERC20_ABI } from "./nodeSDKTypes";
|
|
5
|
+
import PerpetualDataHandler from "./perpetualDataHandler";
|
|
6
|
+
import WriteAccessHandler from "./writeAccessHandler";
|
|
7
|
+
/**
|
|
8
|
+
* Functions to provide liquidity. This class requires a private key and executes
|
|
9
|
+
* smart-contract interactions that require gas-payments.
|
|
10
|
+
* @extends WriteAccessHandler
|
|
11
|
+
*/
|
|
12
|
+
var LiquidityProviderTool = /** @class */ (function (_super) {
|
|
13
|
+
__extends(LiquidityProviderTool, _super);
|
|
14
|
+
/**
|
|
15
|
+
* Constructor
|
|
16
|
+
* @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.
|
|
17
|
+
* readSDKConfig.
|
|
18
|
+
* @example
|
|
19
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
20
|
+
* async function main() {
|
|
21
|
+
* console.log(LiquidityProviderTool);
|
|
22
|
+
* // load configuration for testnet
|
|
23
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
24
|
+
* // LiquidityProviderTool (authentication required, PK is an environment variable with a private key)
|
|
25
|
+
* const pk: string = <string>process.env.PK;
|
|
26
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
27
|
+
* // Create a proxy instance to access the blockchain
|
|
28
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
29
|
+
* }
|
|
30
|
+
* main();
|
|
31
|
+
*
|
|
32
|
+
* @param privateKey private key of account that trades
|
|
33
|
+
*/
|
|
34
|
+
function LiquidityProviderTool(config, privateKey) {
|
|
35
|
+
return _super.call(this, config, privateKey) || this;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Value of the pool share tokens for this liquidity provider
|
|
39
|
+
* in poolSymbol-currency (e.g. MATIC, USDC).
|
|
40
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC).
|
|
41
|
+
* @example
|
|
42
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
43
|
+
* async function main() {
|
|
44
|
+
* console.log(LiquidityProviderTool);
|
|
45
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
46
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
47
|
+
* const pk: string = <string>process.env.PK;
|
|
48
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
49
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
50
|
+
* // get value of pool share token
|
|
51
|
+
* let shareToken = await lqudtProviderTool.getParticipationValue("MATIC");
|
|
52
|
+
* console.log(shareToken);
|
|
53
|
+
* }
|
|
54
|
+
* main();
|
|
55
|
+
*
|
|
56
|
+
* @return Value in poolSymbol-currency (e.g. MATIC, USDC), balance of pool share tokens, and share token symbol.
|
|
57
|
+
*/
|
|
58
|
+
LiquidityProviderTool.prototype.getParticipationValue = function (poolSymbolName) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
60
|
+
var poolId, shareTokenAddr, shareToken, dShareTokenBalanceOfAddr, valueCCDec18, shareTokenBalanceOfAddr;
|
|
61
|
+
return __generator(this, function (_a) {
|
|
62
|
+
switch (_a.label) {
|
|
63
|
+
case 0:
|
|
64
|
+
if (this.proxyContract == null ||
|
|
65
|
+
this.signer == null ||
|
|
66
|
+
this.poolStaticInfos.length == 0 ||
|
|
67
|
+
this.provider == null) {
|
|
68
|
+
throw Error("no proxy contract or wallet or data initialized. Use createProxyInstance().");
|
|
69
|
+
}
|
|
70
|
+
poolId = PerpetualDataHandler._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
71
|
+
shareTokenAddr = this.poolStaticInfos[poolId - 1].shareTokenAddr;
|
|
72
|
+
shareToken = new ethers.Contract(shareTokenAddr, ERC20_ABI, this.signer);
|
|
73
|
+
return [4 /*yield*/, shareToken.balanceOf(this.traderAddr)];
|
|
74
|
+
case 1:
|
|
75
|
+
dShareTokenBalanceOfAddr = _a.sent();
|
|
76
|
+
return [4 /*yield*/, this.proxyContract.getTokenAmountToReturn(poolId, dShareTokenBalanceOfAddr)];
|
|
77
|
+
case 2:
|
|
78
|
+
valueCCDec18 = _a.sent();
|
|
79
|
+
shareTokenBalanceOfAddr = dec18ToFloat(dShareTokenBalanceOfAddr);
|
|
80
|
+
return [2 /*return*/, {
|
|
81
|
+
value: dec18ToFloat(valueCCDec18),
|
|
82
|
+
shareTokenBalance: shareTokenBalanceOfAddr,
|
|
83
|
+
poolShareToken: shareTokenAddr,
|
|
84
|
+
}];
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
/**
|
|
90
|
+
* Add liquidity to the PnL participant fund. The address gets pool shares in return.
|
|
91
|
+
* @param {string} poolSymbolName Name of pool symbol (e.g. MATIC)
|
|
92
|
+
* @param {number} amountCC Amount in pool-collateral currency
|
|
93
|
+
* @example
|
|
94
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
95
|
+
* async function main() {
|
|
96
|
+
* console.log(LiquidityProviderTool);
|
|
97
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
98
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
99
|
+
* const pk: string = <string>process.env.PK;
|
|
100
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
101
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
102
|
+
* // add liquidity
|
|
103
|
+
* await lqudtProviderTool.setAllowance("MATIC");
|
|
104
|
+
* let respAddLiquidity = await lqudtProviderTool.addLiquidity("MATIC", 0.1);
|
|
105
|
+
* console.log(respAddLiquidity);
|
|
106
|
+
* }
|
|
107
|
+
* main();
|
|
108
|
+
*
|
|
109
|
+
* @return Transaction object
|
|
110
|
+
*/
|
|
111
|
+
LiquidityProviderTool.prototype.addLiquidity = function (poolSymbolName, amountCC) {
|
|
112
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
113
|
+
var poolId, tx;
|
|
114
|
+
return __generator(this, function (_a) {
|
|
115
|
+
switch (_a.label) {
|
|
116
|
+
case 0:
|
|
117
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
118
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
119
|
+
}
|
|
120
|
+
poolId = PerpetualDataHandler._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
121
|
+
return [4 /*yield*/, this.proxyContract.addLiquidity(poolId, floatToDec18(amountCC), {
|
|
122
|
+
gasLimit: this.gasLimit,
|
|
123
|
+
})];
|
|
124
|
+
case 1:
|
|
125
|
+
tx = _a.sent();
|
|
126
|
+
return [2 /*return*/, tx];
|
|
127
|
+
}
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
};
|
|
131
|
+
/**
|
|
132
|
+
* Initiates a liquidity withdrawal from the pool
|
|
133
|
+
* It triggers a time-delayed unlocking of the given number of pool shares.
|
|
134
|
+
* The amount of pool shares to be unlocked is fixed by this call, but not their value in pool currency.
|
|
135
|
+
* @param {string} poolSymbolName Name of pool symbol (e.g. MATIC).
|
|
136
|
+
* @param {string} amountPoolShares Amount in pool-shares, removes everything if > available amount.
|
|
137
|
+
* @example
|
|
138
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
139
|
+
* async function main() {
|
|
140
|
+
* console.log(LiquidityProviderTool);
|
|
141
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
142
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
143
|
+
* const pk: string = <string>process.env.PK;
|
|
144
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
145
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
146
|
+
* // initiate withdrawal
|
|
147
|
+
* let respRemoveLiquidity = await lqudtProviderTool.initiateLiquidityWithdrawal("MATIC", 0.1);
|
|
148
|
+
* console.log(respRemoveLiquidity);
|
|
149
|
+
* }
|
|
150
|
+
* main();
|
|
151
|
+
*
|
|
152
|
+
* @return Transaction object.
|
|
153
|
+
*/
|
|
154
|
+
LiquidityProviderTool.prototype.initiateLiquidityWithdrawal = function (poolSymbolName, amountPoolShares) {
|
|
155
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
156
|
+
var poolId, tx;
|
|
157
|
+
return __generator(this, function (_a) {
|
|
158
|
+
switch (_a.label) {
|
|
159
|
+
case 0:
|
|
160
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
161
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
162
|
+
}
|
|
163
|
+
poolId = PerpetualDataHandler._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
164
|
+
return [4 /*yield*/, this.proxyContract.withdrawLiquidity(poolId, floatToDec18(amountPoolShares), {
|
|
165
|
+
gasLimit: this.gasLimit,
|
|
166
|
+
})];
|
|
167
|
+
case 1:
|
|
168
|
+
tx = _a.sent();
|
|
169
|
+
return [2 /*return*/, tx];
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
});
|
|
173
|
+
};
|
|
174
|
+
/**
|
|
175
|
+
* Withdraws as much liquidity as there is available after a call to initiateLiquidityWithdrawal.
|
|
176
|
+
* The address loses pool shares in return.
|
|
177
|
+
* @param poolSymbolName
|
|
178
|
+
* @example
|
|
179
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
180
|
+
* async function main() {
|
|
181
|
+
* console.log(LiquidityProviderTool);
|
|
182
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
183
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
184
|
+
* const pk: string = <string>process.env.PK;
|
|
185
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
186
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
187
|
+
* // remove liquidity
|
|
188
|
+
* let respRemoveLiquidity = await lqudtProviderTool.executeLiquidityWithdrawal("MATIC", 0.1);
|
|
189
|
+
* console.log(respRemoveLiquidity);
|
|
190
|
+
* }
|
|
191
|
+
* main();
|
|
192
|
+
*
|
|
193
|
+
* @returns Transaction object.
|
|
194
|
+
*/
|
|
195
|
+
LiquidityProviderTool.prototype.executeLiquidityWithdrawal = function (poolSymbolName) {
|
|
196
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
197
|
+
var poolId, tx;
|
|
198
|
+
return __generator(this, function (_a) {
|
|
199
|
+
switch (_a.label) {
|
|
200
|
+
case 0:
|
|
201
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
202
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
203
|
+
}
|
|
204
|
+
poolId = PerpetualDataHandler._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
205
|
+
return [4 /*yield*/, this.proxyContract.executeLiquidityWithdrawal(poolId, {
|
|
206
|
+
gasLimit: this.gasLimit,
|
|
207
|
+
})];
|
|
208
|
+
case 1:
|
|
209
|
+
tx = _a.sent();
|
|
210
|
+
return [2 /*return*/, tx];
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
};
|
|
215
|
+
return LiquidityProviderTool;
|
|
216
|
+
}(WriteAccessHandler));
|
|
217
|
+
export default LiquidityProviderTool;
|
|
218
|
+
//# sourceMappingURL=liquidityProviderTool.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"liquidityProviderTool.js","sourceRoot":"","sources":["../../src/liquidityProviderTool.ts"],"names":[],"mappings":";AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAChC,OAAO,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACvD,OAAO,EAAE,SAAS,EAAiB,MAAM,gBAAgB,CAAC;AAC1D,OAAO,oBAAoB,MAAM,wBAAwB,CAAC;AAC1D,OAAO,kBAAkB,MAAM,sBAAsB,CAAC;AACtD;;;;GAIG;AACH;IAAmD,yCAAkB;IACnE;;;;;;;;;;;;;;;;;;;OAmBG;IACH,+BAAmB,MAAqB,EAAE,UAAkB;eAC1D,kBAAM,MAAM,EAAE,UAAU,CAAC;IAC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