code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/margin_modes.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 var listPartyMarginModesOrdering = TableOrdering{ 31 ColumnOrdering{Name: "market_id", Sorting: ASC}, 32 ColumnOrdering{Name: "party_id", Sorting: ASC}, 33 } 34 35 type ListPartyMarginModesFilters struct { 36 MarketID *entities.MarketID 37 PartyID *entities.PartyID 38 } 39 40 type MarginModes struct { 41 *ConnectionSource 42 } 43 44 func (t *MarginModes) UpdatePartyMarginMode(ctx context.Context, update entities.PartyMarginMode) error { 45 defer metrics.StartSQLQuery("MarginModes", "UpdatePartyMarginMode")() 46 if _, err := t.Exec( 47 ctx, 48 `INSERT INTO party_margin_modes(market_id, party_id, margin_mode, margin_factor, min_theoretical_margin_factor, max_theoretical_leverage, at_epoch) 49 VALUES ($1, $2, $3, $4, $5, $6, $7) 50 ON CONFLICT (market_id, party_id) DO UPDATE SET 51 margin_mode = excluded.margin_mode, 52 margin_factor = excluded.margin_factor, 53 min_theoretical_margin_factor = excluded.min_theoretical_margin_factor, 54 max_theoretical_leverage = excluded.max_theoretical_leverage, 55 at_epoch = excluded.at_epoch`, 56 update.MarketID, 57 update.PartyID, 58 update.MarginMode, 59 update.MarginFactor, 60 update.MinTheoreticalMarginFactor, 61 update.MaxTheoreticalLeverage, 62 update.AtEpoch, 63 ); err != nil { 64 return err 65 } 66 67 return nil 68 } 69 70 func (t *MarginModes) ListPartyMarginModes(ctx context.Context, pagination entities.CursorPagination, filters ListPartyMarginModesFilters) ([]entities.PartyMarginMode, entities.PageInfo, error) { 71 defer metrics.StartSQLQuery("MarginModes", "ListPartyMarginModes")() 72 73 var ( 74 modes []entities.PartyMarginMode 75 args []interface{} 76 pageInfo entities.PageInfo 77 ) 78 79 query := `SELECT * FROM party_margin_modes` 80 81 whereClauses := []string{} 82 if filters.MarketID != nil { 83 whereClauses = append(whereClauses, fmt.Sprintf("market_id = %s", nextBindVar(&args, *filters.MarketID))) 84 } 85 if filters.PartyID != nil { 86 whereClauses = append(whereClauses, fmt.Sprintf("party_id = %s", nextBindVar(&args, *filters.PartyID))) 87 } 88 89 if len(whereClauses) > 0 { 90 query += " WHERE " + strings.Join(whereClauses, " AND ") 91 } 92 93 query, args, err := PaginateQuery[entities.PartyMarginModeCursor](query, args, listPartyMarginModesOrdering, pagination) 94 if err != nil { 95 return nil, pageInfo, err 96 } 97 98 if err := pgxscan.Select(ctx, t.ConnectionSource, &modes, query, args...); err != nil { 99 return nil, pageInfo, err 100 } 101 102 modes, pageInfo = entities.PageEntities[*v2.PartyMarginModeEdge](modes, pagination) 103 104 return modes, pageInfo, nil 105 } 106 107 func NewMarginModes(connectionSource *ConnectionSource) *MarginModes { 108 return &MarginModes{ 109 ConnectionSource: connectionSource, 110 } 111 }