Skip to content

Commit

Permalink
Merge pull request #305 from tonkeeper/marshal_tlb_inmsgbody
Browse files Browse the repository at this point in the history
implement marshal tlb for inMsgBody
  • Loading branch information
erokhinav authored Oct 3, 2024
2 parents 7e3e5c2 + 4851167 commit 626c2ed
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
24 changes: 22 additions & 2 deletions abi/messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,26 @@ type InMsgBody struct {
Value any
}

func (body InMsgBody) MarshalTLB(c *boc.Cell, encoder *tlb.Encoder) error {
if body.SumType == EmptyMsgOp {
return nil
}
if body.SumType == UnknownMsgOp {
c, ok := body.Value.(*boc.Cell)
if !ok {
return fmt.Errorf("unknown MsgBody should be Cell")
}
return tlb.Marshal(c, body.Value)
}
if body.OpCode != nil {
err := c.WriteUint(uint64(*body.OpCode), 32)
if err != nil {
return err
}
}
return tlb.Marshal(c, body.Value)
}

func (body *InMsgBody) UnmarshalTLB(cell *boc.Cell, decoder *tlb.Decoder) error {
body.SumType = UnknownMsgOp
tag, name, value, err := InternalMessageDecoder(cell, nil)
Expand Down Expand Up @@ -139,7 +159,7 @@ func (body InMsgBody) MarshalJSON() ([]byte, error) {
return buf.Bytes(), nil
}
if KnownMsgInTypes[body.SumType] == nil {
return nil, fmt.Errorf("unknown MsgBosy type %v", body.SumType)
return nil, fmt.Errorf("unknown MsgBody type %v", body.SumType)
}
b, err := json.Marshal(body.Value)
if err != nil {
Expand Down Expand Up @@ -238,7 +258,7 @@ func (body ExtOutMsgBody) MarshalJSON() ([]byte, error) {
return buf.Bytes(), nil
}
if KnownMsgExtOutTypes[body.SumType] == nil {
return nil, fmt.Errorf("unknown MsgBosy type %v", body.SumType)
return nil, fmt.Errorf("unknown MsgBody type %v", body.SumType)
}
b, err := json.Marshal(body.Value)
if err != nil {
Expand Down
41 changes: 41 additions & 0 deletions abi/messages_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package abi

import (
"github.com/tonkeeper/tongo/boc"
"github.com/tonkeeper/tongo/tlb"
"github.com/tonkeeper/tongo/ton"
"reflect"
"testing"
)

func TestEncodeAndDecodeInMsgBody(t *testing.T) {
value := InMsgBody{
SumType: "JettonTransfer",
OpCode: pointer(uint32(260734629)),
Value: JettonTransferMsgBody{
QueryId: 0,
Amount: mustToVarUInteger16("1000000"),
Destination: pointer(ton.MustParseAccountID("0:6ccd325a858c379693fae2bcaab1c2906831a4e10a6c3bb44ee8b615bca1d220")).ToMsgAddress(),
ResponseDestination: pointer(ton.MustParseAccountID("0:6ccd325a858c379693fae2bcaab1c2906831a4e10a6c3bb44ee8b615bca1d220")).ToMsgAddress(),
CustomPayload: nil,
ForwardTonAmount: mustToVarUInteger16("300000000"),
ForwardPayload: tlb.EitherRef[JettonPayload]{
IsRight: false,
},
},
}

msg := boc.NewCell()
if err := tlb.Marshal(msg, value); err != nil {
t.Fatalf("Unable to marshal: %v", err)
}

var b InMsgBody
if err := tlb.Unmarshal(msg, &b); err != nil {
t.Fatalf("Unable to unmarshal: %v", err)
}

if !reflect.DeepEqual(b, value) {
t.Fatalf("got different result")
}
}

0 comments on commit 626c2ed

Please sign in to comment.