Skip to content

Commit 17e74ce

Browse files
authored
Re-introduce message sent on USDC swap (#87)
1 parent 4e2f550 commit 17e74ce

File tree

2 files changed

+60
-0
lines changed

2 files changed

+60
-0
lines changed

pkg/code/chat/message_kin_purchases.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ func ToUsdcDepositedMessage(signature string, ts time.Time) (*chatpb.ChatMessage
4444
return newProtoChatMessage(signature, content, ts)
4545
}
4646

47+
// NewUsdcBeingConvertedMessage generates a new message generated upon initiating
48+
// a USDC swap to be inserted into the Kin Purchases chat.
49+
func NewUsdcBeingConvertedMessage() (*chatpb.ChatMessage, error) {
50+
messageId, err := common.NewRandomAccount()
51+
if err != nil {
52+
return nil, err
53+
}
54+
55+
content := []*chatpb.Content{
56+
{
57+
Type: &chatpb.Content_Localized{
58+
Localized: &chatpb.LocalizedContent{
59+
KeyOrText: localization.ChatMessageUsdcBeingConverted,
60+
},
61+
},
62+
},
63+
}
64+
return newProtoChatMessage(messageId.PublicKey().ToBase58(), content, time.Now())
65+
}
66+
4767
// ToKinAvailableForUseMessage turns details of a USDC swap transaction into a
4868
// chat message to be inserted into the Kin Purchases chat.
4969
func ToKinAvailableForUseMessage(signature string, ts time.Time, purchases ...*transactionpb.ExchangeDataWithoutRate) (*chatpb.ChatMessage, error) {

pkg/code/server/grpc/transaction/v2/swap.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@ import (
1616
transactionpb "github.com/code-payments/code-protobuf-api/generated/go/transaction/v2"
1717

1818
"github.com/code-payments/code-server/pkg/code/balance"
19+
chat_util "github.com/code-payments/code-server/pkg/code/chat"
1920
"github.com/code-payments/code-server/pkg/code/common"
2021
"github.com/code-payments/code-server/pkg/code/data/account"
22+
push_util "github.com/code-payments/code-server/pkg/code/push"
2123
currency_lib "github.com/code-payments/code-server/pkg/currency"
2224
"github.com/code-payments/code-server/pkg/grpc/client"
2325
"github.com/code-payments/code-server/pkg/jupiter"
@@ -28,6 +30,10 @@ import (
2830
"github.com/code-payments/code-server/pkg/usdc"
2931
)
3032

33+
var (
34+
swapNotificationTimeByOwner = make(map[string]time.Time)
35+
)
36+
3137
func (s *transactionServer) Swap(streamer transactionpb.Transaction_SwapServer) error {
3238
ctx, cancel := context.WithTimeout(streamer.Context(), s.conf.swapTimeout.Get(streamer.Context()))
3339
defer cancel()
@@ -364,6 +370,8 @@ func (s *transactionServer) Swap(streamer transactionpb.Transaction_SwapServer)
364370

365371
log.Debug("submitted transaction")
366372

373+
s.bestEffortNotifyUserOfSwapInProgress(ctx, owner)
374+
367375
if !initiateReq.WaitForBlockchainStatus {
368376
err = streamer.Send(&transactionpb.SwapResponse{
369377
Response: &transactionpb.SwapResponse_Success_{
@@ -475,6 +483,38 @@ func (s *transactionServer) validateSwap(
475483
return nil
476484
}
477485

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+
478518
func (s *transactionServer) mustLoadSwapSubsidizer(ctx context.Context) {
479519
log := s.log.WithFields(logrus.Fields{
480520
"method": "mustLoadSwapSubsidizer",

0 commit comments

Comments
 (0)