Skip to content

Commit 656bb5c

Browse files
authored
fix: #856 pc token ordering (#859)
2 parents 3700fcf + 2524339 commit 656bb5c

File tree

7 files changed

+48
-17
lines changed

7 files changed

+48
-17
lines changed

tycho-indexer/src/services/rpc.rs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -758,11 +758,7 @@ where
758758
.min(total as usize),
759759
)
760760
.take(pagination_params.page_size as usize)
761-
.map(|c| {
762-
let mut pc = dto::ProtocolComponent::from(c);
763-
pc.tokens.sort_unstable();
764-
pc
765-
})
761+
.map(dto::ProtocolComponent::from)
766762
.collect();
767763

768764
return Ok(dto::ProtocolComponentRequestResponse::new(
@@ -817,11 +813,7 @@ where
817813

818814
let response_components = components
819815
.into_iter()
820-
.map(|c| {
821-
let mut pc = dto::ProtocolComponent::from(c);
822-
pc.tokens.sort_unstable();
823-
pc
824-
})
816+
.map(dto::ProtocolComponent::from)
825817
.collect::<Vec<dto::ProtocolComponent>>();
826818
Ok(dto::ProtocolComponentRequestResponse::new(
827819
response_components,
@@ -2548,7 +2540,7 @@ mod tests {
25482540
"ambient",
25492541
"pool",
25502542
Chain::Ethereum,
2551-
vec![Bytes::from_str("0x00").unwrap(), Bytes::from_str("0x01").unwrap()],
2543+
vec![Bytes::from_str("0x01").unwrap(), Bytes::from_str("0x00").unwrap()],
25522544
vec![],
25532545
HashMap::new(),
25542546
ChangeType::Creation,
@@ -2572,7 +2564,7 @@ mod tests {
25722564
"ambient",
25732565
"pool",
25742566
Chain::Ethereum,
2575-
vec![Bytes::from_str("0x00").unwrap(), Bytes::from_str("0x01").unwrap()],
2567+
vec![Bytes::from_str("0x01").unwrap(), Bytes::from_str("0x00").unwrap()],
25762568
vec![],
25772569
HashMap::new(),
25782570
ChangeType::Creation,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
ALTER TABLE protocol_component_holds_token DROP COLUMN token_index;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
-- This trigger drop was missed in a previous migration
2+
DROP TRIGGER IF EXISTS audit_table_token_price ON protocol_component_holds_token;
3+
4+
-- First add the column without any constraints
5+
ALTER TABLE protocol_component_holds_token ADD COLUMN token_index SMALLINT DEFAULT NULL;
6+
7+
-- Migrate existing rows to use lexicographical ordering and negative indexes
8+
UPDATE protocol_component_holds_token
9+
SET token_index = row_num - total_count - 1
10+
FROM (SELECT pcht.protocol_component_id,
11+
pcht.token_id,
12+
ROW_NUMBER() OVER (PARTITION BY pcht.protocol_component_id ORDER BY a.address) as row_num,
13+
COUNT(*) OVER (PARTITION BY pcht.protocol_component_id) as total_count
14+
FROM protocol_component_holds_token pcht
15+
JOIN token t ON pcht.token_id = t.id
16+
JOIN account a ON t.account_id = a.id) AS ranked
17+
WHERE protocol_component_holds_token.protocol_component_id = ranked.protocol_component_id
18+
AND protocol_component_holds_token.token_id = ranked.token_id;
19+
20+
-- Add the NOT NULL constraint to the column
21+
ALTER TABLE protocol_component_holds_token ALTER COLUMN token_index SET NOT NULL;
22+
23+
-- Create a UNIQUE constraint on the combination of protocol_component_id and token_index
24+
ALTER TABLE protocol_component_holds_token ADD CONSTRAINT protocol_holds_token_token_index_unique
25+
UNIQUE (protocol_component_id, token_index);

tycho-storage/src/postgres/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1260,11 +1260,14 @@ pub mod db_fixtures {
12601260
.values(
12611261
t_ids
12621262
.iter()
1263-
.map(|t_id| {
1263+
.enumerate()
1264+
.map(|(t_idx, t_id)| {
12641265
(
12651266
schema::protocol_component_holds_token::protocol_component_id
12661267
.eq(component_id),
12671268
schema::protocol_component_holds_token::token_id.eq(t_id),
1269+
schema::protocol_component_holds_token::token_index
1270+
.eq(t_idx as i16),
12681271
)
12691272
})
12701273
.collect::<Vec<_>>(),

tycho-storage/src/postgres/orm.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,8 @@ impl From<NewSlot> for NewSlotLatest {
18231823
pub struct ProtocolHoldsToken {
18241824
protocol_component_id: i64,
18251825
token_id: i64,
1826+
#[allow(dead_code)]
1827+
token_index: i16,
18261828
pub inserted_ts: NaiveDateTime,
18271829
pub modified_ts: NaiveDateTime,
18281830
}
@@ -1832,6 +1834,7 @@ pub struct ProtocolHoldsToken {
18321834
pub struct NewProtocolComponentHoldsToken {
18331835
pub protocol_component_id: i64,
18341836
pub token_id: i64,
1837+
pub token_index: i16,
18351838
}
18361839

18371840
#[derive(Identifiable, Queryable, Associations, Selectable, Debug)]

tycho-storage/src/postgres/protocol.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,10 @@ impl PostgresGateway {
266266
schema::protocol_component_holds_token::protocol_component_id
267267
.eq_any(protocol_component_ids.clone()),
268268
)
269+
.order_by((
270+
schema::protocol_component_holds_token::protocol_component_id.asc(),
271+
schema::protocol_component_holds_token::token_index.asc(),
272+
))
269273
.load::<(i64, Address)>(conn)
270274
.await
271275
.map_err(PostgresError::from)?;
@@ -513,10 +517,11 @@ impl PostgresGateway {
513517
pc.tokens
514518
.clone()
515519
.into_iter()
516-
.map(move |add| (*pc_id, add))
517-
.collect::<Vec<(i64, Address)>>()
520+
.enumerate()
521+
.map(move |(idx, add)| (*pc_id, add, idx as i16))
522+
.collect::<Vec<(i64, Address, i16)>>()
518523
})
519-
.collect::<Vec<(i64, Address)>>();
524+
.collect::<Vec<(i64, Address, i16)>>();
520525

521526
let token_add_by_id: HashMap<Address, i64> = token
522527
.inner_join(account)
@@ -534,13 +539,14 @@ impl PostgresGateway {
534539
StorageError,
535540
> = pc_tokens_map
536541
.iter()
537-
.map(|(pc_id, t_address)| {
542+
.map(|(pc_id, t_address, token_idx)| {
538543
let t_id = token_add_by_id
539544
.get(t_address)
540545
.ok_or(StorageError::NotFound("Token".to_string(), t_address.to_string()))?;
541546
Ok(orm::NewProtocolComponentHoldsToken {
542547
protocol_component_id: *pc_id,
543548
token_id: *t_id,
549+
token_index: *token_idx,
544550
})
545551
})
546552
.collect();

tycho-storage/src/postgres/schema.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ diesel::table! {
294294
token_id -> Int8,
295295
inserted_ts -> Timestamptz,
296296
modified_ts -> Timestamptz,
297+
token_index -> Int2,
297298
}
298299
}
299300

0 commit comments

Comments
 (0)