Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement marshal tlb for inMsgBody #305

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
}
}
Loading