-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathexternal_deposit.go
472 lines (405 loc) · 15.3 KB
/
external_deposit.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
package async_geyser
import (
"context"
"fmt"
"strconv"
"strings"
"time"
"github.com/mr-tron/base58"
"github.com/pkg/errors"
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
"github.com/code-payments/code-server/pkg/cache"
chat_util "github.com/code-payments/code-server/pkg/code/chat"
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
"github.com/code-payments/code-server/pkg/code/data/balance"
"github.com/code-payments/code-server/pkg/code/data/chat"
"github.com/code-payments/code-server/pkg/code/data/deposit"
"github.com/code-payments/code-server/pkg/code/data/fulfillment"
"github.com/code-payments/code-server/pkg/code/data/intent"
"github.com/code-payments/code-server/pkg/code/data/transaction"
"github.com/code-payments/code-server/pkg/code/push"
currency_lib "github.com/code-payments/code-server/pkg/currency"
"github.com/code-payments/code-server/pkg/database/query"
"github.com/code-payments/code-server/pkg/kin"
push_lib "github.com/code-payments/code-server/pkg/push"
"github.com/code-payments/code-server/pkg/retry"
"github.com/code-payments/code-server/pkg/solana"
"github.com/code-payments/code-server/pkg/usdc"
)
const (
codeMemoValue = "ZTAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA="
)
var (
syncedDepositCache = cache.NewCache(1_000_000)
)
func fixMissingExternalDeposits(ctx context.Context, conf *conf, data code_data.Provider, pusher push_lib.Provider, vault *common.Account) error {
signatures, err := findPotentialExternalDeposits(ctx, data, vault)
if err != nil {
return errors.Wrap(err, "error finding potential external deposits")
}
var anyError error
for _, signature := range signatures {
err := processPotentialExternalDeposit(ctx, conf, data, pusher, signature, vault)
if err != nil {
anyError = errors.Wrap(err, "error processing signature for external deposit")
}
}
if anyError != nil {
return anyError
}
return markDepositsAsSynced(ctx, data, vault)
}
// Note: This puts an upper bound to how far back in history we'll search
//
// todo: We can track the furthest succesful signature, so we have a bound.
// This would also enable us to not reprocess transactions. We'll need to be
// careful to check commitment status, since GetBlockchainHistory could return
// non-finalized transactions.
func findPotentialExternalDeposits(ctx context.Context, data code_data.Provider, vault *common.Account) ([]string, error) {
var res []string
var cursor []byte
var totalTransactionsFound int
for {
history, err := data.GetBlockchainHistory(
ctx,
vault.PublicKey().ToBase58(),
solana.CommitmentConfirmed, // Get signatures faster, which is ok because we'll fetch finalized txn data
query.WithLimit(1_000), // Max supported
query.WithCursor(cursor),
)
if err != nil {
return nil, errors.Wrap(err, "error getting signatures for address")
}
if len(history) == 0 {
return res, nil
}
for _, historyItem := range history {
// If there's a Code memo, then it isn't an external deposit
if historyItem.Memo != nil && strings.Contains(*historyItem.Memo, codeMemoValue) {
continue
}
// Transaction has an error, so we cannot add its funds
if historyItem.Err != nil {
continue
}
res = append(res, base58.Encode(historyItem.Signature[:]))
// Bound total results
if len(res) >= 100 {
return res, nil
}
}
// Bound total history to look for in the past
totalTransactionsFound += len(history)
if totalTransactionsFound >= 10_000 {
return res, nil
}
cursor = query.Cursor(history[len(history)-1].Signature[:])
}
}
func processPotentialExternalDeposit(ctx context.Context, conf *conf, data code_data.Provider, pusher push_lib.Provider, signature string, tokenAccount *common.Account) error {
// Avoid reprocessing deposits we've recently seen and processed. Particularly,
// the backup process will likely be triggered in frequent bursts, so this is
// just an optimization around that.
cacheKey := getSyncedDepositCacheKey(signature, tokenAccount)
_, ok := syncedDepositCache.Retrieve(cacheKey)
if ok {
return nil
}
decodedSignature, err := base58.Decode(signature)
if err != nil {
return errors.Wrap(err, "invalid signature")
}
var typedSignature solana.Signature
copy(typedSignature[:], decodedSignature)
// Is this transaction a fulfillment? If so, it cannot be an external deposit.
_, err = data.GetFulfillmentBySignature(ctx, signature)
if err == nil {
return nil
} else if err != fulfillment.ErrFulfillmentNotFound {
return errors.Wrap(err, "error getting fulfillment record")
}
// Grab transaction token balances to get net quark balances from this transaction.
// This enables us to avoid parsing transaction data and generically handle any
// kind of transaction. It's far too complicated if we need to inspect individual
// instructions.
var tokenBalances *solana.TransactionTokenBalances
_, err = retry.Retry(
func() error {
tokenBalances, err = data.GetBlockchainTransactionTokenBalances(ctx, signature)
return err
},
waitForFinalizationRetryStrategies...,
)
if err != nil {
return errors.Wrap(err, "error getting transaction token balances")
}
// Check whether the Code subsidizer was involved in this transaction. If it is, then
// it cannot be an external deposit.
for _, account := range tokenBalances.Accounts {
if account == common.GetSubsidizer().PublicKey().ToBase58() {
return nil
}
}
deltaQuarks, err := getDeltaQuarksFromTokenBalances(tokenAccount, tokenBalances)
if err != nil {
return errors.Wrap(err, "error getting delta quarks from token balances")
}
// Transaction did not positively affect token account balance, so no new funds
// were externally deposited into the account.
if deltaQuarks <= 0 {
return nil
}
accountInfoRecord, err := data.GetAccountInfoByTokenAddress(ctx, tokenAccount.PublicKey().ToBase58())
if err != nil {
return errors.Wrap(err, "error getting account info record")
}
chatMessageReceiver, err := common.NewAccountFromPublicKeyString(accountInfoRecord.OwnerAccount)
if err != nil {
return errors.Wrap(err, "invalid owner account")
}
blockTime := time.Now()
if tokenBalances.BlockTime != nil {
blockTime = *tokenBalances.BlockTime
}
// Use the account type to determine how we'll process this external deposit
//
// todo: Below logic is beginning to get messy and might be in need of a
// refactor soon
switch accountInfoRecord.AccountType {
case commonpb.AccountType_PRIMARY, commonpb.AccountType_RELATIONSHIP:
// Check whether we've previously processed this external deposit
_, err = data.GetExternalDeposit(ctx, signature, tokenAccount.PublicKey().ToBase58())
if err == nil {
syncedDepositCache.Insert(cacheKey, true, 1)
return nil
}
isCodeSwap, usdcSwapAccount, usdcQuarksSwapped, err := getCodeSwapMetadata(ctx, conf, tokenBalances)
if err != nil {
return errors.Wrap(err, "error getting code swap metadata")
}
var usdMarketValue float64
if isCodeSwap {
usdMarketValue = float64(usdcQuarksSwapped) / float64(usdc.QuarksPerUsdc)
} else {
usdExchangeRecord, err := data.GetExchangeRate(ctx, currency_lib.USD, time.Now())
if err != nil {
return errors.Wrap(err, "error getting usd rate")
}
usdMarketValue = usdExchangeRecord.Rate * float64(deltaQuarks) / float64(kin.QuarksPerKin)
}
if isCodeSwap {
// Checkpoint the Code swap account balance, to minimize chances a
// stale RPC node results in a double counting of funds
bestEffortCacheExternalAccountBalance(ctx, data, usdcSwapAccount, tokenBalances)
}
// For a consistent payment history list
//
// Deprecated in favour of chats (for history purposes)
intentRecord := &intent.Record{
IntentId: fmt.Sprintf("%s-%s", signature, tokenAccount.PublicKey().ToBase58()),
IntentType: intent.ExternalDeposit,
InitiatorOwnerAccount: tokenBalances.Accounts[0], // The fee payer
ExternalDepositMetadata: &intent.ExternalDepositMetadata{
DestinationOwnerAccount: accountInfoRecord.OwnerAccount,
DestinationTokenAccount: tokenAccount.PublicKey().ToBase58(),
Quantity: uint64(deltaQuarks),
UsdMarketValue: usdMarketValue,
},
State: intent.StateConfirmed,
CreatedAt: time.Now(),
}
err = data.SaveIntent(ctx, intentRecord)
if err != nil {
return errors.Wrap(err, "error saving intent record")
}
if isCodeSwap {
chatMessage, err := chat_util.ToKinAvailableForUseMessage(signature, usdcQuarksSwapped, blockTime)
if err != nil {
return errors.Wrap(err, "error creating chat message")
}
canPush, err := chat_util.SendCodeTeamMessage(ctx, data, chatMessageReceiver, chatMessage)
switch err {
case nil:
if canPush {
push.SendChatMessagePushNotification(
ctx,
data,
pusher,
chat_util.CodeTeamName,
chatMessageReceiver,
chatMessage,
)
}
case chat.ErrMessageAlreadyExists:
default:
return errors.Wrap(err, "error sending chat message")
}
} else {
err = chat_util.SendCashTransactionsExchangeMessage(ctx, data, intentRecord)
if err != nil {
return errors.Wrap(err, "error updating cash transactions chat")
}
_, err = chat_util.SendMerchantExchangeMessage(ctx, data, intentRecord, nil)
if err != nil {
return errors.Wrap(err, "error updating merchant chat")
}
push.SendDepositPushNotification(ctx, data, pusher, tokenAccount, uint64(deltaQuarks))
}
// For tracking in balances
externalDepositRecord := &deposit.Record{
Signature: signature,
Destination: tokenAccount.PublicKey().ToBase58(),
Amount: uint64(deltaQuarks),
UsdMarketValue: usdMarketValue,
Slot: tokenBalances.Slot,
ConfirmationState: transaction.ConfirmationFinalized,
CreatedAt: time.Now(),
}
err = data.SaveExternalDeposit(ctx, externalDepositRecord)
if err != nil {
return errors.Wrap(err, "error creating external deposit record")
}
syncedDepositCache.Insert(cacheKey, true, 1)
return nil
case commonpb.AccountType_SWAP:
bestEffortCacheExternalAccountBalance(ctx, data, tokenAccount, tokenBalances)
chatMessage, err := chat_util.ToUsdcDepositedMessage(signature, uint64(deltaQuarks), blockTime)
if err != nil {
return errors.Wrap(err, "error creating chat message")
}
canPush, err := chat_util.SendCodeTeamMessage(ctx, data, chatMessageReceiver, chatMessage)
switch err {
case nil:
if canPush {
push.SendChatMessagePushNotification(
ctx,
data,
pusher,
chat_util.CodeTeamName,
chatMessageReceiver,
chatMessage,
)
}
case chat.ErrMessageAlreadyExists:
default:
return errors.Wrap(err, "error sending chat message")
}
syncedDepositCache.Insert(cacheKey, true, 1)
return nil
default:
// Mark anything other than deposit or swap accounts as synced and move on without
// saving anything. There's a potential someone could overutilize our treasury
// by depositing large sums into temporary or bucket accounts, which have
// more lenient checks ATM. We'll deal with these adhoc as they arise.
syncedDepositCache.Insert(cacheKey, true, 1)
return nil
}
}
func markDepositsAsSynced(ctx context.Context, data code_data.Provider, vault *common.Account) error {
accountInfoRecord, err := data.GetAccountInfoByTokenAddress(ctx, vault.PublicKey().ToBase58())
if err != nil {
return errors.Wrap(err, "error getting account info record")
}
accountInfoRecord.RequiresDepositSync = false
accountInfoRecord.DepositsLastSyncedAt = time.Now()
err = data.UpdateAccountInfo(ctx, accountInfoRecord)
if err != nil {
return errors.Wrap(err, "error updating account info record")
}
return nil
}
// todo: can be promoted more broadly
func getDeltaQuarksFromTokenBalances(tokenAccount *common.Account, tokenBalances *solana.TransactionTokenBalances) (int64, error) {
var preQuarkBalance, postQuarkBalance int64
var err error
for _, tokenBalance := range tokenBalances.PreTokenBalances {
if tokenBalances.Accounts[tokenBalance.AccountIndex] == tokenAccount.PublicKey().ToBase58() {
preQuarkBalance, err = strconv.ParseInt(tokenBalance.TokenAmount.Amount, 10, 64)
if err != nil {
return 0, errors.Wrap(err, "error parsing pre token balance")
}
break
}
}
for _, tokenBalance := range tokenBalances.PostTokenBalances {
if tokenBalances.Accounts[tokenBalance.AccountIndex] == tokenAccount.PublicKey().ToBase58() {
postQuarkBalance, err = strconv.ParseInt(tokenBalance.TokenAmount.Amount, 10, 64)
if err != nil {
return 0, errors.Wrap(err, "error parsing post token balance")
}
break
}
}
return postQuarkBalance - preQuarkBalance, nil
}
// todo: can be promoted more broadly
func getPostQuarkBalance(tokenAccount *common.Account, tokenBalances *solana.TransactionTokenBalances) (uint64, error) {
for _, postBalance := range tokenBalances.PostTokenBalances {
if tokenBalances.Accounts[postBalance.AccountIndex] == tokenAccount.PublicKey().ToBase58() {
postQuarkBalance, err := strconv.ParseUint(postBalance.TokenAmount.Amount, 10, 64)
if err != nil {
return 0, errors.Wrap(err, "error parsing post token balance")
}
return postQuarkBalance, nil
}
}
return 0, errors.New("no post balance for account")
}
func getCodeSwapMetadata(ctx context.Context, conf *conf, tokenBalances *solana.TransactionTokenBalances) (bool, *common.Account, uint64, error) {
// Detect whether this is a Code swap by inspecting whether the swap subsidizer
// is included in the transaction.
var isCodeSwap bool
for _, account := range tokenBalances.Accounts {
if account == conf.swapSubsidizerPublicKey.Get(ctx) {
isCodeSwap = true
break
}
}
if !isCodeSwap {
return false, nil, 0, nil
}
var usdcPaid uint64
var usdcAccount *common.Account
for _, tokenBalance := range tokenBalances.PreTokenBalances {
tokenAccount, err := common.NewAccountFromPublicKeyString(tokenBalances.Accounts[tokenBalance.AccountIndex])
if err != nil {
return false, nil, 0, errors.Wrap(err, "invalid token account")
}
if tokenBalance.Mint == common.UsdcMintAccount.PublicKey().ToBase58() {
deltaQuarks, err := getDeltaQuarksFromTokenBalances(tokenAccount, tokenBalances)
if err != nil {
return false, nil, 0, errors.Wrap(err, "error getting delta quarks")
}
if deltaQuarks >= 0 {
continue
}
absDeltaQuarks := uint64(-1 * deltaQuarks)
if absDeltaQuarks > usdcPaid {
usdcPaid = absDeltaQuarks
usdcAccount = tokenAccount
}
}
}
if usdcAccount == nil {
return false, nil, 0, errors.New("usdc account not found")
}
return true, usdcAccount, usdcPaid, nil
}
// Optimistically tries to cache a balance for an external account not managed
// Code. It doesn't need to be perfect and will be lazily corrected on the next
// balance fetch with a newer state returned by a RPC node.
func bestEffortCacheExternalAccountBalance(ctx context.Context, data code_data.Provider, tokenAccount *common.Account, tokenBalances *solana.TransactionTokenBalances) {
postBalance, err := getPostQuarkBalance(tokenAccount, tokenBalances)
if err == nil {
checkpointRecord := &balance.Record{
TokenAccount: tokenAccount.PublicKey().ToBase58(),
Quarks: postBalance,
SlotCheckpoint: tokenBalances.Slot,
}
data.SaveBalanceCheckpoint(ctx, checkpointRecord)
}
}
func getSyncedDepositCacheKey(signature string, account *common.Account) string {
return fmt.Sprintf("%s:%s", signature, account.PublicKey().ToBase58())
}