-
Notifications
You must be signed in to change notification settings - Fork 19
/
recurring_transfers.go
228 lines (200 loc) · 7.7 KB
/
recurring_transfers.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright (c) 2022 Gobalsky Labs Limited
//
// Use of this software is governed by the Business Source License included
// in the LICENSE.VEGA file and at https://www.mariadb.com/bsl11.
//
// Change Date: 18 months from the later of the date of the first publicly
// available Distribution of this version of the repository, and 25 June 2022.
//
// On the date above, in accordance with the Business Source License, use
// of this software will be governed by version 3 or later of the GNU General
// Public License.
package banking
import (
"context"
"errors"
"fmt"
"code.vegaprotocol.io/vega/core/events"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
vegapb "code.vegaprotocol.io/vega/protos/vega"
)
var (
ErrStartEpochInThePast = errors.New("start epoch in the past")
ErrCannotSubmitDuplicateRecurringTransferWithSameFromAndTo = errors.New("cannot submit duplicate recurring transfer with same from and to")
)
func (e *Engine) recurringTransfer(
ctx context.Context,
transfer *types.RecurringTransfer,
) (err error) {
defer func() {
e.broker.Send(events.NewRecurringTransferFundsEvent(ctx, transfer))
}()
// ensure asset exists
a, err := e.assets.Get(transfer.Asset)
if err != nil {
transfer.Status = types.TransferStatusRejected
e.log.Debug("cannot transfer funds, invalid asset", logging.Error(err))
return fmt.Errorf("could not transfer funds: %w", err)
}
if err := transfer.IsValid(); err != nil {
transfer.Status = types.TransferStatusRejected
return err
}
if err := e.ensureMinimalTransferAmount(a, transfer.Amount); err != nil {
transfer.Status = types.TransferStatusRejected
return err
}
if err := e.ensureNoRecurringTransferDuplicates(transfer); err != nil {
transfer.Status = types.TransferStatusRejected
return err
}
// can't create transfer with start epoch in the past
if transfer.StartEpoch < e.currentEpoch {
transfer.Status = types.TransferStatusRejected
return ErrStartEpochInThePast
}
// from here all sounds OK, we can add the transfer
// in the recurringTransfer map/slice
e.recurringTransfers = append(e.recurringTransfers, transfer)
e.recurringTransfersMap[transfer.ID] = transfer
return nil
}
func compareStringSlices(a, b []string) bool {
if len(a) != len(b) {
return false
}
for i, v := range a {
if v != b[i] {
return false
}
}
return true
}
func isSimilar(dispatchStrategy1, dispatchStrategy2 *vegapb.DispatchStrategy) bool {
return (dispatchStrategy1 == nil && dispatchStrategy2 == nil) ||
(dispatchStrategy1 != nil && dispatchStrategy2 != nil && dispatchStrategy1.AssetForMetric == dispatchStrategy2.AssetForMetric && dispatchStrategy1.Metric == dispatchStrategy2.Metric && compareStringSlices(dispatchStrategy1.Markets, dispatchStrategy2.Markets))
}
func (e *Engine) ensureNoRecurringTransferDuplicates(
transfer *types.RecurringTransfer,
) error {
for _, v := range e.recurringTransfers {
// NB: 2 transfers are identical and not allowed if they have the same from, to, type AND the same dispatch strategy.
// This is needed so that we can for example setup transfer of USDT from one PK to the reward account with type maker fees received with dispatch based on the asset ETH -
// and then a similar transfer of USDT from the same PK to the same reward type but with different dispatch strategy - one tracking markets for the asset DAI.
if v.From == transfer.From && v.To == transfer.To && v.Asset == transfer.Asset && v.FromAccountType == transfer.FromAccountType && v.ToAccountType == transfer.ToAccountType && isSimilar(v.DispatchStrategy, transfer.DispatchStrategy) {
return ErrCannotSubmitDuplicateRecurringTransferWithSameFromAndTo
}
}
return nil
}
func (e *Engine) getMarketScores(ds *vegapb.DispatchStrategy, payoutAsset, funder string) []*types.MarketContributionScore {
switch ds.Metric {
case vegapb.DispatchMetric_DISPATCH_METRIC_MAKER_FEES_PAID, vegapb.DispatchMetric_DISPATCH_METRIC_MAKER_FEES_RECEIVED, vegapb.DispatchMetric_DISPATCH_METRIC_LP_FEES_RECEIVED:
return e.marketActivityTracker.GetMarketScores(ds.AssetForMetric, ds.Markets, ds.Metric)
case vegapb.DispatchMetric_DISPATCH_METRIC_MARKET_VALUE:
// get a slice of markets for the metric asset that are eligible to be paid for proposer bonus and have not been paid for the specific markets in scope
return e.marketActivityTracker.GetMarketsWithEligibleProposer(ds.AssetForMetric, ds.Markets, payoutAsset, funder)
}
return nil
}
func (e *Engine) distributeRecurringTransfers(ctx context.Context, newEpoch uint64) {
var (
transfersDone = []events.Event{}
tresps = []*types.LedgerMovement{}
currentEpoch = num.NewUint(newEpoch).ToDecimal()
)
// iterate over all transfers
for _, v := range e.recurringTransfers {
if v.StartEpoch > newEpoch {
// not started
continue
}
var (
startEpoch = num.NewUint(v.StartEpoch).ToDecimal()
startAmount = v.Amount.ToDecimal()
amount, _ = num.UintFromDecimal(
startAmount.Mul(
v.Factor.Pow(currentEpoch.Sub(startEpoch)),
),
)
)
// check if the amount is still enough
// ensure asset exists
a, err := e.assets.Get(v.Asset)
if err != nil {
// this should not be possible, asset was validated at first when
// accepting the transfer
e.log.Panic("this should never happen", logging.Error(err))
}
if err := e.ensureMinimalTransferAmount(a, amount); err != nil {
v.Status = types.TransferStatusStopped
transfersDone = append(transfersDone,
events.NewRecurringTransferFundsEvent(ctx, v))
e.deleteTransfer(v.ID)
continue
}
// NB: if no dispatch strategy is defined - the transfer is made to the account as defined in the transfer.
// If a dispatch strategy is defined but there are no relevant markets in scope or no fees in scope then no transfer is made!
var resps []*types.LedgerMovement
if v.DispatchStrategy == nil {
resps, err = e.processTransfer(
ctx, v.From, v.To, v.Asset, "", v.FromAccountType, v.ToAccountType, amount, v.Reference, nil, // last is eventual oneoff, which this is not
)
} else {
marketScores := e.getMarketScores(v.DispatchStrategy, v.Asset, v.From)
for _, fms := range marketScores {
amt, _ := num.UintFromDecimal(amount.ToDecimal().Mul(fms.Score))
if amt.IsZero() {
continue
}
r, err := e.processTransfer(
ctx, v.From, v.To, v.Asset, fms.Market, v.FromAccountType, v.ToAccountType, amt, v.Reference, nil, // last is eventual oneoff, which this is not
)
if err != nil {
break
}
if v.DispatchStrategy.Metric == vegapb.DispatchMetric_DISPATCH_METRIC_MARKET_VALUE && fms.Score.IsPositive() {
e.marketActivityTracker.MarkPaidProposer(fms.Market, v.Asset, v.DispatchStrategy.Markets, v.From)
}
resps = append(resps, r...)
}
}
if err != nil {
v.Status = types.TransferStatusStopped
transfersDone = append(transfersDone,
events.NewRecurringTransferFundsEvent(ctx, v))
e.deleteTransfer(v.ID)
continue
}
tresps = append(tresps, resps...)
// if we don't have anymore
if v.EndEpoch != nil && *v.EndEpoch == e.currentEpoch {
v.Status = types.TransferStatusDone
transfersDone = append(transfersDone, events.NewRecurringTransferFundsEvent(ctx, v))
e.deleteTransfer(v.ID)
}
}
// send events
if len(tresps) > 0 {
e.broker.Send(events.NewLedgerMovements(ctx, tresps))
}
if len(transfersDone) > 0 {
// also set the state change
e.broker.SendBatch(transfersDone)
}
}
func (e *Engine) deleteTransfer(ID string) {
index := -1
for i, rt := range e.recurringTransfers {
if rt.ID == ID {
index = i
break
}
}
if index >= 0 {
e.recurringTransfers = append(e.recurringTransfers[:index], e.recurringTransfers[index+1:]...)
delete(e.recurringTransfersMap, ID)
}
}