-
Notifications
You must be signed in to change notification settings - Fork 19
/
transfers_common.go
251 lines (224 loc) · 7.03 KB
/
transfers_common.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
// 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 banking
import (
"context"
"fmt"
"code.vegaprotocol.io/vega/core/assets"
"code.vegaprotocol.io/vega/core/types"
"code.vegaprotocol.io/vega/libs/num"
"code.vegaprotocol.io/vega/logging"
)
func (e *Engine) OnTransferFeeFactorUpdate(ctx context.Context, f num.Decimal) error {
e.transferFeeFactor = f
return nil
}
func (e *Engine) OnMinTransferQuantumMultiple(ctx context.Context, f num.Decimal) error {
e.minTransferQuantumMultiple = f
return nil
}
func (e *Engine) TransferFunds(
ctx context.Context,
transfer *types.TransferFunds,
) error {
now := e.timeService.GetTimeNow()
// add timestamps straight away
switch transfer.Kind {
case types.TransferCommandKindOneOff:
transfer.OneOff.Timestamp = now
return e.oneOffTransfer(ctx, transfer.OneOff)
case types.TransferCommandKindRecurring:
transfer.Recurring.Timestamp = now
return e.recurringTransfer(ctx, transfer.Recurring)
default:
return ErrUnsupportedTransferKind
}
}
func (e *Engine) CheckTransfer(t *types.TransferBase) error {
// ensure asset exists
a, err := e.assets.Get(t.Asset)
if err != nil {
e.log.Debug("cannot transfer funds, invalid asset", logging.Error(err))
return fmt.Errorf("could not transfer funds, %w", err)
}
if err = t.IsValid(); err != nil {
return fmt.Errorf("could not transfer funds, %w", err)
}
if err := e.ensureMinimalTransferAmount(a, t.Amount); err != nil {
return err
}
_, err = e.ensureFeeForTransferFunds(t.Amount, t.From, t.Asset, t.FromAccountType)
if err != nil {
return fmt.Errorf("could not transfer funds, %w", err)
}
return nil
}
func (e *Engine) ensureMinimalTransferAmount(a *assets.Asset, amount *num.Uint) error {
quantum := a.Type().Details.Quantum
// no reason this would produce an error
minAmount, _ := num.UintFromDecimal(quantum.Mul(e.minTransferQuantumMultiple))
// no verify amount
if amount.LT(minAmount) {
e.log.Debug("cannot transfer funds, less than minimal amount requested to transfer",
logging.BigUint("min-amount", minAmount),
logging.BigUint("requested-amount", amount),
)
return fmt.Errorf("could not transfer funds, less than minimal amount requested to transfer")
}
return nil
}
func (e *Engine) processTransfer(
ctx context.Context,
from, to, asset, toMarket string,
fromAcc, toAcc types.AccountType,
amount *num.Uint,
reference string,
// optional oneoff transfer
// in case we need to schedule the delivery
oneoff *types.OneOffTransfer,
) ([]*types.LedgerMovement, error) {
// ensure the party have enough funds for both the
// amount and the fee for the transfer
feeTransfer, err := e.ensureFeeForTransferFunds(amount, from, asset, fromAcc)
if err != nil {
return nil, fmt.Errorf("could not pay the fee for transfer: %w", err)
}
feeTransferAccountType := []types.AccountType{fromAcc}
fromTransfer, toTransfer := e.makeTransfers(from, to, asset, "", toMarket, amount)
transfers := []*types.Transfer{fromTransfer}
accountTypes := []types.AccountType{fromAcc}
references := []string{reference}
// does the transfer needs to be finalized now?
now := e.timeService.GetTimeNow()
if oneoff == nil || (oneoff.DeliverOn == nil || oneoff.DeliverOn.Before(now)) {
transfers = append(transfers, toTransfer)
accountTypes = append(accountTypes, toAcc)
references = append(references, reference)
// if this goes well the whole transfer will be done
// so we can set it to the proper status
} else {
// schedule the transfer
e.scheduleTransfer(
oneoff,
toTransfer,
toAcc,
reference,
*oneoff.DeliverOn,
)
}
// process the transfer
tresps, err := e.col.TransferFunds(
ctx, transfers, accountTypes, references, []*types.Transfer{feeTransfer}, feeTransferAccountType,
)
if err != nil {
return nil, err
}
return tresps, nil
}
func (e *Engine) makeTransfers(
from, to, asset, fromMarket, toMarket string,
amount *num.Uint,
) (*types.Transfer, *types.Transfer) {
return &types.Transfer{
Owner: from,
Amount: &types.FinancialAmount{
Amount: amount.Clone(),
Asset: asset,
},
Type: types.TransferTypeTransferFundsSend,
MinAmount: amount.Clone(),
Market: fromMarket,
}, &types.Transfer{
Owner: to,
Amount: &types.FinancialAmount{
Amount: amount.Clone(),
Asset: asset,
},
Type: types.TransferTypeTransferFundsDistribute,
MinAmount: amount.Clone(),
Market: toMarket,
}
}
func (e *Engine) makeFeeTransferForTransferFunds(
amount *num.Uint,
from, asset string,
fromAccountType types.AccountType,
) *types.Transfer {
// first we calculate the fee
feeAmount, _ := num.UintFromDecimal(amount.ToDecimal().Mul(e.transferFeeFactor))
switch fromAccountType {
case types.AccountTypeGeneral, types.AccountTypeVestedRewards:
default:
e.log.Panic("from account not supported",
logging.String("account-type", fromAccountType.String()),
logging.String("asset", asset),
logging.String("from", from),
)
}
return &types.Transfer{
Owner: from,
Amount: &types.FinancialAmount{
Amount: feeAmount.Clone(),
Asset: asset,
},
Type: types.TransferTypeInfrastructureFeePay,
MinAmount: feeAmount,
}
}
func (e *Engine) ensureFeeForTransferFunds(
amount *num.Uint,
from, asset string,
fromAccountType types.AccountType,
) (*types.Transfer, error) {
transfer := e.makeFeeTransferForTransferFunds(
amount, from, asset, fromAccountType,
)
var (
totalAmount = num.Sum(transfer.Amount.Amount, amount)
account *types.Account
err error
)
switch fromAccountType {
case types.AccountTypeGeneral:
account, err = e.col.GetPartyGeneralAccount(from, asset)
if err != nil {
return nil, err
}
case types.AccountTypeVestedRewards:
account, err = e.col.GetPartyVestedRewardAccount(from, asset)
if err != nil {
return nil, err
}
default:
e.log.Panic("from account not supported",
logging.String("account-type", fromAccountType.String()),
logging.String("asset", asset),
logging.String("from", from),
)
}
if account.Balance.LT(totalAmount) {
e.log.Debug("not enough funds to transfer",
logging.BigUint("amount", amount),
logging.BigUint("fee", transfer.Amount.Amount),
logging.BigUint("total-amount", totalAmount),
logging.BigUint("account-balance", account.Balance),
logging.String("account-type", fromAccountType.String()),
logging.String("asset", asset),
logging.String("from", from),
)
return nil, ErrNotEnoughFundsToTransfer
}
return transfer, nil
}