@d8x/perpetuals-sdk 0.6.4 → 0.6.5
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/version.d.ts +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +2 -1
- package/src/abi/ERC20.json +288 -0
- package/src/abi/IPerpetualManager.json +5888 -0
- package/src/abi/LimitOrderBook.json +1062 -0
- package/src/abi/LimitOrderBookFactory.json +161 -0
- package/src/abi/MockTokenSwap.json +186 -0
- package/src/abi/ShareToken.json +428 -0
- package/src/accountTrade.ts +428 -0
- package/src/brokerTool.ts +555 -0
- package/src/config/defaultConfig.json +62 -0
- package/src/config/mockSwap.json +6 -0
- package/src/config/priceFeedConfig.json +104 -0
- package/src/config/symbolList.json +13 -0
- package/src/contracts/ERC20.ts +444 -0
- package/src/contracts/IPerpetualManager.ts +7227 -0
- package/src/contracts/LimitOrderBook.ts +1251 -0
- package/src/contracts/LimitOrderBookFactory.ts +348 -0
- package/src/contracts/MockTokenSwap.ts +373 -0
- package/src/contracts/ShareToken.ts +695 -0
- package/src/contracts/common.ts +44 -0
- package/src/contracts/factories/ERC20__factory.ts +306 -0
- package/src/contracts/factories/IPerpetualManager__factory.ts +5912 -0
- package/src/contracts/factories/LimitOrderBookFactory__factory.ts +189 -0
- package/src/contracts/factories/LimitOrderBook__factory.ts +1086 -0
- package/src/contracts/factories/MockTokenSwap__factory.ts +207 -0
- package/src/contracts/factories/ShareToken__factory.ts +449 -0
- package/src/contracts/factories/index.ts +9 -0
- package/src/contracts/index.ts +16 -0
- package/src/d8XMath.ts +376 -0
- package/src/index.ts +29 -0
- package/src/liquidatorTool.ts +270 -0
- package/src/liquidityProviderTool.ts +148 -0
- package/src/marketData.ts +1310 -0
- package/src/nodeSDKTypes.ts +332 -0
- package/src/orderReferrerTool.ts +516 -0
- package/src/perpetualDataHandler.ts +1161 -0
- package/src/perpetualEventHandler.ts +455 -0
- package/src/priceFeeds.ts +382 -0
- package/src/traderDigests.ts +86 -0
- package/src/traderInterface.ts +172 -0
- package/src/triangulator.ts +105 -0
- package/src/utils.ts +134 -0
- package/src/version.ts +1 -0
- package/src/writeAccessHandler.ts +139 -0
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
import { Contract, ContractTransaction, Overrides } from "@ethersproject/contracts";
|
|
2
|
+
import { dec18ToFloat, floatToDec18 } from "./d8XMath";
|
|
3
|
+
import { ERC20_ABI, NodeSDKConfig } from "./nodeSDKTypes";
|
|
4
|
+
import PerpetualDataHandler from "./perpetualDataHandler";
|
|
5
|
+
import WriteAccessHandler from "./writeAccessHandler";
|
|
6
|
+
/**
|
|
7
|
+
* Functions to provide liquidity. This class requires a private key and executes
|
|
8
|
+
* smart-contract interactions that require gas-payments.
|
|
9
|
+
* @extends WriteAccessHandler
|
|
10
|
+
*/
|
|
11
|
+
export default class LiquidityProviderTool extends WriteAccessHandler {
|
|
12
|
+
/**
|
|
13
|
+
* Constructor
|
|
14
|
+
* @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.
|
|
15
|
+
* readSDKConfig.
|
|
16
|
+
* @example
|
|
17
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
18
|
+
* async function main() {
|
|
19
|
+
* console.log(LiquidityProviderTool);
|
|
20
|
+
* // load configuration for testnet
|
|
21
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
22
|
+
* // LiquidityProviderTool (authentication required, PK is an environment variable with a private key)
|
|
23
|
+
* const pk: string = <string>process.env.PK;
|
|
24
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
25
|
+
* // Create a proxy instance to access the blockchain
|
|
26
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
27
|
+
* }
|
|
28
|
+
* main();
|
|
29
|
+
*
|
|
30
|
+
* @param privateKey private key of account that trades
|
|
31
|
+
*/
|
|
32
|
+
public constructor(config: NodeSDKConfig, privateKey: string) {
|
|
33
|
+
super(config, privateKey);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Add liquidity to the PnL participant fund. The address gets pool shares in return.
|
|
38
|
+
* @param {string} poolSymbolName Name of pool symbol (e.g. MATIC)
|
|
39
|
+
* @param {number} amountCC Amount in pool-collateral currency
|
|
40
|
+
* @example
|
|
41
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
42
|
+
* async function main() {
|
|
43
|
+
* console.log(LiquidityProviderTool);
|
|
44
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
45
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
46
|
+
* const pk: string = <string>process.env.PK;
|
|
47
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
48
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
49
|
+
* // add liquidity
|
|
50
|
+
* await lqudtProviderTool.setAllowance("MATIC");
|
|
51
|
+
* let respAddLiquidity = await lqudtProviderTool.addLiquidity("MATIC", 0.1);
|
|
52
|
+
* console.log(respAddLiquidity);
|
|
53
|
+
* }
|
|
54
|
+
* main();
|
|
55
|
+
*
|
|
56
|
+
* @return Transaction object
|
|
57
|
+
*/
|
|
58
|
+
public async addLiquidity(
|
|
59
|
+
poolSymbolName: string,
|
|
60
|
+
amountCC: number,
|
|
61
|
+
overrides?: Overrides
|
|
62
|
+
): Promise<ContractTransaction> {
|
|
63
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
64
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
65
|
+
}
|
|
66
|
+
let poolId = PerpetualDataHandler._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
67
|
+
let tx = await this.proxyContract.addLiquidity(
|
|
68
|
+
poolId,
|
|
69
|
+
floatToDec18(amountCC),
|
|
70
|
+
overrides || { gasLimit: this.gasLimit }
|
|
71
|
+
);
|
|
72
|
+
return tx;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Initiates a liquidity withdrawal from the pool
|
|
77
|
+
* It triggers a time-delayed unlocking of the given number of pool shares.
|
|
78
|
+
* The amount of pool shares to be unlocked is fixed by this call, but not their value in pool currency.
|
|
79
|
+
* @param {string} poolSymbolName Name of pool symbol (e.g. MATIC).
|
|
80
|
+
* @param {string} amountPoolShares Amount in pool-shares, removes everything if > available amount.
|
|
81
|
+
* @example
|
|
82
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
83
|
+
* async function main() {
|
|
84
|
+
* console.log(LiquidityProviderTool);
|
|
85
|
+
* // setup (authentication required, PK is an environment variable with a private key)
|
|
86
|
+
* const config = PerpetualDataHandler.readSDKConfig("testnet");
|
|
87
|
+
* const pk: string = <string>process.env.PK;
|
|
88
|
+
* let lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
89
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
90
|
+
* // initiate withdrawal
|
|
91
|
+
* let respRemoveLiquidity = await lqudtProviderTool.initiateLiquidityWithdrawal("MATIC", 0.1);
|
|
92
|
+
* console.log(respRemoveLiquidity);
|
|
93
|
+
* }
|
|
94
|
+
* main();
|
|
95
|
+
*
|
|
96
|
+
* @return Transaction object.
|
|
97
|
+
*/
|
|
98
|
+
public async initiateLiquidityWithdrawal(
|
|
99
|
+
poolSymbolName: string,
|
|
100
|
+
amountPoolShares: number,
|
|
101
|
+
overrides?: Overrides
|
|
102
|
+
): Promise<ContractTransaction> {
|
|
103
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
104
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
105
|
+
}
|
|
106
|
+
let poolId = PerpetualDataHandler._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
107
|
+
let tx = await this.proxyContract.withdrawLiquidity(
|
|
108
|
+
poolId,
|
|
109
|
+
floatToDec18(amountPoolShares),
|
|
110
|
+
overrides || { gasLimit: this.gasLimit }
|
|
111
|
+
);
|
|
112
|
+
return tx;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Withdraws as much liquidity as there is available after a call to initiateLiquidityWithdrawal.
|
|
117
|
+
* The address loses pool shares in return.
|
|
118
|
+
* @param poolSymbolName
|
|
119
|
+
* @example
|
|
120
|
+
* import { LiquidityProviderTool, PerpetualDataHandler } from '@d8x/perpetuals-sdk';
|
|
121
|
+
* async function main() {
|
|
122
|
+
* console.log(LiquidityProviderTool);
|
|
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 lqudtProviderTool = new LiquidityProviderTool(config, pk);
|
|
127
|
+
* await lqudtProviderTool.createProxyInstance();
|
|
128
|
+
* // remove liquidity
|
|
129
|
+
* let respRemoveLiquidity = await lqudtProviderTool.executeLiquidityWithdrawal("MATIC", 0.1);
|
|
130
|
+
* console.log(respRemoveLiquidity);
|
|
131
|
+
* }
|
|
132
|
+
* main();
|
|
133
|
+
*
|
|
134
|
+
* @returns Transaction object.
|
|
135
|
+
*/
|
|
136
|
+
public async executeLiquidityWithdrawal(poolSymbolName: string, overrides?: Overrides): Promise<ContractTransaction> {
|
|
137
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
138
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
139
|
+
}
|
|
140
|
+
let poolId = PerpetualDataHandler._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
141
|
+
let tx = await this.proxyContract.executeLiquidityWithdrawal(
|
|
142
|
+
poolId,
|
|
143
|
+
this.traderAddr,
|
|
144
|
+
overrides || { gasLimit: this.gasLimit }
|
|
145
|
+
);
|
|
146
|
+
return tx;
|
|
147
|
+
}
|
|
148
|
+
}
|