code.vegaprotocol.io/vega@v0.79.0/core/execution/amm/pool_cache.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  	"code.vegaprotocol.io/vega/core/types"
    20  	"code.vegaprotocol.io/vega/libs/num"
    21  )
    22  
    23  type key struct {
    24  	pos    int64
    25  	status types.AMMPoolStatus
    26  }
    27  
    28  type priceVolume struct {
    29  	price  *num.Uint
    30  	volume uint64
    31  }
    32  
    33  // basically a cache of expensive calculation results given a pool's position/state
    34  // we only keep one result each time and the map is just used as a quick lookup.
    35  type poolCache struct {
    36  	sell      map[key]*priceVolume // map pos+status -> best ask price and volume
    37  	buy       map[key]*priceVolume // map pos+status -> best buy price and volume
    38  	fairPrice map[int64]*num.Uint  // map pos -> fair-price
    39  }
    40  
    41  func NewPoolCache() *poolCache {
    42  	return &poolCache{
    43  		sell:      map[key]*priceVolume{},
    44  		buy:       map[key]*priceVolume{},
    45  		fairPrice: map[int64]*num.Uint{},
    46  	}
    47  }
    48  
    49  func (pc *poolCache) getFairPrice(pos int64) (*num.Uint, bool) {
    50  	if p, ok := pc.fairPrice[pos]; ok {
    51  		return p.Clone(), true
    52  	}
    53  	return nil, false
    54  }
    55  
    56  func (pc *poolCache) setFairPrice(pos int64, fp *num.Uint) {
    57  	pc.fairPrice = map[int64]*num.Uint{
    58  		pos: fp.Clone(),
    59  	}
    60  }
    61  
    62  func (pc *poolCache) getBestPrice(pos int64, side types.Side, status types.AMMPoolStatus) (*num.Uint, uint64, bool) {
    63  	cache := pc.sell
    64  	if side == types.SideBuy {
    65  		cache = pc.buy
    66  	}
    67  
    68  	if pv, ok := cache[key{
    69  		pos:    pos,
    70  		status: status,
    71  	}]; ok {
    72  		return pv.price, pv.volume, true
    73  	}
    74  	return nil, 0, false
    75  }
    76  
    77  func (pc *poolCache) setBestPrice(pos int64, side types.Side, status types.AMMPoolStatus, price *num.Uint, volume uint64) {
    78  	k := key{
    79  		pos:    pos,
    80  		status: status,
    81  	}
    82  	cache := map[key]*priceVolume{
    83  		k: {
    84  			price:  price.Clone(),
    85  			volume: volume,
    86  		},
    87  	}
    88  
    89  	if side == types.SideBuy {
    90  		pc.buy = cache
    91  		return
    92  	}
    93  	pc.sell = cache
    94  }