code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/referral_programs.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 sqlstore 17 18 import ( 19 "context" 20 "time" 21 22 "code.vegaprotocol.io/vega/datanode/entities" 23 "code.vegaprotocol.io/vega/datanode/metrics" 24 "code.vegaprotocol.io/vega/libs/ptr" 25 26 "github.com/georgysavva/scany/pgxscan" 27 ) 28 29 type ReferralPrograms struct { 30 *ConnectionSource 31 } 32 33 func NewReferralPrograms(connectionSource *ConnectionSource) *ReferralPrograms { 34 return &ReferralPrograms{ 35 ConnectionSource: connectionSource, 36 } 37 } 38 39 func (rp *ReferralPrograms) AddReferralProgram(ctx context.Context, referral *entities.ReferralProgram) error { 40 defer metrics.StartSQLQuery("ReferralPrograms", "AddReferralProgram")() 41 return rp.insertReferralProgram(ctx, referral) 42 } 43 44 func (rp *ReferralPrograms) insertReferralProgram(ctx context.Context, referral *entities.ReferralProgram) error { 45 if len(referral.BenefitTiers) > 0 && referral.BenefitTiers[0].TierNumber == nil { 46 // update stores to set tier numbers. 47 for i := range referral.BenefitTiers { 48 referral.BenefitTiers[i].TierNumber = ptr.From(uint64(i + 1)) 49 } 50 } 51 _, err := rp.Exec(ctx, 52 `INSERT INTO referral_programs (id, version, benefit_tiers, end_of_program_timestamp, window_length, staking_tiers, vega_time, seq_num) 53 VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`, 54 referral.ID, 55 referral.Version, 56 referral.BenefitTiers, 57 referral.EndOfProgramTimestamp, 58 referral.WindowLength, 59 referral.StakingTiers, 60 referral.VegaTime, 61 referral.SeqNum, 62 ) 63 return err 64 } 65 66 func (rp *ReferralPrograms) UpdateReferralProgram(ctx context.Context, referral *entities.ReferralProgram) error { 67 defer metrics.StartSQLQuery("ReferralPrograms", "UpdateReferralProgram")() 68 return rp.insertReferralProgram(ctx, referral) 69 } 70 71 func (rp *ReferralPrograms) EndReferralProgram(ctx context.Context, version uint64, endedAt time.Time, vegaTime time.Time, seqNum uint64) error { 72 defer metrics.StartSQLQuery("ReferralPrograms", "EndReferralProgram")() 73 _, err := rp.Exec(ctx, 74 `INSERT INTO referral_programs (id, version, benefit_tiers, end_of_program_timestamp, window_length, staking_tiers, ended_at, vega_time, seq_num) 75 SELECT id, $1, benefit_tiers, end_of_program_timestamp, window_length, staking_tiers, $2, $3, $4 76 FROM current_referral_program`, version, endedAt, vegaTime, seqNum, 77 ) 78 79 return err 80 } 81 82 func (rp *ReferralPrograms) GetCurrentReferralProgram(ctx context.Context) (entities.ReferralProgram, error) { 83 var referralProgram entities.ReferralProgram 84 defer func() { 85 // ensure the tier numbers are set 86 if len(referralProgram.BenefitTiers) > 0 && referralProgram.BenefitTiers[0].TierNumber == nil { 87 for i := range referralProgram.BenefitTiers { 88 referralProgram.BenefitTiers[i].TierNumber = ptr.From(uint64(i + 1)) 89 } 90 } 91 metrics.StartSQLQuery("ReferralPrograms", "GetCurrentReferralProgram")() 92 }() 93 94 query := `SELECT id, version, benefit_tiers, end_of_program_timestamp, window_length, staking_tiers, vega_time, ended_at, seq_num FROM current_referral_program` 95 if err := pgxscan.Get(ctx, rp.ConnectionSource, &referralProgram, query); err != nil { 96 return referralProgram, err 97 } 98 99 return referralProgram, nil 100 }