Skip to content

Commit 2c346a4

Browse files
authored
feat(docs): document fromCell and fromSlice methods on contract types (#3305)
1 parent 79ff790 commit 2c346a4

File tree

6 files changed

+305
-85
lines changed

6 files changed

+305
-85
lines changed

dev-docs/CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
99

1010
### Language features
1111

12-
- Support `fromCell()` and `fromSlice()` methods on contract types: PR [#3303](https://github.com/tact-lang/tact/pull/3303)
12+
- Support `fromCell()` and `fromSlice()` methods on contract types: PR [#3305](https://github.com/tact-lang/tact/pull/3305)
1313

1414
### Release contributors
1515

1616
- [Anton Trunov](https://github.com/anton-trunov)
17+
- [Novus Nota](https://github.com/novusnota)
1718

1819
## [1.6.12] - 2025-05-27
1920

docs/src/content/docs/book/cells.mdx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,9 @@ By using structures instead of manual [`Cell{:tact}`](#cells) composition and pa
340340
[`Message.fromCell(){:tact}` in Core library][msg-fc]\
341341
[`Message.fromSlice(){:tact}` in Core library][msg-fs]\
342342
[`Contract.toCell(){:tact}` in Core library][ct-tc]\
343-
[`Contract.toSlice(){:tact}` in Core library][ct-ts]
343+
[`Contract.toSlice(){:tact}` in Core library][ct-ts]\
344+
[`Contract.fromCell(){:tact}` in Core library][ct-fc]\
345+
[`Contract.fromSlice(){:tact}` in Core library][ct-fs]
344346

345347
:::
346348

@@ -352,6 +354,8 @@ By using structures instead of manual [`Cell{:tact}`](#cells) composition and pa
352354
[msg-fs]: /ref/core-cells#messagefromslice
353355
[ct-tc]: /ref/core-cells#contracttocell
354356
[ct-ts]: /ref/core-cells#contracttoslice
357+
[ct-fc]: /ref/core-cells#contractfromcell
358+
[ct-fs]: /ref/core-cells#contractfromslice
355359

356360
### Check if empty {#operations-empty}
357361

docs/src/content/docs/book/contracts.mdx

Lines changed: 47 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -538,16 +538,15 @@ Read more about them in their dedicated section: [Internal functions](/book/func
538538

539539
:::
540540

541-
### Converting to Cell or Slice
541+
### Operations
542+
543+
#### Converting to Cell or Slice {#tocellslice}
542544

543545
<Badge text="Available since Tact 1.6.12" variant="tip" size="medium"/><p/>
544546

545-
You can convert a contract instance to a [`Cell{:tact}`](/book/cells#cells) or [`Slice{:tact}`](/book/cells#slices) using the `toCell(){:tact}` and `toSlice(){:tact}` methods.
546-
These methods will build a new `Cell{:tact}` or `Slice{:tact}` with the contract data.
547+
You can convert a contract instance to a [`Cell{:tact}`][cell] or [`Slice{:tact}`][slice] using the [`toCell(){:tact}`][ct-tc] and [`toSlice(){:tact}`][ct-ts] methods. These methods will build a new `Cell{:tact}` or `Slice{:tact}` with the contract data.
547548

548549
```tact
549-
// ...
550-
551550
contract WalletV4(
552551
seqno: Int as uint32,
553552
walletId: Int as int32,
@@ -576,7 +575,7 @@ This is useful when you need to explicitly set data of your contract for low lev
576575

577576
:::note
578577

579-
If a contract doesn't use [contract parameters](/book/contracts#parameters), the resulting `Cell{:tact}` or `Slice{:tact}` will contain a leading one bit representing the [lazy initialization bit](/book/functions/#init).
578+
If a contract doesn't use [contract parameters](/book/contracts#parameters), the resulting `Cell{:tact}` or `Slice{:tact}` will contain a leading one bit representing the [lazy initialization bit](/book/functions/#init).
580579

581580
:::
582581

@@ -587,8 +586,48 @@ This is useful when you need to explicitly set data of your contract for low lev
587586

588587
:::
589588

589+
#### Obtaining from Cell or Slice {#fromcellslice}
590+
591+
<Badge text="Available since Tact 1.6.13" variant="tip" size="medium"/><p/>
592+
593+
If you have a contract data [`Cell{:tact}`][cell] or [`Slice{:tact}`][slice] with composed using the [`toCell{:tact}` or `toSlice{:tact}` methods](#tocellslice), you can obtain the initial contract [struct](/book/structs-and-messages#structs) back with the respective [`fromCell(){:tact}`][ct-fc] and [`fromSlice(){:tact}`][ct-fs] functions. These functions would parse a given `Cell{:tact}` or `Slice{:tact}` and produce a struct with contract variables.
594+
595+
```tact
596+
contract PointCaller(
597+
x: Int as uint32,
598+
y: Int as uint32,
599+
) {
600+
receive(msg: NewContractData) {
601+
let contractStruct = PointCaller.fromCell(msg.cell);
602+
self.x = contractStruct.x;
603+
self.y = contractStruct.y;
604+
}
605+
606+
get fun inverseLaw(): Bool {
607+
let contractStruct = PointCaller.fromCell(self.toCell());
608+
let contractCell = self.toCell();
609+
610+
return contractStruct.toCell() == contractCell; // always true
611+
}
612+
}
613+
614+
message NewContractData {
615+
// Can be obtained with a call to PointCaller.toCell()
616+
cell: Cell;
617+
}
618+
```
619+
620+
:::note[Useful links:]
621+
622+
[`Contract.fromCell(){:tact}` in Core library][ct-fc]\
623+
[`Contract.fromSlice(){:tact}` in Core library][ct-fs]
624+
625+
:::
626+
590627
[p]: /book/types#primitive-types
591628
[int]: /book/integers
629+
[cell]: /book/cells#cells
630+
[slice]: /book/cells#slices
592631
[trait]: /book/types#traits
593632

594633
[compute]: https://docs.ton.org/learn/tvm-instructions/tvm-overview#compute-phase
@@ -598,3 +637,5 @@ This is useful when you need to explicitly set data of your contract for low lev
598637

599638
[ct-tc]: /ref/core-cells#contracttocell
600639
[ct-ts]: /ref/core-cells#contracttoslice
640+
[ct-fc]: /ref/core-cells#contractfromcell
641+
[ct-fs]: /ref/core-cells#contractfromslice

docs/src/content/docs/ref/core-cells.mdx

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,6 +2113,80 @@ contract Example {
21132113

21142114
:::
21152115

2116+
## Contract.fromCell
2117+
2118+
<Badge text="Available since Tact 1.6.13" variant="tip" size="medium"/><p/>
2119+
2120+
```tact
2121+
fun fromCell(cell: Cell): Contract;
2122+
```
2123+
2124+
Extension function for any [contract][contract] type.
2125+
2126+
Converts a [`Cell{:tact}`][cell] into the specified contract struct and returns it.
2127+
2128+
Usage example:
2129+
2130+
```tact
2131+
contract GuessCoinContract {
2132+
probably: Int as coins = 42;
2133+
nothing: Int as coins = 27;
2134+
2135+
receive() { cashback(sender()) }
2136+
}
2137+
2138+
fun directParse(payload: Cell): GuessCoinContract {
2139+
return GuessCoinContract.fromCell(payload);
2140+
}
2141+
2142+
fun cautiousParse(payload: Cell): GuessCoinContract? {
2143+
let coin: GuessCoinContract? = null;
2144+
try {
2145+
coin = GuessCoinContract.fromCell(payload);
2146+
} catch (e) {
2147+
dump("Cell payload doesn't match GuessCoinContract struct!");
2148+
}
2149+
return coin;
2150+
}
2151+
```
2152+
2153+
## Contract.fromSlice
2154+
2155+
<Badge text="Available since Tact 1.6.13" variant="tip" size="medium"/><p/>
2156+
2157+
```tact
2158+
fun fromSlice(slice: Slice): Contract;
2159+
```
2160+
2161+
Extension function for any [contract][contract] type.
2162+
2163+
Converts a [`Slice{:tact}`][slice] into the specified contract struct and returns it.
2164+
2165+
Usage example:
2166+
2167+
```tact
2168+
contract GuessCoinContract {
2169+
probably: Int as coins = 42;
2170+
nothing: Int as coins = 27;
2171+
2172+
receive() { cashback(sender()) }
2173+
}
2174+
2175+
fun directParse(payload: Slice): GuessCoinContract {
2176+
return GuessCoinContract.fromSlice(payload);
2177+
}
2178+
2179+
fun cautiousParse(payload: Slice): GuessCoinContract? {
2180+
let coin: GuessCoinContract? = null;
2181+
try {
2182+
coin = GuessCoinContract.fromSlice(payload);
2183+
} catch (e) {
2184+
dump("Slice payload doesn't match GuessCoinContract struct!");
2185+
}
2186+
return coin;
2187+
}
2188+
```
2189+
21162190
[p]: /book/types#primitive-types
21172191
[bool]: /book/types#booleans
21182192
[int]: /book/integers

0 commit comments

Comments
 (0)