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