-
Notifications
You must be signed in to change notification settings - Fork 19
/
the_following_transfers_should_happen.go
144 lines (118 loc) · 3.59 KB
/
the_following_transfers_should_happen.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
// 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"
"github.com/cucumber/godog"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/integration/stubs"
vegapb "code.vegaprotocol.io/vega/protos/vega"
)
func TheFollowingTransfersShouldHappen(
broker *stubs.BrokerStub,
table *godog.Table,
) error {
transfers := broker.GetTransfers(true)
for _, r := range parseTransferTable(table) {
row := transferRow{row: r}
matched, divergingAmounts := matchTransfers(transfers, row)
if matched {
continue
}
if len(divergingAmounts) == 0 {
return errMissingTransfer(row)
}
return errTransferFoundButNotRightAmount(row, divergingAmounts)
}
broker.ResetType(events.LedgerMovementsEvent)
return nil
}
func errTransferFoundButNotRightAmount(row transferRow, divergingAmounts []uint64) error {
return formatDiff(
fmt.Sprintf("invalid amount for transfer from %s to %s", row.FromAccountID(), row.ToAccountID()),
map[string]string{
"amount": u64ToS(row.Amount()),
},
map[string]string{
"amount": u64SToS(divergingAmounts),
},
)
}
func errMissingTransfer(row transferRow) error {
return fmt.Errorf("missing transfers between %v and %v for amount %v",
row.FromAccountID(), row.ToAccountID(), row.Amount(),
)
}
func matchTransfers(ledgerEntries []*vegapb.LedgerEntry, row transferRow) (bool, []uint64) {
divergingAmounts := []uint64{}
for _, transfer := range ledgerEntries {
if transfer.FromAccount.ID() == row.FromAccountID() && transfer.ToAccount.ID() == row.ToAccountID() {
if row.Type() != "" && transfer.Type != vegapb.TransferType(vegapb.TransferType_value[row.Type()]) {
continue
}
if stringToU64(transfer.Amount) == row.Amount() {
return true, nil
}
divergingAmounts = append(divergingAmounts, stringToU64(transfer.Amount))
}
}
return false, divergingAmounts
}
func parseTransferTable(table *godog.Table) []RowWrapper {
return StrictParseTable(table, []string{
"from",
"from account",
"to",
"to account",
"market id",
"amount",
"asset",
}, []string{
"type",
})
}
type transferRow struct {
row RowWrapper
}
func (r transferRow) From() string {
return r.row.MustStr("from")
}
func (r transferRow) FromAccount() vegapb.AccountType {
return r.row.MustAccount("from account")
}
func (r transferRow) FromAccountID() string {
return AccountID(r.MarketID(), r.From(), r.Asset(), r.FromAccount())
}
func (r transferRow) To() string {
return r.row.MustStr("to")
}
func (r transferRow) Type() string {
return r.row.Str("type")
}
func (r transferRow) ToAccount() vegapb.AccountType {
return r.row.MustAccount("to account")
}
func (r transferRow) ToAccountID() string {
return AccountID(r.MarketID(), r.To(), r.Asset(), r.ToAccount())
}
func (r transferRow) MarketID() string {
return r.row.MustStr("market id")
}
func (r transferRow) Amount() uint64 {
return r.row.MustU64("amount")
}
func (r transferRow) Asset() string {
return r.row.MustStr("asset")
}