-
Notifications
You must be signed in to change notification settings - Fork 22
/
volume_discount_program.go
139 lines (119 loc) · 3.66 KB
/
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
// 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 steps
import (
"fmt"
"time"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/core/volumediscount"
"code.vegaprotocol.io/vega/libs/num"
"github.com/cucumber/godog"
)
func VolumeDiscountProgramTiers(
tiers map[string][]*types.VolumeBenefitTier,
volumeDiscountTierName string,
table *godog.Table,
) error {
rows := parseVolumeDiscountTiersTable(table)
vbts := make([]*types.VolumeBenefitTier, 0, len(rows))
for _, r := range rows {
row := volumeDiscountTiersRow{row: r}
p := &types.VolumeBenefitTier{
MinimumRunningNotionalTakerVolume: row.volume(),
VolumeDiscountFactor: row.factor(),
}
vbts = append(vbts, p)
}
tiers[volumeDiscountTierName] = vbts
return nil
}
func parseVolumeDiscountTiersTable(table *godog.Table) []RowWrapper {
return StrictParseTable(table, []string{
"volume",
"factor",
}, []string{})
}
type volumeDiscountTiersRow struct {
row RowWrapper
}
func (r volumeDiscountTiersRow) volume() *num.Uint {
return r.row.MustUint("volume")
}
func (r volumeDiscountTiersRow) factor() num.Decimal {
return r.row.MustDecimal("factor")
}
func VolumeDiscountProgram(
vde *volumediscount.Engine,
tiers map[string][]*types.VolumeBenefitTier,
table *godog.Table,
) error {
rows := parseVolumeDiscountTable(table)
vdp := types.VolumeDiscountProgram{}
for _, r := range rows {
row := volumeDiscountRow{row: r}
vdp.ID = row.id()
vdp.WindowLength = row.windowLength()
if row.closingTimestamp() == 0 {
vdp.EndOfProgramTimestamp = time.Time{}
} else {
vdp.EndOfProgramTimestamp = time.Unix(int64(row.closingTimestamp()), 0)
}
tierName := row.tiers()
if tier := tiers[tierName]; tier != nil {
vdp.VolumeBenefitTiers = tier
}
vde.UpdateProgram(&vdp)
}
return nil
}
func parseVolumeDiscountTable(table *godog.Table) []RowWrapper {
return StrictParseTable(table, []string{
"id",
"tiers",
"closing timestamp",
"window length",
}, []string{})
}
type volumeDiscountRow struct {
row RowWrapper
}
func (r volumeDiscountRow) id() string {
return r.row.MustStr("id")
}
func (r volumeDiscountRow) tiers() string {
return r.row.MustStr("tiers")
}
func (r volumeDiscountRow) closingTimestamp() uint64 {
return r.row.MustU64("closing timestamp")
}
func (r volumeDiscountRow) windowLength() uint64 {
return r.row.MustU64("window length")
}
func PartyHasTheFollowingDiscountFactor(party, discountFactor string, vde *volumediscount.Engine) error {
df := vde.VolumeDiscountFactorForParty(types.PartyID(party))
df2, _ := num.DecimalFromString(discountFactor)
if !df.Equal(df2) {
return fmt.Errorf("%s has the discount factor of %s when we expected %s", party, df, df2)
}
return nil
}
func PartyHasTheFollowingTakerNotional(party, notional string, vde *volumediscount.Engine) error {
tn := vde.TakerNotionalForParty(types.PartyID(party))
tn2, _ := num.DecimalFromString(notional)
if !tn.Equal(tn2) {
return fmt.Errorf("%s has the taker notional of %s when we expected %s", party, tn, tn2)
}
return nil
}