code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/party_locked_balance.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 25 "github.com/georgysavva/scany/pgxscan" 26 ) 27 28 type PartyLockedBalance struct { 29 *ConnectionSource 30 } 31 32 func NewPartyLockedBalances(connectionSource *ConnectionSource) *PartyLockedBalance { 33 return &PartyLockedBalance{ 34 ConnectionSource: connectionSource, 35 } 36 } 37 38 func (plb *PartyLockedBalance) Prune( 39 ctx context.Context, 40 currentEpoch uint64, 41 ) error { 42 defer metrics.StartSQLQuery("PartyLockedBalance", "Prune")() 43 _, err := plb.Exec( 44 ctx, 45 "DELETE FROM party_locked_balances_current WHERE until_epoch <= $1", 46 currentEpoch, 47 ) 48 49 return err 50 } 51 52 func (plb *PartyLockedBalance) Add(ctx context.Context, balance entities.PartyLockedBalance) error { 53 defer metrics.StartSQLQuery("PartyLockedBalance", "Add")() 54 _, err := plb.Exec(ctx, 55 `INSERT INTO party_locked_balances(party_id, asset_id, at_epoch, until_epoch, balance, vega_time) 56 VALUES ($1, $2, $3, $4, $5, $6) 57 ON CONFLICT (vega_time, party_id, asset_id, until_epoch) DO NOTHING`, 58 balance.PartyID, 59 balance.AssetID, 60 balance.AtEpoch, 61 balance.UntilEpoch, 62 balance.Balance, 63 balance.VegaTime, 64 ) 65 return err 66 } 67 68 func (plb *PartyLockedBalance) Get(ctx context.Context, partyID *entities.PartyID, assetID *entities.AssetID) ( 69 []entities.PartyLockedBalance, error, 70 ) { 71 defer metrics.StartSQLQuery("PartyLockedBalance", "Get")() 72 var args []interface{} 73 74 query := `SELECT * FROM party_locked_balances_current` 75 where := []string{} 76 77 if partyID != nil { 78 where = append(where, fmt.Sprintf("party_id = %s", nextBindVar(&args, *partyID))) 79 } 80 81 if assetID != nil { 82 where = append(where, fmt.Sprintf("asset_id = %s", nextBindVar(&args, *assetID))) 83 } 84 85 whereClause := "" 86 87 if len(where) > 0 { 88 whereClause = "WHERE" 89 for i, w := range where { 90 if i > 0 { 91 whereClause = fmt.Sprintf("%s AND", whereClause) 92 } 93 whereClause = fmt.Sprintf("%s %s", whereClause, w) 94 } 95 } 96 97 query = fmt.Sprintf("%s %s", query, whereClause) 98 99 var balances []entities.PartyLockedBalance 100 if err := pgxscan.Select(ctx, plb.ConnectionSource, &balances, query, args...); err != nil { 101 return balances, err 102 } 103 104 return balances, nil 105 }