code.vegaprotocol.io/vega@v0.79.0/core/governance/referral_program.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 governance
    17  
    18  import (
    19  	"fmt"
    20  	"time"
    21  
    22  	"code.vegaprotocol.io/vega/core/netparams"
    23  	"code.vegaprotocol.io/vega/core/types"
    24  )
    25  
    26  func validateUpdateReferralProgram(netp NetParams, p *types.UpdateReferralProgram, enactment int64) (types.ProposalError, error) {
    27  	if enact := time.Unix(enactment, 0); enact.After(p.Changes.EndOfProgramTimestamp) {
    28  		return types.ProposalErrorInvalidReferralProgram, fmt.Errorf("the proposal must be enacted before the referral program ends")
    29  	}
    30  	maxReferralTiers, _ := netp.GetUint(netparams.ReferralProgramMaxReferralTiers)
    31  	if len(p.Changes.BenefitTiers) > int(maxReferralTiers.Uint64()) {
    32  		return types.ProposalErrorInvalidReferralProgram, fmt.Errorf("the number of benefit tiers in the proposal is higher than the maximum allowed by the network parameter %q: maximum is %s, but got %d", netparams.ReferralProgramMaxReferralTiers, maxReferralTiers.String(), len(p.Changes.BenefitTiers))
    33  	}
    34  
    35  	if len(p.Changes.StakingTiers) > int(maxReferralTiers.Uint64()) {
    36  		return types.ProposalErrorInvalidReferralProgram, fmt.Errorf("the number of staking tiers in the proposal is higher than the maximum allowed by the network parameter %q: maximum is %s, but got %d", netparams.ReferralProgramMaxReferralTiers, maxReferralTiers.String(), len(p.Changes.StakingTiers))
    37  	}
    38  
    39  	maxRewardFactor, _ := netp.GetDecimal(netparams.ReferralProgramMaxReferralRewardFactor)
    40  	maxDiscountFactor, _ := netp.GetDecimal(netparams.ReferralProgramMaxReferralDiscountFactor)
    41  	for i, tier := range p.Changes.BenefitTiers {
    42  		if tier.ReferralRewardFactors.Infra.GreaterThan(maxRewardFactor) {
    43  			return types.ProposalErrorInvalidReferralProgram, fmt.Errorf("tier %d defines a referral reward infrastructure factor higher than the maximum allowed by the network parameter %q: maximum is %s, but got %s", i+1, netparams.ReferralProgramMaxReferralRewardFactor, maxRewardFactor.String(), tier.ReferralRewardFactors.Infra.String())
    44  		}
    45  		if tier.ReferralRewardFactors.Maker.GreaterThan(maxRewardFactor) {
    46  			return types.ProposalErrorInvalidReferralProgram, fmt.Errorf("tier %d defines a referral reward maker factor higher than the maximum allowed by the network parameter %q: maximum is %s, but got %s", i+1, netparams.ReferralProgramMaxReferralRewardFactor, maxRewardFactor.String(), tier.ReferralRewardFactors.Maker.String())
    47  		}
    48  		if tier.ReferralRewardFactors.Liquidity.GreaterThan(maxRewardFactor) {
    49  			return types.ProposalErrorInvalidReferralProgram, fmt.Errorf("tier %d defines a referral reward liquidity factor higher than the maximum allowed by the network parameter %q: maximum is %s, but got %s", i+1, netparams.ReferralProgramMaxReferralRewardFactor, maxRewardFactor.String(), tier.ReferralRewardFactors.Liquidity.String())
    50  		}
    51  		if tier.ReferralDiscountFactors.Infra.GreaterThan(maxDiscountFactor) {
    52  			return types.ProposalErrorInvalidReferralProgram, fmt.Errorf("tier %d defines a referral discount infrastructure factor higher than the maximum allowed by the network parameter %q: maximum is %s, but got %s", i+1, netparams.ReferralProgramMaxReferralDiscountFactor, maxDiscountFactor.String(), tier.ReferralDiscountFactors.Infra.String())
    53  		}
    54  		if tier.ReferralDiscountFactors.Maker.GreaterThan(maxDiscountFactor) {
    55  			return types.ProposalErrorInvalidReferralProgram, fmt.Errorf("tier %d defines a referral discount maker factor higher than the maximum allowed by the network parameter %q: maximum is %s, but got %s", i+1, netparams.ReferralProgramMaxReferralDiscountFactor, maxDiscountFactor.String(), tier.ReferralDiscountFactors.Maker.String())
    56  		}
    57  		if tier.ReferralDiscountFactors.Liquidity.GreaterThan(maxDiscountFactor) {
    58  			return types.ProposalErrorInvalidReferralProgram, fmt.Errorf("tier %d defines a referral discount liquidity factor higher than the maximum allowed by the network parameter %q: maximum is %s, but got %s", i+1, netparams.ReferralProgramMaxReferralDiscountFactor, maxDiscountFactor.String(), tier.ReferralDiscountFactors.Liquidity.String())
    59  		}
    60  	}
    61  	return types.ProposalErrorUnspecified, nil
    62  }
    63  
    64  func updatedReferralProgramFromProposal(p *proposal) *types.ReferralProgram {
    65  	terms := p.Terms.GetUpdateReferralProgram()
    66  
    67  	return &types.ReferralProgram{
    68  		ID:                    p.ID,
    69  		EndOfProgramTimestamp: terms.Changes.EndOfProgramTimestamp,
    70  		WindowLength:          terms.Changes.WindowLength,
    71  		BenefitTiers:          terms.Changes.BenefitTiers,
    72  		StakingTiers:          terms.Changes.StakingTiers,
    73  	}
    74  }