use crate::chain::TvmAddress;
use crate::codecs::w5::StateInitCells;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TvmAccountState {
pub address: TvmAddress,
pub balance: u128,
pub is_active: bool,
pub is_uninitialized: bool,
pub is_frozen: bool,
pub state_init: Option<StateInitCells>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TvmJettonWalletData {
pub address: TvmAddress,
pub balance: u128,
pub owner: TvmAddress,
pub jetton_minter: TvmAddress,
}
#[derive(Debug, thiserror::Error)]
pub enum TvmRpcError {
#[error("tvm rpc error: {0}")]
Rpc(String),
#[error("tvm rpc parse error: {0}")]
Parse(String),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TvmProviderKind {
Toncenter,
Tonapi,
}
impl TvmProviderKind {
pub fn parse(name: &str) -> Result<Self, TvmRpcError> {
match name.trim().to_ascii_lowercase().as_str() {
"toncenter" => Ok(Self::Toncenter),
"tonapi" => Ok(Self::Tonapi),
other => Err(TvmRpcError::Parse(format!(
"Unsupported TVM provider: {other}"
))),
}
}
}
pub trait TvmRpc: Send + Sync {
fn get_account_state(
&self,
address: &str,
) -> impl Future<Output = Result<TvmAccountState, TvmRpcError>> + Send;
fn get_jetton_wallet(
&self,
asset: &str,
owner: &str,
) -> impl Future<Output = Result<TvmAddress, TvmRpcError>> + Send;
fn get_jetton_wallet_data(
&self,
address: &str,
) -> impl Future<Output = Result<TvmJettonWalletData, TvmRpcError>> + Send;
fn emulate_trace(
&self,
boc: &[u8],
ignore_chksig: bool,
timeout_seconds: u64,
) -> impl Future<Output = Result<serde_json::Value, TvmRpcError>> + Send;
fn send_message(&self, boc: &[u8]) -> impl Future<Output = Result<String, TvmRpcError>> + Send;
fn get_trace_by_message_hash(
&self,
message_hash: &str,
) -> impl Future<Output = Result<serde_json::Value, TvmRpcError>> + Send;
}
impl<T: TvmRpc + Send + Sync> TvmRpc for &T {
fn get_account_state(
&self,
address: &str,
) -> impl Future<Output = Result<TvmAccountState, TvmRpcError>> + Send {
(**self).get_account_state(address)
}
fn get_jetton_wallet(
&self,
asset: &str,
owner: &str,
) -> impl Future<Output = Result<TvmAddress, TvmRpcError>> + Send {
(**self).get_jetton_wallet(asset, owner)
}
fn get_jetton_wallet_data(
&self,
address: &str,
) -> impl Future<Output = Result<TvmJettonWalletData, TvmRpcError>> + Send {
(**self).get_jetton_wallet_data(address)
}
fn emulate_trace(
&self,
boc: &[u8],
ignore_chksig: bool,
timeout_seconds: u64,
) -> impl Future<Output = Result<serde_json::Value, TvmRpcError>> + Send {
(**self).emulate_trace(boc, ignore_chksig, timeout_seconds)
}
fn send_message(&self, boc: &[u8]) -> impl Future<Output = Result<String, TvmRpcError>> + Send {
(**self).send_message(boc)
}
fn get_trace_by_message_hash(
&self,
message_hash: &str,
) -> impl Future<Output = Result<serde_json::Value, TvmRpcError>> + Send {
(**self).get_trace_by_message_hash(message_hash)
}
}
impl<T: TvmRpc + Send + Sync> TvmRpc for std::sync::Arc<T> {
fn get_account_state(
&self,
address: &str,
) -> impl Future<Output = Result<TvmAccountState, TvmRpcError>> + Send {
(**self).get_account_state(address)
}
fn get_jetton_wallet(
&self,
asset: &str,
owner: &str,
) -> impl Future<Output = Result<TvmAddress, TvmRpcError>> + Send {
(**self).get_jetton_wallet(asset, owner)
}
fn get_jetton_wallet_data(
&self,
address: &str,
) -> impl Future<Output = Result<TvmJettonWalletData, TvmRpcError>> + Send {
(**self).get_jetton_wallet_data(address)
}
fn emulate_trace(
&self,
boc: &[u8],
ignore_chksig: bool,
timeout_seconds: u64,
) -> impl Future<Output = Result<serde_json::Value, TvmRpcError>> + Send {
(**self).emulate_trace(boc, ignore_chksig, timeout_seconds)
}
fn send_message(&self, boc: &[u8]) -> impl Future<Output = Result<String, TvmRpcError>> + Send {
(**self).send_message(boc)
}
fn get_trace_by_message_hash(
&self,
message_hash: &str,
) -> impl Future<Output = Result<serde_json::Value, TvmRpcError>> + Send {
(**self).get_trace_by_message_hash(message_hash)
}
}