Skip to content
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

Fix events being of wrong order #482

Merged
merged 6 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

This file was deleted.

41 changes: 18 additions & 23 deletions backend-rust/src/graphql_api/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,27 +225,36 @@ impl Contract {
let skip = skip.unwrap_or(0);
let take = take.unwrap_or(config.contract_events_collection_limit);

let total_contract_events_count: u64 = sqlx::query_scalar!(
"SELECT
COUNT(*)
FROM contract_events
WHERE contract_index = $1 AND contract_sub_index = $2",
self.contract_address_index.0 as i64,
self.contract_address_sub_index.0 as i64
)
.fetch_one(pool)
.await?
.unwrap_or(0)
.try_into()?;
// If `skip` is 0 and at least one event is taken, include the
// `init_transaction_event`.
let include_initial_event = skip == 0 && take > 0;
let include_initial_event =
skip <= total_contract_events_count && skip + take >= total_contract_events_count;
// Adjust the `take` and `skip` values considering if the
// `init_transaction_event` is requested to be included or not.
let take_without_initial_event = take.saturating_sub(include_initial_event as u64);
let skip_without_initial_event = skip.saturating_sub(1);

// Limit the number of events to be fetched from the `contract_events` table.
let limit = std::cmp::min(
take_without_initial_event,
take,
config.contract_events_collection_limit.saturating_sub(include_initial_event as u64),
);

let mut contract_events = vec![];
let mut initial_contract_event_exists_in_database = false;

// Get the events from the `contract_events` table.
let mut rows = sqlx::query!(
"
SELECT * FROM (
SELECT
event_index_per_contract,
contract_events.transaction_index,
Expand All @@ -266,15 +275,14 @@ impl Contract {
ON contract_events.block_height = blocks.height
WHERE contract_events.contract_index = $1 AND contract_events.contract_sub_index = \
$2
AND event_index_per_contract >= $4
LIMIT $3
) AS contract_data
AND event_index_per_contract < $4
ORDER BY event_index_per_contract DESC
LIMIT $3
",
self.contract_address_index.0 as i64,
self.contract_address_sub_index.0 as i64,
limit as i64 + 1,
skip_without_initial_event as i64
(total_contract_events_count - skip) as i64
)
.fetch_all(pool)
.await?;
Expand Down Expand Up @@ -396,19 +404,6 @@ impl Contract {
contract_events.push(initial_event);
}

let total_contract_events_count: u64 = sqlx::query_scalar!(
"SELECT
COUNT(*)
FROM contract_events
WHERE contract_index = $1 AND contract_sub_index = $2",
self.contract_address_index.0 as i64,
self.contract_address_sub_index.0 as i64
)
.fetch_one(pool)
.await?
.unwrap_or(0)
.try_into()?;

Ok(ContractEventsCollectionSegment {
page_info: CollectionSegmentInfo {
has_next_page,
Expand Down