code.vegaprotocol.io/vega@v0.79.0/datanode/entities/successor_markets.go (about)

     1  // Copyright (C) 2023 Gobalsky Labs Limited
     2  //
     3  // This program is free software: you can redistribute it and/or modify
     4  // it under the terms of the GNU Affero General Public License as
     5  // published by the Free Software Foundation, either version 3 of the
     6  // License, or (at your option) any later version.
     7  //
     8  // This program is distributed in the hope that it will be useful,
     9  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    10  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    11  // GNU Affero General Public License for more details.
    12  //
    13  // You should have received a copy of the GNU Affero General Public License
    14  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    15  
    16  package entities
    17  
    18  import (
    19  	"encoding/json"
    20  	"fmt"
    21  	"time"
    22  
    23  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    24  	"code.vegaprotocol.io/vega/protos/vega"
    25  )
    26  
    27  type SuccessorMarket struct {
    28  	Market    Market
    29  	Proposals []*Proposal
    30  }
    31  
    32  func (s SuccessorMarket) ToProtoEdge(...any) (*v2.SuccessorMarketEdge, error) {
    33  	props := make([]*vega.GovernanceData, len(s.Proposals))
    34  
    35  	for i, p := range s.Proposals {
    36  		props[i] = &vega.GovernanceData{
    37  			Proposal: p.ToProto(),
    38  		}
    39  	}
    40  
    41  	e := &v2.SuccessorMarketEdge{
    42  		Node: &v2.SuccessorMarket{
    43  			Market:    s.Market.ToProto(),
    44  			Proposals: props,
    45  		},
    46  		Cursor: s.Market.Cursor().Encode(),
    47  	}
    48  
    49  	return e, nil
    50  }
    51  
    52  func (s SuccessorMarket) Cursor() *Cursor {
    53  	c := SuccessorMarketCursor{
    54  		VegaTime: s.Market.VegaTime,
    55  	}
    56  	return NewCursor(c.String())
    57  }
    58  
    59  type SuccessorMarketCursor struct {
    60  	VegaTime time.Time `json:"vegaTime"`
    61  }
    62  
    63  func (mc SuccessorMarketCursor) String() string {
    64  	bs, err := json.Marshal(mc)
    65  	if err != nil {
    66  		panic(fmt.Errorf("could not marshal market cursor: %w", err))
    67  	}
    68  	return string(bs)
    69  }
    70  
    71  func (mc *SuccessorMarketCursor) Parse(cursorString string) error {
    72  	if cursorString == "" {
    73  		return nil
    74  	}
    75  
    76  	return json.Unmarshal([]byte(cursorString), mc)
    77  }