code.vegaprotocol.io/vega@v0.79.0/core/integration/steps/the_fees_configuration.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 steps
    17  
    18  import (
    19  	"fmt"
    20  
    21  	"code.vegaprotocol.io/vega/core/integration/steps/market"
    22  	"code.vegaprotocol.io/vega/libs/ptr"
    23  	types "code.vegaprotocol.io/vega/protos/vega"
    24  
    25  	"github.com/cucumber/godog"
    26  )
    27  
    28  func TheFeesConfiguration(config *market.Config, name string, table *godog.Table) error {
    29  	row := feesConfigRow{row: parseFeesConfigTable(table)}
    30  
    31  	liquidityFeeSettings := &types.LiquidityFeeSettings{
    32  		Method: types.LiquidityFeeSettings_METHOD_MARGINAL_COST,
    33  	}
    34  
    35  	method, err := row.liquidityFeeMethod()
    36  	if err != nil {
    37  		return err
    38  	}
    39  
    40  	if method != types.LiquidityFeeSettings_METHOD_UNSPECIFIED {
    41  		liquidityFeeSettings = &types.LiquidityFeeSettings{
    42  			Method:      method,
    43  			FeeConstant: row.liquidityFeeConstant(),
    44  		}
    45  	}
    46  
    47  	return config.FeesConfig.Add(name, &types.Fees{
    48  		Factors: &types.FeeFactors{
    49  			InfrastructureFee: row.infrastructureFee(),
    50  			MakerFee:          row.makerFee(),
    51  			BuyBackFee:        row.buyBackFee(),
    52  			TreasuryFee:       row.treasuryFee(),
    53  		},
    54  		LiquidityFeeSettings: liquidityFeeSettings,
    55  	})
    56  }
    57  
    58  func parseFeesConfigTable(table *godog.Table) RowWrapper {
    59  	return StrictParseFirstRow(table,
    60  		[]string{
    61  			"maker fee",
    62  			"infrastructure fee",
    63  		},
    64  		[]string{
    65  			"liquidity fee method",
    66  			"liquidity fee constant",
    67  			"buy back fee",
    68  			"treasury fee",
    69  		},
    70  	)
    71  }
    72  
    73  type feesConfigRow struct {
    74  	row RowWrapper
    75  }
    76  
    77  func (r feesConfigRow) makerFee() string {
    78  	return r.row.MustStr("maker fee")
    79  }
    80  
    81  func (r feesConfigRow) infrastructureFee() string {
    82  	return r.row.MustStr("infrastructure fee")
    83  }
    84  
    85  func (r feesConfigRow) buyBackFee() string {
    86  	if r.row.HasColumn("buy back fee") {
    87  		return r.row.MustStr("buy back fee")
    88  	}
    89  	return "0"
    90  }
    91  
    92  func (r feesConfigRow) treasuryFee() string {
    93  	if r.row.HasColumn("treasury fee") {
    94  		return r.row.MustStr("treasury fee")
    95  	}
    96  	return "0"
    97  }
    98  
    99  func (r feesConfigRow) liquidityFeeMethod() (types.LiquidityFeeSettings_Method, error) {
   100  	if !r.row.HasColumn("liquidity fee method") {
   101  		return types.LiquidityFeeSettings_METHOD_UNSPECIFIED, nil
   102  	}
   103  	return LiquidityFeeMethodType(r.row.Str("liquidity fee method"))
   104  }
   105  
   106  func (r feesConfigRow) liquidityFeeConstant() *string {
   107  	if !r.row.HasColumn("liquidity fee constant") {
   108  		return nil
   109  	}
   110  	return ptr.From(r.row.Str("liquidity fee constant"))
   111  }
   112  
   113  func LiquidityFeeMethodType(rawValue string) (types.LiquidityFeeSettings_Method, error) {
   114  	ty, ok := types.LiquidityFeeSettings_Method_value[rawValue]
   115  	if !ok {
   116  		return types.LiquidityFeeSettings_Method(ty), fmt.Errorf("invalid liquidity fee method: %v", rawValue)
   117  	}
   118  	return types.LiquidityFeeSettings_Method(ty), nil
   119  }