code.vegaprotocol.io/vega@v0.79.0/datanode/entities/volume_rebate_program.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  	"time"
    20  
    21  	"code.vegaprotocol.io/vega/libs/ptr"
    22  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    23  	"code.vegaprotocol.io/vega/protos/vega"
    24  )
    25  
    26  type (
    27  	_VolumeRebateProgram  struct{}
    28  	VolumeRebateProgramID = ID[_VolumeRebateProgram]
    29  
    30  	VolumeRebateProgram struct {
    31  		ID                    VolumeRebateProgramID
    32  		Version               uint64
    33  		BenefitTiers          []*vega.VolumeRebateBenefitTier
    34  		EndOfProgramTimestamp time.Time
    35  		WindowLength          uint64
    36  		VegaTime              time.Time
    37  		EndedAt               *time.Time
    38  		SeqNum                uint64
    39  	}
    40  )
    41  
    42  func VolumeRebateProgramFromProto(proto *vega.VolumeRebateProgram, vegaTime time.Time, seqNum uint64) *VolumeRebateProgram {
    43  	// set the tier numbers accordingly
    44  	for i := range proto.BenefitTiers {
    45  		proto.BenefitTiers[i].TierNumber = ptr.From(uint64(i + 1))
    46  	}
    47  	return &VolumeRebateProgram{
    48  		ID:                    VolumeRebateProgramID(proto.Id),
    49  		Version:               proto.Version,
    50  		BenefitTiers:          proto.BenefitTiers,
    51  		EndOfProgramTimestamp: time.Unix(proto.EndOfProgramTimestamp, 0),
    52  		WindowLength:          proto.WindowLength,
    53  		VegaTime:              vegaTime,
    54  		SeqNum:                seqNum,
    55  	}
    56  }
    57  
    58  func (rp VolumeRebateProgram) ToProto() *v2.VolumeRebateProgram {
    59  	var endedAt *int64
    60  	if rp.EndedAt != nil {
    61  		endedAt = ptr.From(rp.EndedAt.UnixNano())
    62  	}
    63  
    64  	// While the original program proto from core sends EndOfProgramTimestamp as
    65  	// a timestamp in unix seconds, for the data node API, we publish it as a
    66  	// unix timestamp in nanoseconds as the GraphQL API timestamp will incorrectly
    67  	// treat the timestamp as nanos.
    68  	return &v2.VolumeRebateProgram{
    69  		Id:                    rp.ID.String(),
    70  		Version:               rp.Version,
    71  		BenefitTiers:          rp.BenefitTiers,
    72  		EndOfProgramTimestamp: rp.EndOfProgramTimestamp.UnixNano(),
    73  		WindowLength:          rp.WindowLength,
    74  		EndedAt:               endedAt,
    75  	}
    76  }