tonlib_core_anychain/cell/
state_init.rs

1use super::ArcCell;
2use crate::cell::{Cell, CellBuilder, TonCellError};
3use crate::TonHash;
4
5pub struct StateInitBuilder {
6    code: Option<ArcCell>,
7    data: Option<ArcCell>,
8    split_depth: bool,
9    tick_tock: bool,
10    library: bool,
11}
12pub struct StateInit {
13    pub code: Option<ArcCell>,
14    pub data: Option<ArcCell>,
15}
16
17impl StateInitBuilder {
18    pub fn new(code: &ArcCell, data: &ArcCell) -> StateInitBuilder {
19        StateInitBuilder {
20            code: Some(code.clone()),
21            data: Some(data.clone()),
22            split_depth: false,
23            tick_tock: false,
24            library: false,
25        }
26    }
27
28    pub fn with_split_depth(&mut self, split_depth: bool) -> &mut Self {
29        self.split_depth = split_depth;
30        self
31    }
32
33    pub fn with_tick_tock(&mut self, tick_tock: bool) -> &mut Self {
34        self.tick_tock = tick_tock;
35        self
36    }
37
38    pub fn with_library(&mut self, library: bool) -> &mut Self {
39        self.library = library;
40        self
41    }
42
43    pub fn build(&self) -> Result<Cell, TonCellError> {
44        let mut builder = CellBuilder::new();
45        builder
46            .store_bit(self.split_depth)? //Split depth
47            .store_bit(self.tick_tock)? //Tick tock
48            .store_bit(self.code.is_some())? //Code
49            .store_bit(self.data.is_some())? //Data
50            .store_bit(self.library)?; //Library
51        if let Some(code) = &self.code {
52            builder.store_reference(code)?;
53        }
54        if let Some(data) = &self.data {
55            builder.store_reference(data)?;
56        }
57        builder.build()
58    }
59}
60
61impl StateInit {
62    pub fn create_account_id(code: &ArcCell, data: &ArcCell) -> Result<TonHash, TonCellError> {
63        Ok(StateInitBuilder::new(code, data).build()?.cell_hash())
64    }
65}
66
67#[cfg(test)]
68mod tests {
69    use std::sync::Arc;
70
71    use super::StateInitBuilder;
72    use crate::cell::{CellBuilder, TonCellError};
73
74    #[test]
75    fn test_state_init() -> Result<(), TonCellError> {
76        let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
77        let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
78        let state_init = StateInitBuilder::new(&code, &data)
79            .with_split_depth(true)
80            .with_tick_tock(true)
81            .with_library(true)
82            .build()?;
83
84        assert_eq!(state_init.data[0], 0b11111000);
85        println!("{:08b}", state_init.data[0]);
86
87        let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
88        let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
89        let state_init = StateInitBuilder::new(&code, &data)
90            .with_split_depth(false)
91            .with_tick_tock(false)
92            .with_library(false)
93            .build()?;
94
95        assert_eq!(state_init.data[0], 0b00110000);
96
97        let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
98        let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
99        let state_init = StateInitBuilder::new(&code, &data)
100            .with_split_depth(true)
101            .with_tick_tock(false)
102            .with_library(false)
103            .build()?;
104
105        assert_eq!(state_init.data[0], 0b10110000);
106
107        let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
108        let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
109        let state_init = StateInitBuilder::new(&code, &data)
110            .with_split_depth(false)
111            .with_tick_tock(true)
112            .with_library(false)
113            .build()?;
114        assert_eq!(state_init.data[0], 0b01110000);
115
116        let code = Arc::new(CellBuilder::new().store_string("code")?.build()?);
117        let data = Arc::new(CellBuilder::new().store_string("data")?.build()?);
118        let state_init = StateInitBuilder::new(&code, &data)
119            .with_split_depth(false)
120            .with_tick_tock(false)
121            .with_library(true)
122            .build()?;
123        assert_eq!(state_init.data[0], 0b00111000);
124        Ok(())
125    }
126}