@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,130 @@
|
|
|
1
|
+
import { BytesLike, BigNumber, BigNumberish } from "ethers";
|
|
2
|
+
export declare const DEFAULT_CONFIG_TESTNET = "../config/defaultConfig.json";
|
|
3
|
+
export declare const DEFAULT_CONFIG_MAINNET = "notthereyet";
|
|
4
|
+
export declare const DEFAULT_CONFIG_TESTNET_NAME = "testnet";
|
|
5
|
+
export declare const DEFAULT_CONFIG_MAINNET_NAME = "mainnet";
|
|
6
|
+
export declare const ERC20_ABI: any;
|
|
7
|
+
export declare const COLLATERAL_CURRENCY_QUOTE = 0;
|
|
8
|
+
export declare const COLLATERAL_CURRENCY_BASE = 1;
|
|
9
|
+
export declare const COLLATERAL_CURRENCY_QUANTO = 2;
|
|
10
|
+
export declare const PERP_STATE_STR: string[];
|
|
11
|
+
export declare const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
12
|
+
export declare const ONE_64x64: BigNumber;
|
|
13
|
+
export declare const MAX_64x64: BigNumber;
|
|
14
|
+
export declare const MAX_UINT_256: BigNumber;
|
|
15
|
+
export declare const DECIMALS: BigNumber;
|
|
16
|
+
export declare const ORDER_MAX_DURATION_SEC: number;
|
|
17
|
+
export declare const MASK_CLOSE_ONLY: BigNumber;
|
|
18
|
+
export declare const MASK_LIMIT_ORDER: BigNumber;
|
|
19
|
+
export declare const MASK_MARKET_ORDER: BigNumber;
|
|
20
|
+
export declare const MASK_STOP_ORDER: BigNumber;
|
|
21
|
+
export declare const MASK_KEEP_POS_LEVERAGE: BigNumber;
|
|
22
|
+
export declare const ORDER_TYPE_LIMIT = "LIMIT";
|
|
23
|
+
export declare const ORDER_TYPE_MARKET = "MARKET";
|
|
24
|
+
export declare const ORDER_TYPE_STOP_MARKET = "STOP_MARKET";
|
|
25
|
+
export declare const ORDER_TYPE_STOP_LIMIT = "STOP_LIMIT";
|
|
26
|
+
export declare const BUY_SIDE = "BUY";
|
|
27
|
+
export declare const SELL_SIDE = "SELL";
|
|
28
|
+
export declare const CLOSED_SIDE = "CLOSED";
|
|
29
|
+
export interface NodeSDKConfig {
|
|
30
|
+
nodeURL: string;
|
|
31
|
+
proxyAddr: string;
|
|
32
|
+
proxyABILocation: string;
|
|
33
|
+
limitOrderBookFactoryAddr: string;
|
|
34
|
+
limitOrderBookABILocation: string;
|
|
35
|
+
limitOrderBookFactoryABILocation: string;
|
|
36
|
+
gasLimit?: number | undefined;
|
|
37
|
+
}
|
|
38
|
+
export interface MarginAccount {
|
|
39
|
+
symbol: string;
|
|
40
|
+
positionNotionalBaseCCY: number;
|
|
41
|
+
side: string;
|
|
42
|
+
entryPrice: number;
|
|
43
|
+
leverage: number;
|
|
44
|
+
markPrice: number;
|
|
45
|
+
unrealizedPnlQuoteCCY: number;
|
|
46
|
+
unrealizedFundingCollateralCCY: number;
|
|
47
|
+
collateralCC: number;
|
|
48
|
+
liquidationPrice: [number, number | undefined];
|
|
49
|
+
liquidationLvg: number;
|
|
50
|
+
collToQuoteConversion: number;
|
|
51
|
+
}
|
|
52
|
+
export declare enum CollaterlCCY {
|
|
53
|
+
QUOTE = 0,
|
|
54
|
+
BASE = 1,
|
|
55
|
+
QUANTO = 2
|
|
56
|
+
}
|
|
57
|
+
export interface PoolStaticInfo {
|
|
58
|
+
poolId: number;
|
|
59
|
+
poolMarginSymbol: string;
|
|
60
|
+
poolMarginTokenAddr: string;
|
|
61
|
+
shareTokenAddr: string;
|
|
62
|
+
oracleFactoryAddr: string;
|
|
63
|
+
}
|
|
64
|
+
export interface PerpetualStaticInfo {
|
|
65
|
+
id: number;
|
|
66
|
+
limitOrderBookAddr: string;
|
|
67
|
+
maintenanceMarginRate: number;
|
|
68
|
+
collateralCurrencyType: CollaterlCCY;
|
|
69
|
+
S2Symbol: string;
|
|
70
|
+
S3Symbol: string;
|
|
71
|
+
}
|
|
72
|
+
export interface ExchangeInfo {
|
|
73
|
+
pools: PoolState[];
|
|
74
|
+
}
|
|
75
|
+
export interface PoolState {
|
|
76
|
+
isRunning: boolean;
|
|
77
|
+
marginTokenAddr: string;
|
|
78
|
+
poolShareTokenAddr: string;
|
|
79
|
+
defaultFundCashCC: number;
|
|
80
|
+
pnlParticipantCashCC: number;
|
|
81
|
+
totalAMMFundCashCC: number;
|
|
82
|
+
totalTargetAMMFundSizeCC: number;
|
|
83
|
+
brokerCollateralLotSize: number;
|
|
84
|
+
perpetuals: PerpetualState[];
|
|
85
|
+
}
|
|
86
|
+
export interface PerpetualState {
|
|
87
|
+
id: number;
|
|
88
|
+
state: string;
|
|
89
|
+
baseCurrency: string;
|
|
90
|
+
quoteCurrency: string;
|
|
91
|
+
indexPrice: number;
|
|
92
|
+
collToQuoteIndexPrice: number;
|
|
93
|
+
markPrice: number;
|
|
94
|
+
currentFundingRateBps: number;
|
|
95
|
+
initialMarginRate: number;
|
|
96
|
+
maintenanceMarginRate: number;
|
|
97
|
+
openInterestBC: number;
|
|
98
|
+
maxPositionBC: number;
|
|
99
|
+
}
|
|
100
|
+
export interface Order {
|
|
101
|
+
symbol: string;
|
|
102
|
+
side: string;
|
|
103
|
+
type: string;
|
|
104
|
+
quantity: number;
|
|
105
|
+
reduceOnly?: boolean | undefined;
|
|
106
|
+
limitPrice?: number | undefined;
|
|
107
|
+
keepPositionLvg?: boolean | undefined;
|
|
108
|
+
brokerFeeTbps?: number | undefined;
|
|
109
|
+
brokerAddr?: string | undefined;
|
|
110
|
+
brokerSignature?: BytesLike | undefined;
|
|
111
|
+
stopPrice?: number | undefined;
|
|
112
|
+
leverage?: number | undefined;
|
|
113
|
+
deadline?: number | undefined;
|
|
114
|
+
timestamp: number;
|
|
115
|
+
}
|
|
116
|
+
export interface SmartContractOrder {
|
|
117
|
+
flags: BigNumberish;
|
|
118
|
+
iPerpetualId: BigNumberish;
|
|
119
|
+
brokerFeeTbps: BigNumberish;
|
|
120
|
+
traderAddr: string;
|
|
121
|
+
brokerAddr: string;
|
|
122
|
+
referrerAddr: string;
|
|
123
|
+
brokerSignature: BytesLike;
|
|
124
|
+
fAmount: BigNumberish;
|
|
125
|
+
fLimitPrice: BigNumberish;
|
|
126
|
+
fTriggerPrice: BigNumberish;
|
|
127
|
+
fLeverage: BigNumberish;
|
|
128
|
+
iDeadline: BigNumberish;
|
|
129
|
+
createdTimestamp: BigNumberish;
|
|
130
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CollaterlCCY = exports.CLOSED_SIDE = exports.SELL_SIDE = exports.BUY_SIDE = exports.ORDER_TYPE_STOP_LIMIT = exports.ORDER_TYPE_STOP_MARKET = exports.ORDER_TYPE_MARKET = exports.ORDER_TYPE_LIMIT = exports.MASK_KEEP_POS_LEVERAGE = exports.MASK_STOP_ORDER = exports.MASK_MARKET_ORDER = exports.MASK_LIMIT_ORDER = exports.MASK_CLOSE_ONLY = exports.ORDER_MAX_DURATION_SEC = exports.DECIMALS = exports.MAX_UINT_256 = exports.MAX_64x64 = exports.ONE_64x64 = exports.ZERO_ADDRESS = exports.PERP_STATE_STR = exports.COLLATERAL_CURRENCY_QUANTO = exports.COLLATERAL_CURRENCY_BASE = exports.COLLATERAL_CURRENCY_QUOTE = exports.ERC20_ABI = exports.DEFAULT_CONFIG_MAINNET_NAME = exports.DEFAULT_CONFIG_TESTNET_NAME = exports.DEFAULT_CONFIG_MAINNET = exports.DEFAULT_CONFIG_TESTNET = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
exports.DEFAULT_CONFIG_TESTNET = "../config/defaultConfig.json";
|
|
6
|
+
exports.DEFAULT_CONFIG_MAINNET = "notthereyet";
|
|
7
|
+
exports.DEFAULT_CONFIG_TESTNET_NAME = "testnet";
|
|
8
|
+
exports.DEFAULT_CONFIG_MAINNET_NAME = "mainnet";
|
|
9
|
+
exports.ERC20_ABI = require("../abi/ERC20.json");
|
|
10
|
+
exports.COLLATERAL_CURRENCY_QUOTE = 0;
|
|
11
|
+
exports.COLLATERAL_CURRENCY_BASE = 1;
|
|
12
|
+
exports.COLLATERAL_CURRENCY_QUANTO = 2;
|
|
13
|
+
exports.PERP_STATE_STR = ["INVALID", "INITIALIZING", "NORMAL", "EMERGENCY", "CLEARED"];
|
|
14
|
+
exports.ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
15
|
+
exports.ONE_64x64 = ethers_1.BigNumber.from("0x010000000000000000");
|
|
16
|
+
exports.MAX_64x64 = ethers_1.BigNumber.from("0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF");
|
|
17
|
+
exports.MAX_UINT_256 = ethers_1.BigNumber.from(2).pow(256).sub(ethers_1.BigNumber.from(1));
|
|
18
|
+
exports.DECIMALS = ethers_1.BigNumber.from(10).pow(ethers_1.BigNumber.from(18));
|
|
19
|
+
exports.ORDER_MAX_DURATION_SEC = 60 * 60 * 24 * 30 * 4;
|
|
20
|
+
exports.MASK_CLOSE_ONLY = ethers_1.BigNumber.from("0x80000000");
|
|
21
|
+
exports.MASK_LIMIT_ORDER = ethers_1.BigNumber.from("0x04000000");
|
|
22
|
+
exports.MASK_MARKET_ORDER = ethers_1.BigNumber.from("0x40000000");
|
|
23
|
+
exports.MASK_STOP_ORDER = ethers_1.BigNumber.from("0x20000000");
|
|
24
|
+
exports.MASK_KEEP_POS_LEVERAGE = ethers_1.BigNumber.from("0x08000000");
|
|
25
|
+
exports.ORDER_TYPE_LIMIT = "LIMIT";
|
|
26
|
+
exports.ORDER_TYPE_MARKET = "MARKET";
|
|
27
|
+
exports.ORDER_TYPE_STOP_MARKET = "STOP_MARKET";
|
|
28
|
+
exports.ORDER_TYPE_STOP_LIMIT = "STOP_LIMIT";
|
|
29
|
+
exports.BUY_SIDE = "BUY";
|
|
30
|
+
exports.SELL_SIDE = "SELL";
|
|
31
|
+
exports.CLOSED_SIDE = "CLOSED";
|
|
32
|
+
var CollaterlCCY;
|
|
33
|
+
(function (CollaterlCCY) {
|
|
34
|
+
CollaterlCCY[CollaterlCCY["QUOTE"] = 0] = "QUOTE";
|
|
35
|
+
CollaterlCCY[CollaterlCCY["BASE"] = 1] = "BASE";
|
|
36
|
+
CollaterlCCY[CollaterlCCY["QUANTO"] = 2] = "QUANTO";
|
|
37
|
+
})(CollaterlCCY = exports.CollaterlCCY || (exports.CollaterlCCY = {}));
|
|
38
|
+
/*
|
|
39
|
+
t32 flags;
|
|
40
|
+
uint24 iPerpetualId;
|
|
41
|
+
uint16 brokerFeeTbps;
|
|
42
|
+
address traderAddr;
|
|
43
|
+
address brokerAddr;
|
|
44
|
+
address referrerAddr;
|
|
45
|
+
bytes brokerSignature;
|
|
46
|
+
int128 fAmount;
|
|
47
|
+
int128 fLimitPrice;
|
|
48
|
+
int128 fTriggerPrice;
|
|
49
|
+
int128 fLeverage; // 0 if deposit and trade separate
|
|
50
|
+
uint256 iDeadline;
|
|
51
|
+
uint256 createdTimestamp;
|
|
52
|
+
*/
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import WriteAccessHandler from "./writeAccessHandler";
|
|
2
|
+
import { NodeSDKConfig } from "./nodeSDKTypes";
|
|
3
|
+
/**
|
|
4
|
+
* OrderReferrerTool
|
|
5
|
+
* Methods to refer orders from the limit order book
|
|
6
|
+
*/
|
|
7
|
+
export default class OrderReferrerTool extends WriteAccessHandler {
|
|
8
|
+
/**
|
|
9
|
+
* Constructor
|
|
10
|
+
* @param config configuration
|
|
11
|
+
* @param privateKey private key of account that trades
|
|
12
|
+
*/
|
|
13
|
+
constructor(config: NodeSDKConfig, privateKey: string);
|
|
14
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const writeAccessHandler_1 = __importDefault(require("./writeAccessHandler"));
|
|
7
|
+
/**
|
|
8
|
+
* OrderReferrerTool
|
|
9
|
+
* Methods to refer orders from the limit order book
|
|
10
|
+
*/
|
|
11
|
+
class OrderReferrerTool extends writeAccessHandler_1.default {
|
|
12
|
+
/**
|
|
13
|
+
* Constructor
|
|
14
|
+
* @param config configuration
|
|
15
|
+
* @param privateKey private key of account that trades
|
|
16
|
+
*/
|
|
17
|
+
constructor(config, privateKey) {
|
|
18
|
+
super(config, privateKey);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.default = OrderReferrerTool;
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { ethers, BigNumber } from "ethers";
|
|
2
|
+
import { NodeSDKConfig, Order, SmartContractOrder, PerpetualStaticInfo, MarginAccount, PoolStaticInfo } from "./nodeSDKTypes";
|
|
3
|
+
/**
|
|
4
|
+
* Parent class for AccountTrade and MarketData that handles
|
|
5
|
+
* common data and chain operations
|
|
6
|
+
*/
|
|
7
|
+
export default class PerpetualDataHandler {
|
|
8
|
+
protected symbolToPerpStaticInfo: Map<string, PerpetualStaticInfo>;
|
|
9
|
+
protected poolStaticInfos: Array<PoolStaticInfo>;
|
|
10
|
+
protected symbolToTokenAddrMap: Map<string, string>;
|
|
11
|
+
protected proxyContract: ethers.Contract | null;
|
|
12
|
+
protected proxyABI: ethers.ContractInterface;
|
|
13
|
+
protected proxyAddr: string;
|
|
14
|
+
protected lobFactoryContract: ethers.Contract | null;
|
|
15
|
+
protected lobFactoryABI: ethers.ContractInterface;
|
|
16
|
+
protected lobFactoryAddr: string;
|
|
17
|
+
protected lobABI: ethers.ContractInterface;
|
|
18
|
+
protected nodeURL: string;
|
|
19
|
+
protected provider: ethers.providers.JsonRpcProvider | null;
|
|
20
|
+
private signerOrProvider;
|
|
21
|
+
protected nestedPerpetualIDs: number[][];
|
|
22
|
+
constructor(config: NodeSDKConfig);
|
|
23
|
+
protected initContractsAndData(signerOrProvider: ethers.Signer | ethers.providers.Provider): Promise<void>;
|
|
24
|
+
/**
|
|
25
|
+
* Returns the order-book contract for the symbol if found or fails
|
|
26
|
+
* @param symbol symbol of the form ETH-USD-MATIC
|
|
27
|
+
* @returns order book contract for the perpetual
|
|
28
|
+
*/
|
|
29
|
+
getOrderBookContract(symbol: string): ethers.Contract;
|
|
30
|
+
/**
|
|
31
|
+
* Called when initializing. This function fills this.symbolToTokenAddrMap,
|
|
32
|
+
* and this.nestedPerpetualIDs and this.symbolToPerpStaticInfo
|
|
33
|
+
*
|
|
34
|
+
*/
|
|
35
|
+
protected _fillSymbolMaps(proxyContract: ethers.Contract): Promise<void>;
|
|
36
|
+
getSymbolFromPoolId(poolId: number): string;
|
|
37
|
+
getPoolIdFromSymbol(symbol: string): number;
|
|
38
|
+
protected static _getSymbolFromPoolId(poolId: number, staticInfos: PoolStaticInfo[]): string;
|
|
39
|
+
protected static _getPoolIdFromSymbol(symbol: string, staticInfos: PoolStaticInfo[]): number;
|
|
40
|
+
static getNestedPerpetualIds(_proxyContract: ethers.Contract): Promise<number[][]>;
|
|
41
|
+
static getMarginAccount(traderAddr: string, symbol: string, symbolToPerpStaticInfo: Map<string, PerpetualStaticInfo>, _proxyContract: ethers.Contract): Promise<MarginAccount>;
|
|
42
|
+
/**
|
|
43
|
+
* Liquidation price
|
|
44
|
+
* @param cleanSymbol symbol after calling symbolToBytes4Symbol
|
|
45
|
+
* @param traderState BigInt array according to smart contract
|
|
46
|
+
* @param symbolToPerpStaticInfo mapping symbol->PerpStaticInfo
|
|
47
|
+
* @returns liquidation mark-price, corresponding collateral/quote conversion
|
|
48
|
+
*/
|
|
49
|
+
protected static _calculateLiquidationPrice(cleanSymbol: string, traderState: BigNumber[], symbolToPerpStaticInfo: Map<string, PerpetualStaticInfo>): [number, number, number, number, number];
|
|
50
|
+
/**
|
|
51
|
+
* Finds the perpetual id for a symbol of the form
|
|
52
|
+
* <base>-<quote>-<collateral>. The function first converts the
|
|
53
|
+
* token names into bytes4 representation
|
|
54
|
+
* @param symbol symbol (e.g., BTC-USD-MATIC)
|
|
55
|
+
* @param symbolToPerpStaticInfo map that contains the bytes4-symbol to PerpetualStaticInfo
|
|
56
|
+
* including id mapping
|
|
57
|
+
* @returns perpetual id or it fails
|
|
58
|
+
*/
|
|
59
|
+
protected static symbolToPerpetualId(symbol: string, symbolToPerpStaticInfo: Map<string, PerpetualStaticInfo>): number;
|
|
60
|
+
protected static symbolToBytes4Symbol(symbol: string): string;
|
|
61
|
+
private static _getByValue;
|
|
62
|
+
protected static fromSmartContractOrder(order: SmartContractOrder, symbolToPerpInfoMap: Map<string, PerpetualStaticInfo>): Order;
|
|
63
|
+
/**
|
|
64
|
+
* Transform the convenient form of the order into a smart-contract accepted type of order
|
|
65
|
+
* @param order order type
|
|
66
|
+
* @param traderAddr address of the trader
|
|
67
|
+
* @param symbolToPerpetualMap mapping of symbol to perpetual Id
|
|
68
|
+
* @returns SmartContractOrder
|
|
69
|
+
*/
|
|
70
|
+
protected static toSmartContractOrder(order: Order, traderAddr: string, perpStaticInfo: Map<string, PerpetualStaticInfo>): SmartContractOrder;
|
|
71
|
+
private static _flagToOrderType;
|
|
72
|
+
/**
|
|
73
|
+
* Determine the correct order flags based on the order-properties.
|
|
74
|
+
* Checks for some misspecifications.
|
|
75
|
+
* @param order order type
|
|
76
|
+
* @returns BigNumber flags
|
|
77
|
+
*/
|
|
78
|
+
private static _orderTypeToFlag;
|
|
79
|
+
/**
|
|
80
|
+
* Read config file into NodeSDKConfig interface
|
|
81
|
+
* @param fileLocation json-file with required variables for config
|
|
82
|
+
* @returns NodeSDKConfig
|
|
83
|
+
*/
|
|
84
|
+
static readSDKConfig(fileLocation: string): NodeSDKConfig;
|
|
85
|
+
}
|