code.vegaprotocol.io/vega@v0.79.0/core/plugins/fees.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 plugins
    17  
    18  import (
    19  	"code.vegaprotocol.io/vega/core/types"
    20  	"code.vegaprotocol.io/vega/libs/num"
    21  	"code.vegaprotocol.io/vega/protos/vega"
    22  )
    23  
    24  type feeAmounts struct {
    25  	side  types.Side
    26  	maker *num.Uint
    27  	taker *num.Uint
    28  	other *num.Uint
    29  }
    30  
    31  func newFeeAmounts(side types.Side) *feeAmounts {
    32  	return &feeAmounts{
    33  		side:  side,
    34  		maker: num.UintZero(),
    35  		taker: num.UintZero(),
    36  		other: num.UintZero(),
    37  	}
    38  }
    39  
    40  func getFeeAmounts(trade *vega.Trade) (*feeAmounts, *feeAmounts) {
    41  	buyer, seller := newFeeAmounts(types.SideBuy), newFeeAmounts(types.SideSell)
    42  	buyer.setAmounts(trade)
    43  	seller.setAmounts(trade)
    44  	// auction end trades don't really have an aggressor, maker and taker fees are split.
    45  	if trade.Aggressor == types.SideSell {
    46  		buyer.maker.AddSum(seller.taker)
    47  	} else if trade.Aggressor == types.SideBuy {
    48  		seller.maker.AddSum(buyer.taker)
    49  	} else {
    50  		buyer.maker.AddSum(seller.taker)
    51  		seller.maker.AddSum(buyer.taker)
    52  	}
    53  	return buyer, seller
    54  }
    55  
    56  func (f *feeAmounts) setAmounts(trade *vega.Trade) {
    57  	fee := trade.BuyerFee
    58  	if f.side == types.SideSell {
    59  		fee = trade.SellerFee
    60  	}
    61  	if fee == nil {
    62  		return
    63  	}
    64  	maker, infra, lFee, tFee, bbFee, hvFee := num.UintZero(), num.UintZero(), num.UintZero(), num.UintZero(), num.UintZero(), num.UintZero()
    65  	if len(fee.MakerFee) > 0 {
    66  		maker, _ = num.UintFromString(fee.MakerFee, 10)
    67  	}
    68  	if len(fee.InfrastructureFee) > 0 {
    69  		infra, _ = num.UintFromString(fee.InfrastructureFee, 10)
    70  	}
    71  	if len(fee.LiquidityFee) > 0 {
    72  		lFee, _ = num.UintFromString(fee.LiquidityFee, 10)
    73  	}
    74  	if len(fee.TreasuryFee) > 0 {
    75  		tFee, _ = num.UintFromString(fee.TreasuryFee, 10)
    76  	}
    77  	if len(fee.BuyBackFee) > 0 {
    78  		bbFee, _ = num.UintFromString(fee.BuyBackFee, 10)
    79  	}
    80  	if len(fee.HighVolumeMakerFee) > 0 {
    81  		hvFee, _ = num.UintFromString(fee.HighVolumeMakerFee, 10)
    82  	}
    83  	f.other.AddSum(infra, lFee, tFee, bbFee, hvFee)
    84  	f.taker.AddSum(maker)
    85  }