github.com/mavryk-network/mvgo@v1.19.9/codec/smart_rollup_cement.go (about)

     1  // Copyright (c) 2023 Blockwatch Data Inc.
     2  // Author: alex@blockwatch.cc
     3  
     4  package codec
     5  
     6  import (
     7  	"bytes"
     8  	"strconv"
     9  
    10  	"github.com/mavryk-network/mvgo/mavryk"
    11  )
    12  
    13  // SmartRollupCement represents "smart_rollup_cement" operation
    14  type SmartRollupCement struct {
    15  	Manager
    16  	Rollup mavryk.Address `json:"rollup"`
    17  }
    18  
    19  func (o SmartRollupCement) Kind() mavryk.OpType {
    20  	return mavryk.OpTypeSmartRollupCement
    21  }
    22  
    23  func (o SmartRollupCement) MarshalJSON() ([]byte, error) {
    24  	buf := bytes.NewBuffer(nil)
    25  	buf.WriteByte('{')
    26  	buf.WriteString(`"kind":`)
    27  	buf.WriteString(strconv.Quote(o.Kind().String()))
    28  	buf.WriteByte(',')
    29  	o.Manager.EncodeJSON(buf)
    30  	buf.WriteString(`,"rollup":`)
    31  	buf.WriteString(strconv.Quote(o.Rollup.String()))
    32  	buf.WriteByte('}')
    33  	return buf.Bytes(), nil
    34  }
    35  
    36  func (o SmartRollupCement) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
    37  	buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion))
    38  	o.Manager.EncodeBuffer(buf, p)
    39  	buf.Write(o.Rollup.Hash()) // 20 byte only
    40  	return nil
    41  }
    42  
    43  func (o *SmartRollupCement) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
    44  	if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil {
    45  		return
    46  	}
    47  	if err = o.Manager.DecodeBuffer(buf, p); err != nil {
    48  		return
    49  	}
    50  	o.Rollup = mavryk.NewAddress(mavryk.AddressTypeSmartRollup, buf.Next(20))
    51  	return
    52  }
    53  
    54  func (o SmartRollupCement) MarshalBinary() ([]byte, error) {
    55  	buf := bytes.NewBuffer(nil)
    56  	err := o.EncodeBuffer(buf, mavryk.DefaultParams)
    57  	return buf.Bytes(), err
    58  }
    59  
    60  func (o *SmartRollupCement) UnmarshalBinary(data []byte) error {
    61  	return o.DecodeBuffer(bytes.NewBuffer(data), mavryk.DefaultParams)
    62  }