-
Notifications
You must be signed in to change notification settings - Fork 13
Update deposit and withdraw polling logic #2350
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
Open
findolor
wants to merge
7
commits into
2025-12-05-remove-order-polling
Choose a base branch
from
2025-12-05-transaction-polling
base: 2025-12-05-remove-order-polling
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0ff4108
rename error
findolor 1f121a3
add database query to fetch a transaction by hash
findolor 0932234
add transaction related polling mechanism
findolor 8983bf8
replace subgraph polling mechanism on webapp with sdk usage
findolor a7f7e69
fix implementation
findolor cb71258
formatting
findolor b20de92
Merge 2025-12-05-remove-order-polling [skip ci]
findolor 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
55 changes: 55 additions & 0 deletions
55
crates/common/src/local_db/query/fetch_transaction_by_hash/mod.rs
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,55 @@ | ||
| use crate::local_db::{ | ||
| query::{SqlStatement, SqlValue}, | ||
| OrderbookIdentifier, | ||
| }; | ||
| use alloy::primitives::{Address, B256}; | ||
| use serde::{Deserialize, Serialize}; | ||
|
|
||
| const QUERY_TEMPLATE: &str = include_str!("query.sql"); | ||
|
|
||
| /// Transaction info returned from local DB query. | ||
| #[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)] | ||
| #[serde(rename_all = "camelCase")] | ||
| pub struct LocalDbTransaction { | ||
| pub transaction_hash: B256, | ||
| pub block_number: u64, | ||
| pub block_timestamp: u64, | ||
| pub sender: Address, | ||
| } | ||
|
|
||
| /// Builds a SQL statement to fetch transaction info by transaction hash | ||
| /// from the deposits and withdrawals tables. | ||
| pub fn build_fetch_transaction_by_hash_stmt( | ||
| ob_id: &OrderbookIdentifier, | ||
| tx_hash: B256, | ||
| ) -> SqlStatement { | ||
| SqlStatement::new_with_params( | ||
| QUERY_TEMPLATE, | ||
| vec![ | ||
| SqlValue::from(ob_id.chain_id), | ||
| SqlValue::from(ob_id.orderbook_address), | ||
| SqlValue::from(tx_hash), | ||
| ], | ||
| ) | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use alloy::primitives::{address, b256}; | ||
|
|
||
| #[test] | ||
| fn builds_correct_sql_with_params() { | ||
| let orderbook = address!("0x1234567890123456789012345678901234567890"); | ||
| let tx_hash = b256!("0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"); | ||
| let ob_id = OrderbookIdentifier::new(1, orderbook); | ||
|
|
||
| let stmt = build_fetch_transaction_by_hash_stmt(&ob_id, tx_hash); | ||
|
|
||
| assert!(stmt.sql.contains("SELECT")); | ||
| assert!(stmt.sql.contains("FROM deposits")); | ||
| assert!(stmt.sql.contains("FROM withdrawals")); | ||
| assert!(stmt.sql.contains("transaction_hash")); | ||
| assert_eq!(stmt.params.len(), 3); | ||
| } | ||
| } |
40 changes: 40 additions & 0 deletions
40
crates/common/src/local_db/query/fetch_transaction_by_hash/query.sql
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,40 @@ | ||
| WITH params AS ( | ||
| SELECT | ||
| ?1 AS chain_id, | ||
| ?2 AS orderbook_address, | ||
| ?3 AS transaction_hash | ||
| ), | ||
| combined AS ( | ||
| SELECT | ||
| d.transaction_hash, | ||
| d.block_number, | ||
| d.block_timestamp, | ||
| d.sender, | ||
| d.log_index | ||
| FROM deposits d | ||
| JOIN params p | ||
| ON p.chain_id = d.chain_id | ||
| AND p.orderbook_address = d.orderbook_address | ||
| AND p.transaction_hash = d.transaction_hash | ||
| UNION ALL | ||
| SELECT | ||
| w.transaction_hash, | ||
| w.block_number, | ||
| w.block_timestamp, | ||
| w.sender, | ||
| w.log_index | ||
| FROM withdrawals w | ||
| JOIN params p | ||
| ON p.chain_id = w.chain_id | ||
| AND p.orderbook_address = w.orderbook_address | ||
| AND p.transaction_hash = w.transaction_hash | ||
| ) | ||
| SELECT | ||
| transaction_hash AS transactionHash, | ||
| block_number AS blockNumber, | ||
| block_timestamp AS blockTimestamp, | ||
| sender | ||
| FROM combined | ||
| ORDER BY log_index ASC | ||
| LIMIT 1; | ||
|
|
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
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
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
85 changes: 85 additions & 0 deletions
85
crates/common/src/raindex_client/local_db/query/fetch_transaction_by_hash.rs
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,85 @@ | ||
| use crate::local_db::query::fetch_transaction_by_hash::{ | ||
| build_fetch_transaction_by_hash_stmt, LocalDbTransaction, | ||
| }; | ||
| use crate::local_db::query::{LocalDbQueryError, LocalDbQueryExecutor}; | ||
| use crate::local_db::OrderbookIdentifier; | ||
| use alloy::primitives::B256; | ||
|
|
||
| pub async fn fetch_transaction_by_hash<E: LocalDbQueryExecutor + ?Sized>( | ||
| exec: &E, | ||
| ob_id: &OrderbookIdentifier, | ||
| tx_hash: B256, | ||
| ) -> Result<Vec<LocalDbTransaction>, LocalDbQueryError> { | ||
| let stmt = build_fetch_transaction_by_hash_stmt(ob_id, tx_hash); | ||
| exec.query_json(&stmt).await | ||
| } | ||
|
|
||
| #[cfg(all(test, target_family = "wasm"))] | ||
| mod wasm_tests { | ||
| use super::*; | ||
| use crate::raindex_client::local_db::executor::tests::create_sql_capturing_callback; | ||
| use crate::raindex_client::local_db::executor::JsCallbackExecutor; | ||
| use alloy::primitives::{address, b256, Address}; | ||
| use std::cell::RefCell; | ||
| use std::rc::Rc; | ||
| use wasm_bindgen_test::*; | ||
| use wasm_bindgen_utils::prelude::*; | ||
|
|
||
| #[wasm_bindgen_test] | ||
| async fn wrapper_uses_builder_sql_exactly() { | ||
| let tx_hash = b256!("0x0000000000000000000000000000000000000000000000000000000000000abc"); | ||
| let orderbook = Address::from([0x51; 20]); | ||
| let expected_stmt = | ||
| build_fetch_transaction_by_hash_stmt(&OrderbookIdentifier::new(1, orderbook), tx_hash); | ||
|
|
||
| let store = Rc::new(RefCell::new((String::new(), JsValue::UNDEFINED))); | ||
| let callback = create_sql_capturing_callback("[]", store.clone()); | ||
| let exec = JsCallbackExecutor::from_ref(&callback); | ||
|
|
||
| let res = super::fetch_transaction_by_hash( | ||
| &exec, | ||
| &OrderbookIdentifier::new(1, orderbook), | ||
| tx_hash, | ||
| ) | ||
| .await; | ||
| assert!(res.is_ok()); | ||
| assert_eq!(store.borrow().clone().0, expected_stmt.sql); | ||
| } | ||
|
|
||
| #[wasm_bindgen_test] | ||
| async fn wrapper_returns_rows_when_present() { | ||
| let tx_hash = b256!("0x0000000000000000000000000000000000000000000000000000000000000abc"); | ||
| let orderbook = address!("0x5151515151515151515151515151515151515151"); | ||
| let sender = address!("0x1111111111111111111111111111111111111111"); | ||
| let expected_stmt = | ||
| build_fetch_transaction_by_hash_stmt(&OrderbookIdentifier::new(1, orderbook), tx_hash); | ||
|
|
||
| let row_json = format!( | ||
| r#"[{{ | ||
| "transactionHash":"{}", | ||
| "blockNumber":100, | ||
| "blockTimestamp":999, | ||
| "sender":"{}" | ||
| }}]"#, | ||
| tx_hash, sender | ||
| ); | ||
|
|
||
| let store = Rc::new(RefCell::new(( | ||
| String::new(), | ||
| wasm_bindgen::JsValue::UNDEFINED, | ||
| ))); | ||
| let callback = create_sql_capturing_callback(&row_json, store.clone()); | ||
| let exec = JsCallbackExecutor::from_ref(&callback); | ||
|
|
||
| let res = super::fetch_transaction_by_hash( | ||
| &exec, | ||
| &OrderbookIdentifier::new(1, orderbook), | ||
| tx_hash, | ||
| ) | ||
| .await; | ||
| assert!(res.is_ok()); | ||
| let rows = res.unwrap(); | ||
| assert_eq!(rows.len(), 1); | ||
| assert_eq!(store.borrow().clone().0, expected_stmt.sql); | ||
| } | ||
| } | ||
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
128 changes: 128 additions & 0 deletions
128
crates/common/src/raindex_client/local_db/transactions.rs
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,128 @@ | ||
| use super::super::transactions::RaindexTransaction; | ||
| use super::super::RaindexError; | ||
| use super::LocalDb; | ||
| use crate::local_db::query::fetch_transaction_by_hash::{ | ||
| build_fetch_transaction_by_hash_stmt, LocalDbTransaction, | ||
| }; | ||
| use crate::local_db::query::LocalDbQueryExecutor; | ||
| use crate::local_db::OrderbookIdentifier; | ||
| use alloy::primitives::B256; | ||
|
|
||
| pub struct LocalDbTransactions<'a> { | ||
| pub(crate) db: &'a LocalDb, | ||
| } | ||
|
|
||
| impl<'a> LocalDbTransactions<'a> { | ||
| pub(crate) fn new(db: &'a LocalDb) -> Self { | ||
| Self { db } | ||
| } | ||
|
|
||
| /// Fetch transaction info by transaction hash from the local DB. | ||
| /// Returns None if no transaction with that hash is found. | ||
| pub async fn get_by_tx_hash( | ||
| &self, | ||
| ob_id: &OrderbookIdentifier, | ||
| tx_hash: B256, | ||
| ) -> Result<Option<RaindexTransaction>, RaindexError> { | ||
| let stmt = build_fetch_transaction_by_hash_stmt(ob_id, tx_hash); | ||
| let results: Vec<LocalDbTransaction> = self.db.query_json(&stmt).await?; | ||
|
|
||
| if let Some(local_tx) = results.into_iter().next() { | ||
| let tx = RaindexTransaction::from_local_parts( | ||
| local_tx.transaction_hash, | ||
| local_tx.sender, | ||
| local_tx.block_number, | ||
| local_tx.block_timestamp, | ||
| )?; | ||
| return Ok(Some(tx)); | ||
| } | ||
|
|
||
| Ok(None) | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| #[cfg(target_family = "wasm")] | ||
| use super::*; | ||
|
|
||
| #[cfg(target_family = "wasm")] | ||
| mod wasm_tests { | ||
| use super::*; | ||
| use crate::raindex_client::local_db::executor::JsCallbackExecutor; | ||
| use crate::raindex_client::local_db::LocalDb; | ||
| use alloy::primitives::{address, b256}; | ||
| use serde_json::json; | ||
| use wasm_bindgen_test::wasm_bindgen_test; | ||
| use wasm_bindgen_utils::prelude::*; | ||
|
|
||
| fn create_mock_callback(response_json: &str) -> js_sys::Function { | ||
| let json_str = response_json.to_string(); | ||
| let result = WasmEncodedResult::Success::<String> { | ||
| value: json_str, | ||
| error: None, | ||
| }; | ||
| let payload = js_sys::JSON::stringify(&serde_wasm_bindgen::to_value(&result).unwrap()) | ||
| .unwrap() | ||
| .as_string() | ||
| .unwrap(); | ||
|
|
||
| let closure = | ||
| Closure::wrap(Box::new(move |_sql: String, _params: JsValue| -> JsValue { | ||
| js_sys::JSON::parse(&payload).unwrap() | ||
| }) | ||
| as Box<dyn Fn(String, JsValue) -> JsValue>); | ||
|
|
||
| closure.into_js_value().dyn_into().unwrap() | ||
| } | ||
|
|
||
| #[wasm_bindgen_test] | ||
| async fn test_get_by_tx_hash_returns_transaction_when_found() { | ||
| let tx_hash = | ||
| b256!("0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"); | ||
| let sender = address!("0x1111111111111111111111111111111111111111"); | ||
| let orderbook = address!("0x2222222222222222222222222222222222222222"); | ||
|
|
||
| let tx_json = json!([{ | ||
| "transactionHash": tx_hash.to_string(), | ||
| "blockNumber": 12345, | ||
| "blockTimestamp": 1700000000, | ||
| "sender": sender.to_string() | ||
| }]); | ||
|
|
||
| let callback = create_mock_callback(&tx_json.to_string()); | ||
| let exec = JsCallbackExecutor::from_ref(&callback); | ||
| let local_db = LocalDb::new(exec); | ||
|
|
||
| let transactions = LocalDbTransactions::new(&local_db); | ||
| let ob_id = OrderbookIdentifier::new(1, orderbook); | ||
|
|
||
| let result = transactions.get_by_tx_hash(&ob_id, tx_hash).await; | ||
|
|
||
| assert!(result.is_ok()); | ||
| let tx = result.unwrap(); | ||
| assert!(tx.is_some()); | ||
| let tx = tx.unwrap(); | ||
| assert_eq!(tx.id().to_lowercase(), tx_hash.to_string().to_lowercase()); | ||
| } | ||
|
|
||
| #[wasm_bindgen_test] | ||
| async fn test_get_by_tx_hash_returns_none_when_not_found() { | ||
| let tx_hash = | ||
| b256!("0xabcdef1234567890abcdef1234567890abcdef1234567890abcdef1234567890"); | ||
| let orderbook = address!("0x2222222222222222222222222222222222222222"); | ||
|
|
||
| let callback = create_mock_callback("[]"); | ||
| let exec = JsCallbackExecutor::from_ref(&callback); | ||
| let local_db = LocalDb::new(exec); | ||
|
|
||
| let transactions = LocalDbTransactions::new(&local_db); | ||
| let ob_id = OrderbookIdentifier::new(1, orderbook); | ||
|
|
||
| let result = transactions.get_by_tx_hash(&ob_id, tx_hash).await; | ||
|
|
||
| assert!(result.is_ok()); | ||
| assert!(result.unwrap().is_none()); | ||
| } | ||
| } | ||
| } |
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
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
🧹 Nitpick | 🔵 Trivial
WASM tests exercise both SQL wiring and row deserialization effectively
The two wasm tests confirm that:
build_fetch_transaction_by_hash_stmtis what the wrapper sends, andLocalDbTransaction.This gives good coverage of the wrapper without over-reaching into implementation details; the only minor consideration is that depending on
executor::tests::create_sql_capturing_callbackcouples these tests to another module’s test helpers, which is fine but could be brittle if those helpers are refactored. If that becomes an issue, consider moving the helper into a non-test-only module or re-exporting it at a stable path.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.
it's fine as it is
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.
@findolor, sounds good! The current implementation works well for your use case.
(\ /)
( •.•)
/ > 🔍 noted
🧠 Learnings used