-
Notifications
You must be signed in to change notification settings - Fork 1k
feat: add hyperswap-v2 #2285
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: add hyperswap-v2 #2285
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| const { request, gql } = require('graphql-request'); | ||
| const utils = require('../utils'); | ||
|
|
||
| const PROJECT = 'hyperswap-v2'; | ||
| const CHAIN = 'hyperevm'; | ||
| const MIN_TVL_USD = 1000; | ||
|
|
||
| const SUBGRAPH_URL = | ||
| 'https://api.goldsky.com/api/public/project_cm97l77ib0cz601wlgi9wb0ec/subgraphs/hyperswap-v2/1.0.5/gn'; | ||
|
|
||
| const FEE_TIER = 3000; | ||
|
|
||
| const pairsQuery = gql` | ||
| query getPairs($first: Int!, $skip: Int!) { | ||
| pairs( | ||
| first: $first | ||
| skip: $skip | ||
| orderBy: reserveUSD | ||
| orderDirection: desc | ||
| where: { reserveUSD_gt: 1000 } | ||
| ) { | ||
| id | ||
| token0 { | ||
| id | ||
| symbol | ||
| decimals | ||
| } | ||
| token1 { | ||
| id | ||
| symbol | ||
| decimals | ||
| } | ||
| reserve0 | ||
| reserve1 | ||
| reserveUSD | ||
| volumeUSD | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| const pairDayDataQuery = gql` | ||
| query getPairDayData($pairAddresses: [String!], $startTime: Int!) { | ||
| pairDayDatas( | ||
| first: 1000 | ||
| orderBy: date | ||
| orderDirection: desc | ||
| where: { pairAddress_in: $pairAddresses, date_gt: $startTime } | ||
| ) { | ||
| pairAddress | ||
| dailyVolumeUSD | ||
| date | ||
| } | ||
| } | ||
| `; | ||
|
|
||
| async function fetchAllPairs() { | ||
| let allPairs = []; | ||
| let skip = 0; | ||
| const first = 1000; | ||
|
|
||
| while (true) { | ||
| try { | ||
| const data = await request(SUBGRAPH_URL, pairsQuery, { | ||
| first, | ||
| skip, | ||
| }); | ||
| const pairs = data.pairs; | ||
|
|
||
| if (pairs.length === 0) break; | ||
|
|
||
| allPairs = allPairs.concat(pairs); | ||
|
|
||
| if (pairs.length < first) break; | ||
|
|
||
| skip += first; | ||
| } catch (error) { | ||
| console.error('Error fetching pairs from subgraph:', error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| return allPairs; | ||
| } | ||
|
|
||
| async function fetchPairDayData(pairAddresses) { | ||
| // Get data from the last 7 days | ||
| const startTime = Math.floor(Date.now() / 1000) - 7 * 24 * 60 * 60; | ||
|
|
||
| try { | ||
| const result = await request(SUBGRAPH_URL, pairDayDataQuery, { | ||
| pairAddresses, | ||
| startTime, | ||
| }); | ||
|
|
||
| const volumesByPair = {}; | ||
| const lastDayVolumeByPair = {}; | ||
|
|
||
| const pairDayDatas = result.pairDayDatas || []; | ||
|
|
||
| for (const dayData of pairDayDatas) { | ||
| const pairAddr = dayData.pairAddress.toLowerCase(); | ||
| const volume = parseFloat(dayData.dailyVolumeUSD) || 0; | ||
|
|
||
| if (!volumesByPair[pairAddr]) { | ||
| volumesByPair[pairAddr] = 0; | ||
| } | ||
| volumesByPair[pairAddr] += volume; | ||
|
|
||
| if ( | ||
| !lastDayVolumeByPair[pairAddr] || | ||
| dayData.date > lastDayVolumeByPair[pairAddr].date | ||
| ) { | ||
| lastDayVolumeByPair[pairAddr] = { | ||
| date: dayData.date, | ||
| volume: volume, | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| weeklyVolumes: volumesByPair, | ||
| lastDayVolumes: Object.fromEntries( | ||
| Object.entries(lastDayVolumeByPair).map(([k, v]) => [k, v.volume]) | ||
| ), | ||
| }; | ||
| } catch (error) { | ||
| console.error('Error fetching pair day data:', error); | ||
| return { weeklyVolumes: {}, lastDayVolumes: {} }; | ||
| } | ||
| } | ||
|
|
||
| function calculateApyBase(volumeUSD1d, tvlUSD) { | ||
| if (!tvlUSD || tvlUSD <= 0) return 0; | ||
| if (!volumeUSD1d || volumeUSD1d <= 0) return 0; | ||
|
|
||
| // Fee is 0.3% of volume (FEE_TIER / 1e6) | ||
| const feeUSD1d = (volumeUSD1d * FEE_TIER) / 1e6; | ||
| const apyBase = ((feeUSD1d * 365) / tvlUSD) * 100; | ||
| return apyBase; | ||
| } | ||
|
|
||
| async function apy() { | ||
| try { | ||
| const pairs = await fetchAllPairs(); | ||
|
|
||
| const pairAddresses = pairs.map((p) => p.id.toLowerCase()); | ||
|
|
||
| const { weeklyVolumes, lastDayVolumes } = | ||
| await fetchPairDayData(pairAddresses); | ||
|
|
||
| const formattedPools = pairs | ||
| .map((pair) => { | ||
| const tvlUSD = Number(pair.reserveUSD) || 0; | ||
|
|
||
| if (tvlUSD < MIN_TVL_USD) return null; | ||
|
|
||
| const pairId = pair.id.toLowerCase(); | ||
| const volumeUSD1d = lastDayVolumes[pairId] || 0; | ||
| const volumeUSD7d = weeklyVolumes[pairId] || 0; | ||
|
|
||
| const apyBase = calculateApyBase(volumeUSD1d, tvlUSD); | ||
|
|
||
| // 7-day APY calculation (annualised from weekly) | ||
| const apyBase7d = | ||
| volumeUSD7d > 0 | ||
| ? ((volumeUSD7d * FEE_TIER) / 1e6 / tvlUSD) * 52 * 100 | ||
| : null; | ||
|
|
||
| return { | ||
| pool: pairId, | ||
| chain: utils.formatChain(CHAIN), | ||
| project: PROJECT, | ||
| symbol: utils.formatSymbol( | ||
| `${pair.token0.symbol}-${pair.token1.symbol}` | ||
| ), | ||
| tvlUsd: tvlUSD, | ||
| apyBase: apyBase || 0, | ||
| apyBase7d: apyBase7d, | ||
| underlyingTokens: [ | ||
| pair.token0.id.toLowerCase(), | ||
| pair.token1.id.toLowerCase(), | ||
| ], | ||
| url: `https://app.hyperswap.exchange/#/add/v2/${pair.token0.id}/${pair.token1.id}`, | ||
| volumeUsd1d: volumeUSD1d, | ||
| volumeUsd7d: volumeUSD7d, | ||
| }; | ||
| }) | ||
| .filter((pool) => pool !== null); | ||
|
|
||
| return formattedPools.filter((p) => utils.keepFinite(p)); | ||
| } catch (error) { | ||
| console.error('Error in HyperSwap V2 adapter:', error); | ||
| throw error; | ||
| } | ||
| } | ||
|
|
||
| module.exports = { | ||
| timetravel: false, | ||
| apy, | ||
| url: 'https://app.hyperswap.exchange', | ||
| }; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
🏁 Script executed:
Repository: DefiLlama/yield-server
Length of output: 3074
🌐 Web query:
Goldsky Hyperswap v2 subgraph pairDayDatas query skip pagination limits💡 Result:
On Goldsky, Hyperswap v2 subgraphs use Graph Node query semantics, so you’ll hit the usual pagination constraints:
firstis effectively capped at 1000 entities per query (common across The Graph / Graph Node deployments). You must paginate in multiple requests. [1], [2]skiphard limit: many Graph Node deployments enforce a maximumskip(commonly you’ll see errors like “skip must be between 0 and 5000” when you go past it). This limit is enforced specifically because large offsets perform badly; Graph Node even added a configurable globalskiplimit (GRAPH_GRAPHQL_MAX_SKIP) for this purpose. [3], [4]What to do instead of
skipforpairDayDatasUse keyset pagination (cursor-like) with
orderBy+where:Option A (recommended): paginate by
dateLoop: start with
lastDate = 0, then setlastDateto the last returneddateeach page.Option B: paginate by
idSame idea, but
orderBy: idandwhere: { id_gt: $lastId }.This avoids the
skipceiling and is the approach Graph Node recommends for large result sets. [1], [4]Sources: [1] https://thegraph.com/docs/en/subgraphs/querying/graphql-api/ • [2] https://docs.uniswap.org/contracts/v2/reference/API/queries • [3] https://www.reddit.com/r/thegraph/comments/nx97nk • [4] graphprotocol/graph-node#1912
🏁 Script executed:
Repository: DefiLlama/yield-server
Length of output: 1106
🏁 Script executed:
Repository: DefiLlama/yield-server
Length of output: 860
🌐 Web query:
Graph Node pairAddress_in list size limits Uniswap subgraph batch size💡 Result:
firstper entity query is 1000 (default is often 100 if you omitfirst). So you can’t fetch more than 1000pairs(orswaps, etc.) in a single request—use pagination (skip, or preferably cursor-style where available). [1] [2]pairAddress_in/_infilter list size: The Graph’s GraphQL API supports_in/_not_infilters, but there isn’t a clearly documented fixed maximum number of values you can put in an_inlist; in practice you’ll be constrained by request size + query complexity/timeouts rather than an advertised “N addresses max”. [3]pairAddress_inbatches small enough to avoid gateway/node timeouts—typically ~50–200 addresses per query, then loop batches; separately, keepfirst≤ 1000 for the returned entity list. (If you need “all pairs”, page in chunks of 1000.) [1]Sources
[1] Uniswap docs noting The Graph limits returns to 1000 entities per query: https://docs.uniswap.org/contracts/v2/reference/API/queries
[2] Ethereum StackExchange answer summarizing default
first=100 and maxfirst=1000 on The Graph: https://ethereum.stackexchange.com/questions/114818/what-is-the-default-query-limit-on-the-graph[3] The Graph GraphQL API docs listing
_in/_not_infilters (but no stated list-size max): https://thegraph.com/docs/en/subgraphs/querying/graphql-api/Batch
pairDayDatasrequests to avoid silent volume truncation; use date-based keyset pagination, notskip.pairDayDatas(first: 1000)withpairAddress_in: $pairAddressessilently truncates results in two ways:pairAddress_inwithout batching can exceed query complexity limits.Graph Node subgraphs (Goldsky included) discourage
skippagination due to hard limits (commonly ~5000) and poor performance on large offsets. Instead, use keyset pagination viadate_gt, which is already available in the query. Batch addresses into groups of 50–200 per request to stay within timeout/complexity budgets.🔧 Suggested fix (batching + date-based pagination)
🤖 Prompt for AI Agents