Skip to content

Commit 9af41d1

Browse files
authored
Avoid sending the swap in progress chat message if the USDC deposited message wasn't sent (#88)
1 parent 17e74ce commit 9af41d1

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

pkg/code/chat/message_kin_purchases.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ import (
1515
"github.com/code-payments/code-server/pkg/code/localization"
1616
)
1717

18+
// GetKinPurchasesChatId returns the chat ID for the Kin Purchases chat for a
19+
// given owner account
20+
func GetKinPurchasesChatId(owner *common.Account) chat.ChatId {
21+
return chat.GetChatId(KinPurchasesName, owner.PublicKey().ToBase58(), true)
22+
}
23+
1824
// SendKinPurchasesMessage sends a message to the Kin Purchases chat.
1925
func SendKinPurchasesMessage(ctx context.Context, data code_data.Provider, receiver *common.Account, chatMessage *chatpb.ChatMessage) (bool, error) {
2026
return SendChatMessage(

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,21 @@ import (
1111
"github.com/sirupsen/logrus"
1212
"google.golang.org/grpc/codes"
1313
"google.golang.org/grpc/status"
14+
"google.golang.org/protobuf/proto"
1415

16+
chatpb "github.com/code-payments/code-protobuf-api/generated/go/chat/v1"
1517
commonpb "github.com/code-payments/code-protobuf-api/generated/go/common/v1"
1618
transactionpb "github.com/code-payments/code-protobuf-api/generated/go/transaction/v2"
1719

1820
"github.com/code-payments/code-server/pkg/code/balance"
1921
chat_util "github.com/code-payments/code-server/pkg/code/chat"
2022
"github.com/code-payments/code-server/pkg/code/common"
2123
"github.com/code-payments/code-server/pkg/code/data/account"
24+
"github.com/code-payments/code-server/pkg/code/data/chat"
25+
"github.com/code-payments/code-server/pkg/code/localization"
2226
push_util "github.com/code-payments/code-server/pkg/code/push"
2327
currency_lib "github.com/code-payments/code-server/pkg/currency"
28+
"github.com/code-payments/code-server/pkg/database/query"
2429
"github.com/code-payments/code-server/pkg/grpc/client"
2530
"github.com/code-payments/code-server/pkg/jupiter"
2631
"github.com/code-payments/code-server/pkg/kin"
@@ -493,6 +498,31 @@ func (s *transactionServer) bestEffortNotifyUserOfSwapInProgress(ctx context.Con
493498
}
494499
swapNotificationTimeByOwner[owner.PublicKey().ToBase58()] = time.Now()
495500

501+
chatId := chat_util.GetKinPurchasesChatId(owner)
502+
503+
// Inspect the chat history for a USDC deposited message. If that message
504+
// doesn't exist, then avoid sending the swap in progress chat message, since
505+
// it can lead to user confusion.
506+
chatMessageRecords, err := s.data.GetAllChatMessages(ctx, chatId, query.WithDirection(query.Descending), query.WithLimit(1))
507+
switch err {
508+
case nil:
509+
var protoChatMessage chatpb.ChatMessage
510+
err := proto.Unmarshal(chatMessageRecords[0].Data, &protoChatMessage)
511+
if err != nil {
512+
return
513+
}
514+
515+
switch typed := protoChatMessage.Content[0].Type.(type) {
516+
case *chatpb.Content_Localized:
517+
if typed.Localized.KeyOrText != localization.ChatMessageUsdcDeposited {
518+
return
519+
}
520+
}
521+
case chat.ErrMessageNotFound:
522+
default:
523+
return
524+
}
525+
496526
chatMessage, err := chat_util.NewUsdcBeingConvertedMessage()
497527
if err != nil {
498528
return

0 commit comments

Comments
 (0)