@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,166 @@
|
|
|
1
|
+
[
|
|
2
|
+
{
|
|
3
|
+
"inputs": [
|
|
4
|
+
{
|
|
5
|
+
"internalType": "address",
|
|
6
|
+
"name": "_initImplementation",
|
|
7
|
+
"type": "address"
|
|
8
|
+
}
|
|
9
|
+
],
|
|
10
|
+
"stateMutability": "nonpayable",
|
|
11
|
+
"type": "constructor"
|
|
12
|
+
},
|
|
13
|
+
{
|
|
14
|
+
"anonymous": false,
|
|
15
|
+
"inputs": [
|
|
16
|
+
{
|
|
17
|
+
"indexed": true,
|
|
18
|
+
"internalType": "address",
|
|
19
|
+
"name": "previousOwner",
|
|
20
|
+
"type": "address"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"indexed": true,
|
|
24
|
+
"internalType": "address",
|
|
25
|
+
"name": "newOwner",
|
|
26
|
+
"type": "address"
|
|
27
|
+
}
|
|
28
|
+
],
|
|
29
|
+
"name": "OwnershipTransferred",
|
|
30
|
+
"type": "event"
|
|
31
|
+
},
|
|
32
|
+
{
|
|
33
|
+
"anonymous": false,
|
|
34
|
+
"inputs": [
|
|
35
|
+
{
|
|
36
|
+
"indexed": true,
|
|
37
|
+
"internalType": "uint24",
|
|
38
|
+
"name": "perpetualId",
|
|
39
|
+
"type": "uint24"
|
|
40
|
+
},
|
|
41
|
+
{
|
|
42
|
+
"indexed": false,
|
|
43
|
+
"internalType": "address",
|
|
44
|
+
"name": "perpManagerAddress",
|
|
45
|
+
"type": "address"
|
|
46
|
+
},
|
|
47
|
+
{
|
|
48
|
+
"indexed": false,
|
|
49
|
+
"internalType": "address",
|
|
50
|
+
"name": "limitOrderBookAddress",
|
|
51
|
+
"type": "address"
|
|
52
|
+
}
|
|
53
|
+
],
|
|
54
|
+
"name": "PerpetualLimitOrderBookDeployed",
|
|
55
|
+
"type": "event"
|
|
56
|
+
},
|
|
57
|
+
{
|
|
58
|
+
"inputs": [],
|
|
59
|
+
"name": "beacon",
|
|
60
|
+
"outputs": [
|
|
61
|
+
{
|
|
62
|
+
"internalType": "contract LimitOrderBookBeacon",
|
|
63
|
+
"name": "",
|
|
64
|
+
"type": "address"
|
|
65
|
+
}
|
|
66
|
+
],
|
|
67
|
+
"stateMutability": "view",
|
|
68
|
+
"type": "function"
|
|
69
|
+
},
|
|
70
|
+
{
|
|
71
|
+
"inputs": [
|
|
72
|
+
{
|
|
73
|
+
"internalType": "address",
|
|
74
|
+
"name": "_perpetualManagerAddr",
|
|
75
|
+
"type": "address"
|
|
76
|
+
},
|
|
77
|
+
{
|
|
78
|
+
"internalType": "uint24",
|
|
79
|
+
"name": "_perpetualId",
|
|
80
|
+
"type": "uint24"
|
|
81
|
+
}
|
|
82
|
+
],
|
|
83
|
+
"name": "deployLimitOrderBookProxy",
|
|
84
|
+
"outputs": [],
|
|
85
|
+
"stateMutability": "nonpayable",
|
|
86
|
+
"type": "function"
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
"inputs": [],
|
|
90
|
+
"name": "getBeacon",
|
|
91
|
+
"outputs": [
|
|
92
|
+
{
|
|
93
|
+
"internalType": "address",
|
|
94
|
+
"name": "",
|
|
95
|
+
"type": "address"
|
|
96
|
+
}
|
|
97
|
+
],
|
|
98
|
+
"stateMutability": "view",
|
|
99
|
+
"type": "function"
|
|
100
|
+
},
|
|
101
|
+
{
|
|
102
|
+
"inputs": [],
|
|
103
|
+
"name": "getImplementation",
|
|
104
|
+
"outputs": [
|
|
105
|
+
{
|
|
106
|
+
"internalType": "address",
|
|
107
|
+
"name": "",
|
|
108
|
+
"type": "address"
|
|
109
|
+
}
|
|
110
|
+
],
|
|
111
|
+
"stateMutability": "view",
|
|
112
|
+
"type": "function"
|
|
113
|
+
},
|
|
114
|
+
{
|
|
115
|
+
"inputs": [
|
|
116
|
+
{
|
|
117
|
+
"internalType": "uint24",
|
|
118
|
+
"name": "_perpetualId",
|
|
119
|
+
"type": "uint24"
|
|
120
|
+
}
|
|
121
|
+
],
|
|
122
|
+
"name": "getOrderBookAddress",
|
|
123
|
+
"outputs": [
|
|
124
|
+
{
|
|
125
|
+
"internalType": "address",
|
|
126
|
+
"name": "",
|
|
127
|
+
"type": "address"
|
|
128
|
+
}
|
|
129
|
+
],
|
|
130
|
+
"stateMutability": "view",
|
|
131
|
+
"type": "function"
|
|
132
|
+
},
|
|
133
|
+
{
|
|
134
|
+
"inputs": [],
|
|
135
|
+
"name": "owner",
|
|
136
|
+
"outputs": [
|
|
137
|
+
{
|
|
138
|
+
"internalType": "address",
|
|
139
|
+
"name": "",
|
|
140
|
+
"type": "address"
|
|
141
|
+
}
|
|
142
|
+
],
|
|
143
|
+
"stateMutability": "view",
|
|
144
|
+
"type": "function"
|
|
145
|
+
},
|
|
146
|
+
{
|
|
147
|
+
"inputs": [],
|
|
148
|
+
"name": "renounceOwnership",
|
|
149
|
+
"outputs": [],
|
|
150
|
+
"stateMutability": "nonpayable",
|
|
151
|
+
"type": "function"
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
"inputs": [
|
|
155
|
+
{
|
|
156
|
+
"internalType": "address",
|
|
157
|
+
"name": "newOwner",
|
|
158
|
+
"type": "address"
|
|
159
|
+
}
|
|
160
|
+
],
|
|
161
|
+
"name": "transferOwnership",
|
|
162
|
+
"outputs": [],
|
|
163
|
+
"stateMutability": "nonpayable",
|
|
164
|
+
"type": "function"
|
|
165
|
+
}
|
|
166
|
+
]
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "v1.1",
|
|
3
|
+
"proxyAddr": "0x55F4041DAea27c6F380d6E221d63EF300a293Ee5",
|
|
4
|
+
"nodeURL": "https://rpc-mumbai.maticvigil.com/",
|
|
5
|
+
"proxyABILocation": "../abi/IPerpetualManager.json",
|
|
6
|
+
"limitOrderBookFactoryAddr": "0xd63Faf780687795EBac85ce248a45E457752f021",
|
|
7
|
+
"limitOrderBookFactoryABILocation": "../abi/LimitOrderBookFactory.json",
|
|
8
|
+
"limitOrderBookABILocation": "../abi/LimitOrderBook.json"
|
|
9
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "v1.0",
|
|
3
|
+
"proxyAddr": "0xB1f0069E9adE96045Ad2800da99C823F504040D0",
|
|
4
|
+
"nodeURL": "https://rpc-mumbai.maticvigil.com/",
|
|
5
|
+
"proxyABILocation": "../abi/IPerpetualManager.json",
|
|
6
|
+
"limitOrderBookFactoryAddr": "0x63787232Ac59C5eC12F07DcF37a9B2a4F86AbdD5",
|
|
7
|
+
"limitOrderBookFactoryABILocation": "../abi/LimitOrderBookFactory.json",
|
|
8
|
+
"limitOrderBookABILocation": "../abi/LimitOrderBook.json"
|
|
9
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { ethers } from "ethers";
|
|
2
|
+
import { NodeSDKConfig, Order, PerpetualStaticInfo } from "./nodeSDKTypes";
|
|
3
|
+
import WriteAccessHandler from "./writeAccessHandler";
|
|
4
|
+
/**
|
|
5
|
+
* Account and Trade.
|
|
6
|
+
* This class requires a private key and executes smart-contract interaction that
|
|
7
|
+
* require gas-payments.
|
|
8
|
+
*/
|
|
9
|
+
export default class AccountTrade extends WriteAccessHandler {
|
|
10
|
+
/**
|
|
11
|
+
* Constructor
|
|
12
|
+
* @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.readSDKConfig.
|
|
13
|
+
* @param {string} privateKey Private key of account that trades.
|
|
14
|
+
*/
|
|
15
|
+
constructor(config: NodeSDKConfig, privateKey: string);
|
|
16
|
+
/**
|
|
17
|
+
* Cancels an existing order on the exchange.
|
|
18
|
+
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
19
|
+
* @param {string} orderId ID of the order to be cancelled.
|
|
20
|
+
*/
|
|
21
|
+
cancelOrder(symbol: string, orderId: string): Promise<string | undefined>;
|
|
22
|
+
/**
|
|
23
|
+
* Submits an order to the exchange.
|
|
24
|
+
* @param {Order} order Order struct.
|
|
25
|
+
* @returns {string} Transaction hash.
|
|
26
|
+
*/
|
|
27
|
+
order(order: Order): Promise<string | undefined>;
|
|
28
|
+
/**
|
|
29
|
+
* Static order function
|
|
30
|
+
* @param order order type (not SmartContractOrder but Order)
|
|
31
|
+
* @param traderAddr trader address
|
|
32
|
+
* @param symbolToPerpetualMap maps the symbol (MATIC-USD-MATIC)-type format to the perpetual id
|
|
33
|
+
* @param proxyContract contract instance of D8X perpetuals
|
|
34
|
+
* @param orderBookContract order book contract or null
|
|
35
|
+
* @param chainId chain Id of network
|
|
36
|
+
* @param signer instance of ethers wallet that can write
|
|
37
|
+
* @param gasLimit gas limit to be used for the trade
|
|
38
|
+
* @returns transaction hash
|
|
39
|
+
* @ignore
|
|
40
|
+
*/
|
|
41
|
+
_order(order: Order, traderAddr: string, symbolToPerpetualMap: Map<string, PerpetualStaticInfo>, proxyContract: ethers.Contract, orderBookContract: ethers.Contract | null, chainId: number, signer: ethers.Wallet, gasLimit: number): Promise<string | undefined>;
|
|
42
|
+
protected _cancelOrder(symbol: string, orderId: string, orderBookContract: ethers.Contract | null): Promise<string | undefined>;
|
|
43
|
+
/**
|
|
44
|
+
* Creates a signature
|
|
45
|
+
* @param order smart-contract-type order
|
|
46
|
+
* @param chainId chainId of network
|
|
47
|
+
* @param isNewOrder true unless we cancel
|
|
48
|
+
* @param signer ethereum-type wallet
|
|
49
|
+
* @param proxyAddress address of the contract
|
|
50
|
+
* @returns signature as string
|
|
51
|
+
* @ignore
|
|
52
|
+
*/
|
|
53
|
+
private _createSignature;
|
|
54
|
+
}
|
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const ethers_1 = require("ethers");
|
|
16
|
+
const nodeSDKTypes_1 = require("./nodeSDKTypes");
|
|
17
|
+
const writeAccessHandler_1 = __importDefault(require("./writeAccessHandler"));
|
|
18
|
+
//import { abi, rawEncode } from "ethereumjs-abi";
|
|
19
|
+
/**
|
|
20
|
+
* Account and Trade.
|
|
21
|
+
* This class requires a private key and executes smart-contract interaction that
|
|
22
|
+
* require gas-payments.
|
|
23
|
+
*/
|
|
24
|
+
class AccountTrade extends writeAccessHandler_1.default {
|
|
25
|
+
/**
|
|
26
|
+
* Constructor
|
|
27
|
+
* @param {NodeSDKConfig} config Configuration object, see PerpetualDataHandler.readSDKConfig.
|
|
28
|
+
* @param {string} privateKey Private key of account that trades.
|
|
29
|
+
*/
|
|
30
|
+
constructor(config, privateKey) {
|
|
31
|
+
super(config, privateKey);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Cancels an existing order on the exchange.
|
|
35
|
+
* @param {string} symbol Symbol of the form ETH-USD-MATIC.
|
|
36
|
+
* @param {string} orderId ID of the order to be cancelled.
|
|
37
|
+
*/
|
|
38
|
+
cancelOrder(symbol, orderId) {
|
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
41
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
42
|
+
}
|
|
43
|
+
let orderBookContract = null;
|
|
44
|
+
orderBookContract = this.getOrderBookContract(symbol);
|
|
45
|
+
return yield this._cancelOrder(symbol, orderId, orderBookContract);
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
/*
|
|
49
|
+
TODO: -deposit (margin into account)
|
|
50
|
+
-withdraw margin withdraw(uint24 _iPerpetualId, int128 _fAmount)
|
|
51
|
+
|
|
52
|
+
*/
|
|
53
|
+
/**
|
|
54
|
+
* Submits an order to the exchange.
|
|
55
|
+
* @param {Order} order Order struct.
|
|
56
|
+
* @returns {string} Transaction hash.
|
|
57
|
+
*/
|
|
58
|
+
order(order) {
|
|
59
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
60
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
61
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
62
|
+
}
|
|
63
|
+
let orderBookContract = null;
|
|
64
|
+
if (order.type != nodeSDKTypes_1.ORDER_TYPE_MARKET) {
|
|
65
|
+
orderBookContract = this.getOrderBookContract(order.symbol);
|
|
66
|
+
}
|
|
67
|
+
return yield this._order(order, this.traderAddr, this.symbolToPerpStaticInfo, this.proxyContract, orderBookContract, this.chainId, this.signer, this.gasLimit);
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Static order function
|
|
72
|
+
* @param order order type (not SmartContractOrder but Order)
|
|
73
|
+
* @param traderAddr trader address
|
|
74
|
+
* @param symbolToPerpetualMap maps the symbol (MATIC-USD-MATIC)-type format to the perpetual id
|
|
75
|
+
* @param proxyContract contract instance of D8X perpetuals
|
|
76
|
+
* @param orderBookContract order book contract or null
|
|
77
|
+
* @param chainId chain Id of network
|
|
78
|
+
* @param signer instance of ethers wallet that can write
|
|
79
|
+
* @param gasLimit gas limit to be used for the trade
|
|
80
|
+
* @returns transaction hash
|
|
81
|
+
* @ignore
|
|
82
|
+
*/
|
|
83
|
+
_order(order, traderAddr, symbolToPerpetualMap, proxyContract, orderBookContract, chainId, signer, gasLimit) {
|
|
84
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
85
|
+
let scOrder = AccountTrade.toSmartContractOrder(order, traderAddr, symbolToPerpetualMap);
|
|
86
|
+
// if we are here, we have a clean order
|
|
87
|
+
// decide whether to send order to Limit Order Book or AMM based on order type
|
|
88
|
+
let tx;
|
|
89
|
+
if (order.type == nodeSDKTypes_1.ORDER_TYPE_MARKET) {
|
|
90
|
+
// send market order
|
|
91
|
+
tx = yield proxyContract.trade(scOrder, { gasLimit: gasLimit });
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
// conditional order so the order is sent to the order-book
|
|
95
|
+
if (orderBookContract == null) {
|
|
96
|
+
throw Error("Order book contract not provided.");
|
|
97
|
+
}
|
|
98
|
+
let signature = yield this._createSignature(scOrder, chainId, true, signer, proxyContract.address);
|
|
99
|
+
tx = yield orderBookContract.createLimitOrder(scOrder, signature, { gasLimit: gasLimit });
|
|
100
|
+
}
|
|
101
|
+
return tx.hash;
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
_cancelOrder(symbol, orderId, orderBookContract) {
|
|
105
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
+
if (orderBookContract == null || this.signer == null) {
|
|
107
|
+
throw Error(`Order Book contract for symbol ${symbol} or signer not defined`);
|
|
108
|
+
}
|
|
109
|
+
let scOrder = yield orderBookContract.orderOfDigest(orderId);
|
|
110
|
+
let signature = yield this._createSignature(scOrder, this.chainId, false, this.signer, this.proxyAddr);
|
|
111
|
+
return yield orderBookContract.cancelLimitOrder(orderId, signature);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Creates a signature
|
|
116
|
+
* @param order smart-contract-type order
|
|
117
|
+
* @param chainId chainId of network
|
|
118
|
+
* @param isNewOrder true unless we cancel
|
|
119
|
+
* @param signer ethereum-type wallet
|
|
120
|
+
* @param proxyAddress address of the contract
|
|
121
|
+
* @returns signature as string
|
|
122
|
+
* @ignore
|
|
123
|
+
*/
|
|
124
|
+
_createSignature(order, chainId, isNewOrder, signer, proxyAddress) {
|
|
125
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
126
|
+
const NAME = "Perpetual Trade Manager";
|
|
127
|
+
const DOMAIN_TYPEHASH = ethers_1.ethers.utils.keccak256(Buffer.from("EIP712Domain(string name,uint256 chainId,address verifyingContract)"));
|
|
128
|
+
let abiCoder = ethers_1.ethers.utils.defaultAbiCoder;
|
|
129
|
+
let domainSeparator = ethers_1.ethers.utils.keccak256(abiCoder.encode(["bytes32", "bytes32", "uint256", "address"], [DOMAIN_TYPEHASH, ethers_1.ethers.utils.keccak256(Buffer.from(NAME)), chainId, proxyAddress]));
|
|
130
|
+
const TRADE_ORDER_TYPEHASH = ethers_1.ethers.utils.keccak256(Buffer.from("Order(uint24 iPerpetualId,uint16 brokerFeeTbps,address traderAddr,address brokerAddr,int128 fAmount,int128 fLimitPrice,int128 fTriggerPrice,uint256 iDeadline,uint32 flags,int128 fLeverage,uint256 createdTimestamp)"));
|
|
131
|
+
let structHash = ethers_1.ethers.utils.keccak256(abiCoder.encode([
|
|
132
|
+
"bytes32",
|
|
133
|
+
"uint24",
|
|
134
|
+
"uint16",
|
|
135
|
+
"address",
|
|
136
|
+
"address",
|
|
137
|
+
"int128",
|
|
138
|
+
"int128",
|
|
139
|
+
"int128",
|
|
140
|
+
"uint256",
|
|
141
|
+
"uint32",
|
|
142
|
+
"int128",
|
|
143
|
+
"uint256",
|
|
144
|
+
], [
|
|
145
|
+
TRADE_ORDER_TYPEHASH,
|
|
146
|
+
order.iPerpetualId,
|
|
147
|
+
order.brokerFeeTbps,
|
|
148
|
+
order.traderAddr,
|
|
149
|
+
order.brokerAddr,
|
|
150
|
+
order.fAmount,
|
|
151
|
+
order.fLimitPrice,
|
|
152
|
+
order.fTriggerPrice,
|
|
153
|
+
order.iDeadline,
|
|
154
|
+
order.flags,
|
|
155
|
+
order.fLeverage,
|
|
156
|
+
order.createdTimestamp,
|
|
157
|
+
]));
|
|
158
|
+
let digest = ethers_1.ethers.utils.keccak256(abiCoder.encode(["bytes32", "bytes32", "bool"], [domainSeparator, structHash, isNewOrder]));
|
|
159
|
+
let digestBuffer = Buffer.from(digest.substring(2, digest.length), "hex");
|
|
160
|
+
return yield signer.signMessage(digestBuffer);
|
|
161
|
+
});
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
exports.default = AccountTrade;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import WriteAccessHandler from "./writeAccessHandler";
|
|
2
|
+
import { NodeSDKConfig, Order } from "./nodeSDKTypes";
|
|
3
|
+
import { ethers } from "ethers";
|
|
4
|
+
/**
|
|
5
|
+
* BrokerTool.
|
|
6
|
+
* Signature method for brokers
|
|
7
|
+
*/
|
|
8
|
+
export default class BrokerTool extends WriteAccessHandler {
|
|
9
|
+
/**
|
|
10
|
+
* Constructor
|
|
11
|
+
* @param config configuration
|
|
12
|
+
* @param privateKey private key of account that trades
|
|
13
|
+
*/
|
|
14
|
+
constructor(config: NodeSDKConfig, privateKey: string);
|
|
15
|
+
/**
|
|
16
|
+
*
|
|
17
|
+
* @param symbol symbol of the form "ETH-USD-MATIC" or just "MATIC"
|
|
18
|
+
* @returns broker lot size in collateral currency, e.g. in MATIC for symbol ETH-USD-MATIC or MATIC
|
|
19
|
+
*/
|
|
20
|
+
getLotSize(symbol: string): Promise<number | undefined>;
|
|
21
|
+
brokerDepositToDefaultFund(symbol: string, lots: number): Promise<ethers.providers.TransactionResponse>;
|
|
22
|
+
getFeeForBrokerVolume(symbol: string): Promise<number | undefined>;
|
|
23
|
+
/**
|
|
24
|
+
*
|
|
25
|
+
* @param symbol symbol of the form "ETH-USD-MATIC" or just "MATIC"
|
|
26
|
+
* @returns number of lots deposited by broker
|
|
27
|
+
*/
|
|
28
|
+
getBrokerDesignation(symbol: string): Promise<number>;
|
|
29
|
+
/**
|
|
30
|
+
* @param symbol symbol of the form "ETH-USD-MATIC" or just "MATIC"
|
|
31
|
+
* @param lots number of lots for which to get the fee. Defaults to this broker's current deposit if not specified
|
|
32
|
+
* @returns fee in decimals based on given number of lots
|
|
33
|
+
*/
|
|
34
|
+
getFeeForBrokerDesignation(symbol: string, newLots?: number): Promise<number | undefined>;
|
|
35
|
+
/**
|
|
36
|
+
* @param order order for which to determine the trading fee
|
|
37
|
+
* @param traderAddr address of the trader for whom to determine the fee, defaults to lowest tier
|
|
38
|
+
* @returns fee in decimals (1% is 0.01)
|
|
39
|
+
*/
|
|
40
|
+
determineExchangeFee(order: Order, traderAddr?: string): Promise<number | undefined>;
|
|
41
|
+
}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const writeAccessHandler_1 = __importDefault(require("./writeAccessHandler"));
|
|
16
|
+
const nodeSDKTypes_1 = require("./nodeSDKTypes");
|
|
17
|
+
const perpetualDataHandler_1 = __importDefault(require("./perpetualDataHandler"));
|
|
18
|
+
const d8XMath_1 = require("./d8XMath");
|
|
19
|
+
const accountTrade_1 = __importDefault(require("./accountTrade"));
|
|
20
|
+
/**
|
|
21
|
+
* BrokerTool.
|
|
22
|
+
* Signature method for brokers
|
|
23
|
+
*/
|
|
24
|
+
class BrokerTool extends writeAccessHandler_1.default {
|
|
25
|
+
/**
|
|
26
|
+
* Constructor
|
|
27
|
+
* @param config configuration
|
|
28
|
+
* @param privateKey private key of account that trades
|
|
29
|
+
*/
|
|
30
|
+
constructor(config, privateKey) {
|
|
31
|
+
super(config, privateKey);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
*
|
|
35
|
+
* @param symbol symbol of the form "ETH-USD-MATIC" or just "MATIC"
|
|
36
|
+
* @returns broker lot size in collateral currency, e.g. in MATIC for symbol ETH-USD-MATIC or MATIC
|
|
37
|
+
*/
|
|
38
|
+
getLotSize(symbol) {
|
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
if (this.proxyContract == null) {
|
|
41
|
+
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
42
|
+
}
|
|
43
|
+
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(symbol, this.poolStaticInfos);
|
|
44
|
+
let pool = yield this.proxyContract.getLiquidityPool(poolId);
|
|
45
|
+
let lot = pool === null || pool === void 0 ? void 0 : pool.fBrokerCollateralLotSize;
|
|
46
|
+
if (lot != undefined) {
|
|
47
|
+
lot = (0, d8XMath_1.ABK64x64ToFloat)(pool.fBrokerCollateralLotSize);
|
|
48
|
+
}
|
|
49
|
+
return lot;
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
brokerDepositToDefaultFund(symbol, lots) {
|
|
53
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
55
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
56
|
+
}
|
|
57
|
+
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(symbol, this.poolStaticInfos);
|
|
58
|
+
let tx = yield this.proxyContract.brokerDepositToDefaultFund(poolId, lots, { gasLimit: this.gasLimit });
|
|
59
|
+
return tx;
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
getFeeForBrokerVolume(symbol) {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
if (this.proxyContract == null) {
|
|
65
|
+
throw Error("no proxy contract initialized.");
|
|
66
|
+
}
|
|
67
|
+
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(symbol, this.poolStaticInfos);
|
|
68
|
+
let feeTbps = yield this.proxyContract.getFeeForBrokerVolume(poolId, this.traderAddr);
|
|
69
|
+
return feeTbps / 100000;
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
*
|
|
74
|
+
* @param symbol symbol of the form "ETH-USD-MATIC" or just "MATIC"
|
|
75
|
+
* @returns number of lots deposited by broker
|
|
76
|
+
*/
|
|
77
|
+
getBrokerDesignation(symbol) {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
if (this.proxyContract == null) {
|
|
80
|
+
throw Error("no proxy contract initialized.");
|
|
81
|
+
}
|
|
82
|
+
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(symbol, this.poolStaticInfos);
|
|
83
|
+
let designation = yield this.proxyContract.getBrokerDesignation(poolId, this.traderAddr);
|
|
84
|
+
return designation;
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* @param symbol symbol of the form "ETH-USD-MATIC" or just "MATIC"
|
|
89
|
+
* @param lots number of lots for which to get the fee. Defaults to this broker's current deposit if not specified
|
|
90
|
+
* @returns fee in decimals based on given number of lots
|
|
91
|
+
*/
|
|
92
|
+
getFeeForBrokerDesignation(symbol, newLots = 0) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
if (this.proxyContract == null) {
|
|
95
|
+
throw Error("no proxy contract initialized.");
|
|
96
|
+
}
|
|
97
|
+
if (newLots < 0) {
|
|
98
|
+
throw Error("new lots must be a positive number.");
|
|
99
|
+
}
|
|
100
|
+
let lots = yield this.getBrokerDesignation(symbol);
|
|
101
|
+
lots += newLots;
|
|
102
|
+
let feeTbps = yield this.proxyContract.getFeeForBrokerDesignation(lots);
|
|
103
|
+
return feeTbps / 100000;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* @param order order for which to determine the trading fee
|
|
108
|
+
* @param traderAddr address of the trader for whom to determine the fee, defaults to lowest tier
|
|
109
|
+
* @returns fee in decimals (1% is 0.01)
|
|
110
|
+
*/
|
|
111
|
+
determineExchangeFee(order, traderAddr = nodeSDKTypes_1.ZERO_ADDRESS) {
|
|
112
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
113
|
+
if (this.proxyContract == null) {
|
|
114
|
+
throw Error("no proxy contract initialized.");
|
|
115
|
+
}
|
|
116
|
+
// broker does not need to enter address in the order if he's signed in
|
|
117
|
+
if (order.brokerAddr == undefined) {
|
|
118
|
+
if (this.signer == null) {
|
|
119
|
+
throw Error("no wallet initialized.");
|
|
120
|
+
}
|
|
121
|
+
order.brokerAddr = this.traderAddr;
|
|
122
|
+
}
|
|
123
|
+
let scOrder = accountTrade_1.default.toSmartContractOrder(order, traderAddr, this.symbolToPerpStaticInfo);
|
|
124
|
+
let feeTbps = yield this.proxyContract.determineExchangeFee(scOrder);
|
|
125
|
+
return feeTbps / 100000;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.default = BrokerTool;
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { BigNumber } from "ethers";
|
|
2
|
+
/**
|
|
3
|
+
* Convert ABK64x64 bigint-format to float.
|
|
4
|
+
* Result = x/2^64 if big number, x/2^29 if number
|
|
5
|
+
* @param x number in ABDK-format or 2^29
|
|
6
|
+
* @returns x/2^64 in number-format (float)
|
|
7
|
+
*/
|
|
8
|
+
export declare function ABK64x64ToFloat(x: BigNumber | number): number;
|
|
9
|
+
/**
|
|
10
|
+
*
|
|
11
|
+
* @param x BigNumber in Dec18 format
|
|
12
|
+
* @returns x as a float (number)
|
|
13
|
+
*/
|
|
14
|
+
export declare function dec18ToFloat(x: BigNumber): number;
|
|
15
|
+
/**
|
|
16
|
+
* Converts x into ABDK64x64 format
|
|
17
|
+
* @param x number (float)
|
|
18
|
+
* @returns x^64 in big number format
|
|
19
|
+
*/
|
|
20
|
+
export declare function floatToABK64x64(x: number): BigNumber;
|
|
21
|
+
/**
|
|
22
|
+
*
|
|
23
|
+
* @param x number (float)
|
|
24
|
+
* @returns x as a BigNumber in Dec18 format
|
|
25
|
+
*/
|
|
26
|
+
export declare function floatToDec18(x: number): BigNumber;
|
|
27
|
+
/**
|
|
28
|
+
*
|
|
29
|
+
* @param x
|
|
30
|
+
* @param y
|
|
31
|
+
* @returns x * y
|
|
32
|
+
*/
|
|
33
|
+
export declare function mul64x64(x: BigNumber, y: BigNumber): BigNumber;
|
|
34
|
+
/**
|
|
35
|
+
*
|
|
36
|
+
* @param x
|
|
37
|
+
* @param y
|
|
38
|
+
* @returns x / y
|
|
39
|
+
*/
|
|
40
|
+
export declare function div64x64(x: BigNumber, y: BigNumber): BigNumber;
|
|
41
|
+
/**
|
|
42
|
+
* Determine the liquidation price
|
|
43
|
+
* @param {number} LockedInValueQC - trader locked in value in quote currency
|
|
44
|
+
* @param {number} position - trader position in base currency
|
|
45
|
+
* @param {number} cash_cc - trader available margin cash in collateral currency
|
|
46
|
+
* @param {number} maintenance_margin_rate - maintenance margin ratio
|
|
47
|
+
* @param {number} S3 - collateral to quote conversion (=S2 if base-collateral, =1 if quuote collateral, = index S3 if quanto)
|
|
48
|
+
* @returns {number} Amount to be deposited to have the given leverage when trading into position pos
|
|
49
|
+
*/
|
|
50
|
+
export declare function calculateLiquidationPriceCollateralBase(LockedInValueQC: number, position: number, cash_cc: number, maintenance_margin_rate: number): number;
|
|
51
|
+
/**
|
|
52
|
+
* Determine the liquidation price
|
|
53
|
+
* @param {number} LockedInValueQC - trader locked in value in quote currency
|
|
54
|
+
* @param {number} position - trader position in base currency
|
|
55
|
+
* @param {number} cash_cc - trader available margin cash in collateral currency
|
|
56
|
+
* @param {number} maintenance_margin_rate - maintenance margin ratio
|
|
57
|
+
* @param {number} S3 - collateral to quote conversion (=S2 if base-collateral, =1 if quuote collateral, = index S3 if quanto)
|
|
58
|
+
* @param {number} Sm - mark price
|
|
59
|
+
* @returns {number} Amount to be deposited to have the given leverage when trading into position pos
|
|
60
|
+
*/
|
|
61
|
+
export declare function calculateLiquidationPriceCollateralQuanto(LockedInValueQC: number, position: number, cash_cc: number, maintenance_margin_rate: number, S3: number, Sm: number): number;
|
|
62
|
+
/**
|
|
63
|
+
* Determine the liquidation price
|
|
64
|
+
* @param {number} LockedInValueQC - trader locked in value in quote currency
|
|
65
|
+
* @param {number} position - trader position in base currency
|
|
66
|
+
* @param {number} cash_cc - trader available margin cash in collateral currency
|
|
67
|
+
* @param {number} maintenance_margin_rate - maintenance margin ratio
|
|
68
|
+
* @param {number} S3 - collateral to quote conversion (=S2 if base-collateral, =1 if quuote collateral, = index S3 if quanto)
|
|
69
|
+
* @returns {number} Amount to be deposited to have the given leverage when trading into position pos
|
|
70
|
+
*/
|
|
71
|
+
export declare function calculateLiquidationPriceCollateralQuote(LockedInValueQC: number, position: number, cash_cc: number, maintenance_margin_rate: number): number;
|