code.vegaprotocol.io/vega@v0.79.0/datanode/sqlstore/time_weighted_notional_position_test.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_test
    17  
    18  import (
    19  	"testing"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/datanode/entities"
    23  	"code.vegaprotocol.io/vega/datanode/sqlstore"
    24  	"code.vegaprotocol.io/vega/libs/num"
    25  	"code.vegaprotocol.io/vega/libs/ptr"
    26  
    27  	"github.com/georgysavva/scany/pgxscan"
    28  	"github.com/stretchr/testify/assert"
    29  	"github.com/stretchr/testify/require"
    30  )
    31  
    32  func TestTimeWeightedNotionalPosition_Upsert(t *testing.T) {
    33  	ctx := tempTransaction(t)
    34  	t.Run("Upsert should create a time weighted notional position record if it dpesn't exist", func(t *testing.T) {
    35  		tw := sqlstore.NewTimeWeightedNotionalPosition(connectionSource)
    36  		want := entities.TimeWeightedNotionalPosition{
    37  			AssetID:                      entities.AssetID(GenerateID()),
    38  			PartyID:                      entities.PartyID(GenerateID()),
    39  			GameID:                       entities.GameID(GenerateID()),
    40  			EpochSeq:                     1,
    41  			TimeWeightedNotionalPosition: num.DecimalFromInt64(1000),
    42  			VegaTime:                     time.Now().Truncate(time.Microsecond),
    43  		}
    44  		err := tw.Upsert(ctx, want)
    45  		require.NoError(t, err)
    46  		var got entities.TimeWeightedNotionalPosition
    47  		err = pgxscan.Get(ctx, connectionSource, &got,
    48  			`SELECT * FROM time_weighted_notional_positions WHERE asset_id = $1 AND party_id = $2 and game_id = $3 and epoch_seq = $4`,
    49  			want.AssetID, want.PartyID, want.GameID, want.EpochSeq)
    50  		require.NoError(t, err)
    51  		assert.Equal(t, want, got)
    52  	})
    53  
    54  	t.Run("Upsert should update a time weighted notional position record if it exists", func(t *testing.T) {
    55  		tw := sqlstore.NewTimeWeightedNotionalPosition(connectionSource)
    56  		want := entities.TimeWeightedNotionalPosition{
    57  			AssetID:                      entities.AssetID(GenerateID()),
    58  			PartyID:                      entities.PartyID(GenerateID()),
    59  			GameID:                       entities.GameID(GenerateID()),
    60  			EpochSeq:                     2,
    61  			TimeWeightedNotionalPosition: num.DecimalFromInt64(1000),
    62  			VegaTime:                     time.Now().Truncate(time.Microsecond),
    63  		}
    64  		err := tw.Upsert(ctx, want)
    65  		require.NoError(t, err)
    66  		want.TimeWeightedNotionalPosition = num.DecimalFromInt64(2000)
    67  		err = tw.Upsert(ctx, want)
    68  		require.NoError(t, err)
    69  		var got entities.TimeWeightedNotionalPosition
    70  		err = pgxscan.Get(ctx, connectionSource, &got,
    71  			`SELECT * FROM time_weighted_notional_positions WHERE asset_id = $1 AND party_id = $2 and game_id = $3 and epoch_seq = $4`,
    72  			want.AssetID, want.PartyID, want.GameID, want.EpochSeq)
    73  		require.NoError(t, err)
    74  		assert.Equal(t, want, got)
    75  	})
    76  }
    77  
    78  func TestTimeWeightedNotionalPosition_Get(t *testing.T) {
    79  	ctx := tempTransaction(t)
    80  	t.Run("Get should return a time weighted notional position record if it exists", func(t *testing.T) {
    81  		tw := sqlstore.NewTimeWeightedNotionalPosition(connectionSource)
    82  		want := entities.TimeWeightedNotionalPosition{
    83  			AssetID:                      entities.AssetID(GenerateID()),
    84  			PartyID:                      entities.PartyID(GenerateID()),
    85  			GameID:                       entities.GameID(GenerateID()),
    86  			EpochSeq:                     1,
    87  			TimeWeightedNotionalPosition: num.DecimalFromInt64(1000),
    88  			VegaTime:                     time.Now().Truncate(time.Microsecond),
    89  		}
    90  		err := tw.Upsert(ctx, want)
    91  		require.NoError(t, err)
    92  		got, err := tw.Get(ctx, want.AssetID, want.PartyID, want.GameID, ptr.From(want.EpochSeq))
    93  		require.NoError(t, err)
    94  		assert.Equal(t, want, got)
    95  	})
    96  
    97  	t.Run("Get should return the latest time weighted notional position record if no epoch is specified", func(t *testing.T) {
    98  		tw := sqlstore.NewTimeWeightedNotionalPosition(connectionSource)
    99  		want := entities.TimeWeightedNotionalPosition{
   100  			AssetID:                      entities.AssetID(GenerateID()),
   101  			PartyID:                      entities.PartyID(GenerateID()),
   102  			GameID:                       entities.GameID(GenerateID()),
   103  			EpochSeq:                     1,
   104  			TimeWeightedNotionalPosition: num.DecimalFromInt64(1000),
   105  			VegaTime:                     time.Now().Truncate(time.Microsecond),
   106  		}
   107  		err := tw.Upsert(ctx, want)
   108  		require.NoError(t, err)
   109  		want.EpochSeq = 2
   110  		want.TimeWeightedNotionalPosition = num.DecimalFromInt64(2000)
   111  		want.VegaTime = want.VegaTime.Add(time.Second)
   112  		err = tw.Upsert(ctx, want)
   113  		require.NoError(t, err)
   114  		got, err := tw.Get(ctx, want.AssetID, want.PartyID, want.GameID, nil)
   115  		require.NoError(t, err)
   116  		assert.Equal(t, want, got)
   117  	})
   118  }