/
transfer.go
359 lines (315 loc) · 9.68 KB
/
transfer.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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
// 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 entities
import (
"context"
"encoding/json"
"fmt"
"time"
"code.vegaprotocol.io/vega/libs/ptr"
v2 "code.vegaprotocol.io/vega/protos/data-node/api/v2"
"code.vegaprotocol.io/vega/protos/vega"
eventspb "code.vegaprotocol.io/vega/protos/vega/events/v1"
"github.com/shopspring/decimal"
)
type AccountSource interface {
Obtain(ctx context.Context, a *Account) error
GetByID(ctx context.Context, id AccountID) (Account, error)
}
type _Transfer struct{}
type TransferID = ID[_Transfer]
type TransferDetails struct {
Transfer
Fees []*TransferFees
}
type Transfer struct {
ID TransferID
TxHash TxHash
VegaTime time.Time
FromAccountID AccountID
ToAccountID AccountID
AssetID AssetID
Amount decimal.Decimal
Reference string
Status TransferStatus
TransferType TransferType
DeliverOn *time.Time
StartEpoch *uint64
EndEpoch *uint64
Factor *decimal.Decimal
DispatchStrategy *vega.DispatchStrategy
Reason *string
GameID GameID
}
type TransferFees struct {
TransferID TransferID
EpochSeq uint64
Amount decimal.Decimal
DiscountApplied decimal.Decimal
VegaTime time.Time
}
type TransferFeesDiscount struct {
PartyID PartyID
AssetID AssetID
Amount decimal.Decimal
EpochSeq uint64
VegaTime time.Time
}
func (f *TransferFeesDiscount) ToProto() *eventspb.TransferFeesDiscount {
return &eventspb.TransferFeesDiscount{
Party: f.PartyID.String(),
Asset: f.AssetID.String(),
Amount: f.Amount.String(),
Epoch: f.EpochSeq,
}
}
func TransferFeesDiscountFromProto(f *eventspb.TransferFeesDiscount, vegaTime time.Time) *TransferFeesDiscount {
amt, _ := decimal.NewFromString(f.Amount)
return &TransferFeesDiscount{
PartyID: PartyID(f.Party),
AssetID: AssetID(f.Asset),
EpochSeq: f.Epoch,
Amount: amt,
VegaTime: vegaTime,
}
}
func (t *Transfer) ToProto(ctx context.Context, accountSource AccountSource) (*eventspb.Transfer, error) {
fromAcc, err := accountSource.GetByID(ctx, t.FromAccountID)
if err != nil {
return nil, fmt.Errorf("getting from account for transfer proto:%w", err)
}
toAcc, err := accountSource.GetByID(ctx, t.ToAccountID)
if err != nil {
return nil, fmt.Errorf("getting to account for transfer proto:%w", err)
}
var gameID *string
if len(t.GameID) > 0 {
gameID = ptr.From(t.GameID.String())
}
proto := eventspb.Transfer{
Id: t.ID.String(),
From: fromAcc.PartyID.String(),
FromAccountType: fromAcc.Type,
To: toAcc.PartyID.String(),
ToAccountType: toAcc.Type,
Asset: t.AssetID.String(),
Amount: t.Amount.String(),
Reference: t.Reference,
Status: eventspb.Transfer_Status(t.Status),
Timestamp: t.VegaTime.UnixNano(),
Kind: nil,
Reason: t.Reason,
GameId: gameID,
}
switch t.TransferType {
case OneOff:
proto.Kind = &eventspb.Transfer_OneOff{OneOff: &eventspb.OneOffTransfer{DeliverOn: t.DeliverOn.UnixNano()}}
case Recurring:
recurringTransfer := &eventspb.RecurringTransfer{
StartEpoch: *t.StartEpoch,
Factor: t.Factor.String(),
}
recurringTransfer.DispatchStrategy = t.DispatchStrategy
if t.EndEpoch != nil {
endEpoch := *t.EndEpoch
recurringTransfer.EndEpoch = &endEpoch
}
proto.Kind = &eventspb.Transfer_Recurring{Recurring: recurringTransfer}
case GovernanceOneOff:
proto.Kind = &eventspb.Transfer_OneOffGovernance{OneOffGovernance: &eventspb.OneOffGovernanceTransfer{DeliverOn: t.DeliverOn.UnixNano()}}
case GovernanceRecurring:
recurringTransfer := &eventspb.RecurringGovernanceTransfer{
StartEpoch: *t.StartEpoch,
DispatchStrategy: t.DispatchStrategy,
}
if t.EndEpoch != nil {
endEpoch := *t.EndEpoch
recurringTransfer.EndEpoch = &endEpoch
}
proto.Kind = &eventspb.Transfer_RecurringGovernance{RecurringGovernance: recurringTransfer}
case Unknown:
// leave Kind as nil
}
return &proto, nil
}
func (f *TransferFees) ToProto() *eventspb.TransferFees {
return &eventspb.TransferFees{
TransferId: f.TransferID.String(),
Amount: f.Amount.String(),
Epoch: f.EpochSeq,
DiscountApplied: f.DiscountApplied.String(),
}
}
func TransferFeesFromProto(f *eventspb.TransferFees, vegaTime time.Time) *TransferFees {
amt, _ := decimal.NewFromString(f.Amount)
discount, _ := decimal.NewFromString(f.DiscountApplied)
return &TransferFees{
TransferID: TransferID(f.TransferId),
EpochSeq: f.Epoch,
Amount: amt,
DiscountApplied: discount,
VegaTime: vegaTime,
}
}
func TransferFromProto(ctx context.Context, t *eventspb.Transfer, txHash TxHash, vegaTime time.Time, accountSource AccountSource) (*Transfer, error) {
fromAcc := Account{
ID: "",
PartyID: PartyID(t.From),
AssetID: AssetID(t.Asset),
Type: t.FromAccountType,
TxHash: txHash,
VegaTime: time.Unix(0, t.Timestamp),
}
if t.From == "0000000000000000000000000000000000000000000000000000000000000000" {
fromAcc.PartyID = "network"
}
if err := accountSource.Obtain(ctx, &fromAcc); err != nil {
return nil, fmt.Errorf("could not obtain source account for transfer: %w", err)
}
toAcc := Account{
ID: "",
PartyID: PartyID(t.To),
AssetID: AssetID(t.Asset),
Type: t.ToAccountType,
TxHash: txHash,
VegaTime: vegaTime,
}
if t.To == "0000000000000000000000000000000000000000000000000000000000000000" {
toAcc.PartyID = "network"
}
if err := accountSource.Obtain(ctx, &toAcc); err != nil {
return nil, fmt.Errorf("could not obtain destination account for transfer: %w", err)
}
amount, err := decimal.NewFromString(t.Amount)
if err != nil {
return nil, fmt.Errorf("invalid transfer amount: %w", err)
}
var gameID GameID
if t.GameId != nil {
gameID = GameID(*t.GameId)
}
transfer := Transfer{
ID: TransferID(t.Id),
TxHash: txHash,
VegaTime: vegaTime,
FromAccountID: fromAcc.ID,
ToAccountID: toAcc.ID,
Amount: amount,
AssetID: AssetID(t.Asset),
Reference: t.Reference,
Status: TransferStatus(t.Status),
TransferType: 0,
DeliverOn: nil,
StartEpoch: nil,
EndEpoch: nil,
Factor: nil,
Reason: t.Reason,
GameID: gameID,
}
switch v := t.Kind.(type) {
case *eventspb.Transfer_OneOff:
transfer.TransferType = OneOff
if v.OneOff != nil {
deliverOn := time.Unix(0, v.OneOff.DeliverOn)
transfer.DeliverOn = &deliverOn
}
case *eventspb.Transfer_OneOffGovernance:
transfer.TransferType = GovernanceOneOff
if v.OneOffGovernance != nil {
deliverOn := time.Unix(0, v.OneOffGovernance.DeliverOn)
transfer.DeliverOn = &deliverOn
}
case *eventspb.Transfer_RecurringGovernance:
transfer.TransferType = GovernanceRecurring
transfer.StartEpoch = &v.RecurringGovernance.StartEpoch
transfer.DispatchStrategy = v.RecurringGovernance.DispatchStrategy
transfer.EndEpoch = v.RecurringGovernance.EndEpoch
case *eventspb.Transfer_Recurring:
transfer.TransferType = Recurring
transfer.StartEpoch = &v.Recurring.StartEpoch
transfer.DispatchStrategy = v.Recurring.DispatchStrategy
transfer.EndEpoch = v.Recurring.EndEpoch
factor, err := decimal.NewFromString(v.Recurring.Factor)
if err != nil {
return nil, fmt.Errorf("invalid factor for recurring transfer:%w", err)
}
transfer.Factor = &factor
default:
transfer.TransferType = Unknown
}
return &transfer, nil
}
func (t Transfer) Cursor() *Cursor {
wc := TransferCursor{
VegaTime: t.VegaTime,
ID: t.ID,
}
return NewCursor(wc.String())
}
func (d TransferDetails) ToProtoEdge(input ...any) (*v2.TransferEdge, error) {
te, err := d.Transfer.ToProtoEdge(input...)
if err != nil {
return nil, err
}
if len(d.Fees) == 0 {
return te, nil
}
te.Node.Fees = make([]*eventspb.TransferFees, 0, len(d.Fees))
for _, f := range d.Fees {
te.Node.Fees = append(te.Node.Fees, f.ToProto())
}
return te, nil
}
func (t Transfer) ToProtoEdge(input ...any) (*v2.TransferEdge, error) {
if len(input) != 2 {
return nil, fmt.Errorf("expected account source and context argument")
}
ctx, ok := input[0].(context.Context)
if !ok {
return nil, fmt.Errorf("first argument must be a context.Context, got: %v", input[0])
}
as, ok := input[1].(AccountSource)
if !ok {
return nil, fmt.Errorf("second argument must be an AccountSource, got: %v", input[1])
}
transferProto, err := t.ToProto(ctx, as)
if err != nil {
return nil, err
}
return &v2.TransferEdge{
Node: &v2.TransferNode{
Transfer: transferProto,
},
Cursor: t.Cursor().Encode(),
}, nil
}
type TransferCursor struct {
VegaTime time.Time `json:"vegaTime"`
ID TransferID `json:"id"`
}
func (tc TransferCursor) String() string {
bs, err := json.Marshal(tc)
if err != nil {
// This should never happen
panic(fmt.Errorf("failed to marshal withdrawal cursor: %w", err))
}
return string(bs)
}
func (tc *TransferCursor) Parse(cursorString string) error {
if cursorString == "" {
return nil
}
return json.Unmarshal([]byte(cursorString), tc)
}