code.vegaprotocol.io/vega@v0.79.0/datanode/entities/position_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 entities
    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 getFeeAmountsForSide(trade *vega.Trade, seller bool) *feeAmounts {
    41  	b, s := getFeeAmounts(trade)
    42  	if seller {
    43  		return s
    44  	}
    45  	return b
    46  }
    47  
    48  func getFeeAmounts(trade *vega.Trade) (*feeAmounts, *feeAmounts) {
    49  	buyer, seller := newFeeAmounts(types.SideBuy), newFeeAmounts(types.SideSell)
    50  	buyer.setAmounts(trade)
    51  	seller.setAmounts(trade)
    52  	// auction end trades don't really have an aggressor, maker and taker fees are split.
    53  	if trade.Aggressor == types.SideSell {
    54  		buyer.maker.AddSum(seller.taker)
    55  	} else if trade.Aggressor == types.SideBuy {
    56  		seller.maker.AddSum(buyer.taker)
    57  	} else {
    58  		buyer.maker.AddSum(seller.taker)
    59  		seller.maker.AddSum(buyer.taker)
    60  	}
    61  	return buyer, seller
    62  }
    63  
    64  func (f *feeAmounts) setAmounts(trade *vega.Trade) {
    65  	fee := trade.BuyerFee
    66  	if f.side == types.SideSell {
    67  		fee = trade.SellerFee
    68  	}
    69  	if fee == nil {
    70  		return
    71  	}
    72  	maker, infra, lFee, tFee, bbFee, hvFee := num.UintZero(), num.UintZero(), num.UintZero(), num.UintZero(), num.UintZero(), num.UintZero()
    73  	if len(fee.MakerFee) > 0 {
    74  		maker, _ = num.UintFromString(fee.MakerFee, 10)
    75  	}
    76  	if len(fee.InfrastructureFee) > 0 {
    77  		infra, _ = num.UintFromString(fee.InfrastructureFee, 10)
    78  	}
    79  	if len(fee.LiquidityFee) > 0 {
    80  		lFee, _ = num.UintFromString(fee.LiquidityFee, 10)
    81  	}
    82  	if len(fee.TreasuryFee) > 0 {
    83  		tFee, _ = num.UintFromString(fee.TreasuryFee, 10)
    84  	}
    85  	if len(fee.BuyBackFee) > 0 {
    86  		bbFee, _ = num.UintFromString(fee.BuyBackFee, 10)
    87  	}
    88  	if len(fee.HighVolumeMakerFee) > 0 {
    89  		hvFee, _ = num.UintFromString(fee.HighVolumeMakerFee, 10)
    90  	}
    91  	f.other.AddSum(infra, lFee, tFee, bbFee, hvFee)
    92  	f.taker.AddSum(maker)
    93  }