Skip to content
Open
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
17 changes: 15 additions & 2 deletions src/core/contract_subscription/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,21 @@ impl ContractSubscription {
clock: Arc<dyn Clock>,
transport: Arc<dyn Transport>,
address: MsgAddressInt,
cached_state: Option<ContractSubscriptionCachedState>,
on_contract_state: OnContractState<'_>,
on_transactions_found: Option<OnTransactionsFound<'_>>,
) -> Result<Self> {
let (contract_state, latest_known_lt) = match cached_state {
Some(cached) => (cached.contract_state, cached.latest_known_lt),
None => (Default::default(), None),
};

let mut result = Self {
clock,
transport,
address,
contract_state: Default::default(),
latest_known_lt: None,
contract_state,
latest_known_lt,
pending_transactions: Vec::new(),
transactions_synced: false,
};
Expand Down Expand Up @@ -488,6 +494,13 @@ pub struct TransactionExecutionOptions {
pub override_balance: Option<u64>,
}

#[derive(Debug, Clone, Default, Serialize, Deserialize, Copy)]
#[serde(rename_all = "camelCase")]
pub struct ContractSubscriptionCachedState {
contract_state: ContractState,
latest_known_lt: Option<u64>,
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
7 changes: 6 additions & 1 deletion src/core/generic_contract/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ use ton_block::{GetRepresentationHash, MsgAddressInt};
use nekoton_utils::Clock;

use super::models::{ContractState, PendingTransaction, Transaction, TransactionsBatchInfo};
use super::{ContractSubscription, PollingMethod, TransactionExecutionOptions};
use super::{
ContractSubscription, ContractSubscriptionCachedState, PollingMethod,
TransactionExecutionOptions,
};
use crate::core::utils;
use crate::transport::models::{RawContractState, RawTransaction};
use crate::transport::Transport;
Expand All @@ -24,6 +27,7 @@ impl GenericContract {
address: MsgAddressInt,
handler: Arc<dyn GenericContractSubscriptionHandler>,
preload_transactions: bool,
cached_state: Option<ContractSubscriptionCachedState>,
) -> Result<Self> {
let contract_subscription = {
let handler = handler.as_ref();
Expand All @@ -47,6 +51,7 @@ impl GenericContract {
clock,
transport,
address,
cached_state,
&mut make_contract_state_handler(handler),
on_transactions_found,
)
Expand Down
4 changes: 3 additions & 1 deletion src/core/jetton_wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use num_bigint::{BigInt, BigUint, ToBigInt};
use ton_block::{MsgAddressInt, Serializable};
use ton_types::{BuilderData, IBitstring, SliceData};

use super::{utils, ContractSubscription, InternalMessage};
use super::{utils, ContractSubscription, ContractSubscriptionCachedState, InternalMessage};

pub const JETTON_TRANSFER_OPCODE: u32 = 0x0f8a7ea5;
pub const JETTON_INTERNAL_TRANSFER_OPCODE: u32 = 0x178d4519;
Expand All @@ -36,6 +36,7 @@ impl JettonWallet {
root_token_contract: MsgAddressInt,
handler: Arc<dyn JettonWalletSubscriptionHandler>,
preload_transactions: bool,
cached_state: Option<ContractSubscriptionCachedState>,
) -> Result<JettonWallet> {
let state = match transport.get_contract_state(&root_token_contract).await? {
RawContractState::Exists(state) => state,
Expand Down Expand Up @@ -69,6 +70,7 @@ impl JettonWallet {
clock.clone(),
transport,
address,
cached_state,
&mut make_contract_state_handler(clock.clone(), &mut balance),
on_transactions_found,
)
Expand Down
4 changes: 3 additions & 1 deletion src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ use anyhow::Result;
use nekoton_utils::*;
use serde::{Deserialize, Serialize};

pub use self::contract_subscription::{ContractSubscription, TransactionExecutionOptions};
pub use self::contract_subscription::{
ContractSubscription, ContractSubscriptionCachedState, TransactionExecutionOptions,
};
use self::models::PollingMethod;
use crate::transport::Transport;

Expand Down
1 change: 1 addition & 0 deletions src/core/nft_wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ impl Nft {
clock.clone(),
transport,
nft_address.clone(),
None,
&mut make_contract_state_handler(
clock.as_ref(),
&mut info.owner,
Expand Down
4 changes: 3 additions & 1 deletion src/core/token_wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use num_bigint::{BigInt, BigUint, ToBigInt};
use ton_block::MsgAddressInt;
use ton_executor::BlockchainConfig;

use super::{ContractSubscription, InternalMessage};
use super::{ContractSubscription, ContractSubscriptionCachedState, InternalMessage};

pub struct TokenWallet {
clock: Arc<dyn Clock>,
Expand All @@ -36,6 +36,7 @@ impl TokenWallet {
root_token_contract: MsgAddressInt,
handler: Arc<dyn TokenWalletSubscriptionHandler>,
preload_transactions: bool,
cached_state: Option<ContractSubscriptionCachedState>,
) -> Result<TokenWallet> {
let state = match transport.get_contract_state(&root_token_contract).await? {
RawContractState::Exists(state) => state,
Expand Down Expand Up @@ -77,6 +78,7 @@ impl TokenWallet {
clock.clone(),
transport,
address,
cached_state,
&mut make_contract_state_handler(clock.clone(), version, &mut balance),
on_transactions_found,
)
Expand Down
8 changes: 7 additions & 1 deletion src/core/ton_wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use super::models::{
PendingTransaction, Transaction, TransactionAdditionalInfo, TransactionWithData,
TransactionsBatchInfo,
};
use super::{ContractSubscription, PollingMethod};
use super::{ContractSubscription, ContractSubscriptionCachedState, PollingMethod};
use crate::core::parsing::*;
use crate::core::InternalMessage;
use crate::crypto::UnsignedMessage;
Expand Down Expand Up @@ -52,6 +52,7 @@ impl TonWallet {
public_key: PublicKey,
wallet_type: WalletType,
handler: Arc<dyn TonWalletSubscriptionHandler>,
cached_state: Option<ContractSubscriptionCachedState>,
) -> Result<Self> {
let address = compute_address(&public_key, wallet_type, workchain);

Expand All @@ -61,6 +62,7 @@ impl TonWallet {
clock.clone(),
transport,
address,
cached_state,
&mut make_contract_state_handler(
clock.as_ref(),
handler.as_ref(),
Expand Down Expand Up @@ -90,6 +92,7 @@ impl TonWallet {
transport: Arc<dyn Transport>,
address: MsgAddressInt,
handler: Arc<dyn TonWalletSubscriptionHandler>,
cached_state: Option<ContractSubscriptionCachedState>,
) -> Result<Self> {
let (public_key, wallet_type) = match transport.get_contract_state(&address).await? {
RawContractState::Exists(contract) => extract_wallet_init_data(&contract)?,
Expand All @@ -104,6 +107,7 @@ impl TonWallet {
clock.clone(),
transport,
address,
cached_state,
&mut make_contract_state_handler(
clock.as_ref(),
handler.as_ref(),
Expand Down Expand Up @@ -133,13 +137,15 @@ impl TonWallet {
transport: Arc<dyn Transport>,
existing_wallet: ExistingWalletInfo,
handler: Arc<dyn TonWalletSubscriptionHandler>,
cached_state: Option<ContractSubscriptionCachedState>,
) -> Result<Self> {
let mut wallet_data = WalletData::default();

let contract_subscription = ContractSubscription::subscribe(
clock.clone(),
transport,
existing_wallet.address,
cached_state,
&mut make_contract_state_handler(
clock.as_ref(),
handler.as_ref(),
Expand Down
Loading