code.vegaprotocol.io/vega@v0.79.0/core/execution/amm/pool_cache_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 amm
    17  
    18  import (
    19  	"testing"
    20  
    21  	"code.vegaprotocol.io/vega/core/types"
    22  	"code.vegaprotocol.io/vega/libs/num"
    23  
    24  	"github.com/stretchr/testify/assert"
    25  )
    26  
    27  func TestAMMCache(t *testing.T) {
    28  	t.Run("test fair-price cache", testFairPriceCache)
    29  	t.Run("test best-price cache", testBestPriceCache)
    30  }
    31  
    32  func testFairPriceCache(t *testing.T) {
    33  	c := NewPoolCache()
    34  
    35  	p, ok := c.getFairPrice(0)
    36  	assert.False(t, ok)
    37  	assert.Nil(t, p)
    38  
    39  	// add something
    40  	c.setFairPrice(100, num.NewUint(123))
    41  	p, ok = c.getFairPrice(100)
    42  	assert.True(t, ok)
    43  	assert.Equal(t, "123", p.String())
    44  
    45  	// replace it
    46  	c.setFairPrice(1001, num.NewUint(321))
    47  	p, ok = c.getFairPrice(100)
    48  	assert.False(t, ok)
    49  	assert.Nil(t, p)
    50  
    51  	// get new value
    52  	p, ok = c.getFairPrice(1001)
    53  	assert.True(t, ok)
    54  	assert.Equal(t, "321", p.String())
    55  }
    56  
    57  func testBestPriceCache(t *testing.T) {
    58  	c := NewPoolCache()
    59  
    60  	p, v, ok := c.getBestPrice(0, types.SideBuy, types.AMMPoolStatusActive)
    61  	assert.False(t, ok)
    62  	assert.Nil(t, p)
    63  	assert.Zero(t, v)
    64  
    65  	// add something to buy cache
    66  	c.setBestPrice(100, types.SideBuy, types.AMMPoolStatusActive, num.NewUint(123), 321)
    67  
    68  	// now get it back
    69  	p, v, ok = c.getBestPrice(100, types.SideBuy, types.AMMPoolStatusActive)
    70  	assert.True(t, ok)
    71  	assert.Equal(t, "123", p.String())
    72  	assert.Equal(t, 321, int(v))
    73  
    74  	// now try to get the other side
    75  	p, v, ok = c.getBestPrice(100, types.SideSell, types.AMMPoolStatusActive)
    76  	assert.False(t, ok)
    77  	assert.Nil(t, p)
    78  	assert.Zero(t, v)
    79  
    80  	// now try to get it with a different status
    81  	p, v, ok = c.getBestPrice(100, types.SideBuy, types.AMMPoolStatusReduceOnly)
    82  	assert.False(t, ok)
    83  	assert.Nil(t, p)
    84  	assert.Zero(t, v)
    85  
    86  	// now add one for the other side
    87  	c.setBestPrice(100, types.SideSell, types.AMMPoolStatusActive, num.NewUint(12300), 32100)
    88  
    89  	// check we can still get the buy one
    90  	p, v, ok = c.getBestPrice(100, types.SideBuy, types.AMMPoolStatusActive)
    91  	assert.True(t, ok)
    92  	assert.Equal(t, "123", p.String())
    93  	assert.Equal(t, 321, int(v))
    94  
    95  	// and also the sell one
    96  	p, v, ok = c.getBestPrice(100, types.SideSell, types.AMMPoolStatusActive)
    97  	assert.True(t, ok)
    98  	assert.Equal(t, "12300", p.String())
    99  	assert.Equal(t, 32100, int(v))
   100  }