github.com/mavryk-network/mvgo@v1.19.9/codec/drain_delegate.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  // DrainDelegate represents "drain_delegate" operation
    14  type DrainDelegate struct {
    15  	Simple
    16  	ConsensusKey mavryk.Address `json:"consensus_key"`
    17  	Delegate     mavryk.Address `json:"delegate"`
    18  	Destination  mavryk.Address `json:"destination"`
    19  }
    20  
    21  func (o DrainDelegate) Kind() mavryk.OpType {
    22  	return mavryk.OpTypeDrainDelegate
    23  }
    24  
    25  func (o DrainDelegate) MarshalJSON() ([]byte, error) {
    26  	buf := bytes.NewBuffer(nil)
    27  	buf.WriteByte('{')
    28  	buf.WriteString(`"kind":`)
    29  	buf.WriteString(strconv.Quote(o.Kind().String()))
    30  	buf.WriteString(`,"consensus_key":`)
    31  	buf.WriteString(strconv.Quote(o.ConsensusKey.String()))
    32  	buf.WriteString(`,"delegate":`)
    33  	buf.WriteString(strconv.Quote(o.Delegate.String()))
    34  	buf.WriteString(`,"destination":`)
    35  	buf.WriteString(strconv.Quote(o.Destination.String()))
    36  	buf.WriteByte('}')
    37  	return buf.Bytes(), nil
    38  }
    39  
    40  func (o DrainDelegate) EncodeBuffer(buf *bytes.Buffer, p *mavryk.Params) error {
    41  	buf.WriteByte(o.Kind().TagVersion(p.OperationTagsVersion))
    42  	buf.Write(o.ConsensusKey.Encode())
    43  	buf.Write(o.Delegate.Encode())
    44  	buf.Write(o.Destination.Encode())
    45  	return nil
    46  }
    47  
    48  func (o *DrainDelegate) DecodeBuffer(buf *bytes.Buffer, p *mavryk.Params) (err error) {
    49  	if err = ensureTagAndSize(buf, o.Kind(), p.OperationTagsVersion); err != nil {
    50  		return
    51  	}
    52  	if err = o.ConsensusKey.Decode(buf.Next(21)); err != nil {
    53  		return
    54  	}
    55  	if err = o.Delegate.Decode(buf.Next(21)); err != nil {
    56  		return
    57  	}
    58  	if err = o.Destination.Decode(buf.Next(21)); err != nil {
    59  		return
    60  	}
    61  	return nil
    62  }
    63  
    64  func (o DrainDelegate) MarshalBinary() ([]byte, error) {
    65  	buf := bytes.NewBuffer(nil)
    66  	err := o.EncodeBuffer(buf, mavryk.DefaultParams)
    67  	return buf.Bytes(), err
    68  }
    69  
    70  func (o *DrainDelegate) UnmarshalBinary(data []byte) error {
    71  	return o.DecodeBuffer(bytes.NewBuffer(data), mavryk.DefaultParams)
    72  }