Skip to content

Commit

Permalink
Merge pull request #11573 from vegaprotocol/rebate_validation
Browse files Browse the repository at this point in the history
fix: add missing rebate program validation
  • Loading branch information
ze97286 authored Aug 14, 2024
2 parents 7f436a1 + d9b03f6 commit 028e0a7
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions commands/proposal_submission.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ func checkProposalChanges(terms *protoTypes.ProposalTerms) Errors {
errs.Merge(checkUpdateReferralProgram(terms, c))
case *protoTypes.ProposalTerms_UpdateVolumeDiscountProgram:
errs.Merge(checkVolumeDiscountProgram(terms, c))
case *protoTypes.ProposalTerms_UpdateVolumeRebateProgram:
errs.Merge(checkVolumeRebateProgram(terms, c))
default:
return errs.FinalAddForProperty("proposal_submission.terms.change", ErrIsNotValid)
}
Expand Down Expand Up @@ -409,6 +411,19 @@ func checkReferralProgramChanges(changes *vegapb.ReferralProgramChanges, enactme
return errs
}

func checkVolumeRebateProgram(terms *vegapb.ProposalTerms, change *vegapb.ProposalTerms_UpdateVolumeRebateProgram) Errors {
errs := NewErrors()
if change.UpdateVolumeRebateProgram == nil {
return errs.FinalAddForProperty("proposal_submission.terms.change.update_volume_rebate_program", ErrIsRequired)
}
if change.UpdateVolumeRebateProgram.Changes == nil {
return errs.FinalAddForProperty("proposal_submission.terms.change.update_volume_rebate_program.changes", ErrIsRequired)
}

return checkVolumeRebateProgramChanges(change.UpdateVolumeRebateProgram.Changes, terms.EnactmentTimestamp).
AddPrefix("proposal_submission.terms.change.")
}

func checkVolumeDiscountProgram(terms *vegapb.ProposalTerms, change *vegapb.ProposalTerms_UpdateVolumeDiscountProgram) Errors {
errs := NewErrors()
if change.UpdateVolumeDiscountProgram == nil {
Expand All @@ -422,6 +437,54 @@ func checkVolumeDiscountProgram(terms *vegapb.ProposalTerms, change *vegapb.Prop
AddPrefix("proposal_submission.terms.change.")
}

func checkVolumeRebateProgramChanges(changes *vegapb.VolumeRebateProgramChanges, enactmentTimestamp int64) Errors {
errs := NewErrors()

if changes.EndOfProgramTimestamp == 0 {
errs.AddForProperty("update_volume_rebate_program.changes.end_of_program_timestamp", ErrIsRequired)
} else if changes.EndOfProgramTimestamp < 0 {
errs.AddForProperty("update_volume_rebate_program.changes.end_of_program_timestamp", ErrMustBePositive)
} else if changes.EndOfProgramTimestamp < enactmentTimestamp {
errs.AddForProperty("update_volume_rebate_program.changes.end_of_program_timestamp", ErrMustBeGreaterThanEnactmentTimestamp)
}
if changes.WindowLength == 0 {
errs.AddForProperty("update_volume_rebate_program.changes.window_length", ErrIsRequired)
} else if changes.WindowLength > 100 {
errs.AddForProperty("update_volume_rebate_program.changes.window_length", ErrMustBeAtMost100)
}
for i, tier := range changes.BenefitTiers {
errs.Merge(checkVolumeRebateBenefitTier(i, tier))
}

return errs
}

func checkVolumeRebateBenefitTier(index int, tier *vegapb.VolumeRebateBenefitTier) Errors {
errs := NewErrors()
propertyPath := fmt.Sprintf("update_volume_discount_program.changes.benefit_tiers.%d", index)
if len(tier.MinimumPartyMakerVolumeFraction) == 0 {
errs.AddForProperty(propertyPath+".minimum_party_maker_volume_fraction", ErrIsRequired)
} else {
mrtv, err := num.DecimalFromString(tier.MinimumPartyMakerVolumeFraction)
if err != nil {
errs.AddForProperty(propertyPath+".minimum_party_maker_volume_fraction", ErrIsNotValidNumber)
} else if mrtv.IsNegative() || mrtv.IsZero() {
errs.AddForProperty(propertyPath+".minimum_party_maker_volume_fraction", ErrMustBePositive)
}
}
if len(tier.AdditionalMakerRebate) == 0 {
errs.AddForProperty(propertyPath+".additional_maker_rebate", ErrIsRequired)
} else {
rdf, err := num.DecimalFromString(tier.AdditionalMakerRebate)
if err != nil {
errs.AddForProperty(propertyPath+".additional_maker_rebate", ErrIsNotValidNumber)
} else if rdf.IsNegative() {
errs.AddForProperty(propertyPath+".additional_maker_rebate", ErrMustBePositiveOrZero)
}
}
return errs
}

func checkVolumeDiscountProgramChanges(changes *vegapb.VolumeDiscountProgramChanges, enactmentTimestamp int64) Errors {
errs := NewErrors()

Expand Down

0 comments on commit 028e0a7

Please sign in to comment.