Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2,238 changes: 1,926 additions & 312 deletions Cargo.lock

Large diffs are not rendered by default.

21 changes: 11 additions & 10 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ members = [
bigdecimal = { version = "0.4.8", features = ["serde"] }
chrono = { version = "=0.4.41", features = ["clock", "serde"] }
tokio = "1.47.1"
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.138"
serde_with = "3.14.0"
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
serde_with = "3.16.1"
diesel = "2.2.7"
diesel-async = "0.5.2"
diesel_migrations = "2.2.0"
Expand All @@ -30,13 +30,14 @@ tokio-util = "0.7.15"
tracing = "0.1.41"
tracing-subscriber = "0.3"

sui-indexer-alt-metrics = { git = "https://github.com/MystenLabs/sui.git", rev = "7f1b81169e966fc1af6e3cbb1a3180fb577af075" }
sui-indexer-alt-framework = { git = "https://github.com/MystenLabs/sui.git", rev = "7f1b81169e966fc1af6e3cbb1a3180fb577af075" }
sui-indexer-alt-metrics = { git = "https://github.com/MystenLabs/sui.git", rev = "c7566e601c9a64154850c1cb074a90d7fc11bba4" }
sui-indexer-alt-framework = { git = "https://github.com/MystenLabs/sui.git", rev = "c7566e601c9a64154850c1cb074a90d7fc11bba4" }
sui-sdk-types = { git = "https://github.com/mystenlabs/sui-rust-sdk", features = [
"serde",
], rev = "048124e484f14b9bf2a402227c9bc255c7621bc1" }
telemetry-subscribers = { git = "https://github.com/MystenLabs/sui.git", rev = "7f1b81169e966fc1af6e3cbb1a3180fb577af075" }
sui-pg-db = { git = "https://github.com/MystenLabs/sui.git", rev = "7f1b81169e966fc1af6e3cbb1a3180fb577af075" }
move-core-types = { git = "https://github.com/MystenLabs/sui.git", rev = "7f1b81169e966fc1af6e3cbb1a3180fb577af075" }
sui-types = { git = "https://github.com/MystenLabs/sui.git", rev = "7f1b81169e966fc1af6e3cbb1a3180fb577af075" }
sui-sdk = { git = "https://github.com/MystenLabs/sui.git", rev = "7f1b81169e966fc1af6e3cbb1a3180fb577af075" }
telemetry-subscribers = { git = "https://github.com/MystenLabs/sui.git", rev = "c7566e601c9a64154850c1cb074a90d7fc11bba4" }
sui-pg-db = { git = "https://github.com/MystenLabs/sui.git", rev = "c7566e601c9a64154850c1cb074a90d7fc11bba4" }
move-core-types = { git = "https://github.com/MystenLabs/sui.git", rev = "c7566e601c9a64154850c1cb074a90d7fc11bba4" }
sui-types = { git = "https://github.com/MystenLabs/sui.git", rev = "c7566e601c9a64154850c1cb074a90d7fc11bba4" }
sui-sdk = { git = "https://github.com/MystenLabs/sui.git", rev = "c7566e601c9a64154850c1cb074a90d7fc11bba4" }
sui-field-count = { git = "https://github.com/MystenLabs/sui.git", rev = "c7566e601c9a64154850c1cb074a90d7fc11bba4" }
5 changes: 3 additions & 2 deletions crates/indexer/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ edition = "2021"
tokio.workspace = true
sui-indexer-alt-framework.workspace = true
move-binding-derive = { git = "https://github.com/MystenLabs/move-binding.git", rev = "2321fec265dd2c65560b412b712429d8c993c603" }
move-types = { git = "https://github.com/MystenLabs/move-binding.git", rev = "2321fec265dd2c65560b412b712429d8c993c603" }
sui-sdk-types.workspace = true
sui-transaction-builder = { git = "https://github.com/mystenlabs/sui-rust-sdk", rev = "048124e484f14b9bf2a402227c9bc255c7621bc1" }
clap = { workspace = true, features = ["env"] }
Expand All @@ -23,6 +22,8 @@ diesel = { workspace = true, features = [
"numeric",
] }
diesel-async = { workspace = true, features = ["bb8", "postgres"] }
hex = "0.4"
bigdecimal.workspace = true
tracing.workspace = true
async-trait.workspace = true
bcs.workspace = true
Expand All @@ -44,7 +45,7 @@ deeplook-schema = { path = "../schema" }
sui-storage = { git = "https://github.com/MystenLabs/sui.git", rev = "7f1b81169e966fc1af6e3cbb1a3180fb577af075" }
insta = { version = "1.43.1", features = ["json"] }
serde_json = "1.0.140"
sqlx = { version = "0.8.3", features = ["runtime-tokio", "postgres", "chrono"] }
sqlx = { version = "0.8.3", features = ["runtime-tokio", "postgres", "chrono", "bigdecimal"] }
fastcrypto = { git = "https://github.com/MystenLabs/fastcrypto" }

[[bin]]
Expand Down
105 changes: 19 additions & 86 deletions crates/indexer/src/handlers/balances_handler.rs
Original file line number Diff line number Diff line change
@@ -1,92 +1,25 @@
use crate::handlers::{is_deepbook_tx, try_extract_move_call_package};
use crate::define_handler;
use crate::models::deepbook::balance_manager::BalanceEvent;
use crate::utils::ms_to_secs;
use crate::DeeplookEnv;
use async_trait::async_trait;
use deeplook_schema::models::Balances;
use deeplook_schema::schema::balances;
use diesel_async::RunQueryDsl;
use move_core_types::language_storage::StructTag;
use std::sync::Arc;
use sui_indexer_alt_framework::pipeline::concurrent::Handler;
use sui_indexer_alt_framework::pipeline::Processor;
use sui_pg_db::{Connection, Db};
use sui_types::full_checkpoint_content::CheckpointData;
use tracing::debug;

pub struct BalancesHandler {
event_type: StructTag,
}

impl BalancesHandler {
pub fn new(env: DeeplookEnv) -> Self {
Self {
event_type: env.balance_event_type(),
}
}
}

impl Processor for BalancesHandler {
const NAME: &'static str = "balances";
type Value = Balances;

fn process(&self, checkpoint: &Arc<CheckpointData>) -> anyhow::Result<Vec<Self::Value>> {
checkpoint
.transactions
.iter()
.try_fold(vec![], |result, tx| {
if !is_deepbook_tx(tx) {
return Ok(result);
}
let Some(events) = &tx.events else {
return Ok(result);
};

let package = try_extract_move_call_package(tx).unwrap_or_default();
let checkpoint_timestamp_ms = checkpoint.checkpoint_summary.timestamp_ms as i64;
let checkpoint = checkpoint.checkpoint_summary.sequence_number as i64;
let digest = tx.transaction.digest();

return events
.data
.iter()
.filter(|ev| ev.type_ == self.event_type)
.enumerate()
.try_fold(result, |mut result, (index, ev)| {
let event: BalanceEvent = bcs::from_bytes(&ev.contents)?;
let data = Balances {
digest: digest.to_string(),
event_digest: format!("{digest}{index}"),
sender: tx.transaction.sender_address().to_string(),
checkpoint,
checkpoint_timestamp_ms,
timestamp: ms_to_secs(checkpoint_timestamp_ms),
package: package.clone(),
balance_manager_id: event.balance_manager_id.to_string(),
asset: event.asset.to_string(),
amount: event.amount as i64,
deposit: event.deposit,
};
debug!("Observed Deepbook Balance Event {:?}", data);
result.push(data);
Ok(result)
});
})
}
}

#[async_trait]
impl Handler for BalancesHandler {
type Store = Db;

async fn commit<'a>(
values: &[Self::Value],
conn: &mut Connection<'a>,
) -> anyhow::Result<usize> {
Ok(diesel::insert_into(balances::table)
.values(values)
.on_conflict_do_nothing()
.execute(conn)
.await?)
define_handler! {
name: BalancesHandler,
processor_name: "balances",
event_type: BalanceEvent,
db_model: Balances,
table: balances,
map_event: |event, meta| Balances {
event_digest: meta.event_digest(),
digest: meta.digest(),
sender: meta.sender(),
checkpoint: meta.checkpoint(),
checkpoint_timestamp_ms: meta.checkpoint_timestamp_ms(),
timestamp: ms_to_secs(meta.checkpoint_timestamp_ms()),
package: meta.package(),
balance_manager_id: event.balance_manager_id.to_string(),
asset: event.asset.to_string(),
amount: event.amount as i64,
deposit: event.deposit,
}
}
105 changes: 19 additions & 86 deletions crates/indexer/src/handlers/flash_loan_handler.rs
Original file line number Diff line number Diff line change
@@ -1,92 +1,25 @@
use crate::handlers::{is_deepbook_tx, try_extract_move_call_package};
use crate::define_handler;
use crate::models::deepbook::vault::FlashLoanBorrowed;
use crate::utils::ms_to_secs;
use crate::DeeplookEnv;
use async_trait::async_trait;
use deeplook_schema::models::Flashloan;
use deeplook_schema::schema::flashloans;
use diesel_async::RunQueryDsl;
use move_core_types::language_storage::StructTag;
use std::sync::Arc;
use sui_indexer_alt_framework::pipeline::concurrent::Handler;
use sui_indexer_alt_framework::pipeline::Processor;
use sui_pg_db::{Connection, Db};
use sui_types::full_checkpoint_content::CheckpointData;
use tracing::debug;

pub struct FlashLoanHandler {
event_type: StructTag,
}

impl FlashLoanHandler {
pub fn new(env: DeeplookEnv) -> Self {
Self {
event_type: env.flash_loan_borrowed_event_type(),
}
}
}

impl Processor for FlashLoanHandler {
const NAME: &'static str = "flash_loan";
type Value = Flashloan;

fn process(&self, checkpoint: &Arc<CheckpointData>) -> anyhow::Result<Vec<Self::Value>> {
checkpoint
.transactions
.iter()
.try_fold(vec![], |result, tx| {
if !is_deepbook_tx(tx) {
return Ok(result);
}
let Some(events) = &tx.events else {
return Ok(result);
};

let package = try_extract_move_call_package(tx).unwrap_or_default();
let checkpoint_timestamp_ms = checkpoint.checkpoint_summary.timestamp_ms as i64;
let checkpoint = checkpoint.checkpoint_summary.sequence_number as i64;
let digest = tx.transaction.digest();

return events
.data
.iter()
.filter(|ev| ev.type_ == self.event_type)
.enumerate()
.try_fold(result, |mut result, (index, ev)| {
let event: FlashLoanBorrowed = bcs::from_bytes(&ev.contents)?;
let data = Flashloan {
digest: digest.to_string(),
event_digest: format!("{digest}{index}"),
sender: tx.transaction.sender_address().to_string(),
checkpoint,
checkpoint_timestamp_ms,
timestamp: ms_to_secs(checkpoint_timestamp_ms),
package: package.clone(),
pool_id: event.pool_id.to_string(),
borrow_quantity: event.borrow_quantity as i64,
borrow: true,
type_name: event.type_name.to_string(),
};
debug!("Observed Deepbook Flash Loan Borrowed {:?}", data);
result.push(data);
Ok(result)
});
})
}
}

#[async_trait]
impl Handler for FlashLoanHandler {
type Store = Db;

async fn commit<'a>(
values: &[Self::Value],
conn: &mut Connection<'a>,
) -> anyhow::Result<usize> {
Ok(diesel::insert_into(flashloans::table)
.values(values)
.on_conflict_do_nothing()
.execute(conn)
.await?)
define_handler! {
name: FlashLoanHandler,
processor_name: "flash_loan",
event_type: FlashLoanBorrowed,
db_model: Flashloan,
table: flashloans,
map_event: |event, meta| Flashloan {
event_digest: meta.event_digest(),
digest: meta.digest(),
sender: meta.sender(),
checkpoint: meta.checkpoint(),
checkpoint_timestamp_ms: meta.checkpoint_timestamp_ms(),
timestamp: ms_to_secs(meta.checkpoint_timestamp_ms()),
package: meta.package(),
pool_id: event.pool_id.to_string(),
borrow_quantity: event.borrow_quantity as i64,
borrow: true,
type_name: event.type_name.to_string(),
}
}
Loading