code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/delegations.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  	"fmt"
    21  	"strings"
    22  
    23  	"code.vegaprotocol.io/vega/datanode/entities"
    24  	"code.vegaprotocol.io/vega/datanode/metrics"
    25  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    26  
    27  	"github.com/georgysavva/scany/pgxscan"
    28  )
    29  
    30  type Delegations struct {
    31  	*ConnectionSource
    32  }
    33  
    34  var delegationsOrdering = TableOrdering{
    35  	ColumnOrdering{Name: "vega_time", Sorting: ASC},
    36  	ColumnOrdering{Name: "party_id", Sorting: ASC},
    37  	ColumnOrdering{Name: "node_id", Sorting: ASC},
    38  	ColumnOrdering{Name: "epoch_id", Sorting: ASC},
    39  }
    40  
    41  func NewDelegations(connectionSource *ConnectionSource) *Delegations {
    42  	d := &Delegations{
    43  		ConnectionSource: connectionSource,
    44  	}
    45  	return d
    46  }
    47  
    48  func (ds *Delegations) Add(ctx context.Context, d entities.Delegation) error {
    49  	defer metrics.StartSQLQuery("Delegations", "Add")()
    50  	_, err := ds.Exec(ctx,
    51  		`INSERT INTO delegations(
    52  			party_id,
    53  			node_id,
    54  			epoch_id,
    55  			amount,
    56  			tx_hash,
    57  			vega_time,
    58  			seq_num)
    59  		 VALUES ($1,  $2,  $3,  $4,  $5, $6, $7);`,
    60  		d.PartyID, d.NodeID, d.EpochID, d.Amount, d.TxHash, d.VegaTime, d.SeqNum)
    61  	return err
    62  }
    63  
    64  func (ds *Delegations) GetAll(ctx context.Context) ([]entities.Delegation, error) {
    65  	defer metrics.StartSQLQuery("Delegations", "GetAll")()
    66  	delegations := []entities.Delegation{}
    67  	err := pgxscan.Select(ctx, ds.ConnectionSource, &delegations, `
    68  		SELECT * from delegations;`)
    69  	return delegations, err
    70  }
    71  
    72  func (ds *Delegations) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.Delegation, error) {
    73  	defer metrics.StartSQLQuery("Delegations", "GetByTxHash")()
    74  
    75  	var delegations []entities.Delegation
    76  	query := `SELECT * FROM delegations WHERE tx_hash = $1`
    77  
    78  	err := pgxscan.Select(ctx, ds.ConnectionSource, &delegations, query, txHash)
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	return delegations, nil
    84  }
    85  
    86  func (ds *Delegations) Get(ctx context.Context,
    87  	partyIDHex *string,
    88  	nodeIDHex *string,
    89  	epochID *int64,
    90  	pagination entities.Pagination,
    91  ) ([]entities.Delegation, entities.PageInfo, error) {
    92  	query := `SELECT * from delegations_current`
    93  	var args []interface{}
    94  	var pageInfo entities.PageInfo
    95  
    96  	conditions := []string{}
    97  
    98  	if partyIDHex != nil {
    99  		partyID := entities.PartyID(*partyIDHex)
   100  		conditions = append(conditions, fmt.Sprintf("party_id=%s", nextBindVar(&args, partyID)))
   101  	}
   102  
   103  	if nodeIDHex != nil {
   104  		nodeID := entities.NodeID(*nodeIDHex)
   105  		conditions = append(conditions, fmt.Sprintf("node_id=%s", nextBindVar(&args, nodeID)))
   106  	}
   107  
   108  	if epochID != nil {
   109  		conditions = append(conditions, fmt.Sprintf("epoch_id=%s", nextBindVar(&args, *epochID)))
   110  	}
   111  
   112  	if len(conditions) > 0 {
   113  		query = fmt.Sprintf("%s WHERE %s", query, strings.Join(conditions, " AND "))
   114  	}
   115  
   116  	defer metrics.StartSQLQuery("Delegations", "Get")()
   117  	delegations := []entities.Delegation{}
   118  	var err error
   119  	if pagination != nil {
   120  		switch p := pagination.(type) {
   121  		case entities.CursorPagination:
   122  			query, args, err = PaginateQuery[entities.DelegationCursor](query, args, delegationsOrdering, p)
   123  			if err != nil {
   124  				return nil, pageInfo, err
   125  			}
   126  
   127  			err := pgxscan.Select(ctx, ds.ConnectionSource, &delegations, query, args...)
   128  			if err != nil {
   129  				return nil, pageInfo, fmt.Errorf("querying delegations: %w", err)
   130  			}
   131  
   132  			delegations, pageInfo = entities.PageEntities[*v2.DelegationEdge](delegations, p)
   133  
   134  			return delegations, pageInfo, nil
   135  		default:
   136  			panic("unsupported pagination")
   137  		}
   138  	}
   139  
   140  	err = pgxscan.Select(ctx, ds.ConnectionSource, &delegations, query, args...)
   141  	if err != nil {
   142  		return nil, pageInfo, fmt.Errorf("querying delegations: %w", err)
   143  	}
   144  
   145  	return delegations, pageInfo, nil
   146  }