@@ -16,8 +16,10 @@ import (
16
16
transactionpb "github.com/code-payments/code-protobuf-api/generated/go/transaction/v2"
17
17
18
18
"github.com/code-payments/code-server/pkg/code/balance"
19
+ chat_util "github.com/code-payments/code-server/pkg/code/chat"
19
20
"github.com/code-payments/code-server/pkg/code/common"
20
21
"github.com/code-payments/code-server/pkg/code/data/account"
22
+ push_util "github.com/code-payments/code-server/pkg/code/push"
21
23
currency_lib "github.com/code-payments/code-server/pkg/currency"
22
24
"github.com/code-payments/code-server/pkg/grpc/client"
23
25
"github.com/code-payments/code-server/pkg/jupiter"
@@ -28,6 +30,10 @@ import (
28
30
"github.com/code-payments/code-server/pkg/usdc"
29
31
)
30
32
33
+ var (
34
+ swapNotificationTimeByOwner = make (map [string ]time.Time )
35
+ )
36
+
31
37
func (s * transactionServer ) Swap (streamer transactionpb.Transaction_SwapServer ) error {
32
38
ctx , cancel := context .WithTimeout (streamer .Context (), s .conf .swapTimeout .Get (streamer .Context ()))
33
39
defer cancel ()
@@ -364,6 +370,8 @@ func (s *transactionServer) Swap(streamer transactionpb.Transaction_SwapServer)
364
370
365
371
log .Debug ("submitted transaction" )
366
372
373
+ s .bestEffortNotifyUserOfSwapInProgress (ctx , owner )
374
+
367
375
if ! initiateReq .WaitForBlockchainStatus {
368
376
err = streamer .Send (& transactionpb.SwapResponse {
369
377
Response : & transactionpb.SwapResponse_Success_ {
@@ -475,6 +483,38 @@ func (s *transactionServer) validateSwap(
475
483
return nil
476
484
}
477
485
486
+ func (s * transactionServer ) bestEffortNotifyUserOfSwapInProgress (ctx context.Context , owner * common.Account ) {
487
+ // Avoid spamming users chat messages due to retries of the Swap RPC within
488
+ // small periods of time. Implementation isn't perfect, but we'll be updating
489
+ // notifications later anyways.
490
+ lastNotificationTs , ok := swapNotificationTimeByOwner [owner .PublicKey ().ToBase58 ()]
491
+ if ok && time .Since (lastNotificationTs ) < time .Minute {
492
+ return
493
+ }
494
+ swapNotificationTimeByOwner [owner .PublicKey ().ToBase58 ()] = time .Now ()
495
+
496
+ chatMessage , err := chat_util .NewUsdcBeingConvertedMessage ()
497
+ if err != nil {
498
+ return
499
+ }
500
+
501
+ canPush , err := chat_util .SendKinPurchasesMessage (ctx , s .data , owner , chatMessage )
502
+ if err != nil {
503
+ return
504
+ }
505
+
506
+ if canPush {
507
+ push_util .SendChatMessagePushNotification (
508
+ ctx ,
509
+ s .data ,
510
+ s .pusher ,
511
+ chat_util .KinPurchasesName ,
512
+ owner ,
513
+ chatMessage ,
514
+ )
515
+ }
516
+ }
517
+
478
518
func (s * transactionServer ) mustLoadSwapSubsidizer (ctx context.Context ) {
479
519
log := s .log .WithFields (logrus.Fields {
480
520
"method" : "mustLoadSwapSubsidizer" ,
0 commit comments