@gmx-io/sdk 1.5.0-alpha-8 → 1.5.0-alpha-9
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/build/cjs/src/clients/v1/testUtil.js +1 -1
- package/build/cjs/src/clients/v2/index.js +6 -2
- package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +34 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +230 -0
- package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +62 -0
- package/build/cjs/src/configs/__tests__/tokens.spec.js +1 -0
- package/build/cjs/src/configs/api.js +4 -3
- package/build/cjs/src/configs/batch.js +13 -0
- package/build/cjs/src/configs/chainIds.js +2 -1
- package/build/cjs/src/configs/chains.js +48 -2
- package/build/cjs/src/configs/contracts.js +67 -0
- package/build/cjs/src/configs/express.js +2 -0
- package/build/cjs/src/configs/gasLimits.js +7 -0
- package/build/cjs/src/configs/markets.js +30 -0
- package/build/cjs/src/configs/oracleKeeper.js +2 -0
- package/build/cjs/src/configs/tokens.js +89 -1
- package/build/cjs/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +2 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
- package/build/cjs/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -0
- package/build/cjs/src/utils/__tests__/uiFeeReceiver.spec.js +24 -0
- package/build/cjs/src/utils/errors/parseError.js +3 -0
- package/build/cjs/src/utils/staking/api.js +11 -0
- package/build/cjs/src/utils/staking/types.js +2 -0
- package/build/cjs/src/utils/trade/decrease.js +131 -6
- package/build/cjs/src/utils/trade/increase.js +2 -6
- package/build/cjs/src/utils/twap/uiFeeReceiver.js +29 -13
- package/build/esm/src/clients/v1/testUtil.js +1 -1
- package/build/esm/src/clients/v2/index.js +4 -0
- package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +34 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +230 -0
- package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +62 -0
- package/build/esm/src/configs/__tests__/tokens.spec.js +2 -1
- package/build/esm/src/configs/api.js +5 -4
- package/build/esm/src/configs/batch.js +14 -1
- package/build/esm/src/configs/chainIds.js +1 -0
- package/build/esm/src/configs/chains.js +48 -3
- package/build/esm/src/configs/contracts.js +68 -1
- package/build/esm/src/configs/express.js +3 -1
- package/build/esm/src/configs/gasLimits.js +8 -1
- package/build/esm/src/configs/markets.js +31 -1
- package/build/esm/src/configs/oracleKeeper.js +3 -1
- package/build/esm/src/configs/tokens.js +90 -2
- package/build/esm/src/utils/__tests__/orderTransactions/decreaseOrders.spec.js +2 -0
- package/build/esm/src/utils/__tests__/orderTransactions/increaseOrders.spec.js +2 -0
- package/build/esm/src/utils/__tests__/orderTransactions/swapOrders.spec.js +1 -0
- package/build/esm/src/utils/__tests__/uiFeeReceiver.spec.js +25 -1
- package/build/esm/src/utils/errors/parseError.js +3 -0
- package/build/esm/src/utils/staking/api.js +7 -0
- package/build/esm/src/utils/staking/types.js +1 -0
- package/build/esm/src/utils/trade/decrease.js +127 -7
- package/build/esm/src/utils/trade/increase.js +3 -7
- package/build/esm/src/utils/twap/uiFeeReceiver.js +27 -12
- package/build/types/src/clients/v1/index.d.ts +1 -1
- package/build/types/src/clients/v1/modules/base.d.ts +1 -1
- package/build/types/src/clients/v1/multicall.d.ts +1 -1
- package/build/types/src/clients/v2/index.d.ts +5 -0
- package/build/types/src/codegen/prebuilt/index.d.ts +326 -0
- package/build/types/src/configs/chainIds.d.ts +1 -0
- package/build/types/src/configs/chains.d.ts +26 -6
- package/build/types/src/configs/contracts.d.ts +64 -0
- package/build/types/src/utils/errors/parseError.d.ts +3 -0
- package/build/types/src/utils/staking/api.d.ts +7 -0
- package/build/types/src/utils/staking/types.d.ts +14 -0
- package/build/types/src/utils/trade/decrease.d.ts +41 -2
- package/build/types/src/utils/trade/types.d.ts +8 -0
- package/build/types/src/utils/twap/uiFeeReceiver.d.ts +9 -6
- package/package.json +1 -1
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { zeroAddress } from "viem";
|
|
2
|
-
import { ARBITRUM, ARBITRUM_SEPOLIA, AVALANCHE, AVALANCHE_FUJI, BOTANIX } from "./chains";
|
|
2
|
+
import { ARBITRUM, ARBITRUM_SEPOLIA, AVALANCHE, AVALANCHE_FUJI, BOTANIX, MEGAETH } from "./chains";
|
|
3
3
|
export const CONTRACTS = {
|
|
4
4
|
[ARBITRUM]: {
|
|
5
5
|
// V1
|
|
@@ -200,6 +200,73 @@ export const CONTRACTS = {
|
|
|
200
200
|
StBTC: "0xF4586028FFdA7Eca636864F80f8a3f2589E33795",
|
|
201
201
|
PBTC: "0x0D2437F93Fed6EA64Ef01cCde385FB1263910C56",
|
|
202
202
|
},
|
|
203
|
+
[MEGAETH]: {
|
|
204
|
+
// Synthetics
|
|
205
|
+
DataStore: "0xE43C7B694f6b652a9F4A0f275C008d18758Dce35",
|
|
206
|
+
EventEmitter: "0xAf2E131d483cedE068e21a9228aD91E623a989C2",
|
|
207
|
+
SubaccountRouter: "0x3133aC88af73d3187f1700a2426AD95B5d6E0562",
|
|
208
|
+
ExchangeRouter: "0x73B3593F01CF8e573a412D1d0c972b581794ebE0",
|
|
209
|
+
DepositVault: "0x8231A60862F9b0bA93fFA050c0E94AC902D901d2",
|
|
210
|
+
WithdrawalVault: "0x0Ec53dda9676219dE63eC703212219b07811F33C",
|
|
211
|
+
OrderVault: "0xD5AE04762E2afb1506695b3F36286EBE7B0E6772",
|
|
212
|
+
ShiftVault: "0xC255c70b50623054CADbAD9A02E1CFE73d286666",
|
|
213
|
+
SyntheticsReader: "0x0f038EB4a38B08cd3c937a3256b51aa01904a684",
|
|
214
|
+
SyntheticsRouter: "0x1eAfB14236C489C28845EC04F78DECA5Fb9879Aa",
|
|
215
|
+
GlvReader: "0x424527a588D56513cB2F5161958D83883EE8aB0f",
|
|
216
|
+
GlvRouter: "0x505F0cCADA00F0CcB4EEbf6467531cF4dd907B0E",
|
|
217
|
+
GlvVault: "0x52e4875EB5603d21912d30A1dBA6B0B97192459A",
|
|
218
|
+
GelatoRelayRouter: "0x24eD625B9C47fDEbF088A4d12B7f9B4B2f556297",
|
|
219
|
+
SubaccountGelatoRelayRouter: "0xD515fA0B4d704f3E2C57270F1F53BEeE16348B3b",
|
|
220
|
+
MultichainClaimsRouter: "0xfE9fD31e499bA6d8733Aec49ECe5b41381103433",
|
|
221
|
+
MultichainGlvRouter: "0x7EF7d01316425de5d7C2EFDf8b802A250c222faB",
|
|
222
|
+
MultichainGmRouter: "0x041336A3DaF0a12d004a95f1511393d9A3d7236d",
|
|
223
|
+
MultichainOrderRouter: "0x976363dFbA3AeB8Fb10b733baD74e7099cCB558A",
|
|
224
|
+
MultichainSubaccountRouter: "0xeB8f828A4B89dc3A854f278227A2A5E136E50bF9",
|
|
225
|
+
MultichainTransferRouter: "0xCa62C570D8667a00A56EB989881ECbA4364BFe9e",
|
|
226
|
+
MultichainVault: "0xd6922E889cE4CF14e59427F20e7d857ff81A5A9D",
|
|
227
|
+
LayerZeroProvider: "0x9c41F854f123a7905907FfcF2578dFB7E47D02E0",
|
|
228
|
+
ChainlinkPriceFeedProvider: "0x7452c558d45f8afC8c83dAe62C3f8A5BE19c71f6",
|
|
229
|
+
ClaimHandler: "0x7e42e350FEF7c0A766590A6b2F4eF3F38D8A2988",
|
|
230
|
+
// External
|
|
231
|
+
ExternalHandler: "0xa7EE2737249e0099906cB079BCEe85f0bbd837d4",
|
|
232
|
+
OpenOceanRouter: zeroAddress,
|
|
233
|
+
Multicall: "0xF516BC01c50eebdBad4d7E506c8f690ae8EAFc52",
|
|
234
|
+
LayerZeroEndpoint: "0x6F475642a6e85809B1c36Fa62763669b1b48DD5B",
|
|
235
|
+
ArbitrumNodeInterface: zeroAddress,
|
|
236
|
+
GelatoRelayAddress: "0xcd565435e0d2109feFde337a66491541Df0D1420",
|
|
237
|
+
Vault: zeroAddress,
|
|
238
|
+
Reader: zeroAddress,
|
|
239
|
+
PositionRouter: zeroAddress,
|
|
240
|
+
ReferralStorage: "0xAd917849372eaEF498E982F90bA6459a43ecbd31",
|
|
241
|
+
VaultReader: zeroAddress,
|
|
242
|
+
GlpManager: zeroAddress,
|
|
243
|
+
RewardRouter: zeroAddress,
|
|
244
|
+
RewardReader: zeroAddress,
|
|
245
|
+
GlpRewardRouter: zeroAddress,
|
|
246
|
+
StakedGmxTracker: zeroAddress,
|
|
247
|
+
FeeGmxTracker: zeroAddress,
|
|
248
|
+
GLP: zeroAddress,
|
|
249
|
+
GMX: zeroAddress,
|
|
250
|
+
ES_GMX: zeroAddress,
|
|
251
|
+
BN_GMX: zeroAddress,
|
|
252
|
+
USDG: zeroAddress,
|
|
253
|
+
BonusGmxTracker: zeroAddress,
|
|
254
|
+
StakedGlpTracker: zeroAddress,
|
|
255
|
+
FeeGlpTracker: zeroAddress,
|
|
256
|
+
ExtendedGmxTracker: zeroAddress,
|
|
257
|
+
StakedGmxDistributor: zeroAddress,
|
|
258
|
+
StakedGlpDistributor: zeroAddress,
|
|
259
|
+
GmxVester: zeroAddress,
|
|
260
|
+
GlpVester: zeroAddress,
|
|
261
|
+
AffiliateVester: zeroAddress,
|
|
262
|
+
Router: zeroAddress,
|
|
263
|
+
GovToken: zeroAddress,
|
|
264
|
+
ES_GMX_IOU: zeroAddress,
|
|
265
|
+
OrderBook: zeroAddress,
|
|
266
|
+
UniswapGmxEthPool: zeroAddress,
|
|
267
|
+
// megaeth specific
|
|
268
|
+
NATIVE_TOKEN: "0x4200000000000000000000000000000000000006",
|
|
269
|
+
},
|
|
203
270
|
[AVALANCHE_FUJI]: {
|
|
204
271
|
// V1
|
|
205
272
|
Vault: zeroAddress,
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { USD_DECIMALS } from "../utils/numbers";
|
|
2
2
|
import { periodToSeconds } from "../utils/time";
|
|
3
|
-
import { ARBITRUM, ARBITRUM_SEPOLIA, AVALANCHE, AVALANCHE_FUJI, BOTANIX } from "./chains";
|
|
3
|
+
import { ARBITRUM, ARBITRUM_SEPOLIA, AVALANCHE, AVALANCHE_FUJI, BOTANIX, MEGAETH } from "./chains";
|
|
4
4
|
import { getTokenBySymbol, getWrappedToken } from "./tokens";
|
|
5
5
|
export const SUBACCOUNT_MESSAGE = "Generate a GMX 1CT (One-Click Trading) session. Only sign this message on a trusted website.";
|
|
6
6
|
export const SUBACCOUNT_DOCS_URL = "https://docs.gmx.io/docs/trading/overview/#express-trading-and-one-click-trading";
|
|
@@ -14,6 +14,7 @@ export const GELATO_API_KEYS = {
|
|
|
14
14
|
[AVALANCHE]: "FalsQh9loL6V0rwPy4gWgnQPR6uTHfWjSVT2qlTzUq4_",
|
|
15
15
|
[BOTANIX]: "s5GgkfX7dvd_2uYqsRSCjzMekUrXh0dibUvfLab1Anc_",
|
|
16
16
|
[ARBITRUM_SEPOLIA]: "nx5nyAg4h2kI_64YtOuPt7LSPDEXo4u8eJY_idF9xDw_",
|
|
17
|
+
[MEGAETH]: "MDPcJgFTWgc8ifFElmv_vHLaioG_h8GU2ZDju1eyQ4Y_",
|
|
17
18
|
};
|
|
18
19
|
export const EXPRESS_EXTRA_EXECUTION_FEE_BUFFER_BPS = 1000;
|
|
19
20
|
const GAS_PAYMENT_TOKENS = {
|
|
@@ -36,6 +37,7 @@ const GAS_PAYMENT_TOKENS = {
|
|
|
36
37
|
getTokenBySymbol(ARBITRUM_SEPOLIA, "WETH").address,
|
|
37
38
|
],
|
|
38
39
|
[BOTANIX]: [getTokenBySymbol(BOTANIX, "pBTC").address],
|
|
40
|
+
[MEGAETH]: [getTokenBySymbol(MEGAETH, "USDM").address, getTokenBySymbol(MEGAETH, "WETH").address],
|
|
39
41
|
};
|
|
40
42
|
export function getGasPaymentTokens(chainId) {
|
|
41
43
|
return GAS_PAYMENT_TOKENS[chainId];
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ARBITRUM, AVALANCHE, AVALANCHE_FUJI, ARBITRUM_SEPOLIA, BOTANIX } from "./chainIds";
|
|
1
|
+
import { ARBITRUM, AVALANCHE, AVALANCHE_FUJI, ARBITRUM_SEPOLIA, BOTANIX, MEGAETH } from "./chainIds";
|
|
2
2
|
export const GAS_LIMITS_STATIC_CONFIG = {
|
|
3
3
|
[ARBITRUM]: {
|
|
4
4
|
createOrderGasLimit: 1000000n,
|
|
@@ -35,4 +35,11 @@ export const GAS_LIMITS_STATIC_CONFIG = {
|
|
|
35
35
|
tokenPermitGasLimit: 90000n,
|
|
36
36
|
gmxAccountCollateralGasLimit: 0n,
|
|
37
37
|
},
|
|
38
|
+
[MEGAETH]: {
|
|
39
|
+
createOrderGasLimit: 4000000n,
|
|
40
|
+
updateOrderGasLimit: 3200000n,
|
|
41
|
+
cancelOrderGasLimit: 2800000n,
|
|
42
|
+
tokenPermitGasLimit: 360000n,
|
|
43
|
+
gmxAccountCollateralGasLimit: 0n,
|
|
44
|
+
},
|
|
38
45
|
};
|
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
Avoid adding client-side code here, as it can break the build process.
|
|
4
4
|
*/
|
|
5
5
|
import { zeroAddress } from "viem";
|
|
6
|
-
import { ARBITRUM, ARBITRUM_SEPOLIA, AVALANCHE, AVALANCHE_FUJI, BOTANIX } from "./chains";
|
|
6
|
+
import { ARBITRUM, ARBITRUM_SEPOLIA, AVALANCHE, AVALANCHE_FUJI, BOTANIX, MEGAETH } from "./chains";
|
|
7
7
|
import { convertTokenAddress, getToken, getTokenBySymbol } from "./tokens";
|
|
8
8
|
export const SWAP_GRAPH_MAX_MARKETS_PER_TOKEN = 5;
|
|
9
9
|
/*
|
|
@@ -1246,6 +1246,36 @@ export const MARKETS = {
|
|
|
1246
1246
|
shortTokenAddress: "0x0D2437F93Fed6EA64Ef01cCde385FB1263910C56",
|
|
1247
1247
|
},
|
|
1248
1248
|
},
|
|
1249
|
+
[MEGAETH]: {
|
|
1250
|
+
// BTC/USD [USDM-USDM]
|
|
1251
|
+
"0x31EdCc52bE2Fa55Ba68f50409F9e6b7d9EbF3D59": {
|
|
1252
|
+
marketTokenAddress: "0x31EdCc52bE2Fa55Ba68f50409F9e6b7d9EbF3D59",
|
|
1253
|
+
indexTokenAddress: "0xc258141Ad80cafF9ab82a04bEb1c10F4B5bCb8F4",
|
|
1254
|
+
longTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
|
|
1255
|
+
shortTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
|
|
1256
|
+
},
|
|
1257
|
+
// ETH/USD [USDM-USDM]
|
|
1258
|
+
"0x9b1B72720f6D277F3b1e607a0c5fab1B300248b1": {
|
|
1259
|
+
marketTokenAddress: "0x9b1B72720f6D277F3b1e607a0c5fab1B300248b1",
|
|
1260
|
+
indexTokenAddress: "0x4200000000000000000000000000000000000006",
|
|
1261
|
+
longTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
|
|
1262
|
+
shortTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
|
|
1263
|
+
},
|
|
1264
|
+
// SOL/USD [USDM-USDM]
|
|
1265
|
+
"0xe8E716F1cddfFD0698B86919D41A8228d701fEe9": {
|
|
1266
|
+
marketTokenAddress: "0xe8E716F1cddfFD0698B86919D41A8228d701fEe9",
|
|
1267
|
+
indexTokenAddress: "0x30998706b1e5C95e9d3645D2e9E0EB1466819962",
|
|
1268
|
+
longTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
|
|
1269
|
+
shortTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
|
|
1270
|
+
},
|
|
1271
|
+
// SWAP-ONLY [ETH-USDM]
|
|
1272
|
+
"0xc5c9B5E23810565763De41144741477eeCB25e2e": {
|
|
1273
|
+
marketTokenAddress: "0xc5c9B5E23810565763De41144741477eeCB25e2e",
|
|
1274
|
+
indexTokenAddress: "0x0000000000000000000000000000000000000000",
|
|
1275
|
+
longTokenAddress: "0x4200000000000000000000000000000000000006",
|
|
1276
|
+
shortTokenAddress: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
|
|
1277
|
+
},
|
|
1278
|
+
},
|
|
1249
1279
|
};
|
|
1250
1280
|
export function getMarketsByChainId(chainId) {
|
|
1251
1281
|
if (!MARKETS[chainId]) {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ARBITRUM, ARBITRUM_SEPOLIA, AVALANCHE, AVALANCHE_FUJI, BOTANIX } from "./chains";
|
|
1
|
+
import { ARBITRUM, ARBITRUM_SEPOLIA, AVALANCHE, AVALANCHE_FUJI, BOTANIX, MEGAETH } from "./chains";
|
|
2
2
|
export const ORACLE_FALLBACK_TRACKER_CONFIG = {
|
|
3
3
|
trackInterval: 10 * 1000, // 10 secs
|
|
4
4
|
checkTimeout: 10 * 1000, // 10 secs
|
|
@@ -17,6 +17,7 @@ const ORACLE_KEEPER_URLS = {
|
|
|
17
17
|
[AVALANCHE]: "https://avalanche-api.gmxinfra.io",
|
|
18
18
|
[AVALANCHE_FUJI]: "https://synthetics-api-avax-fuji-upovm.ondigitalocean.app",
|
|
19
19
|
[BOTANIX]: "https://botanix-api.gmxinfra.io",
|
|
20
|
+
[MEGAETH]: "https://megaeth-api.gmxinfra.io",
|
|
20
21
|
[ARBITRUM_SEPOLIA]: "https://dolphin-app-a2dup.ondigitalocean.app",
|
|
21
22
|
};
|
|
22
23
|
const ORACLE_KEEPER_FALLBACK_URLS = {
|
|
@@ -24,6 +25,7 @@ const ORACLE_KEEPER_FALLBACK_URLS = {
|
|
|
24
25
|
[AVALANCHE]: ["https://avalanche-api-fallback.gmxinfra.io", "https://avalanche-api-fallback.gmxinfra2.io"],
|
|
25
26
|
[AVALANCHE_FUJI]: ["https://synthetics-api-avax-fuji.gmxinfra.io"],
|
|
26
27
|
[BOTANIX]: ["https://botanix-api-fallback.gmxinfra.io", "https://botanix-api-fallback.gmxinfra2.io"],
|
|
28
|
+
[MEGAETH]: ["https://megaeth-api-fallback.gmxinfra.io"],
|
|
27
29
|
[ARBITRUM_SEPOLIA]: ["https://dolphin-app-a2dup.ondigitalocean.app"],
|
|
28
30
|
};
|
|
29
31
|
export function getOracleKeeperUrl(chainId) {
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { zeroAddress } from "viem";
|
|
2
|
-
import { ARBITRUM, ARBITRUM_SEPOLIA, AVALANCHE, AVALANCHE_FUJI, BOTANIX } from "./chains";
|
|
2
|
+
import { ARBITRUM, ARBITRUM_SEPOLIA, AVALANCHE, AVALANCHE_FUJI, BOTANIX, MEGAETH } from "./chains";
|
|
3
3
|
import { getContract } from "./contracts";
|
|
4
4
|
export const NATIVE_TOKEN_ADDRESS = zeroAddress;
|
|
5
5
|
export const GM_STUB_ADDRESS = "<GM-ADDRESS>";
|
|
@@ -1961,6 +1961,94 @@ export const TOKENS = {
|
|
|
1961
1961
|
isPlatformToken: true,
|
|
1962
1962
|
},
|
|
1963
1963
|
],
|
|
1964
|
+
[MEGAETH]: [
|
|
1965
|
+
{
|
|
1966
|
+
name: "Ethereum",
|
|
1967
|
+
symbol: "ETH",
|
|
1968
|
+
address: NATIVE_TOKEN_ADDRESS,
|
|
1969
|
+
decimals: 18,
|
|
1970
|
+
isNative: true,
|
|
1971
|
+
isShortable: true,
|
|
1972
|
+
categories: ["layer1"],
|
|
1973
|
+
imageUrl: "https://assets.coingecko.com/coins/images/279/small/ethereum.png?1595348880",
|
|
1974
|
+
},
|
|
1975
|
+
{
|
|
1976
|
+
name: "Wrapped Ethereum",
|
|
1977
|
+
symbol: "WETH",
|
|
1978
|
+
address: "0x4200000000000000000000000000000000000006",
|
|
1979
|
+
decimals: 18,
|
|
1980
|
+
isWrapped: true,
|
|
1981
|
+
baseSymbol: "ETH",
|
|
1982
|
+
imageUrl: "https://assets.coingecko.com/coins/images/2518/thumb/weth.png?1628852295",
|
|
1983
|
+
},
|
|
1984
|
+
{
|
|
1985
|
+
name: "Bitcoin",
|
|
1986
|
+
symbol: "BTC",
|
|
1987
|
+
address: "0xc258141Ad80cafF9ab82a04bEb1c10F4B5bCb8F4",
|
|
1988
|
+
isSynthetic: true,
|
|
1989
|
+
decimals: 8,
|
|
1990
|
+
categories: ["layer1"],
|
|
1991
|
+
imageUrl: "https://assets.coingecko.com/coins/images/7598/thumb/wrapped_bitcoin_wbtc.png?1548822744",
|
|
1992
|
+
},
|
|
1993
|
+
{
|
|
1994
|
+
name: "Solana",
|
|
1995
|
+
symbol: "SOL",
|
|
1996
|
+
address: "0x30998706b1e5C95e9d3645D2e9E0EB1466819962",
|
|
1997
|
+
isSynthetic: true,
|
|
1998
|
+
priceDecimals: 3,
|
|
1999
|
+
decimals: 9,
|
|
2000
|
+
categories: ["layer1"],
|
|
2001
|
+
imageUrl: "https://assets.coingecko.com/coins/images/4128/small/solana.png?1640133422",
|
|
2002
|
+
},
|
|
2003
|
+
{
|
|
2004
|
+
name: "USDM",
|
|
2005
|
+
symbol: "USDM",
|
|
2006
|
+
address: "0xFAfDdbb3FC7688494971a79cc65DCa3EF82079E7",
|
|
2007
|
+
decimals: 18,
|
|
2008
|
+
isStable: true,
|
|
2009
|
+
imageUrl: "https://assets.coingecko.com/coins/images/69955/standard/USDm.png?1760170136",
|
|
2010
|
+
},
|
|
2011
|
+
{
|
|
2012
|
+
name: "GMX",
|
|
2013
|
+
symbol: "GMX",
|
|
2014
|
+
address: GMX_STUB_ADDRESS,
|
|
2015
|
+
decimals: 18,
|
|
2016
|
+
imageUrl: "https://assets.coingecko.com/coins/images/18323/small/arbit.png?1631532468",
|
|
2017
|
+
isPlatformToken: true,
|
|
2018
|
+
},
|
|
2019
|
+
{
|
|
2020
|
+
name: "Escrowed GMX",
|
|
2021
|
+
symbol: "ESGMX",
|
|
2022
|
+
address: ESGMX_STUB_ADDRESS,
|
|
2023
|
+
decimals: 18,
|
|
2024
|
+
isPlatformToken: true,
|
|
2025
|
+
},
|
|
2026
|
+
{
|
|
2027
|
+
name: "GMX LP",
|
|
2028
|
+
symbol: "GLP",
|
|
2029
|
+
address: GLP_STUB_ADDRESS,
|
|
2030
|
+
decimals: 18,
|
|
2031
|
+
imageUrl: "https://github.com/gmx-io/gmx-assets/blob/main/GMX-Assets/PNG/GLP_LOGO%20ONLY.png?raw=true",
|
|
2032
|
+
isPlatformToken: true,
|
|
2033
|
+
},
|
|
2034
|
+
/** Placeholder tokens */
|
|
2035
|
+
{
|
|
2036
|
+
name: "GMX Market tokens",
|
|
2037
|
+
symbol: "GM",
|
|
2038
|
+
address: GM_STUB_ADDRESS,
|
|
2039
|
+
decimals: 18,
|
|
2040
|
+
imageUrl: "https://raw.githubusercontent.com/gmx-io/gmx-assets/main/GMX-Assets/PNG/GM_LOGO.png",
|
|
2041
|
+
isPlatformToken: true,
|
|
2042
|
+
},
|
|
2043
|
+
{
|
|
2044
|
+
name: "GLV Market tokens",
|
|
2045
|
+
symbol: "GLV",
|
|
2046
|
+
address: GLV_STUB_ADDRESS,
|
|
2047
|
+
decimals: 18,
|
|
2048
|
+
imageUrl: "https://raw.githubusercontent.com/gmx-io/gmx-assets/main/GMX-Assets/PNG/GLV_LOGO.png",
|
|
2049
|
+
isPlatformToken: true,
|
|
2050
|
+
},
|
|
2051
|
+
],
|
|
1964
2052
|
};
|
|
1965
2053
|
export const TOKEN_COLOR_MAP = {
|
|
1966
2054
|
ETH: "#6062a6",
|
|
@@ -2004,7 +2092,7 @@ export const SYNTHETIC_TOKENS = {};
|
|
|
2004
2092
|
export const TOKENS_BY_SYMBOL_MAP = {};
|
|
2005
2093
|
export const WRAPPED_TOKENS_MAP = {};
|
|
2006
2094
|
export const NATIVE_TOKENS_MAP = {};
|
|
2007
|
-
const CHAIN_IDS = [ARBITRUM, AVALANCHE, AVALANCHE_FUJI, BOTANIX, ARBITRUM_SEPOLIA];
|
|
2095
|
+
const CHAIN_IDS = [ARBITRUM, AVALANCHE, AVALANCHE_FUJI, BOTANIX, ARBITRUM_SEPOLIA, MEGAETH];
|
|
2008
2096
|
for (let j = 0; j < CHAIN_IDS.length; j++) {
|
|
2009
2097
|
const chainId = CHAIN_IDS[j];
|
|
2010
2098
|
TOKENS_MAP[chainId] = {};
|
|
@@ -374,6 +374,7 @@ describe("Decrease Order Payloads", () => {
|
|
|
374
374
|
twapId: "8000",
|
|
375
375
|
numberOfParts: twapParams.numberOfParts,
|
|
376
376
|
isExpress: false,
|
|
377
|
+
isJit: false,
|
|
377
378
|
});
|
|
378
379
|
return {
|
|
379
380
|
orderPayload: {
|
|
@@ -468,6 +469,7 @@ describe("Decrease Order Payloads", () => {
|
|
|
468
469
|
const decoded = decodeTwapUiFeeReceiver(uiFeeReceiver);
|
|
469
470
|
expect(decoded).toEqual({
|
|
470
471
|
isExpress: false,
|
|
472
|
+
isJit: false,
|
|
471
473
|
twapId: "8000",
|
|
472
474
|
numberOfParts: twapParams.numberOfParts,
|
|
473
475
|
});
|
|
@@ -542,6 +542,7 @@ describe("Increase Order Payloads", () => {
|
|
|
542
542
|
const decoded = decodeTwapUiFeeReceiver(uiFeeReceiver);
|
|
543
543
|
expect(decoded).toEqual({
|
|
544
544
|
isExpress: false,
|
|
545
|
+
isJit: false,
|
|
545
546
|
twapId: "8000",
|
|
546
547
|
numberOfParts: twapParams.numberOfParts,
|
|
547
548
|
});
|
|
@@ -638,6 +639,7 @@ describe("Increase Order Payloads", () => {
|
|
|
638
639
|
const decoded = decodeTwapUiFeeReceiver(uiFeeReceiver);
|
|
639
640
|
expect(decoded).toEqual({
|
|
640
641
|
isExpress: false,
|
|
642
|
+
isJit: false,
|
|
641
643
|
twapId: "8000",
|
|
642
644
|
numberOfParts: twapParams.numberOfParts,
|
|
643
645
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { isAddress } from "viem";
|
|
2
2
|
import { describe, it, expect } from "vitest";
|
|
3
|
-
import { createTwapUiFeeReceiver, decodeTwapUiFeeReceiver, setUiFeeReceiverIsExpress } from "../twap/uiFeeReceiver";
|
|
3
|
+
import { createTwapUiFeeReceiver, decodeTwapUiFeeReceiver, setUiFeeReceiverIsExpress, setUiFeeReceiverIsJit, } from "../twap/uiFeeReceiver";
|
|
4
4
|
describe("uiFeeReceiver", () => {
|
|
5
5
|
describe("decodeTwapUiFeeReceiver", () => {
|
|
6
6
|
it("should return undefined if the address is not a valid address", () => {
|
|
@@ -14,6 +14,15 @@ describe("uiFeeReceiver", () => {
|
|
|
14
14
|
it("should correctly decode isExpress", () => {
|
|
15
15
|
expect(decodeTwapUiFeeReceiver("0xff0000000000000000000000000000010a123401")).toEqual({
|
|
16
16
|
isExpress: true,
|
|
17
|
+
isJit: false,
|
|
18
|
+
twapId: "1234",
|
|
19
|
+
numberOfParts: 10,
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
it("should correctly decode isJit", () => {
|
|
23
|
+
expect(decodeTwapUiFeeReceiver("0xff0000000000000000000000000001000a123401")).toEqual({
|
|
24
|
+
isExpress: false,
|
|
25
|
+
isJit: true,
|
|
17
26
|
twapId: "1234",
|
|
18
27
|
numberOfParts: 10,
|
|
19
28
|
});
|
|
@@ -21,11 +30,13 @@ describe("uiFeeReceiver", () => {
|
|
|
21
30
|
it("should return the twapId and numberOfParts if the uiFeeReceiver is valid twap", () => {
|
|
22
31
|
expect(decodeTwapUiFeeReceiver("0xff0000000000000000000000000000000a123401")).toEqual({
|
|
23
32
|
isExpress: false,
|
|
33
|
+
isJit: false,
|
|
24
34
|
twapId: "1234",
|
|
25
35
|
numberOfParts: 10,
|
|
26
36
|
});
|
|
27
37
|
expect(decodeTwapUiFeeReceiver("0xff000000000000000000000000000000153a4f01")).toEqual({
|
|
28
38
|
isExpress: false,
|
|
39
|
+
isJit: false,
|
|
29
40
|
twapId: "3a4f",
|
|
30
41
|
numberOfParts: 21,
|
|
31
42
|
});
|
|
@@ -60,3 +71,16 @@ describe("setUiFeeReceiverIsExpress", () => {
|
|
|
60
71
|
expect(setUiFeeReceiverIsExpress("0xff0000000000000000000000000000000a123401", false)).toEqual("0xff0000000000000000000000000000000a123401");
|
|
61
72
|
});
|
|
62
73
|
});
|
|
74
|
+
describe("setUiFeeReceiverIsJit", () => {
|
|
75
|
+
it("should correctly set isJit for simple uiFeeReceiver", () => {
|
|
76
|
+
expect(setUiFeeReceiverIsJit("0xff00000000000000000000000000000000000001", true)).toEqual("0xff00000000000000000000000000010000000001");
|
|
77
|
+
expect(setUiFeeReceiverIsJit("0xff00000000000000000000000000010000000001", false)).toEqual("0xff00000000000000000000000000000000000001");
|
|
78
|
+
});
|
|
79
|
+
it("should preserve isExpress when setting isJit", () => {
|
|
80
|
+
expect(setUiFeeReceiverIsJit("0xff0000000000000000000000000000010a123401", true)).toEqual("0xff0000000000000000000000000001010a123401");
|
|
81
|
+
});
|
|
82
|
+
it("should correctly set isJit for twap uiFeeReceiver", () => {
|
|
83
|
+
expect(setUiFeeReceiverIsJit("0xff0000000000000000000000000000000a123401", true)).toEqual("0xff0000000000000000000000000001000a123401");
|
|
84
|
+
expect(setUiFeeReceiverIsJit("0xff0000000000000000000000000001000a123401", false)).toEqual("0xff0000000000000000000000000000000a123401");
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -31,6 +31,7 @@ export function extendError(error, params) {
|
|
|
31
31
|
error.errorSource = params.errorSource;
|
|
32
32
|
error.isAdditionalValidationPassed = params.isAdditionalValidationPassed;
|
|
33
33
|
error.additionalValidationType = params.additionalValidationType;
|
|
34
|
+
error.simulationMethod = params.simulationMethod;
|
|
34
35
|
error.data = params.data;
|
|
35
36
|
return error;
|
|
36
37
|
}
|
|
@@ -44,6 +45,7 @@ export function parseError(error, errorDepth = 0) {
|
|
|
44
45
|
const errorContext = typeof error === "string" ? undefined : error?.errorContext;
|
|
45
46
|
const isAdditionalValidationPassed = typeof error === "string" ? undefined : error?.isAdditionalValidationPassed;
|
|
46
47
|
const additionalValidationType = typeof error === "string" ? undefined : error?.additionalValidationType;
|
|
48
|
+
const simulationMethod = typeof error === "string" ? undefined : error?.simulationMethod;
|
|
47
49
|
const data = typeof error === "string" ? undefined : error?.data;
|
|
48
50
|
let errorMessage = "Unknown error";
|
|
49
51
|
let errorStack = undefined;
|
|
@@ -153,6 +155,7 @@ export function parseError(error, errorDepth = 0) {
|
|
|
153
155
|
parentError,
|
|
154
156
|
isAdditionalValidationPassed,
|
|
155
157
|
additionalValidationType,
|
|
158
|
+
simulationMethod,
|
|
156
159
|
errorDepth,
|
|
157
160
|
};
|
|
158
161
|
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { deserializeBigIntsInObject } from "../numbers";
|
|
2
|
+
export async function fetchApiStakingPower(ctx, params) {
|
|
3
|
+
const data = await ctx.api.fetchJson("/staking/power", {
|
|
4
|
+
query: { address: params.address },
|
|
5
|
+
});
|
|
6
|
+
return deserializeBigIntsInObject(data, { handleInts: true });
|
|
7
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -5,10 +5,10 @@ import { applyFactor, BASIS_POINTS_DIVISOR_BIGINT, expandDecimals, getBasisPoint
|
|
|
5
5
|
import { DecreasePositionSwapType, OrderType, SwapPricingType } from "../orders/types";
|
|
6
6
|
import { getLeverage, getLiquidationPrice, getMinCollateralFactorForPosition, getNetPriceImpactDeltaUsdForDecrease, getPositionPnlUsd, } from "../positions";
|
|
7
7
|
import { getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getMarkPrice, getOrderThresholdType, } from "../prices";
|
|
8
|
-
import { getSwapStats } from "../swap";
|
|
8
|
+
import { getSwapAmountsByFromValue, getSwapStats } from "../swap";
|
|
9
9
|
import { convertToTokenAmount, convertToUsd, getIsEquivalentTokens } from "../tokens";
|
|
10
10
|
export function getDecreasePositionAmounts(p) {
|
|
11
|
-
const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType: orderType, receiveToken: receiveTokenArg, isSetAcceptablePriceImpactEnabled, } = p;
|
|
11
|
+
const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType: orderType, receiveToken: receiveTokenArg, isSetAcceptablePriceImpactEnabled, forceDecreaseSwapType, } = p;
|
|
12
12
|
const { indexToken } = marketInfo;
|
|
13
13
|
const receiveToken = receiveTokenArg ?? collateralToken;
|
|
14
14
|
const values = {
|
|
@@ -39,17 +39,20 @@ export function getDecreasePositionAmounts(p) {
|
|
|
39
39
|
fundingFeeUsd: 0n,
|
|
40
40
|
feeDiscountUsd: 0n,
|
|
41
41
|
swapProfitFeeUsd: 0n,
|
|
42
|
+
swapProfitUsdIn: 0n,
|
|
42
43
|
payedOutputUsd: 0n,
|
|
43
44
|
payedRemainingCollateralUsd: 0n,
|
|
44
45
|
payedRemainingCollateralAmount: 0n,
|
|
45
46
|
receiveTokenAmount: 0n,
|
|
46
47
|
receiveUsd: 0n,
|
|
48
|
+
primaryOutput: { tokenAddress: "", amount: 0n, usd: 0n },
|
|
49
|
+
secondaryOutput: { tokenAddress: "", amount: 0n, usd: 0n },
|
|
47
50
|
triggerOrderType: orderType,
|
|
48
51
|
triggerThresholdType: undefined,
|
|
49
52
|
decreaseSwapType: DecreasePositionSwapType.NoSwap,
|
|
50
53
|
};
|
|
51
54
|
const pnlToken = isLong ? marketInfo.longToken : marketInfo.shortToken;
|
|
52
|
-
values.decreaseSwapType = getDecreaseSwapType(pnlToken, collateralToken, receiveToken);
|
|
55
|
+
values.decreaseSwapType = forceDecreaseSwapType ?? getDecreaseSwapType(pnlToken, collateralToken, receiveToken);
|
|
53
56
|
const markPrice = getMarkPrice({ prices: indexToken.prices, isIncrease: false, isLong });
|
|
54
57
|
const isTrigger = orderType !== undefined;
|
|
55
58
|
if (orderType) {
|
|
@@ -164,20 +167,25 @@ export function getDecreasePositionAmounts(p) {
|
|
|
164
167
|
values.borrowingFeeUsd = borrowFeeCost.usd;
|
|
165
168
|
const fundingFeeCost = estimateCollateralCost(position.pendingFundingFeesUsd, collateralToken, values.collateralPrice);
|
|
166
169
|
values.fundingFeeUsd = fundingFeeCost.usd;
|
|
167
|
-
if (profitUsd > 0 &&
|
|
170
|
+
if (profitUsd > 0 &&
|
|
171
|
+
(values.decreaseSwapType === DecreasePositionSwapType.SwapPnlTokenToCollateralToken ||
|
|
172
|
+
values.decreaseSwapType === DecreasePositionSwapType.SwapCollateralTokenToPnlToken)) {
|
|
173
|
+
const isSwapToCollateral = values.decreaseSwapType === DecreasePositionSwapType.SwapPnlTokenToCollateralToken;
|
|
168
174
|
const swapProfitStats = getSwapStats({
|
|
169
175
|
marketInfo,
|
|
170
|
-
tokenInAddress: pnlToken.address,
|
|
171
|
-
tokenOutAddress: collateralToken.address,
|
|
176
|
+
tokenInAddress: isSwapToCollateral ? pnlToken.address : collateralToken.address,
|
|
177
|
+
tokenOutAddress: isSwapToCollateral ? collateralToken.address : pnlToken.address,
|
|
172
178
|
usdIn: profitUsd,
|
|
173
179
|
shouldApplyPriceImpact: true,
|
|
174
180
|
swapPricingType: SwapPricingType.Swap,
|
|
175
181
|
});
|
|
176
182
|
values.swapProfitFeeUsd = swapProfitStats.swapFeeUsd - swapProfitStats.priceImpactDeltaUsd;
|
|
183
|
+
values.swapProfitUsdIn = swapProfitStats.usdIn;
|
|
177
184
|
values.swapUiFeeUsd = applyFactor(swapProfitStats.usdIn, uiFeeFactor);
|
|
178
185
|
}
|
|
179
186
|
else {
|
|
180
187
|
values.swapProfitFeeUsd = 0n;
|
|
188
|
+
values.swapProfitUsdIn = 0n;
|
|
181
189
|
}
|
|
182
190
|
const totalFeesUsd = getTotalFeesUsdForDecrease({
|
|
183
191
|
positionFeeUsd: values.positionFeeUsd,
|
|
@@ -235,6 +243,24 @@ export function getDecreasePositionAmounts(p) {
|
|
|
235
243
|
values.receiveTokenAmount = payedInfo.outputAmount;
|
|
236
244
|
}
|
|
237
245
|
values.receiveUsd = convertToUsd(values.receiveTokenAmount, collateralToken.decimals, values.collateralPrice);
|
|
246
|
+
// SwapCollateralTokenToPnlToken → primaryOutput (pnl token), otherwise → secondaryOutput (collateral token)
|
|
247
|
+
const pnlTokenPrice = pnlToken.prices.minPrice;
|
|
248
|
+
if (values.decreaseSwapType === DecreasePositionSwapType.SwapCollateralTokenToPnlToken) {
|
|
249
|
+
values.primaryOutput = {
|
|
250
|
+
tokenAddress: pnlToken.address,
|
|
251
|
+
amount: convertToTokenAmount(values.receiveUsd, pnlToken.decimals, pnlTokenPrice) ?? 0n,
|
|
252
|
+
usd: values.receiveUsd,
|
|
253
|
+
};
|
|
254
|
+
values.secondaryOutput = { tokenAddress: collateralToken.address, amount: 0n, usd: 0n };
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
values.primaryOutput = { tokenAddress: pnlToken.address, amount: 0n, usd: 0n };
|
|
258
|
+
values.secondaryOutput = {
|
|
259
|
+
tokenAddress: collateralToken.address,
|
|
260
|
+
amount: values.receiveTokenAmount,
|
|
261
|
+
usd: values.receiveUsd,
|
|
262
|
+
};
|
|
263
|
+
}
|
|
238
264
|
return values;
|
|
239
265
|
}
|
|
240
266
|
export function getIsFullClose(p) {
|
|
@@ -444,7 +470,7 @@ export function getNextPositionValuesForDecreaseTrade(p) {
|
|
|
444
470
|
nextPendingImpactDeltaUsd,
|
|
445
471
|
};
|
|
446
472
|
}
|
|
447
|
-
function getDecreaseSwapType(pnlToken, collateralToken, receiveToken) {
|
|
473
|
+
export function getDecreaseSwapType(pnlToken, collateralToken, receiveToken) {
|
|
448
474
|
if (getIsEquivalentTokens(pnlToken, collateralToken)) {
|
|
449
475
|
return DecreasePositionSwapType.NoSwap;
|
|
450
476
|
}
|
|
@@ -455,3 +481,97 @@ function getDecreaseSwapType(pnlToken, collateralToken, receiveToken) {
|
|
|
455
481
|
return DecreasePositionSwapType.SwapPnlTokenToCollateralToken;
|
|
456
482
|
}
|
|
457
483
|
}
|
|
484
|
+
export function isThirdTokenDecreaseSwap(pnlToken, collateralToken, receiveToken) {
|
|
485
|
+
return (!getIsEquivalentTokens(receiveToken, pnlToken) &&
|
|
486
|
+
!getIsEquivalentTokens(receiveToken, collateralToken) &&
|
|
487
|
+
!getIsEquivalentTokens(pnlToken, collateralToken));
|
|
488
|
+
}
|
|
489
|
+
export function getCollateralToPnlInternalSwapStats(p) {
|
|
490
|
+
const { pnlToken, receiveUsd } = p;
|
|
491
|
+
// Fees already deducted in getDecreasePositionAmounts — convert at oracle price to avoid double-counting
|
|
492
|
+
const amountOut = convertToTokenAmount(receiveUsd, pnlToken.decimals, pnlToken.prices.minPrice) ?? 0n;
|
|
493
|
+
return { amountOut };
|
|
494
|
+
}
|
|
495
|
+
export function selectBetterDecreaseSwapType(p) {
|
|
496
|
+
return p.pathBUsdOut > p.pathAUsdOut ? "pathB" : "pathA";
|
|
497
|
+
}
|
|
498
|
+
export function getOptimalDecreaseAndSwapAmounts(p) {
|
|
499
|
+
const { marketInfo, collateralToken, isLong, position, closeSizeUsd, keepLeverage, triggerPrice, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, userReferralInfo, minCollateralUsd, minPositionSizeUsd, uiFeeFactor, triggerOrderType, isSetAcceptablePriceImpactEnabled, receiveToken, findSwapPath, findSwapPathFromPnl, marketsInfoData, chainId, } = p;
|
|
500
|
+
const decreaseBaseParams = {
|
|
501
|
+
marketInfo,
|
|
502
|
+
collateralToken,
|
|
503
|
+
isLong,
|
|
504
|
+
position,
|
|
505
|
+
closeSizeUsd,
|
|
506
|
+
keepLeverage,
|
|
507
|
+
triggerPrice,
|
|
508
|
+
fixedAcceptablePriceImpactBps,
|
|
509
|
+
acceptablePriceImpactBuffer,
|
|
510
|
+
userReferralInfo,
|
|
511
|
+
minCollateralUsd,
|
|
512
|
+
minPositionSizeUsd,
|
|
513
|
+
uiFeeFactor,
|
|
514
|
+
triggerOrderType,
|
|
515
|
+
isSetAcceptablePriceImpactEnabled,
|
|
516
|
+
receiveToken,
|
|
517
|
+
};
|
|
518
|
+
const pathADecrease = getDecreasePositionAmounts(decreaseBaseParams);
|
|
519
|
+
const pnlToken = isLong ? marketInfo.longToken : marketInfo.shortToken;
|
|
520
|
+
const shouldSwap = !getIsEquivalentTokens(collateralToken, receiveToken);
|
|
521
|
+
if (!shouldSwap) {
|
|
522
|
+
return { decreaseAmounts: pathADecrease, swapAmounts: undefined };
|
|
523
|
+
}
|
|
524
|
+
const isThirdToken = isThirdTokenDecreaseSwap(pnlToken, collateralToken, receiveToken);
|
|
525
|
+
const pathASwap = pathADecrease.receiveTokenAmount > 0n
|
|
526
|
+
? getSwapAmountsByFromValue({
|
|
527
|
+
tokenIn: collateralToken,
|
|
528
|
+
tokenOut: receiveToken,
|
|
529
|
+
amountIn: pathADecrease.receiveTokenAmount,
|
|
530
|
+
isLimit: false,
|
|
531
|
+
findSwapPath,
|
|
532
|
+
uiFeeFactor,
|
|
533
|
+
marketsInfoData,
|
|
534
|
+
chainId,
|
|
535
|
+
externalSwapQuoteParams: undefined,
|
|
536
|
+
allowSameTokenSwap: false,
|
|
537
|
+
})
|
|
538
|
+
: undefined;
|
|
539
|
+
if (!isThirdToken) {
|
|
540
|
+
return { decreaseAmounts: pathADecrease, swapAmounts: pathASwap };
|
|
541
|
+
}
|
|
542
|
+
// Path B: force internal collateral→pnl swap, then external pnl→receive
|
|
543
|
+
const pathBDecrease = getDecreasePositionAmounts({
|
|
544
|
+
...decreaseBaseParams,
|
|
545
|
+
forceDecreaseSwapType: DecreasePositionSwapType.SwapCollateralTokenToPnlToken,
|
|
546
|
+
});
|
|
547
|
+
if (pathBDecrease.primaryOutput.usd <= 0n) {
|
|
548
|
+
return { decreaseAmounts: pathADecrease, swapAmounts: pathASwap };
|
|
549
|
+
}
|
|
550
|
+
const internalSwapStats = getCollateralToPnlInternalSwapStats({
|
|
551
|
+
pnlToken,
|
|
552
|
+
receiveUsd: pathBDecrease.primaryOutput.usd,
|
|
553
|
+
});
|
|
554
|
+
if (internalSwapStats.amountOut <= 0n) {
|
|
555
|
+
return { decreaseAmounts: pathADecrease, swapAmounts: pathASwap };
|
|
556
|
+
}
|
|
557
|
+
const pathBSwap = getSwapAmountsByFromValue({
|
|
558
|
+
tokenIn: pnlToken,
|
|
559
|
+
tokenOut: receiveToken,
|
|
560
|
+
amountIn: internalSwapStats.amountOut,
|
|
561
|
+
isLimit: false,
|
|
562
|
+
findSwapPath: findSwapPathFromPnl,
|
|
563
|
+
uiFeeFactor,
|
|
564
|
+
marketsInfoData,
|
|
565
|
+
chainId,
|
|
566
|
+
externalSwapQuoteParams: undefined,
|
|
567
|
+
allowSameTokenSwap: false,
|
|
568
|
+
});
|
|
569
|
+
const winner = selectBetterDecreaseSwapType({
|
|
570
|
+
pathAUsdOut: pathASwap?.usdOut ?? 0n,
|
|
571
|
+
pathBUsdOut: pathBSwap.usdOut,
|
|
572
|
+
});
|
|
573
|
+
if (winner === "pathB") {
|
|
574
|
+
return { decreaseAmounts: pathBDecrease, swapAmounts: pathBSwap };
|
|
575
|
+
}
|
|
576
|
+
return { decreaseAmounts: pathADecrease, swapAmounts: pathASwap };
|
|
577
|
+
}
|
|
@@ -7,7 +7,7 @@ import { OrderType } from "../orders/types";
|
|
|
7
7
|
import { getEntryPrice, getLeverage, getLiquidationPrice, getPositionPnlUsd, getPriceImpactDiffUsd, } from "../positions";
|
|
8
8
|
import { getAcceptablePriceInfo, getDefaultAcceptablePriceImpactBps, getMarkPrice, getOrderThresholdType, } from "../prices";
|
|
9
9
|
import { getSwapAmountsByFromValue, getSwapAmountsByToValue } from "../swap";
|
|
10
|
-
import { convertToTokenAmount, convertToUsd,
|
|
10
|
+
import { convertToTokenAmount, convertToUsd, getTokensRatioByPrice } from "../tokens";
|
|
11
11
|
export function getIncreasePositionAmounts(p) {
|
|
12
12
|
const { marketInfo, indexToken, initialCollateralToken, collateralToken, initialCollateralAmount, indexTokenAmount, isLong, leverage, triggerPrice, limitOrderType, position, fixedAcceptablePriceImpactBps, acceptablePriceImpactBuffer, externalSwapQuote, findSwapPath, userReferralInfo, uiFeeFactor, strategy, marketsInfoData, chainId, externalSwapQuoteParams, isSetAcceptablePriceImpactEnabled, } = p;
|
|
13
13
|
const swapStrategy = {
|
|
@@ -328,12 +328,8 @@ export function getIncreasePositionPrices({ triggerPrice, indexToken, initialCol
|
|
|
328
328
|
let collateralPrice;
|
|
329
329
|
if (triggerPrice !== undefined && triggerPrice > 0 && limitOrderType !== undefined) {
|
|
330
330
|
indexPrice = triggerPrice;
|
|
331
|
-
initialCollateralPrice =
|
|
332
|
-
|
|
333
|
-
: initialCollateralToken.prices.minPrice;
|
|
334
|
-
collateralPrice = getIsEquivalentTokens(indexToken, collateralToken)
|
|
335
|
-
? triggerPrice
|
|
336
|
-
: collateralToken.prices.minPrice;
|
|
331
|
+
initialCollateralPrice = initialCollateralToken.prices.minPrice;
|
|
332
|
+
collateralPrice = collateralToken.prices.minPrice;
|
|
337
333
|
triggerThresholdType = getOrderThresholdType(limitOrderType, isLong);
|
|
338
334
|
}
|
|
339
335
|
else {
|