@d8x/perpetuals-sdk 0.0.1 → 0.0.2
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/accountTrade.d.ts +12 -2
- package/dist/accountTrade.js +24 -3
- package/dist/brokerTool.d.ts +83 -24
- package/dist/brokerTool.js +169 -54
- package/dist/d8XMath.d.ts +17 -14
- package/dist/d8XMath.js +17 -15
- package/dist/liquidatorTool.d.ts +38 -0
- package/dist/liquidatorTool.js +83 -0
- package/dist/liquidityProviderTool.d.ts +11 -11
- package/dist/liquidityProviderTool.js +11 -11
- package/dist/marketData.d.ts +20 -8
- package/dist/marketData.js +20 -8
- package/dist/orderReferrerTool.d.ts +25 -6
- package/dist/orderReferrerTool.js +94 -5
- package/dist/perpetualDataHandler.d.ts +2 -0
- package/dist/perpetualDataHandler.js +14 -0
- package/dist/utils.d.ts +8 -8
- package/dist/utils.js +11 -9
- package/package.json +2 -2
- package/src/accountTrade.ts +25 -23
- package/src/brokerTool.ts +214 -99
- package/src/d8XMath.ts +18 -15
- package/src/liquidatorTool.ts +71 -0
- package/src/liquidityProviderTool.ts +11 -11
- package/src/marketData.ts +139 -122
- package/src/orderReferrerTool.ts +108 -6
- package/src/perpetualDataHandler.ts +22 -0
- package/src/utils.ts +40 -39
- package/dist/liquiditatorTool.d.ts +0 -14
- package/dist/liquiditatorTool.js +0 -21
- package/src/liquiditatorTool.ts +0 -21
package/dist/accountTrade.d.ts
CHANGED
|
@@ -2,8 +2,8 @@ import { ethers } from "ethers";
|
|
|
2
2
|
import { NodeSDKConfig, Order, PerpetualStaticInfo } from "./nodeSDKTypes";
|
|
3
3
|
import WriteAccessHandler from "./writeAccessHandler";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* This class requires a private key and executes smart-contract
|
|
5
|
+
* Functions to create, submit and cancel orders on the exchange.
|
|
6
|
+
* This class requires a private key and executes smart-contract interactions that
|
|
7
7
|
* require gas-payments.
|
|
8
8
|
*/
|
|
9
9
|
export default class AccountTrade extends WriteAccessHandler {
|
|
@@ -25,6 +25,16 @@ export default class AccountTrade extends WriteAccessHandler {
|
|
|
25
25
|
* @returns {string} Transaction hash.
|
|
26
26
|
*/
|
|
27
27
|
order(order: Order): Promise<string | undefined>;
|
|
28
|
+
/**
|
|
29
|
+
* Fee charged by the exchange for trading any perpetual on a given pool.
|
|
30
|
+
* It accounts for the current trader's fee tier (based on the trader's D8X balance and trading volume).
|
|
31
|
+
* If trading with a broker, it also accounts for the selected broker's fee tier.
|
|
32
|
+
* Note that this result only includes exchange fees, additional broker fees are not included.
|
|
33
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
34
|
+
* @param {string=} brokerAddr Optional address of a broker this trader may use to trade under.
|
|
35
|
+
* @returns Exchange fee, in decimals (i.e. 0.1% is 0.001).
|
|
36
|
+
*/
|
|
37
|
+
queryExchangeFee(poolSymbolName: string, brokerAddr?: string): Promise<number>;
|
|
28
38
|
/**
|
|
29
39
|
* Static order function
|
|
30
40
|
* @param order order type (not SmartContractOrder but Order)
|
package/dist/accountTrade.js
CHANGED
|
@@ -15,10 +15,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
const ethers_1 = require("ethers");
|
|
16
16
|
const nodeSDKTypes_1 = require("./nodeSDKTypes");
|
|
17
17
|
const writeAccessHandler_1 = __importDefault(require("./writeAccessHandler"));
|
|
18
|
-
//import { abi, rawEncode } from "ethereumjs-abi";
|
|
19
18
|
/**
|
|
20
|
-
*
|
|
21
|
-
* This class requires a private key and executes smart-contract
|
|
19
|
+
* Functions to create, submit and cancel orders on the exchange.
|
|
20
|
+
* This class requires a private key and executes smart-contract interactions that
|
|
22
21
|
* require gas-payments.
|
|
23
22
|
*/
|
|
24
23
|
class AccountTrade extends writeAccessHandler_1.default {
|
|
@@ -67,6 +66,28 @@ class AccountTrade extends writeAccessHandler_1.default {
|
|
|
67
66
|
return yield this._order(order, this.traderAddr, this.symbolToPerpStaticInfo, this.proxyContract, orderBookContract, this.chainId, this.signer, this.gasLimit);
|
|
68
67
|
});
|
|
69
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Fee charged by the exchange for trading any perpetual on a given pool.
|
|
71
|
+
* It accounts for the current trader's fee tier (based on the trader's D8X balance and trading volume).
|
|
72
|
+
* If trading with a broker, it also accounts for the selected broker's fee tier.
|
|
73
|
+
* Note that this result only includes exchange fees, additional broker fees are not included.
|
|
74
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
75
|
+
* @param {string=} brokerAddr Optional address of a broker this trader may use to trade under.
|
|
76
|
+
* @returns Exchange fee, in decimals (i.e. 0.1% is 0.001).
|
|
77
|
+
*/
|
|
78
|
+
queryExchangeFee(poolSymbolName, brokerAddr) {
|
|
79
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
81
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
82
|
+
}
|
|
83
|
+
if (typeof brokerAddr == "undefined") {
|
|
84
|
+
brokerAddr = nodeSDKTypes_1.ZERO_ADDRESS;
|
|
85
|
+
}
|
|
86
|
+
let poolId = writeAccessHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
87
|
+
let feeTbps = yield this.proxyContract.queryExchangeFee(poolId, this.traderAddr, brokerAddr);
|
|
88
|
+
return feeTbps / 100000;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
70
91
|
/**
|
|
71
92
|
* Static order function
|
|
72
93
|
* @param order order type (not SmartContractOrder but Order)
|
package/dist/brokerTool.d.ts
CHANGED
|
@@ -1,41 +1,100 @@
|
|
|
1
1
|
import WriteAccessHandler from "./writeAccessHandler";
|
|
2
|
-
import { NodeSDKConfig, Order } from "./nodeSDKTypes";
|
|
3
|
-
import { ethers } from "ethers";
|
|
2
|
+
import { NodeSDKConfig, Order, PerpetualStaticInfo } from "./nodeSDKTypes";
|
|
3
|
+
import { BigNumber, ethers } from "ethers";
|
|
4
4
|
/**
|
|
5
|
-
*
|
|
6
|
-
* Signature method for brokers
|
|
5
|
+
* Functions for brokers to determine fees, deposit lots, and sign-up traders.
|
|
7
6
|
*/
|
|
8
7
|
export default class BrokerTool extends WriteAccessHandler {
|
|
9
8
|
/**
|
|
10
9
|
* Constructor
|
|
11
|
-
* @param config
|
|
12
|
-
* @param privateKey
|
|
10
|
+
* @param {NodeSDKConfig} config Configuration object.
|
|
11
|
+
* @param {string} privateKey Private key of a broker.
|
|
13
12
|
*/
|
|
14
13
|
constructor(config: NodeSDKConfig, privateKey: string);
|
|
14
|
+
getBrokerInducedFee(poolSymbolName: string): Promise<number>;
|
|
15
15
|
/**
|
|
16
|
-
*
|
|
17
|
-
*
|
|
18
|
-
* @
|
|
16
|
+
* Total amount of collateral currency a broker has to deposit into the default fund to purchase one lot.
|
|
17
|
+
* This is equivalent to the price of a lot expressed in a given pool's currency (e.g. MATIC, USDC, etc).
|
|
18
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
19
|
+
* @returns Broker lot size in a given pool's currency, e.g. in MATIC for poolSymbolName MATIC.
|
|
19
20
|
*/
|
|
20
|
-
getLotSize(
|
|
21
|
-
brokerDepositToDefaultFund(symbol: string, lots: number): Promise<ethers.providers.TransactionResponse>;
|
|
22
|
-
getFeeForBrokerVolume(symbol: string): Promise<number | undefined>;
|
|
21
|
+
getLotSize(poolSymbolName: string): Promise<number>;
|
|
23
22
|
/**
|
|
24
|
-
*
|
|
25
|
-
*
|
|
26
|
-
* @
|
|
23
|
+
* Provides information on how many lots a broker purchased for a given pool.
|
|
24
|
+
* This is relevant to determine the broker's fee tier.
|
|
25
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
26
|
+
* @returns Number of lots purchased by this broker.
|
|
27
27
|
*/
|
|
28
|
-
getBrokerDesignation(
|
|
28
|
+
getBrokerDesignation(poolSymbolName: string): Promise<number>;
|
|
29
29
|
/**
|
|
30
|
-
*
|
|
31
|
-
* @param
|
|
32
|
-
* @
|
|
30
|
+
* Deposit lots to the default fund of a given pool.
|
|
31
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
32
|
+
* @param lots Number of lots to deposit into this pool.
|
|
33
|
+
* @returns Transaction object.
|
|
33
34
|
*/
|
|
34
|
-
|
|
35
|
+
brokerDepositToDefaultFund(poolSymbolName: string, lots: number): Promise<ethers.providers.TransactionResponse>;
|
|
35
36
|
/**
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
37
|
+
* Determine the exchange fee based on lots purchased by this broker.
|
|
38
|
+
* The final exchange fee paid by the broker is equal to
|
|
39
|
+
* maximum(brokerTool.getFeeForBrokerDesignation(poolSymbolName), brokerTool.getFeeForBrokerVolume(poolSymbolName), brokerTool.getFeeForBrokerStake())
|
|
40
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
41
|
+
* @param {number} lots Optional, designation to use if different from this broker's.
|
|
42
|
+
* @returns Fee based solely on this broker's designation, in decimals (i.e. 0.1% is 0.001).
|
|
39
43
|
*/
|
|
40
|
-
|
|
44
|
+
getFeeForBrokerDesignation(poolSymbolName: string, lots?: number): Promise<number>;
|
|
45
|
+
/**
|
|
46
|
+
* Determine the exchange fee based on volume traded under this broker.
|
|
47
|
+
* The final exchange fee paid by the broker is equal to
|
|
48
|
+
* maximum(brokerTool.getFeeForBrokerDesignation(poolSymbolName), brokerTool.getFeeForBrokerVolume(poolSymbolName), brokerTool.getFeeForBrokerStake())
|
|
49
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
50
|
+
* @returns Fee based solely on a broker's traded volume in the corresponding pool, in decimals (i.e. 0.1% is 0.001).
|
|
51
|
+
*/
|
|
52
|
+
getFeeForBrokerVolume(poolSymbolName: string): Promise<number | undefined>;
|
|
53
|
+
/**
|
|
54
|
+
* Determine the exchange fee based on the current D8X balance in a broker's wallet.
|
|
55
|
+
* The final exchange fee paid by the broker is equal to
|
|
56
|
+
* maximum(brokerTool.getFeeForBrokerDesignation(symbol, lots), brokerTool.getFeeForBrokerVolume(symbol), brokerTool.getFeeForBrokerStake)
|
|
57
|
+
* @param {string=} brokerAddr Address of the broker in question, if different from the one calling this function.
|
|
58
|
+
* @returns Fee based solely on a broker's D8X balance, in decimals (i.e. 0.1% is 0.001).
|
|
59
|
+
*/
|
|
60
|
+
getFeeForBrokerStake(brokerAddr?: string): Promise<number>;
|
|
61
|
+
/**
|
|
62
|
+
* Determine exchange fee based on an order and a trader.
|
|
63
|
+
* This is the fee charged by the exchange only, excluding the broker fee,
|
|
64
|
+
* and it takes into account whether the order given here has been signed by a broker or not.
|
|
65
|
+
* Use this, for instance, to verify that the fee to be charged for a given order is as expected,
|
|
66
|
+
* before and after signing it with brokerTool.signOrder.
|
|
67
|
+
* @param {Order} order Order for which to determine the exchange fee. Not necessarily signed by this broker.
|
|
68
|
+
* @param {string} traderAddr Address of the trader for whom to determine the fee.
|
|
69
|
+
* @returns Fee in decimals (i.e. 0.1% is 0.001).
|
|
70
|
+
*/
|
|
71
|
+
determineExchangeFee(order: Order, traderAddr: string): Promise<number>;
|
|
72
|
+
/**
|
|
73
|
+
* Adds this broker's signature to an order so that it can be submitted by an approved trader.
|
|
74
|
+
* @param {Order} order Order to sign.
|
|
75
|
+
* @param {string} traderAddr Address of trader submitting the order.
|
|
76
|
+
* @param {number} feeDecimals Fee that this broker is approving for the trader.
|
|
77
|
+
* @param {number} deadline Deadline for the order to be executed.
|
|
78
|
+
* @returns An order signed by this broker, which can be submitted directly with AccountTrade.order.
|
|
79
|
+
*/
|
|
80
|
+
signOrder(order: Order, traderAddr: string, brokerFee: number, deadline: number): Promise<Order>;
|
|
81
|
+
/**
|
|
82
|
+
* Creates a signature that a trader can use to place orders with this broker.
|
|
83
|
+
* This signature can be used to pass on to a trader who wishes to trade via this SDK or directly on the blockchain.
|
|
84
|
+
* @param {string} traderAddr Address of the trader signing up with this broker.
|
|
85
|
+
* @param {string} symbol Perpetual that this trader will be trading, of the form ETH-USD-MATIC.
|
|
86
|
+
* @param {number} brokerFee Broker fee for this trader, in decimals (i.e. 0.1% is 0.001).
|
|
87
|
+
* @param {number} deadline Deadline for the order to be executed.
|
|
88
|
+
* @returns Broker signature approving this trader's fee, symbol, and deadline.
|
|
89
|
+
* @ignore
|
|
90
|
+
*/
|
|
91
|
+
createSignatureForTrader(traderAddr: string, symbol: string, brokerFee: number, deadline: number): Promise<string>;
|
|
92
|
+
static _signOrder(symbol: string, brokerFeeTbps: number, traderAddr: string, iDeadline: BigNumber, signer: ethers.Wallet, chainId: number, proxyAddress: string, symbolToPerpStaticInfo: Map<string, PerpetualStaticInfo>): Promise<string>;
|
|
93
|
+
/**
|
|
94
|
+
* Transfer ownership of a broker's status to a new wallet.
|
|
95
|
+
* @param poolSymbolName Symbol refers to the pool (e.g., MATIC for the MATIC-pool)
|
|
96
|
+
* @param newAddress The address this broker wants to use from now on.
|
|
97
|
+
* @returns ethers transaction object
|
|
98
|
+
*/
|
|
99
|
+
transferOwnership(poolSymbolName: string, newAddress: string): Promise<ethers.providers.TransactionResponse>;
|
|
41
100
|
}
|
package/dist/brokerTool.js
CHANGED
|
@@ -13,117 +13,232 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
13
13
|
};
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
const writeAccessHandler_1 = __importDefault(require("./writeAccessHandler"));
|
|
16
|
-
const nodeSDKTypes_1 = require("./nodeSDKTypes");
|
|
17
16
|
const perpetualDataHandler_1 = __importDefault(require("./perpetualDataHandler"));
|
|
18
17
|
const d8XMath_1 = require("./d8XMath");
|
|
18
|
+
const ethers_1 = require("ethers");
|
|
19
19
|
const accountTrade_1 = __importDefault(require("./accountTrade"));
|
|
20
20
|
/**
|
|
21
|
-
*
|
|
22
|
-
* Signature method for brokers
|
|
21
|
+
* Functions for brokers to determine fees, deposit lots, and sign-up traders.
|
|
23
22
|
*/
|
|
24
23
|
class BrokerTool extends writeAccessHandler_1.default {
|
|
25
24
|
/**
|
|
26
25
|
* Constructor
|
|
27
|
-
* @param config
|
|
28
|
-
* @param privateKey
|
|
26
|
+
* @param {NodeSDKConfig} config Configuration object.
|
|
27
|
+
* @param {string} privateKey Private key of a broker.
|
|
29
28
|
*/
|
|
30
29
|
constructor(config, privateKey) {
|
|
31
30
|
super(config, privateKey);
|
|
32
31
|
}
|
|
32
|
+
getBrokerInducedFee(poolSymbolName) {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
35
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
36
|
+
}
|
|
37
|
+
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
38
|
+
let feeTbps = yield this.proxyContract.getBrokerInducedFee(poolId, this.traderAddr);
|
|
39
|
+
return feeTbps / 100000;
|
|
40
|
+
});
|
|
41
|
+
}
|
|
33
42
|
/**
|
|
34
|
-
*
|
|
35
|
-
*
|
|
36
|
-
* @
|
|
43
|
+
* Total amount of collateral currency a broker has to deposit into the default fund to purchase one lot.
|
|
44
|
+
* This is equivalent to the price of a lot expressed in a given pool's currency (e.g. MATIC, USDC, etc).
|
|
45
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
46
|
+
* @returns Broker lot size in a given pool's currency, e.g. in MATIC for poolSymbolName MATIC.
|
|
37
47
|
*/
|
|
38
|
-
getLotSize(
|
|
48
|
+
getLotSize(poolSymbolName) {
|
|
39
49
|
return __awaiter(this, void 0, void 0, function* () {
|
|
40
50
|
if (this.proxyContract == null) {
|
|
41
51
|
throw Error("no proxy contract initialized. Use createProxyInstance().");
|
|
42
52
|
}
|
|
43
|
-
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(
|
|
53
|
+
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
44
54
|
let pool = yield this.proxyContract.getLiquidityPool(poolId);
|
|
45
|
-
let lot =
|
|
46
|
-
if (lot != undefined) {
|
|
47
|
-
lot = (0, d8XMath_1.ABK64x64ToFloat)(pool.fBrokerCollateralLotSize);
|
|
48
|
-
}
|
|
55
|
+
let lot = (0, d8XMath_1.ABK64x64ToFloat)(pool.fBrokerCollateralLotSize);
|
|
49
56
|
return lot;
|
|
50
57
|
});
|
|
51
58
|
}
|
|
52
|
-
|
|
59
|
+
/**
|
|
60
|
+
* Provides information on how many lots a broker purchased for a given pool.
|
|
61
|
+
* This is relevant to determine the broker's fee tier.
|
|
62
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
63
|
+
* @returns Number of lots purchased by this broker.
|
|
64
|
+
*/
|
|
65
|
+
getBrokerDesignation(poolSymbolName) {
|
|
53
66
|
return __awaiter(this, void 0, void 0, function* () {
|
|
54
67
|
if (this.proxyContract == null || this.signer == null) {
|
|
55
68
|
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
56
69
|
}
|
|
57
|
-
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(
|
|
70
|
+
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
71
|
+
let designation = yield this.proxyContract.getBrokerDesignation(poolId, this.traderAddr);
|
|
72
|
+
return designation;
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Deposit lots to the default fund of a given pool.
|
|
77
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
78
|
+
* @param lots Number of lots to deposit into this pool.
|
|
79
|
+
* @returns Transaction object.
|
|
80
|
+
*/
|
|
81
|
+
brokerDepositToDefaultFund(poolSymbolName, lots) {
|
|
82
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
83
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
84
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
85
|
+
}
|
|
86
|
+
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
58
87
|
let tx = yield this.proxyContract.brokerDepositToDefaultFund(poolId, lots, { gasLimit: this.gasLimit });
|
|
59
88
|
return tx;
|
|
60
89
|
});
|
|
61
90
|
}
|
|
62
|
-
|
|
91
|
+
/**
|
|
92
|
+
* Determine the exchange fee based on lots purchased by this broker.
|
|
93
|
+
* The final exchange fee paid by the broker is equal to
|
|
94
|
+
* maximum(brokerTool.getFeeForBrokerDesignation(poolSymbolName), brokerTool.getFeeForBrokerVolume(poolSymbolName), brokerTool.getFeeForBrokerStake())
|
|
95
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
96
|
+
* @param {number} lots Optional, designation to use if different from this broker's.
|
|
97
|
+
* @returns Fee based solely on this broker's designation, in decimals (i.e. 0.1% is 0.001).
|
|
98
|
+
*/
|
|
99
|
+
getFeeForBrokerDesignation(poolSymbolName, lots) {
|
|
63
100
|
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
-
if (this.proxyContract == null) {
|
|
65
|
-
throw Error("no proxy contract initialized.");
|
|
101
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
102
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
66
103
|
}
|
|
67
|
-
|
|
68
|
-
let
|
|
104
|
+
// check if designation should be taken from the caller or as a parameter
|
|
105
|
+
let brokerDesignation;
|
|
106
|
+
if (typeof lots == "undefined") {
|
|
107
|
+
brokerDesignation = yield this.getBrokerDesignation(poolSymbolName);
|
|
108
|
+
}
|
|
109
|
+
else {
|
|
110
|
+
brokerDesignation = lots;
|
|
111
|
+
}
|
|
112
|
+
let feeTbps = yield this.proxyContract.getFeeForBrokerDesignation(brokerDesignation);
|
|
69
113
|
return feeTbps / 100000;
|
|
70
114
|
});
|
|
71
115
|
}
|
|
72
116
|
/**
|
|
73
|
-
*
|
|
74
|
-
*
|
|
75
|
-
*
|
|
117
|
+
* Determine the exchange fee based on volume traded under this broker.
|
|
118
|
+
* The final exchange fee paid by the broker is equal to
|
|
119
|
+
* maximum(brokerTool.getFeeForBrokerDesignation(poolSymbolName), brokerTool.getFeeForBrokerVolume(poolSymbolName), brokerTool.getFeeForBrokerStake())
|
|
120
|
+
* @param {string} poolSymbolName Pool symbol name (e.g. MATIC, USDC, etc).
|
|
121
|
+
* @returns Fee based solely on a broker's traded volume in the corresponding pool, in decimals (i.e. 0.1% is 0.001).
|
|
76
122
|
*/
|
|
77
|
-
|
|
123
|
+
getFeeForBrokerVolume(poolSymbolName) {
|
|
78
124
|
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
-
if (this.proxyContract == null) {
|
|
80
|
-
throw Error("no proxy contract initialized.");
|
|
125
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
126
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
81
127
|
}
|
|
82
|
-
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(
|
|
83
|
-
let
|
|
84
|
-
return
|
|
128
|
+
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
129
|
+
let feeTbps = yield this.proxyContract.getFeeForBrokerVolume(poolId, this.traderAddr);
|
|
130
|
+
return feeTbps / 100000;
|
|
85
131
|
});
|
|
86
132
|
}
|
|
87
133
|
/**
|
|
88
|
-
*
|
|
89
|
-
*
|
|
90
|
-
*
|
|
134
|
+
* Determine the exchange fee based on the current D8X balance in a broker's wallet.
|
|
135
|
+
* The final exchange fee paid by the broker is equal to
|
|
136
|
+
* maximum(brokerTool.getFeeForBrokerDesignation(symbol, lots), brokerTool.getFeeForBrokerVolume(symbol), brokerTool.getFeeForBrokerStake)
|
|
137
|
+
* @param {string=} brokerAddr Address of the broker in question, if different from the one calling this function.
|
|
138
|
+
* @returns Fee based solely on a broker's D8X balance, in decimals (i.e. 0.1% is 0.001).
|
|
91
139
|
*/
|
|
92
|
-
|
|
140
|
+
getFeeForBrokerStake(brokerAddr) {
|
|
93
141
|
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
-
if (this.proxyContract == null) {
|
|
95
|
-
throw Error("no proxy contract initialized.");
|
|
142
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
143
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
96
144
|
}
|
|
97
|
-
if (
|
|
98
|
-
|
|
145
|
+
if (typeof brokerAddr == "undefined") {
|
|
146
|
+
brokerAddr = this.traderAddr;
|
|
99
147
|
}
|
|
100
|
-
let
|
|
101
|
-
lots += newLots;
|
|
102
|
-
let feeTbps = yield this.proxyContract.getFeeForBrokerDesignation(lots);
|
|
148
|
+
let feeTbps = yield this.proxyContract.getFeeForBrokerStake(brokerAddr);
|
|
103
149
|
return feeTbps / 100000;
|
|
104
150
|
});
|
|
105
151
|
}
|
|
106
152
|
/**
|
|
107
|
-
*
|
|
108
|
-
*
|
|
109
|
-
*
|
|
153
|
+
* Determine exchange fee based on an order and a trader.
|
|
154
|
+
* This is the fee charged by the exchange only, excluding the broker fee,
|
|
155
|
+
* and it takes into account whether the order given here has been signed by a broker or not.
|
|
156
|
+
* Use this, for instance, to verify that the fee to be charged for a given order is as expected,
|
|
157
|
+
* before and after signing it with brokerTool.signOrder.
|
|
158
|
+
* @param {Order} order Order for which to determine the exchange fee. Not necessarily signed by this broker.
|
|
159
|
+
* @param {string} traderAddr Address of the trader for whom to determine the fee.
|
|
160
|
+
* @returns Fee in decimals (i.e. 0.1% is 0.001).
|
|
110
161
|
*/
|
|
111
|
-
determineExchangeFee(order, traderAddr
|
|
162
|
+
determineExchangeFee(order, traderAddr) {
|
|
112
163
|
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;
|
|
164
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
165
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
122
166
|
}
|
|
123
167
|
let scOrder = accountTrade_1.default.toSmartContractOrder(order, traderAddr, this.symbolToPerpStaticInfo);
|
|
124
168
|
let feeTbps = yield this.proxyContract.determineExchangeFee(scOrder);
|
|
125
169
|
return feeTbps / 100000;
|
|
126
170
|
});
|
|
127
171
|
}
|
|
172
|
+
/**
|
|
173
|
+
* Adds this broker's signature to an order so that it can be submitted by an approved trader.
|
|
174
|
+
* @param {Order} order Order to sign.
|
|
175
|
+
* @param {string} traderAddr Address of trader submitting the order.
|
|
176
|
+
* @param {number} feeDecimals Fee that this broker is approving for the trader.
|
|
177
|
+
* @param {number} deadline Deadline for the order to be executed.
|
|
178
|
+
* @returns An order signed by this broker, which can be submitted directly with AccountTrade.order.
|
|
179
|
+
*/
|
|
180
|
+
signOrder(order, traderAddr, brokerFee, deadline) {
|
|
181
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
182
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
183
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
184
|
+
}
|
|
185
|
+
order.brokerAddr = this.traderAddr;
|
|
186
|
+
order.brokerFeeTbps = brokerFee * 100000;
|
|
187
|
+
order.deadline = deadline;
|
|
188
|
+
order.brokerSignature = yield BrokerTool._signOrder(order.symbol, order.brokerFeeTbps, traderAddr, ethers_1.BigNumber.from(deadline), this.signer, this.chainId, this.proxyAddr, this.symbolToPerpStaticInfo);
|
|
189
|
+
return order;
|
|
190
|
+
});
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Creates a signature that a trader can use to place orders with this broker.
|
|
194
|
+
* This signature can be used to pass on to a trader who wishes to trade via this SDK or directly on the blockchain.
|
|
195
|
+
* @param {string} traderAddr Address of the trader signing up with this broker.
|
|
196
|
+
* @param {string} symbol Perpetual that this trader will be trading, of the form ETH-USD-MATIC.
|
|
197
|
+
* @param {number} brokerFee Broker fee for this trader, in decimals (i.e. 0.1% is 0.001).
|
|
198
|
+
* @param {number} deadline Deadline for the order to be executed.
|
|
199
|
+
* @returns Broker signature approving this trader's fee, symbol, and deadline.
|
|
200
|
+
* @ignore
|
|
201
|
+
*/
|
|
202
|
+
createSignatureForTrader(traderAddr, symbol, brokerFee, deadline) {
|
|
203
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
204
|
+
if (this.proxyContract == null || this.signer == null) {
|
|
205
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
206
|
+
}
|
|
207
|
+
let iDeadline = ethers_1.BigNumber.from(deadline);
|
|
208
|
+
let brokerFeeTbps = 100000 * brokerFee;
|
|
209
|
+
return yield BrokerTool._signOrder(symbol, brokerFeeTbps, traderAddr, iDeadline, this.signer, this.chainId, this.proxyAddr, this.symbolToPerpStaticInfo);
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
static _signOrder(symbol, brokerFeeTbps, traderAddr, iDeadline, signer, chainId, proxyAddress, symbolToPerpStaticInfo) {
|
|
213
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
214
|
+
const NAME = "Perpetual Trade Manager";
|
|
215
|
+
const DOMAIN_TYPEHASH = ethers_1.ethers.utils.keccak256(Buffer.from("EIP712Domain(string name,uint256 chainId,address verifyingContract)"));
|
|
216
|
+
let abiCoder = ethers_1.ethers.utils.defaultAbiCoder;
|
|
217
|
+
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]));
|
|
218
|
+
//
|
|
219
|
+
const TRADE_BROKER_TYPEHASH = ethers_1.ethers.utils.keccak256(Buffer.from("Order(uint24 iPerpetualId,uint16 brokerFeeTbps,address traderAddr,uint256 iDeadline)"));
|
|
220
|
+
let iPerpetualId = perpetualDataHandler_1.default.symbolToPerpetualId(symbol, symbolToPerpStaticInfo);
|
|
221
|
+
let structHash = ethers_1.ethers.utils.keccak256(abiCoder.encode(["bytes32", "uint24", "uint16", "address", "uint256"], [TRADE_BROKER_TYPEHASH, iPerpetualId, brokerFeeTbps, traderAddr, iDeadline]));
|
|
222
|
+
let digest = ethers_1.ethers.utils.keccak256(abiCoder.encode(["bytes32", "bytes32"], [domainSeparator, structHash]));
|
|
223
|
+
let digestBuffer = Buffer.from(digest.substring(2, digest.length), "hex");
|
|
224
|
+
return yield signer.signMessage(digestBuffer);
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Transfer ownership of a broker's status to a new wallet.
|
|
229
|
+
* @param poolSymbolName Symbol refers to the pool (e.g., MATIC for the MATIC-pool)
|
|
230
|
+
* @param newAddress The address this broker wants to use from now on.
|
|
231
|
+
* @returns ethers transaction object
|
|
232
|
+
*/
|
|
233
|
+
transferOwnership(poolSymbolName, newAddress) {
|
|
234
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
235
|
+
if (this.proxyContract == null) {
|
|
236
|
+
throw Error("no proxy contract or wallet initialized. Use createProxyInstance().");
|
|
237
|
+
}
|
|
238
|
+
let poolId = perpetualDataHandler_1.default._getPoolIdFromSymbol(poolSymbolName, this.poolStaticInfos);
|
|
239
|
+
let tx = yield this.proxyContract.transferOwnership(poolId, newAddress);
|
|
240
|
+
return tx;
|
|
241
|
+
});
|
|
242
|
+
}
|
|
128
243
|
}
|
|
129
244
|
exports.default = BrokerTool;
|
package/dist/d8XMath.d.ts
CHANGED
|
@@ -1,41 +1,44 @@
|
|
|
1
1
|
import { BigNumber } from "ethers";
|
|
2
|
+
/**
|
|
3
|
+
* @module d8xMath
|
|
4
|
+
*/
|
|
2
5
|
/**
|
|
3
6
|
* Convert ABK64x64 bigint-format to float.
|
|
4
7
|
* Result = x/2^64 if big number, x/2^29 if number
|
|
5
|
-
* @param
|
|
6
|
-
* @returns x/2^64 in number-format (float)
|
|
8
|
+
* @param {BigNumber|number} x number in ABDK-format or 2^29
|
|
9
|
+
* @returns {number} x/2^64 in number-format (float)
|
|
7
10
|
*/
|
|
8
11
|
export declare function ABK64x64ToFloat(x: BigNumber | number): number;
|
|
9
12
|
/**
|
|
10
13
|
*
|
|
11
|
-
* @param x BigNumber in Dec18 format
|
|
12
|
-
* @returns x as a float (number)
|
|
14
|
+
* @param {BigNumber} x BigNumber in Dec18 format
|
|
15
|
+
* @returns {number} x as a float (number)
|
|
13
16
|
*/
|
|
14
17
|
export declare function dec18ToFloat(x: BigNumber): number;
|
|
15
18
|
/**
|
|
16
19
|
* Converts x into ABDK64x64 format
|
|
17
|
-
* @param
|
|
18
|
-
* @returns x^64 in big number format
|
|
20
|
+
* @param {number} x number (float)
|
|
21
|
+
* @returns {BigNumber} x^64 in big number format
|
|
19
22
|
*/
|
|
20
23
|
export declare function floatToABK64x64(x: number): BigNumber;
|
|
21
24
|
/**
|
|
22
25
|
*
|
|
23
|
-
* @param x number (float)
|
|
24
|
-
* @returns x as a BigNumber in Dec18 format
|
|
26
|
+
* @param {number} x number (float)
|
|
27
|
+
* @returns {BigNumber} x as a BigNumber in Dec18 format
|
|
25
28
|
*/
|
|
26
29
|
export declare function floatToDec18(x: number): BigNumber;
|
|
27
30
|
/**
|
|
28
31
|
*
|
|
29
|
-
* @param x
|
|
30
|
-
* @param y
|
|
31
|
-
* @returns x * y
|
|
32
|
+
* @param {BigNumber} x
|
|
33
|
+
* @param {BigNumber} y
|
|
34
|
+
* @returns {BigNumber} x * y
|
|
32
35
|
*/
|
|
33
36
|
export declare function mul64x64(x: BigNumber, y: BigNumber): BigNumber;
|
|
34
37
|
/**
|
|
35
38
|
*
|
|
36
|
-
* @param x
|
|
37
|
-
* @param y
|
|
38
|
-
* @returns x / y
|
|
39
|
+
* @param {BigNumber} x
|
|
40
|
+
* @param {BigNumber} y
|
|
41
|
+
* @returns {BigNumber} x / y
|
|
39
42
|
*/
|
|
40
43
|
export declare function div64x64(x: BigNumber, y: BigNumber): BigNumber;
|
|
41
44
|
/**
|
package/dist/d8XMath.js
CHANGED
|
@@ -3,12 +3,14 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.calculateLiquidationPriceCollateralQuote = exports.calculateLiquidationPriceCollateralQuanto = exports.calculateLiquidationPriceCollateralBase = exports.div64x64 = exports.mul64x64 = exports.floatToDec18 = exports.floatToABK64x64 = exports.dec18ToFloat = exports.ABK64x64ToFloat = void 0;
|
|
4
4
|
const ethers_1 = require("ethers");
|
|
5
5
|
const nodeSDKTypes_1 = require("./nodeSDKTypes");
|
|
6
|
-
|
|
6
|
+
/**
|
|
7
|
+
* @module d8xMath
|
|
8
|
+
*/
|
|
7
9
|
/**
|
|
8
10
|
* Convert ABK64x64 bigint-format to float.
|
|
9
11
|
* Result = x/2^64 if big number, x/2^29 if number
|
|
10
|
-
* @param
|
|
11
|
-
* @returns x/2^64 in number-format (float)
|
|
12
|
+
* @param {BigNumber|number} x number in ABDK-format or 2^29
|
|
13
|
+
* @returns {number} x/2^64 in number-format (float)
|
|
12
14
|
*/
|
|
13
15
|
function ABK64x64ToFloat(x) {
|
|
14
16
|
if (typeof x == "number") {
|
|
@@ -29,8 +31,8 @@ function ABK64x64ToFloat(x) {
|
|
|
29
31
|
exports.ABK64x64ToFloat = ABK64x64ToFloat;
|
|
30
32
|
/**
|
|
31
33
|
*
|
|
32
|
-
* @param x BigNumber in Dec18 format
|
|
33
|
-
* @returns x as a float (number)
|
|
34
|
+
* @param {BigNumber} x BigNumber in Dec18 format
|
|
35
|
+
* @returns {number} x as a float (number)
|
|
34
36
|
*/
|
|
35
37
|
function dec18ToFloat(x) {
|
|
36
38
|
//x: BigNumber in Dec18 format to float
|
|
@@ -46,8 +48,8 @@ function dec18ToFloat(x) {
|
|
|
46
48
|
exports.dec18ToFloat = dec18ToFloat;
|
|
47
49
|
/**
|
|
48
50
|
* Converts x into ABDK64x64 format
|
|
49
|
-
* @param
|
|
50
|
-
* @returns x^64 in big number format
|
|
51
|
+
* @param {number} x number (float)
|
|
52
|
+
* @returns {BigNumber} x^64 in big number format
|
|
51
53
|
*/
|
|
52
54
|
function floatToABK64x64(x) {
|
|
53
55
|
// convert float to ABK64x64 bigint-format
|
|
@@ -69,8 +71,8 @@ function floatToABK64x64(x) {
|
|
|
69
71
|
exports.floatToABK64x64 = floatToABK64x64;
|
|
70
72
|
/**
|
|
71
73
|
*
|
|
72
|
-
* @param x number (float)
|
|
73
|
-
* @returns x as a BigNumber in Dec18 format
|
|
74
|
+
* @param {number} x number (float)
|
|
75
|
+
* @returns {BigNumber} x as a BigNumber in Dec18 format
|
|
74
76
|
*/
|
|
75
77
|
function floatToDec18(x) {
|
|
76
78
|
// float number to dec 18
|
|
@@ -89,9 +91,9 @@ function floatToDec18(x) {
|
|
|
89
91
|
exports.floatToDec18 = floatToDec18;
|
|
90
92
|
/**
|
|
91
93
|
*
|
|
92
|
-
* @param x
|
|
93
|
-
* @param y
|
|
94
|
-
* @returns x * y
|
|
94
|
+
* @param {BigNumber} x
|
|
95
|
+
* @param {BigNumber} y
|
|
96
|
+
* @returns {BigNumber} x * y
|
|
95
97
|
*/
|
|
96
98
|
function mul64x64(x, y) {
|
|
97
99
|
return x.mul(y).div(nodeSDKTypes_1.ONE_64x64);
|
|
@@ -99,9 +101,9 @@ function mul64x64(x, y) {
|
|
|
99
101
|
exports.mul64x64 = mul64x64;
|
|
100
102
|
/**
|
|
101
103
|
*
|
|
102
|
-
* @param x
|
|
103
|
-
* @param y
|
|
104
|
-
* @returns x / y
|
|
104
|
+
* @param {BigNumber} x
|
|
105
|
+
* @param {BigNumber} y
|
|
106
|
+
* @returns {BigNumber} x / y
|
|
105
107
|
*/
|
|
106
108
|
function div64x64(x, y) {
|
|
107
109
|
return x.mul(nodeSDKTypes_1.ONE_64x64).div(y);
|