code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/time_weighted_notional_position.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  
    21  	"code.vegaprotocol.io/vega/datanode/entities"
    22  
    23  	"github.com/georgysavva/scany/pgxscan"
    24  )
    25  
    26  type (
    27  	TimeWeightedNotionalPosition struct {
    28  		*ConnectionSource
    29  	}
    30  )
    31  
    32  func NewTimeWeightedNotionalPosition(connectionSource *ConnectionSource) *TimeWeightedNotionalPosition {
    33  	return &TimeWeightedNotionalPosition{
    34  		ConnectionSource: connectionSource,
    35  	}
    36  }
    37  
    38  func (tw *TimeWeightedNotionalPosition) Upsert(ctx context.Context, twNotionalPos entities.TimeWeightedNotionalPosition) error {
    39  	_, err := tw.Exec(ctx, `
    40  		INSERT INTO time_weighted_notional_positions (asset_id, party_id, game_id, epoch_seq, time_weighted_notional_position, vega_time)
    41  		VALUES ($1, $2, $3, $4, $5, $6)
    42  		ON CONFLICT (asset_id, party_id, game_id, epoch_seq, vega_time)
    43  		DO UPDATE
    44  			SET time_weighted_notional_position = $5
    45  	`,
    46  		twNotionalPos.AssetID, twNotionalPos.PartyID, twNotionalPos.GameID, twNotionalPos.EpochSeq,
    47  		twNotionalPos.TimeWeightedNotionalPosition, twNotionalPos.VegaTime)
    48  	return err
    49  }
    50  
    51  func (tw *TimeWeightedNotionalPosition) Get(ctx context.Context, assetID entities.AssetID, partyID entities.PartyID, gameID entities.GameID,
    52  	epochSeq *uint64,
    53  ) (entities.TimeWeightedNotionalPosition, error) {
    54  	var twNotionalPos entities.TimeWeightedNotionalPosition
    55  	if epochSeq == nil {
    56  		err := pgxscan.Get(ctx, tw.ConnectionSource, &twNotionalPos,
    57  			`SELECT * FROM time_weighted_notional_positions WHERE asset_id = $1 AND party_id = $2 AND game_id = $3
    58  		ORDER BY epoch_seq DESC, vega_time DESC LIMIT 1`,
    59  			assetID, partyID, gameID)
    60  		if err != nil {
    61  			return twNotionalPos, err
    62  		}
    63  		return twNotionalPos, nil
    64  	}
    65  	err := pgxscan.Get(ctx, tw.ConnectionSource, &twNotionalPos,
    66  		`SELECT * FROM time_weighted_notional_positions WHERE asset_id = $1 AND party_id = $2 AND game_id = $3
    67  	AND epoch_seq = $4 ORDER BY vega_time DESC LIMIT 1`,
    68  		assetID, partyID, gameID, *epochSeq)
    69  	if err != nil {
    70  		return twNotionalPos, err
    71  	}
    72  	return twNotionalPos, err
    73  }