-
Notifications
You must be signed in to change notification settings - Fork 19
/
transfers_common.go
243 lines (216 loc) · 6.64 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
// 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"
"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, market 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, market, 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, market 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: market,
}, &types.Transfer{
Owner: to,
Amount: &types.FinancialAmount{
Amount: amount.Clone(),
Asset: asset,
},
Type: types.TransferTypeTransferFundsDistribute,
MinAmount: amount.Clone(),
Market: market,
}
}
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:
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
}
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
}