code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/margin_levels.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 var mlOrdering = TableOrdering{ 30 ColumnOrdering{Name: "vega_time", Sorting: ASC}, 31 ColumnOrdering{Name: "account_id", Sorting: ASC}, 32 } 33 34 type AccountSource interface { 35 Query(filter entities.AccountFilter) ([]entities.Account, error) 36 } 37 38 type MarginLevels struct { 39 *ConnectionSource 40 batcher MapBatcher[entities.MarginLevelsKey, entities.MarginLevels] 41 } 42 43 const ( 44 sqlMarginLevelColumns = `account_id,order_margin_account_id,timestamp,maintenance_margin,search_level,initial_margin,collateral_release_level,order_margin,tx_hash,vega_time,margin_mode,margin_factor` 45 ) 46 47 func NewMarginLevels(connectionSource *ConnectionSource) *MarginLevels { 48 return &MarginLevels{ 49 ConnectionSource: connectionSource, 50 batcher: NewMapBatcher[entities.MarginLevelsKey, entities.MarginLevels]( 51 "margin_levels", 52 entities.MarginLevelsColumns), 53 } 54 } 55 56 func (ml *MarginLevels) Add(marginLevel entities.MarginLevels) error { 57 ml.batcher.Add(marginLevel) 58 return nil 59 } 60 61 func (ml *MarginLevels) Flush(ctx context.Context) ([]entities.MarginLevels, error) { 62 defer metrics.StartSQLQuery("MarginLevels", "Flush")() 63 return ml.batcher.Flush(ctx, ml.ConnectionSource) 64 } 65 66 func buildAccountWhereClause(partyID, marketID string) (string, []interface{}) { 67 party := entities.PartyID(partyID) 68 market := entities.MarketID(marketID) 69 70 var bindVars []interface{} 71 72 whereParty := "" 73 if partyID != "" { 74 whereParty = fmt.Sprintf("party_id = %s", nextBindVar(&bindVars, party)) 75 } 76 77 whereMarket := "" 78 if marketID != "" { 79 whereMarket = fmt.Sprintf("market_id = %s", nextBindVar(&bindVars, market)) 80 } 81 82 accountsWhereClause := "" 83 84 if whereParty != "" && whereMarket != "" { 85 accountsWhereClause = fmt.Sprintf("where %s and %s", whereParty, whereMarket) 86 } else if whereParty != "" { 87 accountsWhereClause = fmt.Sprintf("where %s", whereParty) 88 } else if whereMarket != "" { 89 accountsWhereClause = fmt.Sprintf("where %s", whereMarket) 90 } 91 92 return fmt.Sprintf("where current_margin_levels.account_id in (select id from accounts %s)", accountsWhereClause), bindVars 93 } 94 95 func (ml *MarginLevels) GetMarginLevelsByIDWithCursorPagination(ctx context.Context, partyID, marketID string, pagination entities.CursorPagination) ([]entities.MarginLevels, entities.PageInfo, error) { 96 whereClause, bindVars := buildAccountWhereClause(partyID, marketID) 97 98 query := fmt.Sprintf(`select %s 99 from current_margin_levels 100 %s`, sqlMarginLevelColumns, 101 whereClause) 102 103 var err error 104 var pageInfo entities.PageInfo 105 query, bindVars, err = PaginateQuery[entities.MarginCursor](query, bindVars, mlOrdering, pagination) 106 if err != nil { 107 return nil, pageInfo, err 108 } 109 var marginLevels []entities.MarginLevels 110 111 if err = pgxscan.Select(ctx, ml.ConnectionSource, &marginLevels, query, bindVars...); err != nil { 112 return nil, entities.PageInfo{}, err 113 } 114 115 pagedMargins, pageInfo := entities.PageEntities[*v2.MarginEdge](marginLevels, pagination) 116 return pagedMargins, pageInfo, nil 117 } 118 119 func (ml *MarginLevels) GetByTxHash(ctx context.Context, txHash entities.TxHash) ([]entities.MarginLevels, error) { 120 var marginLevels []entities.MarginLevels 121 query := fmt.Sprintf(`SELECT %s FROM margin_levels WHERE tx_hash = $1`, sqlMarginLevelColumns) 122 123 if err := pgxscan.Select(ctx, ml.ConnectionSource, &marginLevels, query, txHash); err != nil { 124 return nil, err 125 } 126 127 return marginLevels, nil 128 }