@reyaxyz/api-sdk 0.157.0 → 0.158.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/clients/api-client.js +0 -60
- package/dist/clients/api-client.js.map +1 -1
- package/dist/clients/modules/account/index.js +0 -89
- package/dist/clients/modules/account/index.js.map +1 -1
- package/dist/clients/modules/markets/index.js +0 -15
- package/dist/clients/modules/markets/index.js.map +1 -1
- package/dist/clients/modules/owner/index.js +0 -36
- package/dist/clients/modules/owner/index.js.map +1 -1
- package/dist/clients/types.js +0 -3
- package/dist/clients/types.js.map +1 -1
- package/dist/types/clients/api-client.d.ts +0 -42
- package/dist/types/clients/api-client.d.ts.map +1 -1
- package/dist/types/clients/modules/account/index.d.ts +2 -10
- package/dist/types/clients/modules/account/index.d.ts.map +1 -1
- package/dist/types/clients/modules/markets/index.d.ts +1 -2
- package/dist/types/clients/modules/markets/index.d.ts.map +1 -1
- package/dist/types/clients/modules/owner/index.d.ts +1 -5
- package/dist/types/clients/modules/owner/index.d.ts.map +1 -1
- package/dist/types/clients/socket-client.d.ts +1 -1
- package/dist/types/clients/types.d.ts +0 -3
- package/dist/types/clients/types.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/clients/api-client.ts +0 -58
- package/src/clients/modules/account/index.ts +1 -93
- package/src/clients/modules/markets/index.ts +1 -12
- package/src/clients/modules/owner/index.ts +0 -36
- package/src/clients/types.ts +0 -3
- package/dist/clients/modules/isolated-order.simulation/index.js +0 -302
- package/dist/clients/modules/isolated-order.simulation/index.js.map +0 -1
- package/dist/clients/modules/isolated-order.simulation/types.js +0 -3
- package/dist/clients/modules/isolated-order.simulation/types.js.map +0 -1
- package/dist/clients/modules/rage-trade/index.js +0 -99
- package/dist/clients/modules/rage-trade/index.js.map +0 -1
- package/dist/clients/modules/rage-trade/types.js +0 -3
- package/dist/clients/modules/rage-trade/types.js.map +0 -1
- package/dist/clients/modules/trade.simulation/index.js +0 -331
- package/dist/clients/modules/trade.simulation/index.js.map +0 -1
- package/dist/clients/modules/trade.simulation/types.js +0 -3
- package/dist/clients/modules/trade.simulation/types.js.map +0 -1
- package/dist/clients/modules/trade.simulation/utils.js +0 -35
- package/dist/clients/modules/trade.simulation/utils.js.map +0 -1
- package/dist/types/clients/modules/isolated-order.simulation/index.d.ts +0 -24
- package/dist/types/clients/modules/isolated-order.simulation/index.d.ts.map +0 -1
- package/dist/types/clients/modules/isolated-order.simulation/types.d.ts +0 -44
- package/dist/types/clients/modules/isolated-order.simulation/types.d.ts.map +0 -1
- package/dist/types/clients/modules/rage-trade/index.d.ts +0 -9
- package/dist/types/clients/modules/rage-trade/index.d.ts.map +0 -1
- package/dist/types/clients/modules/rage-trade/types.d.ts +0 -80
- package/dist/types/clients/modules/rage-trade/types.d.ts.map +0 -1
- package/dist/types/clients/modules/trade.simulation/index.d.ts +0 -22
- package/dist/types/clients/modules/trade.simulation/index.d.ts.map +0 -1
- package/dist/types/clients/modules/trade.simulation/types.d.ts +0 -85
- package/dist/types/clients/modules/trade.simulation/types.d.ts.map +0 -1
- package/dist/types/clients/modules/trade.simulation/utils.d.ts +0 -8
- package/dist/types/clients/modules/trade.simulation/utils.d.ts.map +0 -1
- package/src/clients/modules/isolated-order.simulation/index.ts +0 -490
- package/src/clients/modules/isolated-order.simulation/types.ts +0 -55
- package/src/clients/modules/rage-trade/index.ts +0 -38
- package/src/clients/modules/rage-trade/types.ts +0 -91
- package/src/clients/modules/trade.simulation/index.ts +0 -534
- package/src/clients/modules/trade.simulation/types.ts +0 -98
- package/src/clients/modules/trade.simulation/utils.ts +0 -52
|
@@ -1,38 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
GetRageTradeDailyVolumeForPeriodParams,
|
|
3
|
-
GetRageTradeDailyVolumeForPeriodResult,
|
|
4
|
-
GetRageTradeLeaderboardForEpochAndTierResult,
|
|
5
|
-
GetRageTradeLeaderboardForWalletAddressParams,
|
|
6
|
-
GetRageTradeLeaderboardForWalletAddressResult,
|
|
7
|
-
GetRageTradeRewardsPerEpochAndTierParams,
|
|
8
|
-
GetRageTradeRewardsPerEpochAndTierResult,
|
|
9
|
-
} from './types';
|
|
10
|
-
import { RestClient } from '@reyaxyz/common';
|
|
11
|
-
|
|
12
|
-
export default class RageTradeClient extends RestClient {
|
|
13
|
-
async getRewardsPerEpochAndTier(): Promise<GetRageTradeRewardsPerEpochAndTierResult> {
|
|
14
|
-
const uri = `/api/rage-trade/rewards-per-epoch-and-tier`;
|
|
15
|
-
return this.get(uri);
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
async getLeaderboardForEpochAndTier(
|
|
19
|
-
params: GetRageTradeRewardsPerEpochAndTierParams,
|
|
20
|
-
): Promise<GetRageTradeLeaderboardForEpochAndTierResult> {
|
|
21
|
-
const uri = `/api/rage-trade/leaderboard-for-epoch-and-tier/${params.walletAddress}?epochId=${params.epochId}&tierId=${params.tierId}&skip=${params.skip}&limit=${params.limit}`;
|
|
22
|
-
return this.get(uri);
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
async getLeaderboardDetailsForWalletAddress(
|
|
26
|
-
params: GetRageTradeLeaderboardForWalletAddressParams,
|
|
27
|
-
): Promise<GetRageTradeLeaderboardForWalletAddressResult> {
|
|
28
|
-
const uri = `/api/rage-trade/leaderboard/${params.walletAddress}`;
|
|
29
|
-
return this.get(uri);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
async getDailyVolumeForPeriod(
|
|
33
|
-
params: GetRageTradeDailyVolumeForPeriodParams,
|
|
34
|
-
): Promise<GetRageTradeDailyVolumeForPeriodResult> {
|
|
35
|
-
const uri = `/api/rage-trade/daily-volume-for-period?timestampFrom=${params.timestampFrom}×tampTo=${params.timestampTo}`;
|
|
36
|
-
return this.get(uri);
|
|
37
|
-
}
|
|
38
|
-
}
|
|
@@ -1,91 +0,0 @@
|
|
|
1
|
-
export type GetRageTradeRewardsPerEpochAndTierResult = {
|
|
2
|
-
rewards: RageTradeRewards[];
|
|
3
|
-
collateralTierLimits: RageTradeCollateralTierLimits;
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
export type RageTradeCollateralTierLimits = {
|
|
7
|
-
'1': number;
|
|
8
|
-
'2': number;
|
|
9
|
-
'3': number;
|
|
10
|
-
'4': number;
|
|
11
|
-
};
|
|
12
|
-
|
|
13
|
-
export type RageTradeRewards = {
|
|
14
|
-
epochId: number;
|
|
15
|
-
startTimeStamp: number;
|
|
16
|
-
endTimeStamp: number;
|
|
17
|
-
competitionName: string;
|
|
18
|
-
rewards: RageTradeReward[];
|
|
19
|
-
};
|
|
20
|
-
|
|
21
|
-
export type RageTradeReward = {
|
|
22
|
-
tierId: number;
|
|
23
|
-
rewardsPerRank: RageTradeRewardsPerRank;
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
export type RageTradeRewardsPerRank = {
|
|
27
|
-
'1': number;
|
|
28
|
-
'2': number;
|
|
29
|
-
'3': number;
|
|
30
|
-
'4': number;
|
|
31
|
-
'5': number;
|
|
32
|
-
'6': number;
|
|
33
|
-
'7': number;
|
|
34
|
-
'8': number;
|
|
35
|
-
'9': number;
|
|
36
|
-
'10': number;
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export type GetRageTradeLeaderboardForEpochAndTierResult = {
|
|
40
|
-
totalCount: number;
|
|
41
|
-
leaderboard: RageTradeLeaderboard[];
|
|
42
|
-
maxCollateralAverage: number;
|
|
43
|
-
};
|
|
44
|
-
|
|
45
|
-
export type RageTradeLeaderboard = {
|
|
46
|
-
epochId: number;
|
|
47
|
-
tierId: number;
|
|
48
|
-
rank: number;
|
|
49
|
-
address: string;
|
|
50
|
-
maxCollateral: number;
|
|
51
|
-
pnl: number;
|
|
52
|
-
roi: number;
|
|
53
|
-
};
|
|
54
|
-
|
|
55
|
-
export type GetRageTradeRewardsPerEpochAndTierParams = {
|
|
56
|
-
epochId: string;
|
|
57
|
-
tierId: string;
|
|
58
|
-
skip: number;
|
|
59
|
-
limit: number;
|
|
60
|
-
walletAddress: string;
|
|
61
|
-
};
|
|
62
|
-
|
|
63
|
-
export type GetRageTradeLeaderboardForWalletAddressParams = {
|
|
64
|
-
walletAddress: string;
|
|
65
|
-
};
|
|
66
|
-
|
|
67
|
-
export type GetRageTradeLeaderboardForWalletAddressResult = {
|
|
68
|
-
tierId: number;
|
|
69
|
-
address: string;
|
|
70
|
-
rank: number;
|
|
71
|
-
points: number;
|
|
72
|
-
pnl: number;
|
|
73
|
-
roi: number;
|
|
74
|
-
maxCollateral: number;
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
export type GetRageTradeDailyVolumeForPeriodParams = {
|
|
78
|
-
timestampFrom: number;
|
|
79
|
-
timestampTo: number;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
export type GetRageTradeDailyVolumeForPeriodResult = {
|
|
83
|
-
total: number;
|
|
84
|
-
data: RageTradeDailyVolumeForPeriodData[];
|
|
85
|
-
};
|
|
86
|
-
|
|
87
|
-
export type RageTradeDailyVolumeForPeriodData = {
|
|
88
|
-
dateStartTimestamp: number;
|
|
89
|
-
dateEndTimestamp: number;
|
|
90
|
-
total_volume: number;
|
|
91
|
-
};
|
|
@@ -1,534 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
EstimatedPriceParams,
|
|
3
|
-
EstimatedPriceResult,
|
|
4
|
-
LimitTradeMaxOrderSizeParams,
|
|
5
|
-
LimitTradeMaxOrderSizeResult,
|
|
6
|
-
SimulateLimitTradeEntity,
|
|
7
|
-
SimulateTradeEntity,
|
|
8
|
-
TradeSimulationConvertValueEstimatedPriceParams,
|
|
9
|
-
TradeSimulationConvertValueParams,
|
|
10
|
-
TradeSimulationConvertValueResult,
|
|
11
|
-
TradeSimulationLoadDataParams,
|
|
12
|
-
TradeSimulationSimulateLimitParams,
|
|
13
|
-
TradeSimulationSimulateParams,
|
|
14
|
-
} from './types';
|
|
15
|
-
import AccountClient from '../account';
|
|
16
|
-
import {
|
|
17
|
-
amountNormalizer,
|
|
18
|
-
ExposureCommand,
|
|
19
|
-
MarginInfo,
|
|
20
|
-
PositionInfo,
|
|
21
|
-
TradeSimulationState,
|
|
22
|
-
} from '@reyaxyz/common';
|
|
23
|
-
import BigNumber from 'bignumber.js';
|
|
24
|
-
import { INSTANT_TRADING_RATE_XP } from '@reyaxyz/common';
|
|
25
|
-
import { computeFee } from './utils';
|
|
26
|
-
|
|
27
|
-
export default class TradeSimulationClient {
|
|
28
|
-
private marketId: number | null = null;
|
|
29
|
-
private accountId: number | null = null;
|
|
30
|
-
private loadedData: {
|
|
31
|
-
tradeSimulationState: TradeSimulationState;
|
|
32
|
-
userAccountExposure: ExposureCommand;
|
|
33
|
-
passivePoolExposure: ExposureCommand;
|
|
34
|
-
} | null = null;
|
|
35
|
-
private accountClient: AccountClient;
|
|
36
|
-
|
|
37
|
-
constructor(accountClient: AccountClient) {
|
|
38
|
-
// Constructor added
|
|
39
|
-
this.accountClient = accountClient;
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
// Method to asynchronously load data based on marketId and accountId
|
|
43
|
-
async arm(params: TradeSimulationLoadDataParams): Promise<void> {
|
|
44
|
-
this.marketId = params.marketId;
|
|
45
|
-
this.accountId = params.marginAccountId;
|
|
46
|
-
|
|
47
|
-
const tradeSimulationState = await this.fetchMarketData(
|
|
48
|
-
this.marketId,
|
|
49
|
-
this.accountId,
|
|
50
|
-
);
|
|
51
|
-
|
|
52
|
-
const userAccountExposure = new ExposureCommand(
|
|
53
|
-
tradeSimulationState.exposureDataAccount.accountId,
|
|
54
|
-
tradeSimulationState.exposureDataAccount.rootCollateralPoolId,
|
|
55
|
-
tradeSimulationState.exposureDataAccount.oraclePricePerMarket,
|
|
56
|
-
tradeSimulationState.exposureDataAccount.accountBalancePerAsset,
|
|
57
|
-
tradeSimulationState.exposureDataAccount.groupedByCollateral,
|
|
58
|
-
tradeSimulationState.exposureDataAccount.riskMultipliers,
|
|
59
|
-
tradeSimulationState.exposureDataAccount.riskMatrices,
|
|
60
|
-
tradeSimulationState.exposureDataAccount.exchangeInfoPerAsset,
|
|
61
|
-
tradeSimulationState.exposureDataAccount.positionInfoMarketConfiguration,
|
|
62
|
-
tradeSimulationState.exposureDataAccount.uniqueTokenAddresses,
|
|
63
|
-
tradeSimulationState.exposureDataAccount.uniqueQuoteCollaterals,
|
|
64
|
-
tradeSimulationState.exposureDataAccount.tokenMarginInfoPerAsset,
|
|
65
|
-
tradeSimulationState.exposureDataAccount.realizedPnLSum,
|
|
66
|
-
tradeSimulationState.exposureDataAccount.unrealizedPnLSum,
|
|
67
|
-
tradeSimulationState.exposureDataAccount.mtmRpnlSum,
|
|
68
|
-
tradeSimulationState.exposureDataAccount.collateralAddressToExchangePrice,
|
|
69
|
-
);
|
|
70
|
-
|
|
71
|
-
const passivePoolExposure = new ExposureCommand(
|
|
72
|
-
tradeSimulationState.exposureDataPassivePool.accountId,
|
|
73
|
-
tradeSimulationState.exposureDataPassivePool.rootCollateralPoolId,
|
|
74
|
-
tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket,
|
|
75
|
-
tradeSimulationState.exposureDataPassivePool.accountBalancePerAsset,
|
|
76
|
-
tradeSimulationState.exposureDataPassivePool.groupedByCollateral,
|
|
77
|
-
tradeSimulationState.exposureDataPassivePool.riskMultipliers,
|
|
78
|
-
tradeSimulationState.exposureDataPassivePool.riskMatrices,
|
|
79
|
-
tradeSimulationState.exposureDataPassivePool.exchangeInfoPerAsset,
|
|
80
|
-
tradeSimulationState.exposureDataPassivePool.positionInfoMarketConfiguration,
|
|
81
|
-
tradeSimulationState.exposureDataPassivePool.uniqueTokenAddresses,
|
|
82
|
-
tradeSimulationState.exposureDataPassivePool.uniqueQuoteCollaterals,
|
|
83
|
-
tradeSimulationState.exposureDataPassivePool.tokenMarginInfoPerAsset,
|
|
84
|
-
tradeSimulationState.exposureDataPassivePool.realizedPnLSum,
|
|
85
|
-
tradeSimulationState.exposureDataPassivePool.unrealizedPnLSum,
|
|
86
|
-
tradeSimulationState.exposureDataPassivePool.mtmRpnlSum,
|
|
87
|
-
tradeSimulationState.exposureDataPassivePool.collateralAddressToExchangePrice,
|
|
88
|
-
);
|
|
89
|
-
|
|
90
|
-
this.loadedData = {
|
|
91
|
-
tradeSimulationState,
|
|
92
|
-
userAccountExposure,
|
|
93
|
-
passivePoolExposure,
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
private async fetchMarketData(
|
|
98
|
-
marketId: number,
|
|
99
|
-
accountId: number,
|
|
100
|
-
): Promise<TradeSimulationState> {
|
|
101
|
-
return this.accountClient.getTransactionSimulationInitialData({
|
|
102
|
-
marginAccountId: accountId,
|
|
103
|
-
marketId: marketId,
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
// Synchronous method to simulate operations based on an amount
|
|
108
|
-
simulate(params: TradeSimulationSimulateParams): SimulateTradeEntity {
|
|
109
|
-
if (!this.loadedData || !this.marketId) {
|
|
110
|
-
throw new Error('Data not loaded. Call arm() first.');
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const { tradeSimulationState, userAccountExposure, passivePoolExposure } =
|
|
114
|
-
this.loadedData;
|
|
115
|
-
|
|
116
|
-
const oraclePrice =
|
|
117
|
-
tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
|
|
118
|
-
tradeSimulationState.marketConfiguration.market_id
|
|
119
|
-
];
|
|
120
|
-
|
|
121
|
-
let amount;
|
|
122
|
-
if (params.fromBase) {
|
|
123
|
-
amount = params.amount;
|
|
124
|
-
} else {
|
|
125
|
-
amount = BigNumber(params.amount).div(oraclePrice).toNumber();
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
/*
|
|
129
|
-
max amount of margin in rUSD terms that can be transferred from the source account to the destination account
|
|
130
|
-
that performs the isolated position trade (PRE TRADE)
|
|
131
|
-
*/
|
|
132
|
-
|
|
133
|
-
const availableMargin =
|
|
134
|
-
userAccountExposure.getUsdNodeMarginInfo.initialDelta;
|
|
135
|
-
|
|
136
|
-
const marginBalance =
|
|
137
|
-
userAccountExposure.getUsdNodeMarginInfo.marginBalance;
|
|
138
|
-
|
|
139
|
-
const slippage = passivePoolExposure.getSlippage(
|
|
140
|
-
BigNumber(amount).negated().toNumber(),
|
|
141
|
-
tradeSimulationState.marketConfiguration,
|
|
142
|
-
tradeSimulationState.marketStorage,
|
|
143
|
-
);
|
|
144
|
-
|
|
145
|
-
const estimatedPrice = ExposureCommand.calculateEstimatedPrice(
|
|
146
|
-
oraclePrice,
|
|
147
|
-
slippage,
|
|
148
|
-
);
|
|
149
|
-
|
|
150
|
-
const feesV2 = computeFee({
|
|
151
|
-
tradeSimulationState,
|
|
152
|
-
marketId: this.marketId,
|
|
153
|
-
orderBase: amount,
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
const {
|
|
157
|
-
usdNodeMarginInfo: newMarginInfo,
|
|
158
|
-
tokenMarginInfoPerAsset,
|
|
159
|
-
positionInfoMarketConfiguration,
|
|
160
|
-
} = userAccountExposure.getUsdNodeMarginInfoPostTrade(
|
|
161
|
-
amount,
|
|
162
|
-
tradeSimulationState.marketStorage.quote_collateral,
|
|
163
|
-
tradeSimulationState.marketConfiguration,
|
|
164
|
-
tradeSimulationState.marketStorage.risk_block_id,
|
|
165
|
-
);
|
|
166
|
-
|
|
167
|
-
const newQuoteTokenMarginInfo = tokenMarginInfoPerAsset.find(
|
|
168
|
-
(marginInfo: MarginInfo) => {
|
|
169
|
-
return (
|
|
170
|
-
marginInfo.assetAddress ===
|
|
171
|
-
tradeSimulationState.marketStorage.quote_collateral
|
|
172
|
-
);
|
|
173
|
-
},
|
|
174
|
-
);
|
|
175
|
-
|
|
176
|
-
const positionInfo = positionInfoMarketConfiguration.find(
|
|
177
|
-
(positionInfo: PositionInfo) => {
|
|
178
|
-
return (
|
|
179
|
-
positionInfo.market_id ===
|
|
180
|
-
tradeSimulationState.marketConfiguration.market_id
|
|
181
|
-
);
|
|
182
|
-
},
|
|
183
|
-
);
|
|
184
|
-
|
|
185
|
-
if (!newQuoteTokenMarginInfo) {
|
|
186
|
-
throw new Error('Error performing simulation');
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
/*
|
|
190
|
-
* Note, required margin is the initial margin requirement in rUSD terms of the account after the trade.
|
|
191
|
-
* margin balance rusd - initial delta rusd = margin balance rusd - (margin balance rusd - imr rusd) = imr rusd
|
|
192
|
-
* */
|
|
193
|
-
|
|
194
|
-
const requiredMargin =
|
|
195
|
-
newQuoteTokenMarginInfo.marginBalance -
|
|
196
|
-
newQuoteTokenMarginInfo.initialDelta;
|
|
197
|
-
|
|
198
|
-
const liquidationPrice = ExposureCommand.calculateLiquidation(
|
|
199
|
-
newMarginInfo.marginBalance,
|
|
200
|
-
newQuoteTokenMarginInfo.liquidationMarginRequirement,
|
|
201
|
-
oraclePrice,
|
|
202
|
-
BigNumber(positionInfo?.base || 0).toNumber(),
|
|
203
|
-
);
|
|
204
|
-
|
|
205
|
-
const marginRatio = ExposureCommand.getMarginRatio(newMarginInfo);
|
|
206
|
-
|
|
207
|
-
const marginRatioHealth = ExposureCommand.evaluateHealthStatus(
|
|
208
|
-
marginRatio * 100,
|
|
209
|
-
);
|
|
210
|
-
|
|
211
|
-
const baseSpacing = amountNormalizer(
|
|
212
|
-
tradeSimulationState.marketConfiguration.base_spacing,
|
|
213
|
-
).toNumber();
|
|
214
|
-
|
|
215
|
-
const snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);
|
|
216
|
-
const snappedAmount = snappedAmountInBase * estimatedPrice;
|
|
217
|
-
const xpEarnRangeMin = Math.round(
|
|
218
|
-
Math.abs(snappedAmount) / INSTANT_TRADING_RATE_XP,
|
|
219
|
-
);
|
|
220
|
-
const xpEarnRangeMax = Math.round(
|
|
221
|
-
(100 * Math.abs(snappedAmount)) / INSTANT_TRADING_RATE_XP,
|
|
222
|
-
);
|
|
223
|
-
|
|
224
|
-
return {
|
|
225
|
-
estimatedPrice,
|
|
226
|
-
estimatedSlippage: slippage * 100,
|
|
227
|
-
fees: feesV2.fee,
|
|
228
|
-
takerFeesPercentage: feesV2.feePercentage,
|
|
229
|
-
feesV2,
|
|
230
|
-
liquidationPrice: liquidationPrice.toNumber(),
|
|
231
|
-
marginRatio: marginRatio * 100,
|
|
232
|
-
marginRatioHealth,
|
|
233
|
-
marginBalance,
|
|
234
|
-
availableMargin,
|
|
235
|
-
requiredMargin,
|
|
236
|
-
snappedAmount,
|
|
237
|
-
snappedAmountInBase,
|
|
238
|
-
xpEarnRange: {
|
|
239
|
-
min: xpEarnRangeMin,
|
|
240
|
-
max: xpEarnRangeMax,
|
|
241
|
-
},
|
|
242
|
-
maxSlippage: 1,
|
|
243
|
-
};
|
|
244
|
-
}
|
|
245
|
-
|
|
246
|
-
getRiskMatrixElement(): number {
|
|
247
|
-
if (!this.loadedData) {
|
|
248
|
-
throw new Error('Data not loaded. Call arm() first.');
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
const { tradeSimulationState } = this.loadedData;
|
|
252
|
-
|
|
253
|
-
const riskBlockId = tradeSimulationState.marketStorage.risk_block_id;
|
|
254
|
-
const riskMatrices = tradeSimulationState.exposureDataAccount.riskMatrices;
|
|
255
|
-
|
|
256
|
-
const riskMatrix = riskMatrices.find(
|
|
257
|
-
(riskMatrix) => riskMatrix.risk_block_id === riskBlockId,
|
|
258
|
-
);
|
|
259
|
-
|
|
260
|
-
if (!riskMatrix) {
|
|
261
|
-
throw new Error('Risk matrix not found');
|
|
262
|
-
}
|
|
263
|
-
|
|
264
|
-
const riskMatrixIndex =
|
|
265
|
-
tradeSimulationState.marketConfiguration.risk_matrix_index;
|
|
266
|
-
|
|
267
|
-
return Number(riskMatrix.matrix[riskMatrixIndex][riskMatrixIndex]);
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
// Synchronous method to simulate operations based on an amount
|
|
271
|
-
simulateLimit(
|
|
272
|
-
params: TradeSimulationSimulateLimitParams,
|
|
273
|
-
): SimulateLimitTradeEntity {
|
|
274
|
-
if (!this.loadedData) {
|
|
275
|
-
throw new Error('Data not loaded. Call arm() first.');
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
const { tradeSimulationState, userAccountExposure } = this.loadedData;
|
|
279
|
-
|
|
280
|
-
let amount;
|
|
281
|
-
if (params.fromBase) {
|
|
282
|
-
amount = params.amount;
|
|
283
|
-
} else {
|
|
284
|
-
amount = BigNumber(params.amount).div(params.triggerPrice).toNumber();
|
|
285
|
-
}
|
|
286
|
-
|
|
287
|
-
const baseSpacing = amountNormalizer(
|
|
288
|
-
tradeSimulationState.marketConfiguration.base_spacing,
|
|
289
|
-
).toNumber();
|
|
290
|
-
|
|
291
|
-
const snappedAmountInBase = this.roundToBaseSpacing(amount, baseSpacing);
|
|
292
|
-
const snappedAmount = snappedAmountInBase * params.triggerPrice;
|
|
293
|
-
|
|
294
|
-
const { fee: fees } = ExposureCommand.calculateFee({
|
|
295
|
-
oraclePrice: params.triggerPrice,
|
|
296
|
-
orderBase: amount,
|
|
297
|
-
poolBasePreTrade: 0,
|
|
298
|
-
feeParameter: tradeSimulationState.feeParameter,
|
|
299
|
-
rebalancingFeeParameter: BigNumber(0),
|
|
300
|
-
});
|
|
301
|
-
|
|
302
|
-
const marginBalance =
|
|
303
|
-
userAccountExposure.getUsdNodeMarginInfo.marginBalance;
|
|
304
|
-
|
|
305
|
-
const availableMargin =
|
|
306
|
-
userAccountExposure.getUsdNodeMarginInfo.initialDelta;
|
|
307
|
-
|
|
308
|
-
const imrMultiplier = amountNormalizer(
|
|
309
|
-
String(userAccountExposure.riskMultipliers.im_multiplier),
|
|
310
|
-
).toNumber();
|
|
311
|
-
|
|
312
|
-
const riskMatrixElement = this.getRiskMatrixElement();
|
|
313
|
-
|
|
314
|
-
const accountLMR =
|
|
315
|
-
userAccountExposure.getUsdNodeMarginInfo.liquidationMarginRequirement;
|
|
316
|
-
const accountIMR = accountLMR * imrMultiplier;
|
|
317
|
-
|
|
318
|
-
const limitOrderLMR =
|
|
319
|
-
Math.sqrt(riskMatrixElement) * Math.abs(snappedAmount);
|
|
320
|
-
const limitOrderIMR = limitOrderLMR * imrMultiplier;
|
|
321
|
-
|
|
322
|
-
const requiredMargin = accountIMR + limitOrderIMR;
|
|
323
|
-
|
|
324
|
-
const liquidationMarginRequirement =
|
|
325
|
-
userAccountExposure.getUsdNodeMarginInfo.liquidationMarginRequirement +
|
|
326
|
-
limitOrderLMR;
|
|
327
|
-
|
|
328
|
-
const marginRatio = ExposureCommand.getMarginRatio({
|
|
329
|
-
marginBalance,
|
|
330
|
-
liquidationMarginRequirement,
|
|
331
|
-
});
|
|
332
|
-
|
|
333
|
-
const marginRatioHealth = ExposureCommand.evaluateHealthStatus(
|
|
334
|
-
marginRatio * 100,
|
|
335
|
-
);
|
|
336
|
-
|
|
337
|
-
const xpEarnRangeMin = Math.round(
|
|
338
|
-
Math.abs(snappedAmount) / INSTANT_TRADING_RATE_XP,
|
|
339
|
-
);
|
|
340
|
-
const xpEarnRangeMax = Math.round(
|
|
341
|
-
(100 * Math.abs(snappedAmount)) / INSTANT_TRADING_RATE_XP,
|
|
342
|
-
);
|
|
343
|
-
|
|
344
|
-
return {
|
|
345
|
-
fees,
|
|
346
|
-
takerFeesPercentage:
|
|
347
|
-
BigNumber(tradeSimulationState.feeParameter).toNumber() * 100,
|
|
348
|
-
snappedAmount,
|
|
349
|
-
snappedAmountInBase,
|
|
350
|
-
estimatedPrice: params.triggerPrice,
|
|
351
|
-
marginRatio: marginRatio * 100,
|
|
352
|
-
marginRatioHealth,
|
|
353
|
-
availableMargin,
|
|
354
|
-
marginBalance,
|
|
355
|
-
requiredMargin,
|
|
356
|
-
xpEarnRange: {
|
|
357
|
-
min: xpEarnRangeMin,
|
|
358
|
-
max: xpEarnRangeMax,
|
|
359
|
-
},
|
|
360
|
-
};
|
|
361
|
-
}
|
|
362
|
-
|
|
363
|
-
getMaxAmountForLimitOrder(
|
|
364
|
-
params: LimitTradeMaxOrderSizeParams,
|
|
365
|
-
): LimitTradeMaxOrderSizeResult {
|
|
366
|
-
if (!this.loadedData) {
|
|
367
|
-
throw new Error('Data not loaded. Call arm() first.');
|
|
368
|
-
}
|
|
369
|
-
|
|
370
|
-
const { userAccountExposure, tradeSimulationState } = this.loadedData;
|
|
371
|
-
|
|
372
|
-
const riskMatrixElement = this.getRiskMatrixElement();
|
|
373
|
-
|
|
374
|
-
const imrMultiplier = amountNormalizer(
|
|
375
|
-
String(userAccountExposure.riskMultipliers.im_multiplier),
|
|
376
|
-
).toNumber();
|
|
377
|
-
|
|
378
|
-
const availableMargin =
|
|
379
|
-
userAccountExposure.getUsdNodeMarginInfo.initialDelta;
|
|
380
|
-
|
|
381
|
-
const maxAmountSize = Math.max(
|
|
382
|
-
(availableMargin / Math.sqrt(riskMatrixElement) / imrMultiplier) * 0.97, // @todo implement dynamic buffer
|
|
383
|
-
0,
|
|
384
|
-
);
|
|
385
|
-
const maxAmountBase = maxAmountSize / params.triggerPrice;
|
|
386
|
-
|
|
387
|
-
const minAmountBase = amountNormalizer(
|
|
388
|
-
tradeSimulationState.marketConfiguration.minimum_order_base,
|
|
389
|
-
).toNumber();
|
|
390
|
-
|
|
391
|
-
const minAmountSize = minAmountBase * params.triggerPrice;
|
|
392
|
-
|
|
393
|
-
return {
|
|
394
|
-
maxAmountBase,
|
|
395
|
-
maxAmountSize,
|
|
396
|
-
minAmountBase,
|
|
397
|
-
minAmountSize,
|
|
398
|
-
};
|
|
399
|
-
}
|
|
400
|
-
|
|
401
|
-
convertValue(
|
|
402
|
-
params: TradeSimulationConvertValueParams,
|
|
403
|
-
): TradeSimulationConvertValueResult {
|
|
404
|
-
if (!this.loadedData) {
|
|
405
|
-
throw new Error('Data not loaded. Call arm() first.');
|
|
406
|
-
}
|
|
407
|
-
|
|
408
|
-
const { tradeSimulationState } = this.loadedData;
|
|
409
|
-
|
|
410
|
-
if (!params.fromBase)
|
|
411
|
-
return BigNumber(params.amount)
|
|
412
|
-
.div(
|
|
413
|
-
tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
|
|
414
|
-
tradeSimulationState.marketConfiguration.market_id
|
|
415
|
-
],
|
|
416
|
-
)
|
|
417
|
-
.toNumber();
|
|
418
|
-
else
|
|
419
|
-
return BigNumber(params.amount)
|
|
420
|
-
.times(
|
|
421
|
-
tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
|
|
422
|
-
tradeSimulationState.marketConfiguration.market_id
|
|
423
|
-
],
|
|
424
|
-
)
|
|
425
|
-
.toNumber();
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
convertValueEstimatedPrice(
|
|
429
|
-
params: TradeSimulationConvertValueEstimatedPriceParams,
|
|
430
|
-
): TradeSimulationConvertValueResult {
|
|
431
|
-
if (!this.loadedData) {
|
|
432
|
-
throw new Error('Data not loaded. Call arm() first.');
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
const { tradeSimulationState, passivePoolExposure } = this.loadedData;
|
|
436
|
-
|
|
437
|
-
const amountForSlippage = params.fromBase
|
|
438
|
-
? params.amount
|
|
439
|
-
: BigNumber(params.amount)
|
|
440
|
-
.div(
|
|
441
|
-
tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
|
|
442
|
-
tradeSimulationState.marketConfiguration.market_id
|
|
443
|
-
],
|
|
444
|
-
)
|
|
445
|
-
.toNumber();
|
|
446
|
-
|
|
447
|
-
const slippage = passivePoolExposure.getSlippage(
|
|
448
|
-
BigNumber(amountForSlippage).negated().toNumber(),
|
|
449
|
-
tradeSimulationState.marketConfiguration,
|
|
450
|
-
tradeSimulationState.marketStorage,
|
|
451
|
-
);
|
|
452
|
-
const estimatedPrice = ExposureCommand.calculateEstimatedPrice(
|
|
453
|
-
tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
|
|
454
|
-
tradeSimulationState.marketConfiguration.market_id
|
|
455
|
-
],
|
|
456
|
-
slippage,
|
|
457
|
-
);
|
|
458
|
-
|
|
459
|
-
if (!params.fromBase)
|
|
460
|
-
return BigNumber(params.amount).div(estimatedPrice).toNumber();
|
|
461
|
-
else return BigNumber(params.amount).times(estimatedPrice).toNumber();
|
|
462
|
-
}
|
|
463
|
-
|
|
464
|
-
customLeverage(): number | null {
|
|
465
|
-
if (!this.loadedData) {
|
|
466
|
-
throw new Error('Data not loaded. Call arm() first.');
|
|
467
|
-
}
|
|
468
|
-
|
|
469
|
-
const { tradeSimulationState } = this.loadedData;
|
|
470
|
-
|
|
471
|
-
return tradeSimulationState.customLeverage;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
estimatedPrice(params: EstimatedPriceParams): EstimatedPriceResult {
|
|
475
|
-
if (!this.loadedData) {
|
|
476
|
-
throw new Error('Data not loaded. Call arm() first.');
|
|
477
|
-
}
|
|
478
|
-
|
|
479
|
-
const { tradeSimulationState, passivePoolExposure } = this.loadedData;
|
|
480
|
-
|
|
481
|
-
const slippage = passivePoolExposure.getSlippage(
|
|
482
|
-
BigNumber(params.amount).negated().toNumber(),
|
|
483
|
-
tradeSimulationState.marketConfiguration,
|
|
484
|
-
tradeSimulationState.marketStorage,
|
|
485
|
-
);
|
|
486
|
-
|
|
487
|
-
const price =
|
|
488
|
-
tradeSimulationState.exposureDataAccount.oraclePricePerMarket[
|
|
489
|
-
tradeSimulationState.marketConfiguration.market_id
|
|
490
|
-
];
|
|
491
|
-
|
|
492
|
-
const estimatedPrice = ExposureCommand.calculateEstimatedPrice(
|
|
493
|
-
tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
|
|
494
|
-
tradeSimulationState.marketConfiguration.market_id
|
|
495
|
-
],
|
|
496
|
-
slippage,
|
|
497
|
-
);
|
|
498
|
-
|
|
499
|
-
return {
|
|
500
|
-
estimatedPrice,
|
|
501
|
-
markPrice: price,
|
|
502
|
-
};
|
|
503
|
-
}
|
|
504
|
-
|
|
505
|
-
roundToBaseSpacing(amount: number, baseSpacing: number): number {
|
|
506
|
-
const snappedAmount = BigNumber(amount)
|
|
507
|
-
.abs()
|
|
508
|
-
.dividedBy(baseSpacing)
|
|
509
|
-
.integerValue(BigNumber.ROUND_FLOOR)
|
|
510
|
-
.multipliedBy(baseSpacing)
|
|
511
|
-
.toNumber();
|
|
512
|
-
|
|
513
|
-
if (amount < 0) {
|
|
514
|
-
return -snappedAmount;
|
|
515
|
-
}
|
|
516
|
-
return snappedAmount;
|
|
517
|
-
}
|
|
518
|
-
|
|
519
|
-
updatePrice(price: number): void {
|
|
520
|
-
if (!this.loadedData || !this.marketId) {
|
|
521
|
-
throw new Error('Data not loaded. Call arm() first.');
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
const { tradeSimulationState } = this.loadedData;
|
|
525
|
-
|
|
526
|
-
// update price
|
|
527
|
-
const marketId = this.marketId;
|
|
528
|
-
tradeSimulationState.exposureDataPassivePool.oraclePricePerMarket[
|
|
529
|
-
marketId
|
|
530
|
-
] = price;
|
|
531
|
-
tradeSimulationState.exposureDataAccount.oraclePricePerMarket[marketId] =
|
|
532
|
-
price;
|
|
533
|
-
}
|
|
534
|
-
}
|