From 1390980f0d64de6a6c95db69cfaefbca05ecdbb1 Mon Sep 17 00:00:00 2001 From: Victoria Erokhina Date: Thu, 3 Oct 2024 19:42:40 +0000 Subject: [PATCH 1/2] implement marshal tlb for inMsgBody --- abi/messages.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/abi/messages.go b/abi/messages.go index 8b612397..89dfd16c 100644 --- a/abi/messages.go +++ b/abi/messages.go @@ -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) @@ -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 { @@ -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 { From 4851167e9b6d0b98126a737f86c26ca794d8a810 Mon Sep 17 00:00:00 2001 From: Victoria Erokhina Date: Thu, 3 Oct 2024 20:23:53 +0000 Subject: [PATCH 2/2] Add test --- abi/messages_test.go | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 abi/messages_test.go diff --git a/abi/messages_test.go b/abi/messages_test.go new file mode 100644 index 00000000..6bfdcc36 --- /dev/null +++ b/abi/messages_test.go @@ -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") + } +}