-
Notifications
You must be signed in to change notification settings - Fork 49
/
main.go
66 lines (58 loc) · 1.81 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
import (
"encoding/hex"
"fmt"
"github.com/tonkeeper/tongo"
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
)
// You can serialize and deserialize (to/from Cells) structures described by TL-B schemas.
func main() {
// Deserialize
// TL-B schema:
// transfer#5fcc3d14 query_id:uint64 new_owner:MsgAddress
// response_destination:MsgAddress custom_payload:(Maybe ^Cell)
// forward_amount:(VarUInteger 16) forward_payload:(Either Cell ^Cell) = InternalMsgBody;
// Design new struct using ready-made TLB primitives
type InternalMsgBody struct {
Magic tlb.Magic `tlb:"transfer#5fcc3d14"`
QueryId uint64
NewOwner tlb.MsgAddress
ResponseDestination tlb.MsgAddress
CustomPayload tlb.Maybe[tlb.Ref[boc.Cell]]
ForwardAmount tlb.VarUInteger16
ForwardPayload tlb.EitherRef[boc.Cell]
}
b, err := hex.DecodeString("b5ee9c72c10101010056000000a75fcc3d140000000000000000800c0674dd00e3a7231084788441cc873e60eb8681f44901cba3a9107c5c322dc4500034a37c6673343b360e10d4e438483b555805a20e5f056742b6a42ba35311994c802625a008a90c976e")
if err != nil {
panic(err)
}
cell, err := boc.DeserializeBoc(b) // deserialize to bag-of-cells with one root cell
if err != nil {
panic(err)
}
var res InternalMsgBody
err = tlb.Unmarshal(cell[0], &res)
if err != nil {
panic(err)
}
newOwner, err := tongo.AccountIDFromTlb(res.NewOwner) // convert tongo.MsgAddress to basic AccountID type
if err != nil {
panic(err)
}
fmt.Printf("Deserialized data:\n QueryId: %v\n NewOwner: %v\n",
res.QueryId, newOwner.String())
// Serialize
newCell := boc.NewCell()
var X struct {
A uint8
B tlb.Ref[uint8]
}
X.A = 10
X.B.Value = 11
err = tlb.Marshal(newCell, X)
if err != nil {
panic(err)
}
fmt.Printf("Serialized cell:\n %v", newCell.ToString())
}