-
Notifications
You must be signed in to change notification settings - Fork 19
/
funding_payment_events.go
149 lines (128 loc) · 4.39 KB
/
funding_payment_events.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
// 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/events"
"code.vegaprotocol.io/vega/core/integration/stubs"
"code.vegaprotocol.io/vega/logging"
"github.com/cucumber/godog"
)
func TheFollowingFundingPeriodEventsShouldBeEmitted(broker *stubs.BrokerStub, table *godog.Table) error {
fundingPeriodEvents := broker.GetFundingPeriodEvents()
for _, row := range parseFundingPeriodEventTable(table) {
fpe := FundingPeriodEventWrapper{
row: row,
}
matched := false
for _, evt := range fundingPeriodEvents {
if checkFundingPeriodEvent(evt, fpe) {
matched = true
break
}
}
if !matched {
start, _ := fpe.Start()
end, _ := fpe.End()
return fmt.Errorf("Funding period event with start=%v, end=%v, internal TWAP=%s, external TWAP=%s not found", start, end, fpe.InternalTWAP(), fpe.ExternalTWAP())
}
}
return nil
}
func DebugFundingPeriodEventss(broker *stubs.BrokerStub, log *logging.Logger) {
log.Info("DUMPING FUNDING PERIOD EVENTS")
data := broker.GetFundingPeriodEvents()
for _, evt := range data {
p := evt.Proto()
log.Infof("%s\n", p.String())
}
}
func VerifyTime(now time.Time, expected int64) error {
actual := now.Unix()
if actual == expected || now.UnixNano() == expected {
return nil
}
return fmt.Errorf("Expected unix time=%v, actual=%v", expected, actual)
}
func checkFundingPeriodEvent(evt events.FundingPeriod, row FundingPeriodEventWrapper) bool {
fundingPeriod := evt.FundingPeriod()
expectedStart, b := row.Start()
actualStart := fundingPeriod.GetStart()
if b && !actualEqualsExpectedInSecondsOrNanos(actualStart, expectedStart) {
return false
}
expectedEnd, b := row.End()
actualEnd := fundingPeriod.GetEnd()
if b && !actualEqualsExpectedInSecondsOrNanos(actualEnd, expectedEnd) {
return false
}
expectedInternalTwap := row.InternalTWAP()
actualInternalTwap := fundingPeriod.GetInternalTwap()
if actualInternalTwap == "" && len(expectedInternalTwap) > 0 || expectedInternalTwap != actualInternalTwap {
return false
}
expectedExternalTwap := row.ExternalTWAP()
actualExternalTwap := fundingPeriod.GetExternalTwap()
if actualExternalTwap == "" && len(expectedExternalTwap) > 0 || expectedExternalTwap != actualExternalTwap {
return false
}
expectedFundingPayment, b := row.FundingPayment()
actualFundingPayment := fundingPeriod.GetFundingPayment()
if b && (actualFundingPayment == "" && len(expectedFundingPayment) > 0 || expectedFundingPayment != actualFundingPayment) {
return false
}
expectedFundingRate, b := row.FundingRate()
actualFundingRate := fundingPeriod.GetFundingRate()
if b && (actualFundingRate == "" && len(expectedFundingRate) > 0 || expectedFundingRate != actualFundingRate) {
return false
}
return true
}
func actualEqualsExpectedInSecondsOrNanos(actual, expected int64) bool {
return expected == actual || expected*int64(time.Second) == actual
}
type FundingPeriodEventWrapper struct {
row RowWrapper
}
func parseFundingPeriodEventTable(table *godog.Table) []RowWrapper {
return StrictParseTable(table, []string{
"internal twap",
"external twap",
}, []string{
"funding payment",
"funding rate",
"start",
"end",
})
}
func (f FundingPeriodEventWrapper) InternalTWAP() string {
return f.row.MustStr("internal twap")
}
func (f FundingPeriodEventWrapper) ExternalTWAP() string {
return f.row.MustStr("external twap")
}
func (f FundingPeriodEventWrapper) FundingPayment() (string, bool) {
return f.row.StrB("funding payment")
}
func (f FundingPeriodEventWrapper) FundingRate() (string, bool) {
return f.row.StrB("funding rate")
}
func (f FundingPeriodEventWrapper) Start() (int64, bool) {
return f.row.I64B("start")
}
func (f FundingPeriodEventWrapper) End() (int64, bool) {
return f.row.I64B("end")
}