-
Notifications
You must be signed in to change notification settings - Fork 4
/
limits.go
223 lines (189 loc) · 7.71 KB
/
limits.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
package transaction_v2
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
transactionpb "github.com/code-payments/code-protobuf-api/generated/go/transaction/v2"
"github.com/code-payments/code-server/pkg/code/balance"
"github.com/code-payments/code-server/pkg/code/common"
"github.com/code-payments/code-server/pkg/code/data/phone"
exchange_rate_util "github.com/code-payments/code-server/pkg/code/exchangerate"
"github.com/code-payments/code-server/pkg/code/limit"
currency_lib "github.com/code-payments/code-server/pkg/currency"
"github.com/code-payments/code-server/pkg/grpc/client"
"github.com/code-payments/code-server/pkg/kin"
)
func (s *transactionServer) GetLimits(ctx context.Context, req *transactionpb.GetLimitsRequest) (*transactionpb.GetLimitsResponse, error) {
log := s.log.WithField("method", "GetLimits")
log = client.InjectLoggingMetadata(ctx, log)
ownerAccount, err := common.NewAccountFromProto(req.Owner)
if err != nil {
log.WithError(err).Warn("invalid owner account")
return nil, status.Error(codes.Internal, "")
}
log = log.WithField("owner_account", ownerAccount.PublicKey().ToBase58())
sig := req.Signature
req.Signature = nil
if err := s.auth.Authenticate(ctx, ownerAccount, req, sig); err != nil {
return nil, err
}
zeroSendLimits := make(map[string]*transactionpb.SendLimit)
zeroMicroPaymentLimits := make(map[string]*transactionpb.MicroPaymentLimit)
zeroBuyModuleLimits := make(map[string]*transactionpb.BuyModuleLimit)
for currency := range limit.SendLimits {
zeroSendLimits[string(currency)] = &transactionpb.SendLimit{
NextTransaction: 0,
MaxPerTransaction: 0,
MaxPerDay: 0,
}
zeroBuyModuleLimits[string(currency)] = &transactionpb.BuyModuleLimit{
MaxPerTransaction: 0,
MinPerTransaction: 0,
}
}
for currency := range limit.MicroPaymentLimits {
zeroMicroPaymentLimits[string(currency)] = &transactionpb.MicroPaymentLimit{
MaxPerTransaction: 0,
MinPerTransaction: 0,
}
}
zeroSendLimits[string(currency_lib.KIN)] = &transactionpb.SendLimit{
NextTransaction: 0,
MaxPerTransaction: 0,
MaxPerDay: 0,
}
zeroMicroPaymentLimits[string(currency_lib.KIN)] = &transactionpb.MicroPaymentLimit{
MaxPerTransaction: 0,
MinPerTransaction: 0,
}
zeroResp := &transactionpb.GetLimitsResponse{
Result: transactionpb.GetLimitsResponse_OK,
SendLimitsByCurrency: zeroSendLimits,
MicroPaymentLimitsByCurrency: zeroMicroPaymentLimits,
BuyModuleLimitsByCurrency: zeroBuyModuleLimits,
DepositLimit: &transactionpb.DepositLimit{
MaxQuarks: 0,
},
}
verificationRecord, err := s.data.GetLatestPhoneVerificationForAccount(ctx, ownerAccount.PublicKey().ToBase58())
if err == phone.ErrVerificationNotFound {
// We've detected an account that's not verified, so set all limits to 0
return zeroResp, nil
} else if err != nil {
log.WithError(err).Warn("failure getting phone verification record")
return nil, status.Error(codes.Internal, "")
}
_, consumedUsdForPayments, err := s.data.GetTransactedAmountForAntiMoneyLaundering(ctx, verificationRecord.PhoneNumber, req.ConsumedSince.AsTime())
if err != nil {
log.WithError(err).Warn("failure calculating consumed usd payment value")
return nil, status.Error(codes.Internal, "")
}
_, consumedUsdForDeposits, err := s.data.GetDepositedAmountForAntiMoneyLaundering(ctx, verificationRecord.PhoneNumber, req.ConsumedSince.AsTime())
if err != nil {
log.WithError(err).Warn("failure calculating consumed usd payment value")
return nil, status.Error(codes.Internal, "")
}
privateBalance, err := balance.GetPrivateBalance(ctx, s.data, ownerAccount)
if err == balance.ErrNotManagedByCode {
// We've detected an account that's not managed by code, so set all limits to 0
return zeroResp, nil
} else if err != nil {
log.WithError(err).Warn("failure calculating owner account private balance")
return nil, status.Error(codes.Internal, "")
}
multiRateRecord, err := s.data.GetAllExchangeRates(ctx, exchange_rate_util.GetLatestExchangeRateTime())
if err != nil {
log.WithError(err).Warn("failure getting current exchange rates")
return nil, status.Error(codes.Internal, "")
}
usdRate, ok := multiRateRecord.Rates[string(currency_lib.USD)]
if !ok {
log.WithError(err).Warn("usd rate is missing")
return nil, status.Error(codes.Internal, "")
}
//
// Part 1: Calculate send limits
//
sendLimits := make(map[string]*transactionpb.SendLimit)
for currency, sendLimit := range limit.SendLimits {
otherRate, ok := multiRateRecord.Rates[string(currency)]
if !ok {
log.WithError(err).Warnf("%s rate is missing", currency)
continue
}
// How much have we consumed in the other currency?
consumedInOtherCurrency := consumedUsdForPayments * otherRate / usdRate
// How much of the daily limit is remaining?
remainingDaily := sendLimit.Daily - consumedInOtherCurrency
// The per-transaction limit applies up until our remaining daily limit is below it.
remainingNextTransaction := sendLimit.PerTransaction
if remainingDaily < remainingNextTransaction {
remainingNextTransaction = remainingDaily
}
// Avoid negative limits, possibly caused by fluctuating exchange rates
if remainingNextTransaction < 0 {
remainingNextTransaction = 0
}
sendLimits[string(currency)] = &transactionpb.SendLimit{
NextTransaction: float32(remainingNextTransaction),
MaxPerTransaction: float32(sendLimit.PerTransaction),
MaxPerDay: float32(sendLimit.Daily),
}
}
usdSendLimits := sendLimits[string(currency_lib.USD)]
// Inject a Kin limit based on the remaining USD amount and rate
sendLimits[string(currency_lib.KIN)] = &transactionpb.SendLimit{
NextTransaction: usdSendLimits.NextTransaction / float32(usdRate),
MaxPerTransaction: usdSendLimits.MaxPerTransaction / float32(usdRate),
MaxPerDay: usdSendLimits.MaxPerDay / float32(usdRate),
}
//
// Part 2: Calculate micro payment limits
//
microPaymentLimits := make(map[string]*transactionpb.MicroPaymentLimit)
for currency, limits := range limit.MicroPaymentLimits {
microPaymentLimits[string(currency)] = &transactionpb.MicroPaymentLimit{
MaxPerTransaction: float32(limits.Max),
MinPerTransaction: float32(limits.Min),
}
}
//
// Part 3: Calculate buy module limits
//
buyModuleLimits := make(map[string]*transactionpb.BuyModuleLimit)
for currency, limits := range limit.SendLimits {
buyModuleLimits[string(currency)] = &transactionpb.BuyModuleLimit{
MaxPerTransaction: float32(limits.PerTransaction),
MinPerTransaction: float32(limits.PerTransaction / 10),
}
}
//
// Part 4: Calculate deposit limits
//
usdForNextDeposit := limit.MaxPerDepositUsdAmount
// Does the user already have sufficient balance in their organizer? If so,
// then the limit is completely nullified.
maxPerDepositQuarkAmount := kin.ToQuarks(uint64(limit.MaxPerDepositUsdAmount / usdRate))
if privateBalance >= maxPerDepositQuarkAmount {
usdForNextDeposit = 0
}
// How much of the daily limit is remaining?
remainingUsdForDeposits := limit.MaxDailyDepositUsdAmount - consumedUsdForDeposits
// The per-transaction limit applies up until our remaining daily limit is below it.
if remainingUsdForDeposits < usdForNextDeposit {
usdForNextDeposit = remainingUsdForDeposits
}
// Avoid negative limits, possibly caused by fluctuating exchange rates
if usdForNextDeposit < 0 {
usdForNextDeposit = 0
}
return &transactionpb.GetLimitsResponse{
Result: transactionpb.GetLimitsResponse_OK,
SendLimitsByCurrency: sendLimits,
MicroPaymentLimitsByCurrency: microPaymentLimits,
BuyModuleLimitsByCurrency: buyModuleLimits,
DepositLimit: &transactionpb.DepositLimit{
MaxQuarks: kin.ToQuarks(uint64(usdForNextDeposit / usdRate)),
},
}, nil
}