-
Notifications
You must be signed in to change notification settings - Fork 18
/
governance_update_volume_discount_program.go
190 lines (162 loc) · 6.04 KB
/
governance_update_volume_discount_program.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Copyright (C) 2023 Gobalsky Labs Limited
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package types
import (
"fmt"
"time"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/libs/stringer"
vegapb "code.vegaprotocol.io/vega/protos/vega"
)
type ProposalTermsUpdateVolumeDiscountProgram struct {
UpdateVolumeDiscountProgram *UpdateVolumeDiscountProgram
}
func (a ProposalTermsUpdateVolumeDiscountProgram) String() string {
return fmt.Sprintf(
"updateVolumeDiscountProgram(%s)",
stringer.ReflectPointerToString(a.UpdateVolumeDiscountProgram),
)
}
func (a ProposalTermsUpdateVolumeDiscountProgram) IntoProto() *vegapb.ProposalTerms_UpdateVolumeDiscountProgram {
return &vegapb.ProposalTerms_UpdateVolumeDiscountProgram{
UpdateVolumeDiscountProgram: a.UpdateVolumeDiscountProgram.IntoProto(),
}
}
func (a ProposalTermsUpdateVolumeDiscountProgram) isPTerm() {}
func (a ProposalTermsUpdateVolumeDiscountProgram) oneOfProto() interface{} {
return a.IntoProto()
}
func (a ProposalTermsUpdateVolumeDiscountProgram) GetTermType() ProposalTermsType {
return ProposalTermsTypeUpdateVolumeDiscountProgram
}
func (a ProposalTermsUpdateVolumeDiscountProgram) DeepClone() proposalTerm {
if a.UpdateVolumeDiscountProgram == nil {
return &ProposalTermsUpdateVolumeDiscountProgram{}
}
return &ProposalTermsUpdateVolumeDiscountProgram{
UpdateVolumeDiscountProgram: a.UpdateVolumeDiscountProgram.DeepClone(),
}
}
func NewUpdateVolumeDiscountProgramProposalFromProto(p *vegapb.ProposalTerms_UpdateVolumeDiscountProgram) (*ProposalTermsUpdateVolumeDiscountProgram, error) {
return &ProposalTermsUpdateVolumeDiscountProgram{
UpdateVolumeDiscountProgram: NewUpdateVolumeDiscountProgramFromProto(p.UpdateVolumeDiscountProgram),
}, nil
}
type UpdateVolumeDiscountProgram struct {
Changes *VolumeDiscountProgramChanges
}
func (p UpdateVolumeDiscountProgram) IntoProto() *vegapb.UpdateVolumeDiscountProgram {
return &vegapb.UpdateVolumeDiscountProgram{
Changes: p.Changes.IntoProto(),
}
}
func (p UpdateVolumeDiscountProgram) String() string {
return fmt.Sprintf(
"changes(%s)",
stringer.ReflectPointerToString(p.Changes),
)
}
func (p UpdateVolumeDiscountProgram) DeepClone() *UpdateVolumeDiscountProgram {
if p.Changes == nil {
return &UpdateVolumeDiscountProgram{}
}
return &UpdateVolumeDiscountProgram{
Changes: p.Changes.DeepClone(),
}
}
func NewUpdateVolumeDiscountProgramFromProto(p *vegapb.UpdateVolumeDiscountProgram) *UpdateVolumeDiscountProgram {
if p == nil {
return &UpdateVolumeDiscountProgram{}
}
return &UpdateVolumeDiscountProgram{
Changes: NewVolumeDiscountProgramChangesFromProto(p.Changes),
}
}
type VolumeDiscountProgramChanges struct {
ID string
Version uint64
EndOfProgramTimestamp time.Time
WindowLength uint64
VolumeBenefitTiers []*VolumeBenefitTier
}
func (v VolumeDiscountProgramChanges) IntoProto() *vegapb.VolumeDiscountProgramChanges {
benefitTiers := make([]*vegapb.VolumeBenefitTier, 0, len(v.VolumeBenefitTiers))
for _, tier := range v.VolumeBenefitTiers {
benefitTiers = append(benefitTiers, &vegapb.VolumeBenefitTier{
MinimumRunningNotionalTakerVolume: tier.MinimumRunningNotionalTakerVolume.String(),
VolumeDiscountFactor: tier.VolumeDiscountFactor.String(),
})
}
return &vegapb.VolumeDiscountProgramChanges{
BenefitTiers: benefitTiers,
EndOfProgramTimestamp: v.EndOfProgramTimestamp.Unix(),
WindowLength: v.WindowLength,
}
}
func (v VolumeDiscountProgramChanges) DeepClone() *VolumeDiscountProgramChanges {
benefitTiers := make([]*VolumeBenefitTier, 0, len(v.VolumeBenefitTiers))
for _, tier := range v.VolumeBenefitTiers {
benefitTiers = append(benefitTiers, &VolumeBenefitTier{
MinimumRunningNotionalTakerVolume: tier.MinimumRunningNotionalTakerVolume.Clone(),
VolumeDiscountFactor: tier.VolumeDiscountFactor,
})
}
cpy := VolumeDiscountProgramChanges{
ID: v.ID,
Version: v.Version,
EndOfProgramTimestamp: v.EndOfProgramTimestamp,
WindowLength: v.WindowLength,
VolumeBenefitTiers: benefitTiers,
}
return &cpy
}
func (c VolumeDiscountProgramChanges) String() string {
benefitTierStr := ""
for i, tier := range c.VolumeBenefitTiers {
if i > 1 {
benefitTierStr += ", "
}
benefitTierStr += fmt.Sprintf("%d(minimumRunningNotionalTakerVolume(%s), volumeDiscountFactor(%s))",
i,
tier.MinimumRunningNotionalTakerVolume.String(),
tier.VolumeDiscountFactor.String(),
)
}
return fmt.Sprintf(
"endOfProgramTimestamp(%d), windowLength(%d), benefitTiers(%s)",
c.EndOfProgramTimestamp.Unix(),
c.WindowLength,
benefitTierStr,
)
}
func NewVolumeDiscountProgramChangesFromProto(v *vegapb.VolumeDiscountProgramChanges) *VolumeDiscountProgramChanges {
if v == nil {
return &VolumeDiscountProgramChanges{}
}
benefitTiers := make([]*VolumeBenefitTier, 0, len(v.BenefitTiers))
for _, tier := range v.BenefitTiers {
minimumRunningVolume, _ := num.UintFromString(tier.MinimumRunningNotionalTakerVolume, 10)
discountFactor, _ := num.DecimalFromString(tier.VolumeDiscountFactor)
benefitTiers = append(benefitTiers, &VolumeBenefitTier{
MinimumRunningNotionalTakerVolume: minimumRunningVolume,
VolumeDiscountFactor: discountFactor,
})
}
return &VolumeDiscountProgramChanges{
EndOfProgramTimestamp: time.Unix(v.EndOfProgramTimestamp, 0),
WindowLength: v.WindowLength,
VolumeBenefitTiers: benefitTiers,
}
}