-
Notifications
You must be signed in to change notification settings - Fork 0
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
Get trailing avg from graphql api #296
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
1bcebbb
WIP: Get trailing avg from graphql api
leomassazza a2dd81e
Merge branch 'main' into trailingavg
leomassazza bbf398a
checkpoint
leomassazza 155eef0
api done, todo. Use it in the app
leomassazza e074221
Merge branch 'main' into trailingavg
leomassazza d948c24
use new graphql function in app
leomassazza c845a07
fix loading + lint
leomassazza 02c2112
implement sliding window to trailing avg
leomassazza a06a38e
add perf log
leomassazza fde2da4
add perf log
leomassazza 6470981
fix idx
leomassazza 4edea7e
remove logs
leomassazza 66e2254
Merge branch 'trailingavg' into improve-trailing-performance
leomassazza c3890e6
Implement sliding window to trailing avg (#299)
leomassazza b8449d9
Merge branch 'main' into trailingavg
leomassazza 9711a75
Merge branch 'main' into trailingavg
leomassazza e9e0085
PR Review fixes
leomassazza 644729d
Merge branch 'trailingavg' of github.com:foilxyz/foil into trailingavg
leomassazza 71c5b87
Merge branch 'main' into trailingavg
leomassazza 5919bf2
lint
leomassazza 5d68a71
Merge branch 'trailingavg' of github.com:foilxyz/foil into trailingavg
leomassazza 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 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
This file contains 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 |
---|---|---|
|
@@ -14,6 +14,13 @@ interface PricePoint { | |
value: string; | ||
} | ||
|
||
interface ResourcePricePoint { | ||
timestamp: number; | ||
value: string; | ||
used: string; | ||
feePaid: string; | ||
} | ||
|
||
const groupPricesByInterval = ( | ||
prices: PricePoint[], | ||
intervalSeconds: number, | ||
|
@@ -72,6 +79,92 @@ const groupPricesByInterval = ( | |
return candles; | ||
}; | ||
|
||
const getTrailingAveragePricesByInterval = ( | ||
prices: ResourcePricePoint[], | ||
trailingIntervalSeconds: number, | ||
intervalSeconds: number, | ||
startTimestamp: number, | ||
endTimestamp: number, | ||
lastKnownPrice?: string | ||
): CandleType[] => { | ||
const candles: CandleType[] = []; | ||
|
||
// If we have no prices and no reference price, return empty array | ||
if (prices.length === 0 && !lastKnownPrice) return []; | ||
|
||
// Normalize timestamps to interval boundaries | ||
const normalizedStartTimestamp = | ||
Math.floor(startTimestamp / intervalSeconds) * intervalSeconds; | ||
const normalizedEndTimestamp = | ||
Math.floor(endTimestamp / intervalSeconds) * intervalSeconds; | ||
|
||
// Initialize lastClose with lastKnownPrice if available, otherwise use first price | ||
let lastClose = lastKnownPrice || prices[0].value; | ||
|
||
// Ensure it's ordered (it should come ordered from the query, then the sort is just a sanity check) | ||
const orderedPrices = prices.sort((a, b) => a.timestamp - b.timestamp); | ||
let searchStartIdx = 0; // Add this to track where to start searching from | ||
let lastStartIdx = 0; | ||
let lastEndIdx = 0; | ||
|
||
let totalGasUsed: bigint = 0n; | ||
let totalBaseFeesPaid: bigint = 0n; | ||
|
||
for ( | ||
let timestamp = normalizedStartTimestamp; | ||
timestamp <= normalizedEndTimestamp; | ||
timestamp += intervalSeconds | ||
) { | ||
// Search for start index from the last known position | ||
while (searchStartIdx < orderedPrices.length && | ||
orderedPrices[searchStartIdx].timestamp < timestamp - trailingIntervalSeconds) { | ||
searchStartIdx++; | ||
} | ||
const startIdx = searchStartIdx < orderedPrices.length ? searchStartIdx : -1; | ||
|
||
// Search for end index from the start index | ||
let searchEndIdx = Math.max(searchStartIdx, lastEndIdx); | ||
while (searchEndIdx < orderedPrices.length && | ||
orderedPrices[searchEndIdx].timestamp <= timestamp) { | ||
searchEndIdx++; | ||
} | ||
const endIdx = searchEndIdx - 1; // No need for correction since we're getting the last valid index directly | ||
|
||
// Remove from the sliding window trailing average the prices that are no longer in the interval | ||
if (startIdx != -1) { | ||
for (let i = lastStartIdx; i <= startIdx; i++) { | ||
totalGasUsed -= BigInt(orderedPrices[i].used); | ||
totalBaseFeesPaid -= BigInt(orderedPrices[i].feePaid); | ||
} | ||
} | ||
lastStartIdx = startIdx; | ||
|
||
// Add to the sliding window trailing average the prices that are now in the interval | ||
for (let i = lastEndIdx; i <= endIdx; i++) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could try to make the calculations faster; right now the runtime is approx. O(n^2) since we have a nested loop. If the performance is not too big of a concern we should be good though |
||
totalGasUsed += BigInt(orderedPrices[i].used); | ||
totalBaseFeesPaid += BigInt(orderedPrices[i].feePaid); | ||
} | ||
lastEndIdx = endIdx; | ||
|
||
// Calculate the average price for the interval | ||
if (totalGasUsed > 0n) { | ||
const averagePrice: bigint = totalBaseFeesPaid / totalGasUsed; | ||
lastClose = averagePrice.toString(); | ||
} | ||
|
||
// Create candle with last known closing price (calculated in the loop or previous candle) | ||
candles.push({ | ||
timestamp, | ||
open: lastClose, | ||
high: lastClose, | ||
low: lastClose, | ||
close: lastClose, | ||
}); | ||
} | ||
|
||
return candles; | ||
}; | ||
|
||
@Resolver() | ||
export class CandleResolver { | ||
@Query(() => [CandleType]) | ||
|
@@ -126,6 +219,72 @@ export class CandleResolver { | |
} | ||
} | ||
|
||
@Query(() => [CandleType]) | ||
async resourceTrailingAverageCandles( | ||
@Arg('slug', () => String) slug: string, | ||
@Arg('from', () => Int) from: number, | ||
@Arg('to', () => Int) to: number, | ||
@Arg('interval', () => Int) interval: number, | ||
@Arg('trailingTime', () => Int) trailingTime: number | ||
): Promise<CandleType[]> { | ||
try { | ||
const trailingFrom = from - trailingTime; | ||
const resource = await dataSource.getRepository(Resource).findOne({ | ||
where: { slug }, | ||
}); | ||
|
||
if (!resource) { | ||
throw new Error(`Resource not found with slug: ${slug}`); | ||
} | ||
|
||
// First get the most recent price before the trailingFrom timestamp | ||
const lastPriceBefore = await dataSource | ||
.getRepository(ResourcePrice) | ||
.createQueryBuilder('price') | ||
.where('price.resourceId = :resourceId', { resourceId: resource.id }) | ||
.andWhere('price.timestamp < :from', { from: trailingFrom }) | ||
.orderBy('price.timestamp', 'DESC') | ||
.take(1) | ||
.getOne(); | ||
|
||
// Then get all prices within the range | ||
const pricesInRange = await dataSource.getRepository(ResourcePrice).find({ | ||
where: { | ||
resource: { id: resource.id }, | ||
timestamp: Between(trailingFrom, to), | ||
}, | ||
order: { timestamp: 'ASC' }, | ||
}); | ||
|
||
// Combine the results, putting the last price before first if it exists | ||
const prices = pricesInRange; | ||
|
||
const lastKnownPrice = | ||
lastPriceBefore?.feePaid && lastPriceBefore?.used | ||
? ( | ||
BigInt(lastPriceBefore?.feePaid) / BigInt(lastPriceBefore?.used) | ||
).toString() | ||
: lastPriceBefore?.value; | ||
|
||
return getTrailingAveragePricesByInterval( | ||
prices.map((p) => ({ | ||
timestamp: Number(p.timestamp), | ||
value: p.value, | ||
used: p.used, | ||
feePaid: p.feePaid, | ||
})), | ||
trailingTime, | ||
interval, | ||
from, | ||
to, | ||
lastKnownPrice | ||
); | ||
} catch (error) { | ||
console.error('Error fetching resource candles:', error); | ||
throw new Error('Failed to fetch resource candles'); | ||
} | ||
} | ||
|
||
@Query(() => [CandleType]) | ||
async indexCandles( | ||
@Arg('chainId', () => Int) chainId: number, | ||
|
This file contains 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
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.
aren't they already sorted?