use super::{Address, Boc, Cursor, Hash, HistoryPage, Nanotons, Network};
use crate::error::Error;
use serde::{Deserialize, Serialize};
use tycho_types::{
cell::Load,
models::{Message, MsgInfo},
};
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "FeeRequestFields")]
pub struct FeeRequest {
network: Network,
address: Address,
body: Boc,
init_code: Option<Boc>,
init_data: Option<Boc>,
ignore_signature: bool,
}
impl FeeRequest {
pub fn new(
network: Network,
address: Address,
body: Boc,
init_code: Option<Boc>,
init_data: Option<Boc>,
ignore_signature: bool,
) -> Result<Self, Error> {
address.check_network(network)?;
Ok(Self {
network,
address,
body,
init_code,
init_data,
ignore_signature,
})
}
#[must_use]
pub const fn network(&self) -> Network {
self.network
}
#[must_use]
pub const fn address(&self) -> Address {
self.address
}
#[must_use]
pub const fn body(&self) -> &Boc {
&self.body
}
#[must_use]
pub const fn init_code(&self) -> Option<&Boc> {
self.init_code.as_ref()
}
#[must_use]
pub const fn init_data(&self) -> Option<&Boc> {
self.init_data.as_ref()
}
#[must_use]
pub const fn ignore_signature(&self) -> bool {
self.ignore_signature
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct FeeRequestFields {
network: Network,
address: Address,
body: Boc,
init_code: Option<Boc>,
init_data: Option<Boc>,
ignore_signature: bool,
}
impl TryFrom<FeeRequestFields> for FeeRequest {
type Error = Error;
fn try_from(v: FeeRequestFields) -> Result<Self, Error> {
Self::new(
v.network,
v.address,
v.body,
v.init_code,
v.init_data,
v.ignore_signature,
)
}
}
#[derive(Clone, Copy, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FeePart {
pub incoming_forwarding_fee: Nanotons,
pub storage_fee: Nanotons,
pub gas_fee: Nanotons,
pub forwarding_fee: Nanotons,
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "FeeEstimateFields")]
pub struct FeeEstimate {
request: FeeRequest,
source: FeePart,
destinations: Vec<FeePart>,
}
impl FeeEstimate {
pub fn new(
request: FeeRequest,
source: FeePart,
destinations: Vec<FeePart>,
) -> Result<Self, Error> {
if destinations.len() > 256 {
return Err(super::invalid_record());
}
Ok(Self {
request,
source,
destinations,
})
}
#[must_use]
pub const fn request(&self) -> &FeeRequest {
&self.request
}
#[must_use]
pub const fn source(&self) -> FeePart {
self.source
}
#[must_use]
pub fn destinations(&self) -> &[FeePart] {
&self.destinations
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct FeeEstimateFields {
request: FeeRequest,
source: FeePart,
destinations: Vec<FeePart>,
}
impl TryFrom<FeeEstimateFields> for FeeEstimate {
type Error = Error;
fn try_from(v: FeeEstimateFields) -> Result<Self, Error> {
Self::new(v.request, v.source, v.destinations)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "MessageStatusFields")]
pub struct MessageStatus {
message_hash: Hash,
page: HistoryPage,
observed: Vec<Cursor>,
}
impl MessageStatus {
pub fn scan(message_hash: Hash, page: HistoryPage) -> Result<Self, Error> {
if message_hash == Hash::ZERO {
return Err(super::invalid_record());
}
let mut observed = Vec::new();
for tx in page.transactions() {
if tx.incoming().map(super::Message::hash).transpose()? == Some(message_hash) {
observed.push(tx.cursor());
}
}
Ok(Self {
message_hash,
page,
observed,
})
}
#[must_use]
pub const fn message_hash(&self) -> Hash {
self.message_hash
}
#[must_use]
pub const fn page(&self) -> &HistoryPage {
&self.page
}
#[must_use]
pub fn observed(&self) -> &[Cursor] {
&self.observed
}
#[must_use]
pub fn not_observed_within_page(&self) -> bool {
self.observed.is_empty()
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct MessageStatusFields {
message_hash: Hash,
page: HistoryPage,
observed: Vec<Cursor>,
}
impl TryFrom<MessageStatusFields> for MessageStatus {
type Error = Error;
fn try_from(v: MessageStatusFields) -> Result<Self, Error> {
let result = Self::scan(v.message_hash, v.page)?;
if result.observed != v.observed {
return Err(super::invalid_record());
}
Ok(result)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "SubmissionFields")]
pub struct SignedSubmission {
network: Network,
destination: Address,
boc: Boc,
message_hash: Hash,
}
impl SignedSubmission {
pub fn new(network: Network, destination: Address, boc: Boc) -> Result<Self, Error> {
destination.check_network(network)?;
let root = boc.root()?;
let mut slice = root.as_slice().map_err(|_| super::invalid_record())?;
let message = Message::load_from(&mut slice).map_err(|_| super::invalid_record())?;
if !slice.is_empty() {
return Err(super::invalid_record());
}
let MsgInfo::ExtIn(info) = message.info else {
return Err(super::invalid_record());
};
let actual = super::transaction::standard(&info.dst).ok_or_else(super::invalid_record)?;
if !actual.same_account(destination) {
return Err(super::invalid_record());
}
let message_hash = boc.hash()?;
Ok(Self {
network,
destination,
boc,
message_hash,
})
}
#[must_use]
pub const fn network(&self) -> Network {
self.network
}
#[must_use]
pub const fn destination(&self) -> Address {
self.destination
}
#[must_use]
pub const fn boc(&self) -> &Boc {
&self.boc
}
#[must_use]
pub const fn message_hash(&self) -> Hash {
self.message_hash
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct SubmissionFields {
network: Network,
destination: Address,
boc: Boc,
message_hash: Hash,
}
impl TryFrom<SubmissionFields> for SignedSubmission {
type Error = Error;
fn try_from(v: SubmissionFields) -> Result<Self, Error> {
let result = Self::new(v.network, v.destination, v.boc)?;
if result.message_hash != v.message_hash {
return Err(super::invalid_record());
}
Ok(result)
}
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(try_from = "SubmissionResultFields")]
pub struct SubmissionResult {
submission: SignedSubmission,
source_message_hash: Hash,
}
impl SubmissionResult {
pub fn new(submission: SignedSubmission, source_message_hash: Hash) -> Result<Self, Error> {
if submission.message_hash() != source_message_hash {
return Err(super::invalid_record());
}
Ok(Self {
submission,
source_message_hash,
})
}
#[must_use]
pub const fn submission(&self) -> &SignedSubmission {
&self.submission
}
#[must_use]
pub const fn source_message_hash(&self) -> Hash {
self.source_message_hash
}
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct SubmissionResultFields {
submission: SignedSubmission,
source_message_hash: Hash,
}
impl TryFrom<SubmissionResultFields> for SubmissionResult {
type Error = Error;
fn try_from(v: SubmissionResultFields) -> Result<Self, Error> {
Self::new(v.submission, v.source_message_hash)
}
}