code.vegaprotocol.io/vega@v0.79.0/core/execution/common/liquidity_provision_snapshot_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 common_test
    17  
    18  import (
    19  	"context"
    20  	"testing"
    21  	"time"
    22  
    23  	"code.vegaprotocol.io/vega/core/execution/common"
    24  	"code.vegaprotocol.io/vega/core/idgeneration"
    25  	"code.vegaprotocol.io/vega/core/types"
    26  	vgcrypto "code.vegaprotocol.io/vega/libs/crypto"
    27  	"code.vegaprotocol.io/vega/libs/num"
    28  
    29  	"github.com/golang/mock/gomock"
    30  	"github.com/stretchr/testify/assert"
    31  )
    32  
    33  func TestAMMStateSnapshot(t *testing.T) {
    34  	testLiquidity := newMarketLiquidity(t)
    35  	// set fee factor to 1, so fees are not paid out based on score.
    36  	testLiquidity.marketLiquidity.SetELSFeeFraction(num.DecimalOne())
    37  	testLiquidity.liquidityEngine.EXPECT().ReadyForFeesAllocation(gomock.Any()).Return(false).AnyTimes()
    38  	testLiquidity.liquidityEngine.EXPECT().UpdateAverageLiquidityScores(gomock.Any(), gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
    39  	testLiquidity.liquidityEngine.EXPECT().OnStakeToCcyVolumeUpdate(gomock.Any()).AnyTimes()
    40  	testLiquidity.marketLiquidity.OnStakeToCcyVolumeUpdate(num.DecimalOne())
    41  
    42  	testLiquidity.orderBook.EXPECT().GetBestStaticAskPrice().Return(num.NewUint(200), nil).AnyTimes()
    43  	testLiquidity.orderBook.EXPECT().GetBestStaticBidPrice().Return(num.NewUint(100), nil).AnyTimes()
    44  	testLiquidity.liquidityEngine.EXPECT().GetPartyLiquidityScore(
    45  		gomock.Any(),
    46  		gomock.Any(),
    47  		gomock.Any(),
    48  		gomock.Any(),
    49  		gomock.Any(),
    50  	).Return(num.DecimalOne()).AnyTimes()
    51  
    52  	party1 := vgcrypto.RandomHash()
    53  	party2 := vgcrypto.RandomHash()
    54  	amms := map[string]common.AMMPool{
    55  		party1: dummyAMM{},
    56  	}
    57  
    58  	// get some AMM's to create some state
    59  	testLiquidity.amm.EXPECT().GetAMMPoolsBySubAccount().Return(amms)
    60  
    61  	testLiquidity.marketLiquidity.OnTick(context.Background(), time.Now())
    62  
    63  	state := testLiquidity.marketLiquidity.GetState()
    64  	assert.Equal(t, int64(1), state.Tick)
    65  	assert.Equal(t, int64(1), state.Amm[0].Tick)
    66  
    67  	// next tick we have a new AMM enter the ring
    68  	amms[party2] = dummyAMM{}
    69  	testLiquidity.amm.EXPECT().GetAMMPoolsBySubAccount().Return(amms)
    70  
    71  	testLiquidity.marketLiquidity.OnTick(context.Background(), time.Now())
    72  
    73  	state = testLiquidity.marketLiquidity.GetState()
    74  	assert.Equal(t, int64(2), state.Tick)
    75  	if state.Amm[0].Party == party1 {
    76  		assert.Equal(t, int64(1), state.Amm[1].Tick)
    77  		assert.Equal(t, int64(2), state.Amm[0].Tick)
    78  	} else {
    79  		assert.Equal(t, int64(1), state.Amm[0].Tick)
    80  		assert.Equal(t, int64(2), state.Amm[1].Tick)
    81  	}
    82  }
    83  
    84  type dummyAMM struct{}
    85  
    86  func (d dummyAMM) OrderbookShape(from, to *num.Uint, idgen *idgeneration.IDGenerator) *types.OrderbookShapeResult {
    87  	return &types.OrderbookShapeResult{}
    88  }
    89  
    90  func (d dummyAMM) LiquidityFee() num.Decimal {
    91  	return num.DecimalZero()
    92  }
    93  
    94  func (d dummyAMM) CommitmentAmount() *num.Uint {
    95  	return num.UintOne()
    96  }