code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/funding_payments.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  
    22  	"code.vegaprotocol.io/vega/datanode/entities"
    23  	"code.vegaprotocol.io/vega/datanode/metrics"
    24  	v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
    25  
    26  	"github.com/georgysavva/scany/pgxscan"
    27  )
    28  
    29  type FundingPayments struct {
    30  	*ConnectionSource
    31  }
    32  
    33  var fundingPaymentOrdering = TableOrdering{
    34  	ColumnOrdering{Name: "vega_time", Sorting: ASC},
    35  	ColumnOrdering{Name: "market_id", Sorting: ASC},
    36  	ColumnOrdering{Name: "funding_period_seq", Sorting: ASC},
    37  	ColumnOrdering{Name: "party_id", Sorting: ASC},
    38  }
    39  
    40  func NewFundingPayments(connectionSource *ConnectionSource) *FundingPayments {
    41  	return &FundingPayments{
    42  		ConnectionSource: connectionSource,
    43  	}
    44  }
    45  
    46  func (fp *FundingPayments) Add(
    47  	ctx context.Context,
    48  	fundingPayments []*entities.FundingPayment,
    49  ) error {
    50  	defer metrics.StartSQLQuery("FundingPayments", "Add")()
    51  
    52  	for _, v := range fundingPayments {
    53  		_, err := fp.Exec(ctx,
    54  			`insert into funding_payment(market_id, party_id, funding_period_seq, amount, vega_time, tx_hash, loss_socialisation_amount)
    55  values ($1, $2, $3, $4, $5, $6, $7)
    56  	ON CONFLICT (party_id, market_id, vega_time) DO UPDATE SET
    57  		funding_period_seq=EXCLUDED.funding_period_seq,
    58  		amount=EXCLUDED.amount,
    59  		tx_hash=EXCLUDED.tx_hash,
    60  		loss_socialisation_amount=EXCLUDED.loss_socialisation_amount`,
    61  			v.MarketID, v.PartyID, v.FundingPeriodSeq, v.Amount, v.VegaTime, v.TxHash, v.LossSocialisationAmount)
    62  		if err != nil {
    63  			return err
    64  		}
    65  	}
    66  
    67  	return nil
    68  }
    69  
    70  func (fp *FundingPayments) List(
    71  	ctx context.Context,
    72  	partyID entities.PartyID,
    73  	marketID *entities.MarketID,
    74  	pagination entities.CursorPagination,
    75  ) ([]entities.FundingPayment, entities.PageInfo, error) {
    76  	defer metrics.StartSQLQuery("FundingPayments", "List")()
    77  	var fundingPayments []entities.FundingPayment
    78  	var pageInfo entities.PageInfo
    79  	var args []interface{}
    80  	var err error
    81  
    82  	query := fmt.Sprintf("select * from funding_payment where party_id = %s", nextBindVar(&args, partyID))
    83  
    84  	if marketID != nil {
    85  		query = fmt.Sprintf("%s and market_id = %s", query, nextBindVar(&args, *marketID))
    86  	}
    87  
    88  	query, args, err = PaginateQuery[entities.FundingPaymentCursor](query, args, fundingPaymentOrdering, pagination)
    89  	if err != nil {
    90  		return fundingPayments, pageInfo, err
    91  	}
    92  
    93  	err = pgxscan.Select(ctx, fp.ConnectionSource, &fundingPayments, query, args...)
    94  	if err != nil {
    95  		return fundingPayments, pageInfo, err
    96  	}
    97  
    98  	fundingPayments, pageInfo = entities.PageEntities[*v2.FundingPaymentEdge](fundingPayments, pagination)
    99  
   100  	return fundingPayments, pageInfo, nil
   101  }
   102  
   103  func (fp *FundingPayments) GetByPartyAndMarket(ctx context.Context, party, market string) (entities.FundingPayment, error) {
   104  	partyID, marketID := entities.PartyID(party), entities.MarketID(market)
   105  	defer metrics.StartSQLQuery("FundingPayments", "GetByPartyAndMarket")()
   106  	var (
   107  		err error
   108  		ret entities.FundingPayment
   109  	)
   110  	query := "SELECT * FROM funding_payment WHERE party_id = $1 AND market_id = $2 ORDER BY vega_time DESC LIMIT 1"
   111  	if err = pgxscan.Select(ctx, fp.ConnectionSource, &ret, query, partyID, marketID); err != nil {
   112  		return ret, err
   113  	}
   114  	return ret, nil
   115  }