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