code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/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 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 VolumeRebatePrograms struct { 30 *ConnectionSource 31 } 32 33 func NewVolumeRebatePrograms(connectionSource *ConnectionSource) *VolumeRebatePrograms { 34 return &VolumeRebatePrograms{ 35 ConnectionSource: connectionSource, 36 } 37 } 38 39 func (rp *VolumeRebatePrograms) AddVolumeRebateProgram(ctx context.Context, program *entities.VolumeRebateProgram) error { 40 defer metrics.StartSQLQuery("VolumeRebatePrograms", "AddVolumeRebateProgram")() 41 return rp.insertVolumeRebateProgram(ctx, program) 42 } 43 44 func (rp *VolumeRebatePrograms) insertVolumeRebateProgram(ctx context.Context, program *entities.VolumeRebateProgram) error { 45 if len(program.BenefitTiers) > 0 && program.BenefitTiers[0].TierNumber == nil { 46 for i := range program.BenefitTiers { 47 program.BenefitTiers[i].TierNumber = ptr.From(uint64(i + 1)) 48 } 49 } 50 _, err := rp.Exec(ctx, 51 `INSERT INTO volume_rebate_programs (id, version, benefit_tiers, end_of_program_timestamp, window_length, vega_time, seq_num) 52 VALUES ($1, $2, $3, $4, $5, $6, $7)`, 53 program.ID, 54 program.Version, 55 program.BenefitTiers, 56 program.EndOfProgramTimestamp, 57 program.WindowLength, 58 program.VegaTime, 59 program.SeqNum, 60 ) 61 return err 62 } 63 64 func (rp *VolumeRebatePrograms) UpdateVolumeRebateProgram(ctx context.Context, program *entities.VolumeRebateProgram) error { 65 defer metrics.StartSQLQuery("VolumeRebatePrograms", "UpdateVolumeRebateProgram")() 66 return rp.insertVolumeRebateProgram(ctx, program) 67 } 68 69 func (rp *VolumeRebatePrograms) EndVolumeRebateProgram(ctx context.Context, version uint64, endedAt time.Time, vegaTime time.Time, seqNum uint64) error { 70 defer metrics.StartSQLQuery("VolumeRebatePrograms", "EndVolumeRebateProgram")() 71 _, err := rp.Exec(ctx, 72 `INSERT INTO volume_rebate_programs (id, version, benefit_tiers, end_of_program_timestamp, window_length, ended_at, vega_time, seq_num) 73 SELECT id, $1, benefit_tiers, end_of_program_timestamp, window_length, $2, $3, $4 74 FROM current_volume_rebate_program`, version, endedAt, vegaTime, seqNum, 75 ) 76 77 return err 78 } 79 80 func (rp *VolumeRebatePrograms) GetCurrentVolumeRebateProgram(ctx context.Context) (entities.VolumeRebateProgram, error) { 81 var program entities.VolumeRebateProgram 82 defer func() { 83 if len(program.BenefitTiers) > 0 && program.BenefitTiers[0].TierNumber == nil { 84 for i := range program.BenefitTiers { 85 program.BenefitTiers[i].TierNumber = ptr.From(uint64(i + 1)) 86 } 87 } 88 metrics.StartSQLQuery("VolumeRebatePrograms", "GetCurrentVolumeRebateProgram")() 89 }() 90 91 query := `SELECT id, version, benefit_tiers, end_of_program_timestamp, window_length, vega_time, ended_at, seq_num FROM current_volume_rebate_program` 92 if err := pgxscan.Get(ctx, rp.ConnectionSource, &program, query); err != nil { 93 return program, err 94 } 95 96 return program, nil 97 }