code.vegaprotocol.io/vega@v0.79.0/core/events/amm_pool.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 events
    17  
    18  import (
    19  	"context"
    20  
    21  	"code.vegaprotocol.io/vega/core/types"
    22  	"code.vegaprotocol.io/vega/libs/num"
    23  	eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
    24  )
    25  
    26  type AMMPool struct {
    27  	*Base
    28  	pool *eventspb.AMM
    29  }
    30  
    31  type AMMCurve struct {
    32  	VirtualLiquidity    num.Decimal
    33  	TheoreticalPosition num.Decimal
    34  }
    35  
    36  func (a *AMMCurve) ToProtoEvent() *eventspb.AMM_Curve {
    37  	if a == nil {
    38  		return nil
    39  	}
    40  
    41  	return &eventspb.AMM_Curve{
    42  		VirtualLiquidity:    a.VirtualLiquidity.String(),
    43  		TheoreticalPosition: a.TheoreticalPosition.String(),
    44  	}
    45  }
    46  
    47  func NewAMMPoolEvent(
    48  	ctx context.Context,
    49  	party, market, ammParty, poolID string,
    50  	commitment *num.Uint,
    51  	p *types.ConcentratedLiquidityParameters,
    52  	status types.AMMPoolStatus,
    53  	statusReason types.AMMStatusReason,
    54  	fees num.Decimal,
    55  	lowerCurve *AMMCurve,
    56  	upperCurve *AMMCurve,
    57  	minimumPriceChangeTrigger num.Decimal,
    58  ) *AMMPool {
    59  	return &AMMPool{
    60  		Base: newBase(ctx, AMMPoolEvent),
    61  		pool: &eventspb.AMM{
    62  			Id:                        poolID,
    63  			PartyId:                   party,
    64  			MarketId:                  market,
    65  			AmmPartyId:                ammParty,
    66  			Commitment:                commitment.String(),
    67  			Parameters:                p.ToProtoEvent(),
    68  			Status:                    status,
    69  			StatusReason:              statusReason,
    70  			ProposedFee:               fees.String(),
    71  			LowerCurve:                lowerCurve.ToProtoEvent(),
    72  			UpperCurve:                upperCurve.ToProtoEvent(),
    73  			MinimumPriceChangeTrigger: minimumPriceChangeTrigger.String(),
    74  		},
    75  	}
    76  }
    77  
    78  func (p AMMPool) IsParty(id string) bool {
    79  	return p.pool.PartyId == id
    80  }
    81  
    82  func (p AMMPool) PartyID() string {
    83  	return p.pool.PartyId
    84  }
    85  
    86  func (p AMMPool) MarketID() string {
    87  	return p.pool.MarketId
    88  }
    89  
    90  func (p *AMMPool) AMMPool() *eventspb.AMM {
    91  	return p.pool
    92  }
    93  
    94  func (p AMMPool) Proto() eventspb.AMM {
    95  	return *p.pool
    96  }
    97  
    98  func (p AMMPool) StreamMessage() *eventspb.BusEvent {
    99  	busEvent := newBusEventFromBase(p.Base)
   100  	busEvent.Event = &eventspb.BusEvent_Amm{
   101  		Amm: p.pool,
   102  	}
   103  
   104  	return busEvent
   105  }
   106  
   107  func AMMPoolEventFromStream(ctx context.Context, be *eventspb.BusEvent) *AMMPool {
   108  	pool := &AMMPool{
   109  		Base: newBaseFromBusEvent(ctx, AMMPoolEvent, be),
   110  		pool: be.GetAmm(),
   111  	}
   112  	return pool
   113  }