@gmx-io/sdk 0.0.1-alpha-1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +40 -0
- package/README.md +79 -0
- package/build/abis/CustomErrors.json +3254 -0
- package/build/abis/DataStore.json +1440 -0
- package/build/abis/ERC721.json +442 -0
- package/build/abis/EventEmitter.json +2065 -0
- package/build/abis/ExchangeRouter.json +1598 -0
- package/build/abis/GMT.json +522 -0
- package/build/abis/GlpManager.json +592 -0
- package/build/abis/GlvReader.json +1425 -0
- package/build/abis/GlvRouter.json +613 -0
- package/build/abis/GmxMigrator.json +657 -0
- package/build/abis/GovToken.json +1121 -0
- package/build/abis/MintableBaseToken.json +696 -0
- package/build/abis/Multicall.json +333 -0
- package/build/abis/OrderBook.json +1997 -0
- package/build/abis/OrderBookReader.json +111 -0
- package/build/abis/OrderExecutor.json +122 -0
- package/build/abis/PositionManager.json +1165 -0
- package/build/abis/PositionRouter.json +2030 -0
- package/build/abis/Reader.json +355 -0
- package/build/abis/ReaderV2.json +630 -0
- package/build/abis/ReferralStorage.json +507 -0
- package/build/abis/RewardReader.json +86 -0
- package/build/abis/RewardRouter.json +769 -0
- package/build/abis/RewardTracker.json +918 -0
- package/build/abis/Router-v2.json +605 -0
- package/build/abis/Router.json +605 -0
- package/build/abis/SubaccountRouter.json +648 -0
- package/build/abis/SyntheticsReader.json +4577 -0
- package/build/abis/SyntheticsRouter.json +77 -0
- package/build/abis/Timelock.json +1737 -0
- package/build/abis/Token.json +345 -0
- package/build/abis/Treasury.json +411 -0
- package/build/abis/UniPool.json +89 -0
- package/build/abis/UniswapV2.json +1 -0
- package/build/abis/Vault.json +3220 -0
- package/build/abis/VaultReader.json +87 -0
- package/build/abis/VaultV2.json +3019 -0
- package/build/abis/VaultV2b.json +3220 -0
- package/build/abis/Vester.json +1031 -0
- package/build/abis/WETH.json +320 -0
- package/build/abis/YieldFarm.json +599 -0
- package/build/abis/YieldToken.json +560 -0
- package/build/prebuilt/hashedMarketConfigKeys.json +3658 -0
- package/build/prebuilt/hashedMarketValuesKeys.json +1103 -0
- package/build/prebuilt/index.ts +28 -0
- package/build/src/abis/CustomErrors.json +3254 -0
- package/build/src/abis/DataStore.json +1440 -0
- package/build/src/abis/ERC721.json +442 -0
- package/build/src/abis/EventEmitter.json +2065 -0
- package/build/src/abis/ExchangeRouter.json +1598 -0
- package/build/src/abis/GMT.json +522 -0
- package/build/src/abis/GlpManager.json +591 -0
- package/build/src/abis/GlvReader.json +1424 -0
- package/build/src/abis/GlvRouter.json +612 -0
- package/build/src/abis/GmxMigrator.json +657 -0
- package/build/src/abis/GovToken.json +1121 -0
- package/build/src/abis/MintableBaseToken.json +696 -0
- package/build/src/abis/Multicall.json +333 -0
- package/build/src/abis/OrderBook.json +1997 -0
- package/build/src/abis/OrderBookReader.json +111 -0
- package/build/src/abis/OrderExecutor.json +122 -0
- package/build/src/abis/PositionManager.json +1165 -0
- package/build/src/abis/PositionRouter.json +2030 -0
- package/build/src/abis/Reader.json +355 -0
- package/build/src/abis/ReaderV2.json +630 -0
- package/build/src/abis/ReferralStorage.json +507 -0
- package/build/src/abis/RewardReader.json +86 -0
- package/build/src/abis/RewardRouter.json +769 -0
- package/build/src/abis/RewardTracker.json +918 -0
- package/build/src/abis/Router-v2.json +605 -0
- package/build/src/abis/Router.json +605 -0
- package/build/src/abis/SubaccountRouter.json +648 -0
- package/build/src/abis/SyntheticsReader.json +4577 -0
- package/build/src/abis/SyntheticsRouter.json +77 -0
- package/build/src/abis/Timelock.json +1737 -0
- package/build/src/abis/Token.json +345 -0
- package/build/src/abis/Treasury.json +411 -0
- package/build/src/abis/UniPool.json +89 -0
- package/build/src/abis/UniswapV2.json +1 -0
- package/build/src/abis/Vault.json +3220 -0
- package/build/src/abis/VaultReader.json +87 -0
- package/build/src/abis/VaultV2.json +3019 -0
- package/build/src/abis/VaultV2b.json +3220 -0
- package/build/src/abis/Vester.json +1031 -0
- package/build/src/abis/WETH.json +320 -0
- package/build/src/abis/YieldFarm.json +599 -0
- package/build/src/abis/YieldToken.json +560 -0
- package/build/src/configs/batch.d.ts +6 -0
- package/build/src/configs/batch.js +51 -0
- package/build/src/configs/chains.d.ts +37 -0
- package/build/src/configs/chains.js +90 -0
- package/build/src/configs/contracts.d.ts +2 -0
- package/build/src/configs/contracts.js +218 -0
- package/build/src/configs/dataStore.d.ts +149 -0
- package/build/src/configs/dataStore.js +276 -0
- package/build/src/configs/factors.d.ts +20 -0
- package/build/src/configs/factors.js +21 -0
- package/build/src/configs/markets.d.ts +8 -0
- package/build/src/configs/markets.js +528 -0
- package/build/src/configs/subgraph.d.ts +3 -0
- package/build/src/configs/subgraph.js +3 -0
- package/build/src/configs/tokens.d.ts +89 -0
- package/build/src/configs/tokens.js +1269 -0
- package/build/src/index.d.ts +44 -0
- package/build/src/index.js +245 -0
- package/build/src/modules/accounts/accounts.d.ts +5 -0
- package/build/src/modules/accounts/accounts.js +36 -0
- package/build/src/modules/accounts/accounts.spec.d.ts +1 -0
- package/build/src/modules/accounts/accounts.spec.js +7 -0
- package/build/src/modules/base.d.ts +9 -0
- package/build/src/modules/base.js +15 -0
- package/build/src/modules/markets/index.d.ts +26 -0
- package/build/src/modules/markets/index.js +339 -0
- package/build/src/modules/markets/markets.spec.d.ts +1 -0
- package/build/src/modules/markets/markets.spec.js +38 -0
- package/build/src/modules/markets/query-builders.d.ts +40 -0
- package/build/src/modules/markets/query-builders.js +379 -0
- package/build/src/modules/markets/types.d.ts +42 -0
- package/build/src/modules/markets/types.js +1 -0
- package/build/src/modules/oracle.d.ts +31 -0
- package/build/src/modules/oracle.js +53 -0
- package/build/src/modules/orders/orders.d.ts +60 -0
- package/build/src/modules/orders/orders.js +270 -0
- package/build/src/modules/orders/orders.spec.d.ts +1 -0
- package/build/src/modules/orders/orders.spec.js +16 -0
- package/build/src/modules/orders/transactions/cancelOrdersTxn.d.ts +6 -0
- package/build/src/modules/orders/transactions/cancelOrdersTxn.js +15 -0
- package/build/src/modules/orders/transactions/createDecreaseOrderTxn.d.ts +32 -0
- package/build/src/modules/orders/transactions/createDecreaseOrderTxn.js +98 -0
- package/build/src/modules/orders/transactions/createIncreaseOrderTxn.d.ts +77 -0
- package/build/src/modules/orders/transactions/createIncreaseOrderTxn.js +164 -0
- package/build/src/modules/orders/transactions/createSwapOrderTxn.d.ts +16 -0
- package/build/src/modules/orders/transactions/createSwapOrderTxn.js +97 -0
- package/build/src/modules/orders/transactions/createWrapOrUnwrapTxn.d.ts +6 -0
- package/build/src/modules/orders/transactions/createWrapOrUnwrapTxn.js +13 -0
- package/build/src/modules/orders/transactions/updateOrderTxn.d.ts +23 -0
- package/build/src/modules/orders/transactions/updateOrderTxn.js +44 -0
- package/build/src/modules/orders/utils.d.ts +248 -0
- package/build/src/modules/orders/utils.js +151 -0
- package/build/src/modules/positions/positions.d.ts +48 -0
- package/build/src/modules/positions/positions.js +563 -0
- package/build/src/modules/positions/positions.spec.d.ts +1 -0
- package/build/src/modules/positions/positions.spec.js +25 -0
- package/build/src/modules/tokens/tokens.d.ts +26 -0
- package/build/src/modules/tokens/tokens.js +132 -0
- package/build/src/modules/tokens/tokens.spec.d.ts +1 -0
- package/build/src/modules/tokens/tokens.spec.js +19 -0
- package/build/src/modules/trades/trades.d.ts +48 -0
- package/build/src/modules/trades/trades.js +390 -0
- package/build/src/modules/trades/trades.spec.d.ts +1 -0
- package/build/src/modules/trades/trades.spec.js +14 -0
- package/build/src/prebuilt/hashedMarketConfigKeys.json +3658 -0
- package/build/src/prebuilt/hashedMarketValuesKeys.json +1103 -0
- package/build/src/prebuilt/index.d.ts +14 -0
- package/build/src/prebuilt/index.js +9 -0
- package/build/src/types/amounts.d.ts +223 -0
- package/build/src/types/amounts.js +17 -0
- package/build/src/types/fees.d.ts +34 -0
- package/build/src/types/fees.js +1 -0
- package/build/src/types/markets.d.ts +143 -0
- package/build/src/types/markets.js +1 -0
- package/build/src/types/orders.d.ts +74 -0
- package/build/src/types/orders.js +33 -0
- package/build/src/types/positions.d.ts +53 -0
- package/build/src/types/positions.js +1 -0
- package/build/src/types/referrals.d.ts +124 -0
- package/build/src/types/referrals.js +9 -0
- package/build/src/types/sdk.d.ts +32 -0
- package/build/src/types/sdk.js +1 -0
- package/build/src/types/sidecarOrders.d.ts +54 -0
- package/build/src/types/sidecarOrders.js +1 -0
- package/build/src/types/syntheticsEvents.d.ts +88 -0
- package/build/src/types/syntheticsEvents.js +1 -0
- package/build/src/types/tokens.d.ts +89 -0
- package/build/src/types/tokens.js +1 -0
- package/build/src/types/trade.d.ts +223 -0
- package/build/src/types/trade.js +17 -0
- package/build/src/types/tradeHistory.d.ts +107 -0
- package/build/src/types/tradeHistory.js +8 -0
- package/build/src/utils/LruCache.d.ts +10 -0
- package/build/src/utils/LruCache.js +45 -0
- package/build/src/utils/bigmath.d.ts +7 -0
- package/build/src/utils/bigmath.js +23 -0
- package/build/src/utils/buildUrl.d.ts +1 -0
- package/build/src/utils/buildUrl.js +5 -0
- package/build/src/utils/callContract.d.ts +17 -0
- package/build/src/utils/callContract.js +133 -0
- package/build/src/utils/common.d.ts +2 -0
- package/build/src/utils/common.js +2 -0
- package/build/src/utils/fees/estimateOraclePriceCount.d.ts +6 -0
- package/build/src/utils/fees/estimateOraclePriceCount.js +19 -0
- package/build/src/utils/fees/executionFee.d.ts +59 -0
- package/build/src/utils/fees/executionFee.js +108 -0
- package/build/src/utils/fees/index.d.ts +29 -0
- package/build/src/utils/fees/index.js +89 -0
- package/build/src/utils/fees/priceImpact.d.ts +75 -0
- package/build/src/utils/fees/priceImpact.js +287 -0
- package/build/src/utils/graphqlFetcher.d.ts +1 -0
- package/build/src/utils/graphqlFetcher.js +18 -0
- package/build/src/utils/hash.d.ts +5 -0
- package/build/src/utils/hash.js +35 -0
- package/build/src/utils/marketKeysAndConfigs.d.ts +66 -0
- package/build/src/utils/marketKeysAndConfigs.js +257 -0
- package/build/src/utils/markets.d.ts +40 -0
- package/build/src/utils/markets.js +129 -0
- package/build/src/utils/multicall.d.ts +64 -0
- package/build/src/utils/multicall.js +116 -0
- package/build/src/utils/numbers.d.ts +9 -0
- package/build/src/utils/numbers.js +30 -0
- package/build/src/utils/objects.d.ts +18 -0
- package/build/src/utils/objects.js +30 -0
- package/build/src/utils/orders.d.ts +28 -0
- package/build/src/utils/orders.js +178 -0
- package/build/src/utils/positions.d.ts +60 -0
- package/build/src/utils/positions.js +133 -0
- package/build/src/utils/prices.d.ts +15 -0
- package/build/src/utils/prices.js +34 -0
- package/build/src/utils/referrals.d.ts +2 -0
- package/build/src/utils/referrals.js +27 -0
- package/build/src/utils/simulateExecuteOrder.d.ts +16 -0
- package/build/src/utils/simulateExecuteOrder.js +143 -0
- package/build/src/utils/slidingWindowFallbackSwitcher.d.ts +24 -0
- package/build/src/utils/slidingWindowFallbackSwitcher.js +40 -0
- package/build/src/utils/subgraph.d.ts +21 -0
- package/build/src/utils/subgraph.js +72 -0
- package/build/src/utils/swapStats.d.ts +35 -0
- package/build/src/utils/swapStats.js +197 -0
- package/build/src/utils/test.d.ts +2 -0
- package/build/src/utils/test.js +24 -0
- package/build/src/utils/tokens.d.ts +18 -0
- package/build/src/utils/tokens.js +64 -0
- package/build/src/utils/trade.d.ts +10 -0
- package/build/src/utils/trade.js +32 -0
- package/build/tsconfig.tsbuildinfo +1 -0
- package/package.json +96 -0
|
@@ -0,0 +1,339 @@
|
|
|
1
|
+
import { zeroAddress } from "viem";
|
|
2
|
+
import { getContract } from "configs/contracts";
|
|
3
|
+
import { getSubgraphUrl } from "configs/subgraph";
|
|
4
|
+
import { convertTokenAddress, getToken } from "configs/tokens";
|
|
5
|
+
import SyntheticsReader from "abis/SyntheticsReader.json";
|
|
6
|
+
import { TIMEZONE_OFFSET_SEC } from "utils/common";
|
|
7
|
+
import graphqlFetcher from "utils/graphqlFetcher";
|
|
8
|
+
import { getMarketDivisor, getMarketFullName } from "utils/markets";
|
|
9
|
+
import { getByKey } from "utils/objects";
|
|
10
|
+
import { Module } from "../base";
|
|
11
|
+
import { buildClaimableFundingDataRequest, buildMarketsConfigsRequest, buildMarketsValuesRequest, } from "./query-builders";
|
|
12
|
+
export class Markets extends Module {
|
|
13
|
+
async getMarkets(offset = 0n, limit = 100n) {
|
|
14
|
+
if (this._marketsData) {
|
|
15
|
+
return this._marketsData;
|
|
16
|
+
}
|
|
17
|
+
const readerAddress = getContract(this.chainId, "SyntheticsReader");
|
|
18
|
+
const dataStoreAddress = getContract(this.chainId, "DataStore");
|
|
19
|
+
const apiMarkets = await this.sdk.oracle.getMarkets();
|
|
20
|
+
const marketsMap = apiMarkets.reduce((acc, market) => {
|
|
21
|
+
return {
|
|
22
|
+
...acc,
|
|
23
|
+
[market.marketToken]: market,
|
|
24
|
+
};
|
|
25
|
+
}, {});
|
|
26
|
+
const markets = await this.sdk
|
|
27
|
+
.executeMulticall({
|
|
28
|
+
markets: {
|
|
29
|
+
contractAddress: readerAddress,
|
|
30
|
+
abi: SyntheticsReader.abi,
|
|
31
|
+
calls: {
|
|
32
|
+
markets: {
|
|
33
|
+
methodName: "getMarkets",
|
|
34
|
+
params: [dataStoreAddress, offset, offset + limit],
|
|
35
|
+
},
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
})
|
|
39
|
+
.then((res) => {
|
|
40
|
+
return res.data.markets.markets.returnValues.map((market) => {
|
|
41
|
+
return {
|
|
42
|
+
marketTokenAddress: market.marketToken,
|
|
43
|
+
indexTokenAddress: market.indexToken,
|
|
44
|
+
longTokenAddress: market.longToken,
|
|
45
|
+
shortTokenAddress: market.shortToken,
|
|
46
|
+
};
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
const chainId = this.chainId;
|
|
50
|
+
const marketsResult = markets.reduce((acc, market) => {
|
|
51
|
+
try {
|
|
52
|
+
if (!marketsMap[market.marketTokenAddress]?.isListed) {
|
|
53
|
+
return acc;
|
|
54
|
+
}
|
|
55
|
+
const indexToken = getToken(chainId, convertTokenAddress(chainId, market.indexTokenAddress, "native"));
|
|
56
|
+
const longToken = getToken(chainId, market.longTokenAddress);
|
|
57
|
+
const shortToken = getToken(chainId, market.shortTokenAddress);
|
|
58
|
+
const isSameCollaterals = market.longTokenAddress === market.shortTokenAddress;
|
|
59
|
+
const isSpotOnly = market.indexTokenAddress === zeroAddress;
|
|
60
|
+
const name = getMarketFullName({ indexToken, longToken, shortToken, isSpotOnly });
|
|
61
|
+
acc.marketsAddresses.push(market.marketTokenAddress);
|
|
62
|
+
acc.marketsData[market.marketTokenAddress] = {
|
|
63
|
+
...market,
|
|
64
|
+
isSameCollaterals,
|
|
65
|
+
isSpotOnly,
|
|
66
|
+
name,
|
|
67
|
+
data: "",
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
catch (e) {
|
|
71
|
+
// eslint-disable-next-line no-console
|
|
72
|
+
console.warn(`Unsupported market ${market.marketTokenAddress}`, e);
|
|
73
|
+
}
|
|
74
|
+
return acc;
|
|
75
|
+
}, { marketsData: {}, marketsAddresses: [] });
|
|
76
|
+
this._marketsData = marketsResult;
|
|
77
|
+
return marketsResult;
|
|
78
|
+
}
|
|
79
|
+
async getClaimableFundingData() {
|
|
80
|
+
const chainId = this.chainId;
|
|
81
|
+
const account = this.account;
|
|
82
|
+
if (!account) {
|
|
83
|
+
return {};
|
|
84
|
+
}
|
|
85
|
+
const { marketsAddresses, marketsData } = await this.getMarkets();
|
|
86
|
+
return this.sdk
|
|
87
|
+
.executeMulticall(buildClaimableFundingDataRequest({
|
|
88
|
+
chainId,
|
|
89
|
+
account,
|
|
90
|
+
marketsAddresses,
|
|
91
|
+
marketsData,
|
|
92
|
+
}))
|
|
93
|
+
.then((result) => {
|
|
94
|
+
return Object.entries(result.data).reduce((claimableFundingData, [marketAddress, callsResult]) => {
|
|
95
|
+
const market = getByKey(marketsData, marketAddress);
|
|
96
|
+
if (!market) {
|
|
97
|
+
return claimableFundingData;
|
|
98
|
+
}
|
|
99
|
+
const marketDivisor = getMarketDivisor(market);
|
|
100
|
+
claimableFundingData[marketAddress] = {
|
|
101
|
+
claimableFundingAmountLong: callsResult.claimableFundingAmountLong.returnValues[0] / marketDivisor,
|
|
102
|
+
claimableFundingAmountShort: callsResult.claimableFundingAmountShort.returnValues[0] / marketDivisor,
|
|
103
|
+
};
|
|
104
|
+
return claimableFundingData;
|
|
105
|
+
}, {});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
async getMarketsValues({ marketsAddresses, marketsData, tokensData, }) {
|
|
109
|
+
const dataStoreAddress = getContract(this.chainId, "DataStore");
|
|
110
|
+
const syntheticsReaderAddress = getContract(this.chainId, "SyntheticsReader");
|
|
111
|
+
const request = await buildMarketsValuesRequest(this.chainId, {
|
|
112
|
+
marketsAddresses,
|
|
113
|
+
marketsData,
|
|
114
|
+
tokensData,
|
|
115
|
+
dataStoreAddress,
|
|
116
|
+
syntheticsReaderAddress,
|
|
117
|
+
});
|
|
118
|
+
return this.sdk.executeMulticall(request).then((res) => {
|
|
119
|
+
const result = marketsAddresses.reduce((acc, marketAddress) => {
|
|
120
|
+
const readerErrors = res.errors[`${marketAddress}-reader`];
|
|
121
|
+
const dataStoreErrors = res.errors[`${marketAddress}-dataStore`];
|
|
122
|
+
const readerValues = res.data[`${marketAddress}-reader`];
|
|
123
|
+
const dataStoreValues = res.data[`${marketAddress}-dataStore`];
|
|
124
|
+
// Skip invalid market
|
|
125
|
+
if (!readerValues || !dataStoreValues || readerErrors || dataStoreErrors) {
|
|
126
|
+
// eslint-disable-next-line no-console
|
|
127
|
+
console.log("market info error", marketAddress, readerErrors, dataStoreErrors, readerValues);
|
|
128
|
+
return acc;
|
|
129
|
+
}
|
|
130
|
+
const market = getByKey(marketsData, marketAddress);
|
|
131
|
+
const marketDivisor = market.isSameCollaterals ? 2n : 1n;
|
|
132
|
+
const longInterestUsingLongToken = BigInt(dataStoreValues.longInterestUsingLongToken.returnValues[0]) / marketDivisor;
|
|
133
|
+
const longInterestUsingShortToken = BigInt(dataStoreValues.longInterestUsingShortToken.returnValues[0]) / marketDivisor;
|
|
134
|
+
const shortInterestUsingLongToken = BigInt(dataStoreValues.shortInterestUsingLongToken.returnValues[0]) / marketDivisor;
|
|
135
|
+
const shortInterestUsingShortToken = BigInt(dataStoreValues.shortInterestUsingShortToken.returnValues[0]) / marketDivisor;
|
|
136
|
+
const longInterestUsd = longInterestUsingLongToken + longInterestUsingShortToken;
|
|
137
|
+
const shortInterestUsd = shortInterestUsingLongToken + shortInterestUsingShortToken;
|
|
138
|
+
const longInterestInTokensUsingLongToken = BigInt(dataStoreValues.longInterestInTokensUsingLongToken.returnValues[0]) / marketDivisor;
|
|
139
|
+
const longInterestInTokensUsingShortToken = BigInt(dataStoreValues.longInterestInTokensUsingShortToken.returnValues[0]) / marketDivisor;
|
|
140
|
+
const shortInterestInTokensUsingLongToken = BigInt(dataStoreValues.shortInterestInTokensUsingLongToken.returnValues[0]) / marketDivisor;
|
|
141
|
+
const shortInterestInTokensUsingShortToken = BigInt(dataStoreValues.shortInterestInTokensUsingShortToken.returnValues[0]) / marketDivisor;
|
|
142
|
+
const longInterestInTokens = longInterestInTokensUsingLongToken + longInterestInTokensUsingShortToken;
|
|
143
|
+
const shortInterestInTokens = shortInterestInTokensUsingLongToken + shortInterestInTokensUsingShortToken;
|
|
144
|
+
const { nextFunding, virtualInventory } = readerValues.marketInfo.returnValues;
|
|
145
|
+
const [, poolValueInfoMin] = readerValues.marketTokenPriceMin.returnValues;
|
|
146
|
+
const [, poolValueInfoMax] = readerValues.marketTokenPriceMax.returnValues;
|
|
147
|
+
acc[marketAddress] = {
|
|
148
|
+
longInterestUsd,
|
|
149
|
+
shortInterestUsd,
|
|
150
|
+
longInterestInTokens,
|
|
151
|
+
shortInterestInTokens,
|
|
152
|
+
longPoolAmount: dataStoreValues.longPoolAmount.returnValues[0] / marketDivisor,
|
|
153
|
+
shortPoolAmount: dataStoreValues.shortPoolAmount.returnValues[0] / marketDivisor,
|
|
154
|
+
poolValueMin: poolValueInfoMin.poolValue,
|
|
155
|
+
poolValueMax: poolValueInfoMax.poolValue,
|
|
156
|
+
totalBorrowingFees: poolValueInfoMax.totalBorrowingFees,
|
|
157
|
+
positionImpactPoolAmount: dataStoreValues.positionImpactPoolAmount.returnValues[0],
|
|
158
|
+
swapImpactPoolAmountLong: dataStoreValues.swapImpactPoolAmountLong.returnValues[0],
|
|
159
|
+
swapImpactPoolAmountShort: dataStoreValues.swapImpactPoolAmountShort.returnValues[0],
|
|
160
|
+
pnlLongMax: poolValueInfoMax.longPnl,
|
|
161
|
+
pnlLongMin: poolValueInfoMin.longPnl,
|
|
162
|
+
pnlShortMax: poolValueInfoMax.shortPnl,
|
|
163
|
+
pnlShortMin: poolValueInfoMin.shortPnl,
|
|
164
|
+
netPnlMax: poolValueInfoMax.netPnl,
|
|
165
|
+
netPnlMin: poolValueInfoMin.netPnl,
|
|
166
|
+
borrowingFactorPerSecondForLongs: readerValues.marketInfo.returnValues.borrowingFactorPerSecondForLongs,
|
|
167
|
+
borrowingFactorPerSecondForShorts: readerValues.marketInfo.returnValues.borrowingFactorPerSecondForShorts,
|
|
168
|
+
fundingFactorPerSecond: nextFunding.fundingFactorPerSecond,
|
|
169
|
+
longsPayShorts: nextFunding.longsPayShorts,
|
|
170
|
+
virtualPoolAmountForLongToken: virtualInventory.virtualPoolAmountForLongToken,
|
|
171
|
+
virtualPoolAmountForShortToken: virtualInventory.virtualPoolAmountForShortToken,
|
|
172
|
+
virtualInventoryForPositions: virtualInventory.virtualInventoryForPositions,
|
|
173
|
+
};
|
|
174
|
+
return acc;
|
|
175
|
+
}, {});
|
|
176
|
+
return result;
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Fetch market configs from the blockchain
|
|
181
|
+
*/
|
|
182
|
+
async getMarketsConfigs({ marketsAddresses, marketsData, }) {
|
|
183
|
+
const dataStoreAddress = getContract(this.chainId, "DataStore");
|
|
184
|
+
const request = await buildMarketsConfigsRequest(this.chainId, {
|
|
185
|
+
marketsData,
|
|
186
|
+
marketsAddresses,
|
|
187
|
+
dataStoreAddress,
|
|
188
|
+
});
|
|
189
|
+
return this.sdk.executeMulticall(request).then((res) => {
|
|
190
|
+
const result = marketsAddresses.reduce((acc, marketAddress) => {
|
|
191
|
+
const dataStoreErrors = res.errors[`${marketAddress}-dataStore`];
|
|
192
|
+
const dataStoreValues = res.data[`${marketAddress}-dataStore`];
|
|
193
|
+
// Skip invalid market
|
|
194
|
+
if (!dataStoreValues || dataStoreErrors) {
|
|
195
|
+
// eslint-disable-next-line no-console
|
|
196
|
+
console.log("Market info error", marketAddress, dataStoreErrors, dataStoreValues);
|
|
197
|
+
return acc;
|
|
198
|
+
}
|
|
199
|
+
acc[marketAddress] = {
|
|
200
|
+
isDisabled: dataStoreValues.isDisabled.returnValues[0],
|
|
201
|
+
maxLongPoolUsdForDeposit: dataStoreValues.maxLongPoolUsdForDeposit.returnValues[0],
|
|
202
|
+
maxShortPoolUsdForDeposit: dataStoreValues.maxShortPoolUsdForDeposit.returnValues[0],
|
|
203
|
+
maxLongPoolAmount: dataStoreValues.maxLongPoolAmount.returnValues[0],
|
|
204
|
+
maxShortPoolAmount: dataStoreValues.maxShortPoolAmount.returnValues[0],
|
|
205
|
+
longPoolAmountAdjustment: dataStoreValues.longPoolAmountAdjustment.returnValues[0],
|
|
206
|
+
shortPoolAmountAdjustment: dataStoreValues.shortPoolAmountAdjustment.returnValues[0],
|
|
207
|
+
reserveFactorLong: dataStoreValues.reserveFactorLong.returnValues[0],
|
|
208
|
+
reserveFactorShort: dataStoreValues.reserveFactorShort.returnValues[0],
|
|
209
|
+
openInterestReserveFactorLong: dataStoreValues.openInterestReserveFactorLong.returnValues[0],
|
|
210
|
+
openInterestReserveFactorShort: dataStoreValues.openInterestReserveFactorShort.returnValues[0],
|
|
211
|
+
maxOpenInterestLong: dataStoreValues.maxOpenInterestLong.returnValues[0],
|
|
212
|
+
maxOpenInterestShort: dataStoreValues.maxOpenInterestShort.returnValues[0],
|
|
213
|
+
minPositionImpactPoolAmount: dataStoreValues.minPositionImpactPoolAmount.returnValues[0],
|
|
214
|
+
positionImpactPoolDistributionRate: dataStoreValues.positionImpactPoolDistributionRate.returnValues[0],
|
|
215
|
+
borrowingFactorLong: dataStoreValues.borrowingFactorLong.returnValues[0],
|
|
216
|
+
borrowingFactorShort: dataStoreValues.borrowingFactorShort.returnValues[0],
|
|
217
|
+
borrowingExponentFactorLong: dataStoreValues.borrowingExponentFactorLong.returnValues[0],
|
|
218
|
+
borrowingExponentFactorShort: dataStoreValues.borrowingExponentFactorShort.returnValues[0],
|
|
219
|
+
fundingFactor: dataStoreValues.fundingFactor.returnValues[0],
|
|
220
|
+
fundingExponentFactor: dataStoreValues.fundingExponentFactor.returnValues[0],
|
|
221
|
+
fundingIncreaseFactorPerSecond: dataStoreValues.fundingIncreaseFactorPerSecond.returnValues[0],
|
|
222
|
+
fundingDecreaseFactorPerSecond: dataStoreValues.fundingDecreaseFactorPerSecond.returnValues[0],
|
|
223
|
+
thresholdForDecreaseFunding: dataStoreValues.thresholdForDecreaseFunding.returnValues[0],
|
|
224
|
+
thresholdForStableFunding: dataStoreValues.thresholdForStableFunding.returnValues[0],
|
|
225
|
+
minFundingFactorPerSecond: dataStoreValues.minFundingFactorPerSecond.returnValues[0],
|
|
226
|
+
maxFundingFactorPerSecond: dataStoreValues.maxFundingFactorPerSecond.returnValues[0],
|
|
227
|
+
maxPnlFactorForTradersLong: dataStoreValues.maxPnlFactorForTradersLong.returnValues[0],
|
|
228
|
+
maxPnlFactorForTradersShort: dataStoreValues.maxPnlFactorForTradersShort.returnValues[0],
|
|
229
|
+
minCollateralFactor: dataStoreValues.minCollateralFactor.returnValues[0],
|
|
230
|
+
minCollateralFactorForOpenInterestLong: dataStoreValues.minCollateralFactorForOpenInterestLong.returnValues[0],
|
|
231
|
+
minCollateralFactorForOpenInterestShort: dataStoreValues.minCollateralFactorForOpenInterestShort.returnValues[0],
|
|
232
|
+
positionFeeFactorForPositiveImpact: dataStoreValues.positionFeeFactorForPositiveImpact.returnValues[0],
|
|
233
|
+
positionFeeFactorForNegativeImpact: dataStoreValues.positionFeeFactorForNegativeImpact.returnValues[0],
|
|
234
|
+
positionImpactFactorPositive: dataStoreValues.positionImpactFactorPositive.returnValues[0],
|
|
235
|
+
positionImpactFactorNegative: dataStoreValues.positionImpactFactorNegative.returnValues[0],
|
|
236
|
+
maxPositionImpactFactorPositive: dataStoreValues.maxPositionImpactFactorPositive.returnValues[0],
|
|
237
|
+
maxPositionImpactFactorNegative: dataStoreValues.maxPositionImpactFactorNegative.returnValues[0],
|
|
238
|
+
maxPositionImpactFactorForLiquidations: dataStoreValues.maxPositionImpactFactorForLiquidations.returnValues[0],
|
|
239
|
+
positionImpactExponentFactor: dataStoreValues.positionImpactExponentFactor.returnValues[0],
|
|
240
|
+
swapFeeFactorForPositiveImpact: dataStoreValues.swapFeeFactorForPositiveImpact.returnValues[0],
|
|
241
|
+
swapFeeFactorForNegativeImpact: dataStoreValues.swapFeeFactorForNegativeImpact.returnValues[0],
|
|
242
|
+
swapImpactFactorPositive: dataStoreValues.swapImpactFactorPositive.returnValues[0],
|
|
243
|
+
swapImpactFactorNegative: dataStoreValues.swapImpactFactorNegative.returnValues[0],
|
|
244
|
+
swapImpactExponentFactor: dataStoreValues.swapImpactExponentFactor.returnValues[0],
|
|
245
|
+
virtualMarketId: dataStoreValues.virtualMarketId.returnValues[0],
|
|
246
|
+
virtualLongTokenId: dataStoreValues.virtualLongTokenId.returnValues[0],
|
|
247
|
+
virtualShortTokenId: dataStoreValues.virtualShortTokenId.returnValues[0],
|
|
248
|
+
};
|
|
249
|
+
return acc;
|
|
250
|
+
}, {});
|
|
251
|
+
return result;
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
async getMarketsInfo() {
|
|
255
|
+
const { marketsData, marketsAddresses } = await this.getMarkets();
|
|
256
|
+
const { tokensData, pricesUpdatedAt, error: tokensDataError, isBalancesLoaded, } = await this.sdk.tokens.getTokensData();
|
|
257
|
+
const [marketsValues, marketsConfigs, claimableFundingData] = await Promise.all([
|
|
258
|
+
this.getMarketsValues({
|
|
259
|
+
account: this.sdk.config.account,
|
|
260
|
+
marketsAddresses,
|
|
261
|
+
marketsData,
|
|
262
|
+
tokensData,
|
|
263
|
+
}),
|
|
264
|
+
this.getMarketsConfigs({
|
|
265
|
+
marketsAddresses,
|
|
266
|
+
marketsData,
|
|
267
|
+
}),
|
|
268
|
+
this.getClaimableFundingData(),
|
|
269
|
+
]);
|
|
270
|
+
if (!marketsValues || !marketsConfigs || !marketsAddresses || !claimableFundingData) {
|
|
271
|
+
return {
|
|
272
|
+
marketsInfoData: {},
|
|
273
|
+
tokensData,
|
|
274
|
+
pricesUpdatedAt,
|
|
275
|
+
error: tokensDataError,
|
|
276
|
+
isBalancesLoaded,
|
|
277
|
+
};
|
|
278
|
+
}
|
|
279
|
+
// Manual merging to avoid cloning tokens as they are sometimes compared by reference
|
|
280
|
+
const marketsInfoData = {};
|
|
281
|
+
for (const marketAddress of marketsAddresses) {
|
|
282
|
+
const market = marketsData?.[marketAddress];
|
|
283
|
+
const marketValues = marketsValues[marketAddress];
|
|
284
|
+
const marketConfig = marketsConfigs[marketAddress];
|
|
285
|
+
const longToken = getByKey(tokensData, market?.longTokenAddress);
|
|
286
|
+
const shortToken = getByKey(tokensData, market?.shortTokenAddress);
|
|
287
|
+
const indexToken = market
|
|
288
|
+
? getByKey(tokensData, convertTokenAddress(this.chainId, market.indexTokenAddress, "native"))
|
|
289
|
+
: undefined;
|
|
290
|
+
if (!market || !marketValues || !marketConfig || !longToken || !shortToken || !indexToken) {
|
|
291
|
+
continue;
|
|
292
|
+
}
|
|
293
|
+
const fullMarketInfo = {
|
|
294
|
+
...marketValues,
|
|
295
|
+
...marketConfig,
|
|
296
|
+
...claimableFundingData,
|
|
297
|
+
...market,
|
|
298
|
+
longToken,
|
|
299
|
+
shortToken,
|
|
300
|
+
indexToken,
|
|
301
|
+
};
|
|
302
|
+
marketsInfoData[marketAddress] = fullMarketInfo;
|
|
303
|
+
}
|
|
304
|
+
const error = tokensDataError;
|
|
305
|
+
return {
|
|
306
|
+
marketsInfoData,
|
|
307
|
+
tokensData,
|
|
308
|
+
pricesUpdatedAt,
|
|
309
|
+
error,
|
|
310
|
+
isBalancesLoaded,
|
|
311
|
+
};
|
|
312
|
+
}
|
|
313
|
+
async getDailyVolumes() {
|
|
314
|
+
const { marketsAddresses } = await this.getMarkets();
|
|
315
|
+
const endpoint = getSubgraphUrl(this.sdk, "subsquid");
|
|
316
|
+
if (!marketsAddresses || !endpoint) {
|
|
317
|
+
return;
|
|
318
|
+
}
|
|
319
|
+
const LAST_DAY_UNIX_TIMESTAMP = Math.floor(Date.now() / 1000) - 24 * 60 * 60;
|
|
320
|
+
const timestamp = LAST_DAY_UNIX_TIMESTAMP + TIMEZONE_OFFSET_SEC;
|
|
321
|
+
const variables = {
|
|
322
|
+
timestamp: timestamp,
|
|
323
|
+
};
|
|
324
|
+
return graphqlFetcher(endpoint, POSITIONS_VOLUME_INFOS_QUERY, variables).then((data) => {
|
|
325
|
+
return data?.positionsVolume.length
|
|
326
|
+
? data?.positionsVolume.reduce((acc, { market, volume }) => {
|
|
327
|
+
return { ...acc, [market]: BigInt(volume) };
|
|
328
|
+
})
|
|
329
|
+
: {};
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
const POSITIONS_VOLUME_INFOS_QUERY = `
|
|
334
|
+
query PositionVolumeInfoResolver($timestamp: Float!) {
|
|
335
|
+
positionsVolume(where: {timestamp: $timestamp}) {
|
|
336
|
+
market
|
|
337
|
+
volume
|
|
338
|
+
}
|
|
339
|
+
}`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { arbitrumSdk } from "utils/test";
|
|
2
|
+
describe("Markets", () => {
|
|
3
|
+
describe("getMarkets", () => {
|
|
4
|
+
it("should be able to get markets", async () => {
|
|
5
|
+
const response = await arbitrumSdk.markets.getMarkets();
|
|
6
|
+
expect(response).toBeDefined();
|
|
7
|
+
});
|
|
8
|
+
});
|
|
9
|
+
describe("getMarkets", () => {
|
|
10
|
+
it("should be able to get markets data", async () => {
|
|
11
|
+
const marketsData = await arbitrumSdk.markets.getMarkets();
|
|
12
|
+
expect(marketsData.marketsAddresses).toBeDefined();
|
|
13
|
+
expect(marketsData.marketsData).toBeDefined();
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
describe("getMarketsConfigs", () => {
|
|
17
|
+
it("should be able to get markets configs", async () => {
|
|
18
|
+
const data = await arbitrumSdk.markets.getMarkets();
|
|
19
|
+
const response = await arbitrumSdk.markets.getMarketsConfigs({
|
|
20
|
+
marketsAddresses: data.marketsAddresses,
|
|
21
|
+
marketsData: data.marketsData,
|
|
22
|
+
});
|
|
23
|
+
expect(response).toBeDefined();
|
|
24
|
+
});
|
|
25
|
+
});
|
|
26
|
+
describe("getMarketsInfo", () => {
|
|
27
|
+
it("should be able to get markets info", async () => {
|
|
28
|
+
const response = await arbitrumSdk.markets.getMarketsInfo();
|
|
29
|
+
expect(response).toBeDefined();
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
describe("getDailyVolumes", () => {
|
|
33
|
+
it("should be able to get daily volumes", async () => {
|
|
34
|
+
const response = await arbitrumSdk.markets.getDailyVolumes();
|
|
35
|
+
expect(response).toBeDefined();
|
|
36
|
+
});
|
|
37
|
+
});
|
|
38
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { MarketsData } from "types/markets";
|
|
2
|
+
import { TokensData } from "types/tokens";
|
|
3
|
+
export declare function buildClaimableFundingDataRequest({ marketsAddresses, marketsData, chainId, account, }: {
|
|
4
|
+
marketsAddresses: string[] | undefined;
|
|
5
|
+
marketsData: MarketsData | undefined;
|
|
6
|
+
account: string;
|
|
7
|
+
chainId: number;
|
|
8
|
+
}): {};
|
|
9
|
+
export declare function buildMarketsValuesRequest(chainId: number, { marketsAddresses, marketsData, tokensData, dataStoreAddress, syntheticsReaderAddress, }: {
|
|
10
|
+
marketsAddresses: string[] | undefined;
|
|
11
|
+
marketsData: MarketsData | undefined;
|
|
12
|
+
tokensData: TokensData | undefined;
|
|
13
|
+
dataStoreAddress: string;
|
|
14
|
+
syntheticsReaderAddress: string;
|
|
15
|
+
}): Promise<import("./types").MarketValuesMulticallRequestConfig<{
|
|
16
|
+
[key: `${string}-reader`]: {
|
|
17
|
+
calls: Record<"marketInfo" | "marketTokenPriceMax" | "marketTokenPriceMin", {
|
|
18
|
+
methodName: string;
|
|
19
|
+
params: any[];
|
|
20
|
+
}>;
|
|
21
|
+
};
|
|
22
|
+
[key: `${string}-dataStore`]: {
|
|
23
|
+
calls: Record<"longPoolAmount" | "shortPoolAmount" | "positionImpactPoolAmount" | "swapImpactPoolAmountLong" | "swapImpactPoolAmountShort" | "longInterestUsingLongToken" | "longInterestUsingShortToken" | "shortInterestUsingLongToken" | "shortInterestUsingShortToken" | "longInterestInTokensUsingLongToken" | "longInterestInTokensUsingShortToken" | "shortInterestInTokensUsingLongToken" | "shortInterestInTokensUsingShortToken", {
|
|
24
|
+
methodName: string;
|
|
25
|
+
params: any[];
|
|
26
|
+
}>;
|
|
27
|
+
};
|
|
28
|
+
}>>;
|
|
29
|
+
export declare function buildMarketsConfigsRequest(chainId: number, { marketsData, marketsAddresses, dataStoreAddress, }: {
|
|
30
|
+
marketsData: MarketsData | undefined;
|
|
31
|
+
marketsAddresses: string[] | undefined;
|
|
32
|
+
dataStoreAddress: string;
|
|
33
|
+
}): Promise<import("./types").MarketValuesMulticallRequestConfig<{
|
|
34
|
+
[key: `${string}-dataStore`]: {
|
|
35
|
+
calls: Record<"isDisabled" | "maxLongPoolAmount" | "maxShortPoolAmount" | "maxLongPoolUsdForDeposit" | "maxShortPoolUsdForDeposit" | "longPoolAmountAdjustment" | "shortPoolAmountAdjustment" | "reserveFactorLong" | "reserveFactorShort" | "openInterestReserveFactorLong" | "openInterestReserveFactorShort" | "maxOpenInterestLong" | "maxOpenInterestShort" | "borrowingFactorLong" | "borrowingFactorShort" | "borrowingExponentFactorLong" | "borrowingExponentFactorShort" | "fundingFactor" | "fundingExponentFactor" | "fundingIncreaseFactorPerSecond" | "fundingDecreaseFactorPerSecond" | "thresholdForStableFunding" | "thresholdForDecreaseFunding" | "minFundingFactorPerSecond" | "maxFundingFactorPerSecond" | "minPositionImpactPoolAmount" | "positionImpactPoolDistributionRate" | "minCollateralFactor" | "minCollateralFactorForOpenInterestLong" | "minCollateralFactorForOpenInterestShort" | "maxPnlFactorForTradersLong" | "maxPnlFactorForTradersShort" | "positionFeeFactorForPositiveImpact" | "positionFeeFactorForNegativeImpact" | "positionImpactFactorPositive" | "positionImpactFactorNegative" | "maxPositionImpactFactorPositive" | "maxPositionImpactFactorNegative" | "maxPositionImpactFactorForLiquidations" | "positionImpactExponentFactor" | "swapFeeFactorForPositiveImpact" | "swapFeeFactorForNegativeImpact" | "swapImpactFactorPositive" | "swapImpactFactorNegative" | "swapImpactExponentFactor" | "virtualMarketId" | "virtualLongTokenId" | "virtualShortTokenId", {
|
|
36
|
+
methodName: string;
|
|
37
|
+
params: any[];
|
|
38
|
+
}>;
|
|
39
|
+
};
|
|
40
|
+
}>>;
|