code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/vesting_stats.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  
    21  	"code.vegaprotocol.io/vega/datanode/entities"
    22  	"code.vegaprotocol.io/vega/datanode/metrics"
    23  
    24  	"github.com/georgysavva/scany/pgxscan"
    25  )
    26  
    27  type (
    28  	VestingStats struct {
    29  		*ConnectionSource
    30  	}
    31  )
    32  
    33  func NewVestingStats(connectionSource *ConnectionSource) *VestingStats {
    34  	return &VestingStats{
    35  		ConnectionSource: connectionSource,
    36  	}
    37  }
    38  
    39  func (vs *VestingStats) Add(ctx context.Context, stats *entities.VestingStatsUpdated) error {
    40  	defer metrics.StartSQLQuery("PartyVestingStats", "Add")()
    41  
    42  	for _, v := range stats.PartyVestingStats {
    43  		_, err := vs.Exec(ctx,
    44  			`INSERT INTO party_vesting_stats(party_id, at_epoch, reward_bonus_multiplier, quantum_balance,
    45  				summed_reward_bonus_multiplier, summed_quantum_balance, vega_time)
    46           VALUES ($1, $2, $3, $4, $5, $6, $7)
    47           ON CONFLICT (vega_time, party_id) DO NOTHING`,
    48  			v.PartyID,
    49  			stats.AtEpoch,
    50  			v.RewardBonusMultiplier,
    51  			v.QuantumBalance,
    52  			v.SummedRewardBonusMultiplier,
    53  			v.SummedQuantumBalance,
    54  			stats.VegaTime,
    55  		)
    56  		if err != nil {
    57  			return err
    58  		}
    59  	}
    60  
    61  	return nil
    62  }
    63  
    64  func (vs *VestingStats) GetByPartyID(
    65  	ctx context.Context, id string,
    66  ) (entities.PartyVestingStats, error) {
    67  	defer metrics.StartSQLQuery("Parties", "GetByID")()
    68  
    69  	pvs := entities.PartyVestingStats{}
    70  	err := pgxscan.Get(ctx, vs.ConnectionSource, &pvs,
    71  		`SELECT party_id, at_epoch, reward_bonus_multiplier, quantum_balance,
    72  		summed_reward_bonus_multiplier, summed_quantum_balance, vega_time
    73  		 FROM party_vesting_stats_current WHERE party_id=$1`,
    74  		entities.PartyID(id))
    75  
    76  	return pvs, vs.wrapE(err)
    77  }