@d8x/perpetuals-sdk 0.0.1
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/LICENSE +21 -0
- package/README.md +17 -0
- package/abi/ERC20.json +288 -0
- package/abi/IPerpetualManager.json +4674 -0
- package/abi/LimitOrderBook.json +865 -0
- package/abi/LimitOrderBookFactory.json +166 -0
- package/config/defaultConfig.json +9 -0
- package/config/oldConfig.json +9 -0
- package/dist/accountTrade.d.ts +54 -0
- package/dist/accountTrade.js +164 -0
- package/dist/brokerTool.d.ts +41 -0
- package/dist/brokerTool.js +129 -0
- package/dist/d8XMath.d.ts +71 -0
- package/dist/d8XMath.js +162 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.js +49 -0
- package/dist/liquiditatorTool.d.ts +14 -0
- package/dist/liquiditatorTool.js +21 -0
- package/dist/liquidityProviderTool.d.ts +39 -0
- package/dist/liquidityProviderTool.js +100 -0
- package/dist/marketData.d.ts +39 -0
- package/dist/marketData.js +160 -0
- package/dist/nodeSDKTypes.d.ts +130 -0
- package/dist/nodeSDKTypes.js +52 -0
- package/dist/orderReferrerTool.d.ts +14 -0
- package/dist/orderReferrerTool.js +21 -0
- package/dist/perpetualDataHandler.d.ts +85 -0
- package/dist/perpetualDataHandler.js +474 -0
- package/dist/utils.d.ts +37 -0
- package/dist/utils.js +84 -0
- package/dist/writeAccessHandler.d.ts +36 -0
- package/dist/writeAccessHandler.js +95 -0
- package/module.d.ts +1 -0
- package/package.json +63 -0
- package/src/accountTrade.ts +217 -0
- package/src/brokerTool.ts +155 -0
- package/src/d8XMath.ts +176 -0
- package/src/index.ts +32 -0
- package/src/liquiditatorTool.ts +21 -0
- package/src/liquidityProviderTool.ts +100 -0
- package/src/marketData.ts +149 -0
- package/src/nodeSDKTypes.ts +158 -0
- package/src/orderReferrerTool.ts +17 -0
- package/src/perpetualDataHandler.ts +549 -0
- package/src/utils.ts +83 -0
- package/src/writeAccessHandler.ts +83 -0
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { BigNumber, BigNumberish, ethers, Wallet } from "ethers";
|
|
2
|
+
import PerpetualDataHandler from "./perpetualDataHandler";
|
|
3
|
+
import { NodeSDKConfig, MAX_UINT_256, ERC20_ABI } from "./nodeSDKTypes";
|
|
4
|
+
import { to4Chars } from "./utils";
|
|
5
|
+
import { floatToDec18 } from "./d8XMath";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* This is a parent class for the classes that require
|
|
9
|
+
* write access to the contracts.
|
|
10
|
+
* This class requires a private key and executes smart-contract interaction that
|
|
11
|
+
* require gas-payments.
|
|
12
|
+
*/
|
|
13
|
+
export default class WriteAccessHandler extends PerpetualDataHandler {
|
|
14
|
+
protected privateKey: string;
|
|
15
|
+
protected traderAddr: string = "";
|
|
16
|
+
protected signer: ethers.Wallet | null = null;
|
|
17
|
+
protected gasLimit: number = 15_000_000;
|
|
18
|
+
protected chainId: number = 0;
|
|
19
|
+
/**
|
|
20
|
+
* Constructor
|
|
21
|
+
* @param config configuration
|
|
22
|
+
* @param privateKey private key of account that trades
|
|
23
|
+
*/
|
|
24
|
+
public constructor(config: NodeSDKConfig, privateKey: string) {
|
|
25
|
+
super(config);
|
|
26
|
+
this.privateKey = privateKey;
|
|
27
|
+
if (config.gasLimit != undefined) {
|
|
28
|
+
this.gasLimit = config.gasLimit;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Initialize the AccountTrade-Class with this function
|
|
34
|
+
* to create instance of D8X perpetual contract and gather information
|
|
35
|
+
* about perpetual currencies
|
|
36
|
+
*/
|
|
37
|
+
public async createProxyInstance() {
|
|
38
|
+
this.provider = new ethers.providers.JsonRpcProvider(this.nodeURL);
|
|
39
|
+
const wallet = new ethers.Wallet(this.privateKey);
|
|
40
|
+
this.signer = wallet.connect(this.provider);
|
|
41
|
+
await this.initContractsAndData(this.signer);
|
|
42
|
+
this.traderAddr = wallet.address;
|
|
43
|
+
this.chainId = (await this.provider.getNetwork()).chainId;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Set allowance for ar margin token (e.g., MATIC, ETH, USDC)
|
|
48
|
+
* @param symbol token in 'long-form' such as MATIC, symbol also fine (ETH-USD-MATIC)
|
|
49
|
+
* @param amount optional, amount to approve if not 'infinity'
|
|
50
|
+
* @returns transaction hash
|
|
51
|
+
*/
|
|
52
|
+
public async setAllowance(symbol: string, amount: number | undefined = undefined): Promise<string> {
|
|
53
|
+
//extract margin-currency name
|
|
54
|
+
let symbolarr = symbol.split("-");
|
|
55
|
+
symbol = symbol.length == 3 ? symbolarr[2] : symbolarr[0];
|
|
56
|
+
//transform into bytes4 currencies (without the space): "BTC", "USD", "MATC"
|
|
57
|
+
symbol = to4Chars(symbol);
|
|
58
|
+
symbol = symbol.replace(/\0/g, "");
|
|
59
|
+
let marginTokenAddr = this.symbolToTokenAddrMap.get(symbol);
|
|
60
|
+
if (marginTokenAddr == undefined || this.signer == null) {
|
|
61
|
+
throw Error("No margin token or signer defined, call createProxyInstance");
|
|
62
|
+
}
|
|
63
|
+
let amountDec18;
|
|
64
|
+
if (amount == undefined) {
|
|
65
|
+
amountDec18 = MAX_UINT_256;
|
|
66
|
+
} else {
|
|
67
|
+
amountDec18 = floatToDec18(amount);
|
|
68
|
+
}
|
|
69
|
+
return WriteAccessHandler._setAllowance(marginTokenAddr, this.proxyAddr, this.signer, amountDec18);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
protected static async _setAllowance(
|
|
73
|
+
tokenAddr: string,
|
|
74
|
+
proxyAddr: string,
|
|
75
|
+
signer: ethers.Wallet,
|
|
76
|
+
amount: BigNumber
|
|
77
|
+
): Promise<string> {
|
|
78
|
+
const marginToken: ethers.Contract = new ethers.Contract(tokenAddr, ERC20_ABI, signer);
|
|
79
|
+
let tx = await marginToken.approve(proxyAddr, amount);
|
|
80
|
+
await tx.wait();
|
|
81
|
+
return tx.hash;
|
|
82
|
+
}
|
|
83
|
+
}
|