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