@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
package/src/triangulator.ts
DELETED
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
export default class Triangulator {
|
|
2
|
-
/**
|
|
3
|
-
* Find all possible triangulation paths
|
|
4
|
-
* @param ccyBase array of base currencies ['BTC', 'ETH', 'MATIC', ...]
|
|
5
|
-
* @param ccyQuote array of quote currencies corresponding to base ['USD', 'USD', ...]
|
|
6
|
-
* @param pair pair we want to calculate, e.g. BTC-USDC
|
|
7
|
-
* @returns array of different paths for which multiplication leads to
|
|
8
|
-
* desired pair, e.g. [['MATIC-USD'], ['MATIC-ETH', 'ETH-USD']]
|
|
9
|
-
*/
|
|
10
|
-
public static findPath(ccyBase: string[], ccyQuote: string[], pair: string): Array<Array<string>> {
|
|
11
|
-
let [base, quote] = pair.split("-");
|
|
12
|
-
let paths = new Array<Array<string>>();
|
|
13
|
-
for (let k = 0; k < ccyBase.length; k++) {
|
|
14
|
-
let currentPath: string[] = [];
|
|
15
|
-
if (ccyBase[k] == base) {
|
|
16
|
-
currentPath.push(ccyBase[k] + "-" + ccyQuote[k]);
|
|
17
|
-
if (ccyQuote[k] == quote) {
|
|
18
|
-
// we are done
|
|
19
|
-
paths.push(currentPath);
|
|
20
|
-
} else {
|
|
21
|
-
// find path for ccyQuote[k]-quote without the currency
|
|
22
|
-
let newBase = new Array<string>();
|
|
23
|
-
let newQuote = new Array<string>();
|
|
24
|
-
for (let j = 0; j < ccyBase.length; j++) {
|
|
25
|
-
if (j != k && ccyQuote[j] + "-" + ccyBase[j] != ccyBase[k] + "-" + ccyQuote[k]) {
|
|
26
|
-
newBase.push(ccyBase[j]);
|
|
27
|
-
newQuote.push(ccyQuote[j]);
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
let recPaths = Triangulator.findPath(newBase, newQuote, ccyQuote[k] + "-" + quote);
|
|
31
|
-
for (let j = 0; j < recPaths.length; j++) {
|
|
32
|
-
paths.push(currentPath.concat(recPaths[j]));
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
}
|
|
37
|
-
return paths;
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
/**
|
|
41
|
-
* Calculate the triangulated price from underlying prices
|
|
42
|
-
* @param triangulation triangulation of with symbol and is-inverted flag
|
|
43
|
-
* @param feedIdxPrices map of symbol to price
|
|
44
|
-
* @returns triangulated price (scalar), true if market closed or price unavailable
|
|
45
|
-
*/
|
|
46
|
-
public static calculateTriangulatedPrice(triangulation: [string[], boolean[]], feedIdxPrices: Map<string, [number, boolean]>) : [number,boolean] {
|
|
47
|
-
let px = 1;
|
|
48
|
-
let isOpen = true;
|
|
49
|
-
for (let j = 0; j < triangulation[0].length; j++) {
|
|
50
|
-
let pxFeed : [number, boolean]|undefined = feedIdxPrices.get(triangulation[0][j]);
|
|
51
|
-
if (pxFeed == undefined) {
|
|
52
|
-
// no price available for given index
|
|
53
|
-
return [-1, true];
|
|
54
|
-
}
|
|
55
|
-
px = triangulation[1][j] ? px / pxFeed[0] : px * pxFeed[0];
|
|
56
|
-
let isClosed = pxFeed[1];
|
|
57
|
-
isOpen = isOpen && !isClosed;
|
|
58
|
-
}
|
|
59
|
-
return [px, !isOpen];
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
/**
|
|
63
|
-
* Finds shortest path and returns indices required and whether to divide or not
|
|
64
|
-
* @example triangulate BTC-USDC: [ [ 'BTC-USD', 'USDC-USD' ], [ false, true ] ]
|
|
65
|
-
* @param ccyArr array of available indices (e.g. from websocket)
|
|
66
|
-
* @param symbol symbol we are looking for (e.g. MATIC-USDC)
|
|
67
|
-
* @returns shortest path with given indices, array whether we have to divide (true) or multiply
|
|
68
|
-
* to arrive at the desired price
|
|
69
|
-
*/
|
|
70
|
-
public static triangulate(ccyArr: string[], symbol: string): [string[], boolean[]] {
|
|
71
|
-
let ccyBase = ccyArr.map((x) => x.split("-")[0]);
|
|
72
|
-
let ccyQuote = ccyArr.map((x) => x.split("-")[1]);
|
|
73
|
-
let p = Triangulator.findPath(ccyBase.concat(ccyQuote), ccyQuote.concat(ccyBase), symbol);
|
|
74
|
-
if (p.length == 0) {
|
|
75
|
-
return [[], []];
|
|
76
|
-
}
|
|
77
|
-
let len = p.map((x) => x.length);
|
|
78
|
-
let minLen = 100;
|
|
79
|
-
let minIdx = -1;
|
|
80
|
-
for (let j = 0; j < p.length; j++) {
|
|
81
|
-
if (len[j] < minLen) {
|
|
82
|
-
minLen = len[j];
|
|
83
|
-
minIdx = j;
|
|
84
|
-
if (minLen == 1) {
|
|
85
|
-
break;
|
|
86
|
-
}
|
|
87
|
-
}
|
|
88
|
-
}
|
|
89
|
-
let path = p[minIdx];
|
|
90
|
-
let invert = new Array<boolean>();
|
|
91
|
-
let indexPath = new Array<string>();
|
|
92
|
-
for (let k = 0; k < path.length; k++) {
|
|
93
|
-
if (ccyArr.includes(path[k], 0)) {
|
|
94
|
-
indexPath.push(path[k]);
|
|
95
|
-
invert.push(false);
|
|
96
|
-
} else {
|
|
97
|
-
let inv = path[k].split("-");
|
|
98
|
-
indexPath.push(inv[1] + "-" + inv[0]);
|
|
99
|
-
invert.push(true);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return [indexPath, invert];
|
|
104
|
-
}
|
|
105
|
-
}
|
package/src/utils.ts
DELETED
|
@@ -1,134 +0,0 @@
|
|
|
1
|
-
import { BigNumber } from "ethers";
|
|
2
|
-
import { Buffer } from "buffer";
|
|
3
|
-
|
|
4
|
-
/**
|
|
5
|
-
* @module utils
|
|
6
|
-
*/
|
|
7
|
-
|
|
8
|
-
function _isVocal(char: string) {
|
|
9
|
-
char = char.toLowerCase();
|
|
10
|
-
return char == "a" || char == "e" || char == "i" || char == "o" || char == "u";
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
*
|
|
15
|
-
* @param {string} s String to shorten/extend to 4 characters
|
|
16
|
-
* @returns {string} String with 4 characters (or characters + null chars)
|
|
17
|
-
*/
|
|
18
|
-
export function to4Chars(s: string) {
|
|
19
|
-
while (s.length < 4) {
|
|
20
|
-
s = s + "\0";
|
|
21
|
-
}
|
|
22
|
-
let k = s.length - 1;
|
|
23
|
-
while (s.length > 4 && k >= 0) {
|
|
24
|
-
// chop off vocals from the end of string
|
|
25
|
-
// e.g. MATIC -> MATC
|
|
26
|
-
if (_isVocal(s.charAt(k))) {
|
|
27
|
-
s = s.substring(0, k) + s.substring(k + 1, s.length);
|
|
28
|
-
}
|
|
29
|
-
k--;
|
|
30
|
-
}
|
|
31
|
-
s = s.substring(0, 4);
|
|
32
|
-
return s;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
/**
|
|
36
|
-
* Converts string into 4-character bytes4
|
|
37
|
-
* uses to4Chars to first convert the string into
|
|
38
|
-
* 4 characters.
|
|
39
|
-
* Resulting buffer can be used with smart contract to
|
|
40
|
-
* identify tokens (BTC, USDC, MATIC etc.)
|
|
41
|
-
* @param {string} s String to encode into bytes4
|
|
42
|
-
* @returns {Buffer} 4-character bytes4.
|
|
43
|
-
*/
|
|
44
|
-
export function toBytes4(s: string): Buffer {
|
|
45
|
-
s = to4Chars(s);
|
|
46
|
-
let valBuff: Buffer = Buffer.from(s, "ascii");
|
|
47
|
-
return valBuff;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Decodes a buffer encoded with toBytes4 into
|
|
52
|
-
* a string. The string is the result of to4Chars of the
|
|
53
|
-
* originally encoded string stripped from null-chars
|
|
54
|
-
* @param {Buffer} b Correctly encoded bytes4 buffer using toBytes4
|
|
55
|
-
* @returns {string} String decoded into to4Chars-type string without null characters
|
|
56
|
-
*/
|
|
57
|
-
export function fromBytes4(b: Buffer): string {
|
|
58
|
-
let val: string = b.toString("ascii");
|
|
59
|
-
val = val.replace(/\0/g, "");
|
|
60
|
-
return val;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
/**
|
|
64
|
-
* Decodes the bytes4 encoded string received from the
|
|
65
|
-
* smart contract as a hex-number in string-format
|
|
66
|
-
* @param {string} s string representing a hex-number ("0x...")
|
|
67
|
-
* @returns {string} x of to4Chars(x) stripped from null-chars,
|
|
68
|
-
* where x was originally encoded and
|
|
69
|
-
* returned by the smart contract as bytes4
|
|
70
|
-
*/
|
|
71
|
-
export function fromBytes4HexString(s: string): string {
|
|
72
|
-
let res = "";
|
|
73
|
-
for (let k = 2; k < s.length; k = k + 2) {
|
|
74
|
-
res = res + String.fromCharCode(parseInt(s.substring(k, k + 2), 16));
|
|
75
|
-
}
|
|
76
|
-
res = res.replace(/\0/g, "");
|
|
77
|
-
return res;
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/**
|
|
81
|
-
*
|
|
82
|
-
* @param {string} s String representing a hex-number ("0x...")
|
|
83
|
-
* @param {Object} mapping List of symbol and clean symbol pairs, e.g. [{symbol: "MATIC", cleanSymbol: "MATC"}, ...]
|
|
84
|
-
* @returns {string} User friendly currency symbol, e.g. "MATIC"
|
|
85
|
-
*/
|
|
86
|
-
export function contractSymbolToSymbol(s: string, mapping: Map<string, string>): string {
|
|
87
|
-
let shortCCY = fromBytes4HexString(s);
|
|
88
|
-
// assume CCY is already short if not in the mapping file
|
|
89
|
-
let longCCY = mapping.get(shortCCY) ?? shortCCY;
|
|
90
|
-
return longCCY;
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
/**
|
|
94
|
-
*
|
|
95
|
-
* @param {string} s User friendly currency symbol, e.g. "MATIC"
|
|
96
|
-
* @param {Object} mapping List of symbol and clean symbol pairs, e.g. [{symbol: "MATIC", cleanSymbol: "MATC"}, ...]
|
|
97
|
-
* @returns {Buffer} Buffer that can be used with smart contract to identify tokens
|
|
98
|
-
*/
|
|
99
|
-
export function symbolToContractSymbol(s: string, mapping: Map<string, string>): Buffer {
|
|
100
|
-
let shortCCY: string | undefined = undefined;
|
|
101
|
-
for (let [k, v] of mapping) {
|
|
102
|
-
if (v == s) {
|
|
103
|
-
shortCCY = k;
|
|
104
|
-
}
|
|
105
|
-
}
|
|
106
|
-
// if not in the mapping file, assume ccy is already valid
|
|
107
|
-
shortCCY = shortCCY ?? s;
|
|
108
|
-
return toBytes4(shortCCY);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/**
|
|
112
|
-
* Converts symbol or symbol combination into long format
|
|
113
|
-
* @param {string} s symbol, e.g., USDC-MATC-USDC, MATC, USDC, ...
|
|
114
|
-
* @param {Object} mapping list of symbol and clean symbol pairs, e.g. [{symbol: "MATIC", cleanSymbol: "MATC"}, ...]
|
|
115
|
-
* @returns {string} long format e.g. MATIC. if not found the element is ""
|
|
116
|
-
*/
|
|
117
|
-
export function symbol4BToLongSymbol(s: string, mapping: Map<string, string>): string {
|
|
118
|
-
let ccy = s.split("-");
|
|
119
|
-
let longCCY = "";
|
|
120
|
-
for (let k = 0; k < ccy.length; k++) {
|
|
121
|
-
let sym = mapping.get(ccy[k]) ?? to4Chars(ccy[k]);
|
|
122
|
-
sym = sym.replace(/\0/g, "");
|
|
123
|
-
longCCY = longCCY + "-" + sym;
|
|
124
|
-
}
|
|
125
|
-
return longCCY.substring(1);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
export function combineFlags(f1: BigNumber, f2: BigNumber): BigNumber {
|
|
129
|
-
return BigNumber.from((parseInt(f1.toString()) | parseInt(f2.toString())) >>> 0);
|
|
130
|
-
}
|
|
131
|
-
|
|
132
|
-
export function containsFlag(f1: BigNumber, f2: BigNumber): boolean {
|
|
133
|
-
return (parseInt(f1.toString()) & parseInt(f2.toString())) > 0;
|
|
134
|
-
}
|
package/src/version.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export const D8X_SDK_VERSION = "0.1.12";
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { BigNumber, ethers } from "ethers";
|
|
2
|
-
import { floatToDec18 } from "./d8XMath";
|
|
3
|
-
import { ERC20_ABI, MAX_UINT_256, MOCK_TOKEN_SWAP_ABI, NodeSDKConfig } from "./nodeSDKTypes";
|
|
4
|
-
import PerpetualDataHandler from "./perpetualDataHandler";
|
|
5
|
-
|
|
6
|
-
/**
|
|
7
|
-
* This is a parent class for the classes that require
|
|
8
|
-
* write access to the contracts.
|
|
9
|
-
* This class requires a private key and executes smart-contract interaction that
|
|
10
|
-
* require gas-payments.
|
|
11
|
-
* @extends PerpetualDataHandler
|
|
12
|
-
*/
|
|
13
|
-
export default class WriteAccessHandler extends PerpetualDataHandler {
|
|
14
|
-
protected privateKey: string | undefined;
|
|
15
|
-
protected traderAddr: string = "";
|
|
16
|
-
protected signer: ethers.Signer | null = null;
|
|
17
|
-
protected gasLimit: number = 15_000_000;
|
|
18
|
-
/**
|
|
19
|
-
* Constructor
|
|
20
|
-
* @param {NodeSDKConfig} config configuration
|
|
21
|
-
* @param {string} privateKey private key of account that trades
|
|
22
|
-
*/
|
|
23
|
-
public constructor(config: NodeSDKConfig, privateKey?: string, signer?: ethers.Signer) {
|
|
24
|
-
super(config);
|
|
25
|
-
if (privateKey) {
|
|
26
|
-
this.privateKey = privateKey;
|
|
27
|
-
} else if (signer) {
|
|
28
|
-
this.signer = signer;
|
|
29
|
-
} else {
|
|
30
|
-
throw new Error("No private key nor signer provided.");
|
|
31
|
-
}
|
|
32
|
-
if (config.gasLimit != undefined) {
|
|
33
|
-
this.gasLimit = config.gasLimit;
|
|
34
|
-
}
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Initialize the writeAccessHandler-Class with this function
|
|
39
|
-
* to create instance of D8X perpetual contract and gather information
|
|
40
|
-
* about perpetual currencies
|
|
41
|
-
* @param provider optional provider
|
|
42
|
-
*/
|
|
43
|
-
public async createProxyInstance(provider?: ethers.providers.Provider) {
|
|
44
|
-
if (provider == undefined) {
|
|
45
|
-
this.provider = new ethers.providers.JsonRpcBatchProvider(this.nodeURL);
|
|
46
|
-
} else {
|
|
47
|
-
this.provider = provider;
|
|
48
|
-
}
|
|
49
|
-
if (!this.signer) {
|
|
50
|
-
const wallet = new ethers.Wallet(this.privateKey!);
|
|
51
|
-
this.signer = wallet.connect(this.provider);
|
|
52
|
-
}
|
|
53
|
-
await this.initContractsAndData(this.signer);
|
|
54
|
-
this.traderAddr = await this.signer.getAddress();
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
/**
|
|
58
|
-
* Set allowance for ar margin token (e.g., MATIC, ETH, USDC)
|
|
59
|
-
* @param symbol token in 'long-form' such as MATIC, symbol also fine (ETH-USD-MATIC)
|
|
60
|
-
* @param amount optional, amount to approve if not 'infinity'
|
|
61
|
-
* @returns ContractTransaction
|
|
62
|
-
*/
|
|
63
|
-
public async setAllowance(
|
|
64
|
-
symbol: string,
|
|
65
|
-
amount: number | undefined = undefined
|
|
66
|
-
): Promise<ethers.ContractTransaction> {
|
|
67
|
-
//extract margin-currency name
|
|
68
|
-
let symbolarr = symbol.split("-");
|
|
69
|
-
symbol = symbol.length == 3 ? symbolarr[2] : symbolarr[0];
|
|
70
|
-
//note: symbol is in long format
|
|
71
|
-
let marginTokenAddr = this.symbolToTokenAddrMap.get(symbol);
|
|
72
|
-
if (marginTokenAddr == undefined || this.signer == null) {
|
|
73
|
-
throw Error("No margin token or signer defined, call createProxyInstance");
|
|
74
|
-
}
|
|
75
|
-
let amountDec18: BigNumber;
|
|
76
|
-
if (amount == undefined) {
|
|
77
|
-
amountDec18 = MAX_UINT_256;
|
|
78
|
-
} else {
|
|
79
|
-
amountDec18 = floatToDec18(amount);
|
|
80
|
-
}
|
|
81
|
-
return WriteAccessHandler._setAllowance(marginTokenAddr, this.proxyAddr, this.signer, amountDec18);
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
protected static async _setAllowance(
|
|
85
|
-
tokenAddr: string,
|
|
86
|
-
proxyAddr: string,
|
|
87
|
-
signer: ethers.Signer,
|
|
88
|
-
amount: BigNumber
|
|
89
|
-
): Promise<ethers.ContractTransaction> {
|
|
90
|
-
const marginToken: ethers.Contract = new ethers.Contract(tokenAddr, ERC20_ABI, signer);
|
|
91
|
-
return await marginToken.approve(proxyAddr, amount);
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
/**
|
|
95
|
-
* Address corresponding to the private key used to instantiate this class.
|
|
96
|
-
* @returns {string} Address of this wallet.
|
|
97
|
-
*/
|
|
98
|
-
public getAddress(): string {
|
|
99
|
-
return this.traderAddr;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
/**
|
|
103
|
-
* Converts a given amount of chain native currency (test MATIC)
|
|
104
|
-
* into a mock token used for trading on testnet, with a rate of 1:100_000
|
|
105
|
-
* @param symbol Pool margin token e.g. MATIC
|
|
106
|
-
* @param amountToPay Amount in chain currency, e.g. "0.1" for 0.1 MATIC
|
|
107
|
-
* @returns Transaction object
|
|
108
|
-
*/
|
|
109
|
-
public async swapForMockToken(symbol: string, amountToPay: string): Promise<ethers.ContractTransaction> {
|
|
110
|
-
if (this.signer == null) {
|
|
111
|
-
throw Error("no wallet initialized. Use createProxyInstance().");
|
|
112
|
-
}
|
|
113
|
-
let tokenAddress = this.getMarginTokenFromSymbol(symbol);
|
|
114
|
-
if (tokenAddress == undefined) {
|
|
115
|
-
throw Error("symbols not found");
|
|
116
|
-
}
|
|
117
|
-
let tokenToSwap = new Map<string, string>(Object.entries(require("../config/mockSwap.json")));
|
|
118
|
-
let swapAddress = tokenToSwap.get(tokenAddress);
|
|
119
|
-
if (swapAddress == undefined) {
|
|
120
|
-
throw Error("No swap contract found for symbol.");
|
|
121
|
-
}
|
|
122
|
-
let contract = new ethers.Contract(swapAddress, MOCK_TOKEN_SWAP_ABI, this.signer);
|
|
123
|
-
return await contract.swapToMockToken({
|
|
124
|
-
value: ethers.utils.parseEther(amountToPay),
|
|
125
|
-
});
|
|
126
|
-
}
|
|
127
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|