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 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ version = "0.1.34"
edition = "2021"

[workspace.dependencies]
intmax2-zkp = { git = "https://github.com/InternetMaximalism/intmax2-zkp" }
intmax2-zkp = { git = "https://github.com/InternetMaximalism/intmax2-zkp", branch = "main" }
plonky2 = { git = "https://github.com/InternetMaximalism/polygon-plonky2.git", branch = "intmax2-dev" }
plonky2_u32 = { git = "https://github.com/InternetMaximalism/plonky2-u32.git", branch = "intmax2-dev" }
plonky2_bn254 = { git = "https://github.com/InternetMaximalism/plonky2_bn254" }
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cli/history.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chrono::TimeZone;
use colored::{ColoredString, Colorize as _};
use intmax2_client_sdk::client::history::EntryStatus;
use intmax2_client_sdk::client::strategy::entry_status::EntryStatus;
use intmax2_interfaces::{
api::store_vault_server::types::{CursorOrder, MetaDataCursor},
data::{
Expand Down
33 changes: 30 additions & 3 deletions client-sdk/src/client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ use super::{
WITHDRAWAL_FEE_MEMO,
},
fee_proof::{generate_fee_proof, quote_transfer_fee},
history::{fetch_deposit_history, fetch_transfer_history, fetch_tx_history, HistoryEntry},
history::{fetch_deposit_history, fetch_transfer_history, fetch_tx_history},
misc::payment_memo::{payment_memo_topic, PaymentMemo},
receipt::validate_transfer_receipt,
strategy::{
Expand All @@ -66,9 +66,11 @@ use super::{
use crate::{
client::{
fee_payment::generate_withdrawal_transfers,
history::{fetch_deposit_batch, fetch_transfer_batch, fetch_tx_batch},
receipt::generate_transfer_receipt,
strategy::{
mining::validate_mining_deposit_criteria, utils::wait_till_validity_prover_synced,
entry_status::HistoryEntry, mining::validate_mining_deposit_criteria,
strategy::GroupedEntries, utils::wait_till_validity_prover_synced,
},
sync::utils::generate_salt,
types::{
Expand Down Expand Up @@ -262,7 +264,7 @@ impl Client {
.await?;

let current_time = chrono::Utc::now().timestamp() as u64;
let tx_info = fetch_all_unprocessed_tx_info(
let tx_history_entries = fetch_all_unprocessed_tx_info(
self.store_vault_server.as_ref(),
self.validity_prover.as_ref(),
view_pair,
Expand All @@ -271,6 +273,7 @@ impl Client {
self.config.tx_timeout,
)
.await?;
let tx_info = GroupedEntries::from_history_entries(&tx_history_entries);
if !tx_info.settled.is_empty() || !tx_info.pending.is_empty() {
if already_synced {
return Err(ClientError::UnexpectedError(
Expand Down Expand Up @@ -784,6 +787,30 @@ impl Client {
fetch_tx_history(self, view_pair, cursor).await
}

pub async fn fetch_deposit_batch(
&self,
view_pair: ViewPair,
digests: &[Bytes32],
) -> Result<Vec<HistoryEntry<DepositData>>, ClientError> {
fetch_deposit_batch(self, view_pair, digests).await
}

pub async fn fetch_transfer_batch(
&self,
view_pair: ViewPair,
digests: &[Bytes32],
) -> Result<Vec<HistoryEntry<TransferData>>, ClientError> {
fetch_transfer_batch(self, view_pair, digests).await
}

pub async fn fetch_tx_batch(
&self,
view_pair: ViewPair,
digests: &[Bytes32],
) -> Result<Vec<HistoryEntry<TxData>>, ClientError> {
fetch_tx_batch(self, view_pair, digests).await
}

pub async fn quote_transfer_fee(
&self,
block_builder_url: &str,
Expand Down
220 changes: 84 additions & 136 deletions client-sdk/src/client/history.rs
Original file line number Diff line number Diff line change
@@ -1,49 +1,18 @@
use intmax2_interfaces::{
api::store_vault_server::types::{MetaDataCursor, MetaDataCursorResponse},
data::{
deposit_data::DepositData,
meta_data::{MetaData, MetaDataWithBlockNumber},
transfer_data::TransferData,
tx_data::TxData,
},
data::{deposit_data::DepositData, transfer_data::TransferData, tx_data::TxData},
utils::key::ViewPair,
};
use intmax2_zkp::ethereum_types::{bytes32::Bytes32, u32limb_trait::U32LimbTrait};
use serde::{Deserialize, Serialize};
use intmax2_zkp::ethereum_types::bytes32::Bytes32;

use crate::client::strategy::entry_status::HistoryEntry;

use super::{
client::Client,
error::ClientError,
strategy::{deposit::fetch_deposit_info, transfer::fetch_transfer_info, tx::fetch_tx_info},
};

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct HistoryEntry<T> {
pub data: T,
pub status: EntryStatus,
pub meta: MetaData,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum EntryStatus {
Settled(u32), // Settled at block number but not processed yet
Processed(u32), // Incorporated into the balance proof
Pending, // Not settled yet
Timeout, // Timed out
}

impl EntryStatus {
pub fn from_settled(processed_digests: &[Bytes32], meta: MetaDataWithBlockNumber) -> Self {
if processed_digests.contains(&meta.meta.digest) {
EntryStatus::Processed(meta.block_number)
} else {
EntryStatus::Settled(meta.block_number)
}
}
}

pub async fn fetch_deposit_history(
client: &Client,
view_pair: ViewPair,
Expand All @@ -52,52 +21,22 @@ pub async fn fetch_deposit_history(
// We don't need to check validity prover's sync status like in strategy
// because fetching history is not a critical operation.
let current_time = chrono::Utc::now().timestamp() as u64;
let user_data = client.get_user_data(view_pair).await?;

let mut history = Vec::new();
let (all_deposit_info, cursor_response) = fetch_deposit_info(
let (history, cursor_response) = fetch_deposit_info(
client.store_vault_server.as_ref(),
client.validity_prover.as_ref(),
&client.liquidity_contract,
view_pair,
current_time,
&[],
&[],
cursor,
Some(cursor),
client.config.deposit_timeout,
)
.await?;
for (meta, settled) in all_deposit_info.settled {
history.push(HistoryEntry {
data: settled,
status: EntryStatus::from_settled(
&user_data.deposit_status.processed_digests,
meta.clone(),
),
meta: meta.meta,
});
}
for (meta, pending) in all_deposit_info.pending {
history.push(HistoryEntry {
data: pending,
status: EntryStatus::Pending,
meta,
});
}
for (meta, timeout) in all_deposit_info.timeout {
history.push(HistoryEntry {
data: timeout,
status: EntryStatus::Timeout,
meta,
});
}

history.sort_by_key(|entry| {
let HistoryEntry { meta, .. } = entry;
(meta.timestamp, meta.digest.to_hex())
});

Ok((history, cursor_response))
Ok((
history,
cursor_response.expect("Cursor response should be present"),
))
}

pub async fn fetch_transfer_history(
Expand All @@ -106,51 +45,23 @@ pub async fn fetch_transfer_history(
cursor: &MetaDataCursor,
) -> Result<(Vec<HistoryEntry<TransferData>>, MetaDataCursorResponse), ClientError> {
let current_time = chrono::Utc::now().timestamp() as u64;
let user_data = client.get_user_data(view_pair).await?;

let mut history = Vec::new();
let (all_transfers_info, cursor_response) = fetch_transfer_info(
let (history, cursor_response) = fetch_transfer_info(
client.store_vault_server.as_ref(),
client.validity_prover.as_ref(),
view_pair,
current_time,
&[],
&[],
cursor,
Some(cursor),
client.config.tx_timeout,
)
.await?;
for (meta, settled) in all_transfers_info.settled {
history.push(HistoryEntry {
data: settled,
status: EntryStatus::from_settled(
&user_data.transfer_status.processed_digests,
meta.clone(),
),
meta: meta.meta,
});
}
for (meta, pending) in all_transfers_info.pending {
history.push(HistoryEntry {
data: pending,
status: EntryStatus::Pending,
meta: meta.clone(),
});
}
for (meta, timeout) in all_transfers_info.timeout {
history.push(HistoryEntry {
data: timeout,
status: EntryStatus::Timeout,
meta: meta.clone(),
});
}

history.sort_by_key(|entry| {
let HistoryEntry { meta, .. } = entry;
(meta.timestamp, meta.digest.to_hex())
});

Ok((history, cursor_response))
Ok((
history,
cursor_response.expect("Cursor response should be present"),
))
}

pub async fn fetch_tx_history(
Expand All @@ -159,46 +70,83 @@ pub async fn fetch_tx_history(
cursor: &MetaDataCursor,
) -> Result<(Vec<HistoryEntry<TxData>>, MetaDataCursorResponse), ClientError> {
let current_time = chrono::Utc::now().timestamp() as u64;
let user_data = client.get_user_data(view_pair).await?;

let mut history = Vec::new();
let (all_tx_info, cursor_response) = fetch_tx_info(
let (history, cursor_response) = fetch_tx_info(
client.store_vault_server.as_ref(),
client.validity_prover.as_ref(),
view_pair,
current_time,
&[],
&[],
cursor,
Some(cursor),
client.config.tx_timeout,
)
.await?;
for (meta, settled) in all_tx_info.settled {
history.push(HistoryEntry {
data: settled,
status: EntryStatus::from_settled(&user_data.tx_status.processed_digests, meta.clone()),
meta: meta.meta.clone(),
});
}
for (meta, pending) in all_tx_info.pending {
history.push(HistoryEntry {
data: pending,
status: EntryStatus::Pending,
meta,
});
}
for (meta, timeout) in all_tx_info.timeout {
history.push(HistoryEntry {
data: timeout,
status: EntryStatus::Timeout,
meta,
});
}

history.sort_by_key(|entry| {
let HistoryEntry { meta, .. } = entry;
(meta.timestamp, meta.digest.to_hex())
});
Ok((
history,
cursor_response.expect("Cursor response should be present"),
))
}

Ok((history, cursor_response))
pub async fn fetch_deposit_batch(
client: &Client,
view_pair: ViewPair,
digests: &[Bytes32],
) -> Result<Vec<HistoryEntry<DepositData>>, ClientError> {
// We don't need to check validity prover's sync status like in strategy
// because fetching history is not a critical operation.
let current_time = chrono::Utc::now().timestamp() as u64;
let (history, _) = fetch_deposit_info(
client.store_vault_server.as_ref(),
client.validity_prover.as_ref(),
&client.liquidity_contract,
view_pair,
current_time,
digests,
&[],
None,
client.config.deposit_timeout,
)
.await?;
Ok(history)
}

pub async fn fetch_transfer_batch(
client: &Client,
view_pair: ViewPair,
digests: &[Bytes32],
) -> Result<Vec<HistoryEntry<TransferData>>, ClientError> {
let current_time = chrono::Utc::now().timestamp() as u64;
let (history, _) = fetch_transfer_info(
client.store_vault_server.as_ref(),
client.validity_prover.as_ref(),
view_pair,
current_time,
digests,
&[],
None,
client.config.tx_timeout,
)
.await?;
Ok(history)
}

pub async fn fetch_tx_batch(
client: &Client,
view_pair: ViewPair,
digests: &[Bytes32],
) -> Result<Vec<HistoryEntry<TxData>>, ClientError> {
let current_time = chrono::Utc::now().timestamp() as u64;
let (history, _) = fetch_tx_info(
client.store_vault_server.as_ref(),
client.validity_prover.as_ref(),
view_pair,
current_time,
digests,
&[],
None,
client.config.tx_timeout,
)
.await?;
Ok(history)
}
Loading