-
Notifications
You must be signed in to change notification settings - Fork 4
/
history.go
289 lines (250 loc) · 10.2 KB
/
history.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
package transaction_v2
import (
"context"
"math"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/types/known/timestamppb"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
transactionpb "github.com/code-payments/code-protobuf-api/generated/go/transaction/v2"
"github.com/code-payments/code-server/pkg/code/common"
"github.com/code-payments/code-server/pkg/code/data/intent"
"github.com/code-payments/code-server/pkg/currency"
"github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/grpc/client"
"github.com/code-payments/code-server/pkg/kin"
)
const maxHistoryPageSize = 100
func (s *transactionServer) GetPaymentHistory(ctx context.Context, req *transactionpb.GetPaymentHistoryRequest) (*transactionpb.GetPaymentHistoryResponse, error) {
log := s.log.WithField("method", "GetPaymentHistory")
log = client.InjectLoggingMetadata(ctx, log)
owner, 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", owner.PublicKey().ToBase58())
signature := req.Signature
req.Signature = nil
if err := s.auth.Authenticate(ctx, owner, req, signature); err != nil {
return nil, err
}
// Get the number of results that should be returned
var limit uint64
if req.PageSize > 0 {
limit = uint64(req.PageSize)
} else {
limit = maxHistoryPageSize
}
// Enforce proto limit
if limit > maxHistoryPageSize {
limit = maxHistoryPageSize
}
// Convert the proto ordering type to the internal type
var direction query.Ordering
if req.Direction == transactionpb.GetPaymentHistoryRequest_ASC {
direction = query.Ascending
} else {
direction = query.Descending
}
// Convert the proto cursor type to our native uint64 cursor (ID)
var cursor query.Cursor
if req.Cursor != nil {
cursor = req.Cursor.Value
} else {
cursor = query.ToCursor(0)
if direction == query.Descending {
cursor = query.ToCursor(math.MaxInt64 - 1)
}
}
// Get all intents for the owner as both a source and destination
intentRecords, err := s.data.GetAllIntentsByOwner(
ctx,
owner.PublicKey().ToBase58(),
query.WithLimit(limit),
query.WithDirection(direction),
query.WithCursor(cursor),
)
if err == intent.ErrIntentNotFound {
return &transactionpb.GetPaymentHistoryResponse{
Result: transactionpb.GetPaymentHistoryResponse_NOT_FOUND,
}, nil
} else if err != nil {
log.WithError(err).Warn("failure querying intent records")
return nil, status.Error(codes.Internal, "")
}
var items []*transactionpb.PaymentHistoryItem
for _, intentRecord := range intentRecords {
var paymentType transactionpb.PaymentHistoryItem_PaymentType
var isDeposit, isWithdrawal, isRemoteSend, isReturned, isAirdrop, isMicroPayment bool
var exchangeData *transactionpb.ExchangeData
var airdropType transactionpb.AirdropType
intentId, err := common.NewAccountFromPublicKeyString(intentRecord.IntentId)
if err != nil {
// Some legacy intent IDs are not public keys, so generate a random one.
// This is fine since this RPC is deprecated, and we are only concerned
// about support use cases for newly created payments.
intentId, err = common.NewRandomAccount()
if err != nil {
log.WithError(err).Warn("failure generating intent id")
return nil, status.Error(codes.Internal, "")
}
}
// Extract payment details from intents, where applicable, into a view
// that makes sense for a user.
switch intentRecord.IntentType {
case intent.SendPrivatePayment:
paymentType = transactionpb.PaymentHistoryItem_SEND
isRemoteSend = intentRecord.SendPrivatePaymentMetadata.IsRemoteSend
isWithdrawal = intentRecord.SendPrivatePaymentMetadata.IsWithdrawal
isDeposit = false
isMicroPayment = intentRecord.SendPrivatePaymentMetadata.IsMicroPayment
if intentRecord.InitiatorOwnerAccount != owner.PublicKey().ToBase58() {
paymentType = transactionpb.PaymentHistoryItem_RECEIVE
isWithdrawal = false
isDeposit = intentRecord.SendPrivatePaymentMetadata.IsWithdrawal
}
// Default to a receive if this is a micro payment within the same owner
if isMicroPayment && intentRecord.SendPrivatePaymentMetadata.DestinationOwnerAccount == owner.PublicKey().ToBase58() {
paymentType = transactionpb.PaymentHistoryItem_RECEIVE
isWithdrawal = false
isDeposit = intentRecord.SendPrivatePaymentMetadata.IsWithdrawal
}
// Funds moving within the same owner don't get populated when they're
// used to support another payment flow that represents the history item
// (eg. public withdrawals with private top ups)
if isWithdrawal && intentRecord.InitiatorOwnerAccount == intentRecord.SendPrivatePaymentMetadata.DestinationOwnerAccount && !isMicroPayment {
continue
}
// Don't show history items where the user voids the gift card.
if isRemoteSend {
claimedIntent, err := s.data.GetGiftCardClaimedIntent(ctx, intentRecord.SendPrivatePaymentMetadata.DestinationTokenAccount)
if err == nil && claimedIntent.ReceivePaymentsPubliclyMetadata.IsIssuerVoidingGiftCard {
continue
} else if err != nil && err != intent.ErrIntentNotFound {
log.WithError(err).Warn("failure getting gift card claimed intent")
return nil, status.Error(codes.Internal, "")
}
}
exchangeData = &transactionpb.ExchangeData{
Currency: string(intentRecord.SendPrivatePaymentMetadata.ExchangeCurrency),
ExchangeRate: intentRecord.SendPrivatePaymentMetadata.ExchangeRate,
NativeAmount: intentRecord.SendPrivatePaymentMetadata.NativeAmount,
Quarks: intentRecord.SendPrivatePaymentMetadata.Quantity,
}
case intent.SendPublicPayment:
paymentType = transactionpb.PaymentHistoryItem_SEND
isRemoteSend = false
isWithdrawal = intentRecord.SendPublicPaymentMetadata.IsWithdrawal
isDeposit = false
isMicroPayment = false
if intentRecord.InitiatorOwnerAccount != owner.PublicKey().ToBase58() {
paymentType = transactionpb.PaymentHistoryItem_RECEIVE
isWithdrawal = false
isDeposit = intentRecord.SendPublicPaymentMetadata.IsWithdrawal
}
// Bonus airdrops only occur within Code->Code withdrawal flows
if s.airdropper != nil {
isAirdrop = (intentRecord.InitiatorOwnerAccount == s.airdropper.VaultOwner.PublicKey().ToBase58())
}
if isAirdrop {
// todo: something less hacky
if intentRecord.SendPublicPaymentMetadata.NativeAmount == 5.0 {
airdropType = transactionpb.AirdropType_GIVE_FIRST_KIN
} else if intentRecord.SendPublicPaymentMetadata.NativeAmount == 1.0 {
airdropType = transactionpb.AirdropType_GET_FIRST_KIN
}
}
exchangeData = &transactionpb.ExchangeData{
Currency: string(intentRecord.SendPublicPaymentMetadata.ExchangeCurrency),
ExchangeRate: intentRecord.SendPublicPaymentMetadata.ExchangeRate,
NativeAmount: intentRecord.SendPublicPaymentMetadata.NativeAmount,
Quarks: intentRecord.SendPublicPaymentMetadata.Quantity,
}
case intent.ReceivePaymentsPrivately:
// Other intents account for history items
continue
case intent.MigrateToPrivacy2022:
// Don't show migrations for dust
if intentRecord.MigrateToPrivacy2022Metadata.Quantity < kin.ToQuarks(1) {
continue
}
paymentType = transactionpb.PaymentHistoryItem_RECEIVE
isDeposit = true
exchangeData = &transactionpb.ExchangeData{
Currency: string(currency.KIN),
ExchangeRate: 1.0,
NativeAmount: float64(intentRecord.MigrateToPrivacy2022Metadata.Quantity) / kin.QuarksPerKin,
Quarks: intentRecord.MigrateToPrivacy2022Metadata.Quantity,
}
case intent.ExternalDeposit:
if intentRecord.ExternalDepositMetadata.DestinationOwnerAccount != owner.PublicKey().ToBase58() {
continue
}
// Don't show deposits for dust
if intentRecord.ExternalDepositMetadata.Quantity < kin.ToQuarks(1) {
continue
}
paymentType = transactionpb.PaymentHistoryItem_RECEIVE
isDeposit = true
exchangeData = &transactionpb.ExchangeData{
Currency: string(currency.KIN),
ExchangeRate: 1.0,
NativeAmount: float64(intentRecord.ExternalDepositMetadata.Quantity) / kin.QuarksPerKin,
Quarks: intentRecord.ExternalDepositMetadata.Quantity,
}
case intent.ReceivePaymentsPublicly:
// The intent to create the remote send gift card has no knowledge of the
// destination owner since it's claimed at a later time, so the history item
// must come from the intent receiving it.
if !intentRecord.ReceivePaymentsPubliclyMetadata.IsRemoteSend {
continue
}
// Don't show history items where the user voids the gift card.
if intentRecord.ReceivePaymentsPubliclyMetadata.IsIssuerVoidingGiftCard {
continue
}
paymentType = transactionpb.PaymentHistoryItem_RECEIVE
isRemoteSend = intentRecord.ReceivePaymentsPubliclyMetadata.IsRemoteSend
isReturned = intentRecord.ReceivePaymentsPubliclyMetadata.IsReturned
isWithdrawal = false
isDeposit = false
exchangeData = &transactionpb.ExchangeData{
Currency: string(intentRecord.ReceivePaymentsPubliclyMetadata.OriginalExchangeCurrency),
ExchangeRate: intentRecord.ReceivePaymentsPubliclyMetadata.OriginalExchangeRate,
NativeAmount: intentRecord.ReceivePaymentsPubliclyMetadata.OriginalNativeAmount,
Quarks: intentRecord.ReceivePaymentsPubliclyMetadata.Quantity,
}
default:
continue
}
item := &transactionpb.PaymentHistoryItem{
Cursor: &transactionpb.Cursor{
Value: query.ToCursor(intentRecord.Id),
},
ExchangeData: exchangeData,
PaymentType: paymentType,
IsWithdraw: isWithdrawal,
IsDeposit: isDeposit,
IsRemoteSend: isRemoteSend,
IsReturned: isReturned,
IsAirdrop: isAirdrop,
IsMicroPayment: isMicroPayment,
AirdropType: airdropType,
IntentId: &commonpb.IntentId{
Value: intentId.PublicKey().ToBytes(),
},
Timestamp: timestamppb.New(intentRecord.CreatedAt),
}
items = append(items, item)
}
if len(items) == 0 {
return &transactionpb.GetPaymentHistoryResponse{
Result: transactionpb.GetPaymentHistoryResponse_NOT_FOUND,
}, nil
}
return &transactionpb.GetPaymentHistoryResponse{
Result: transactionpb.GetPaymentHistoryResponse_OK,
Items: items,
}, nil
}