Skip to content

Commit

Permalink
External deposit chat messages for relationship accounts are now dive…
Browse files Browse the repository at this point in the history
…rted to the verified merchant chat
  • Loading branch information
jeffyanta committed Dec 8, 2023
1 parent a193dc1 commit 21f1ccc
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
4 changes: 4 additions & 0 deletions pkg/code/async/geyser/external_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,10 @@ func processPotentialExternalDeposit(ctx context.Context, data code_data.Provide
if err != nil {
return errors.Wrap(err, "error updating cash transactions chat")
}
err = chat_util.SendMerchantExchangeMessage(ctx, data, intentRecord)
if err != nil {
return errors.Wrap(err, "error updating merchant chat")
}

// For tracking in balances
externalDepositRecord := &deposit.Record{
Expand Down
12 changes: 10 additions & 2 deletions pkg/code/chat/message_cash_transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
)

// SendCashTransactionsExchangeMessage sends a message to the Cash Transactions
// chat with exchange data content related to the submitted intent.
// chat with exchange data content related to the submitted intent. Intents that
// don't belong in the Cash Transactions chat will be ignored.
//
// Note: Tests covered in SubmitIntent history tests
//
Expand Down Expand Up @@ -105,7 +106,14 @@ func SendCashTransactionsExchangeMessage(ctx context.Context, data code_data.Pro

case intent.ExternalDeposit:
messageId = strings.Split(messageId, "-")[0]
verbByMessageReceiver[intentRecord.ExternalDepositMetadata.DestinationOwnerAccount] = chatpb.ExchangeDataContent_DEPOSITED
destinationAccountInfoRecord, err := data.GetAccountInfoByTokenAddress(ctx, intentRecord.ExternalDepositMetadata.DestinationTokenAccount)
if err != nil {
return err
} else if destinationAccountInfoRecord.AccountType != commonpb.AccountType_RELATIONSHIP {
// Relationship accounts payments will show up in the verified
// merchant chat
verbByMessageReceiver[intentRecord.ExternalDepositMetadata.DestinationOwnerAccount] = chatpb.ExchangeDataContent_DEPOSITED
}

default:
return nil
Expand Down
27 changes: 21 additions & 6 deletions pkg/code/chat/message_merchant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package chat

import (
"context"
"strings"

"github.com/pkg/errors"

Expand All @@ -15,7 +16,8 @@ import (
)

// SendMerchantExchangeMessage sends a message to the merchant's chat with
// exchange data content related to the submitted intent.
// exchange data content related to the submitted intent. Intents that
// don't belong in the merchant chat will be ignored.
//
// Note: Tests covered in SubmitIntent history tests
func SendMerchantExchangeMessage(ctx context.Context, data code_data.Provider, intentRecord *intent.Record) error {
Expand All @@ -32,7 +34,7 @@ func SendMerchantExchangeMessage(ctx context.Context, data code_data.Provider, i
// but this is the most flexible solution with the chat model.
chatTitle := PaymentsName
chatType := chat.ChatTypeInternal
isVerified := false
isVerifiedChat := false

exchangeData, ok := getExchangeDataFromIntent(intentRecord)
if !ok {
Expand All @@ -51,7 +53,7 @@ func SendMerchantExchangeMessage(ctx context.Context, data code_data.Provider, i
if paymentRequestRecord.Domain != nil {
chatTitle = *paymentRequestRecord.Domain
chatType = chat.ChatTypeExternalApp
isVerified = paymentRequestRecord.IsVerified
isVerifiedChat = paymentRequestRecord.IsVerified
}

verbByMessageReceiver[intentRecord.InitiatorOwnerAccount] = chatpb.ExchangeDataContent_SPENT
Expand All @@ -68,7 +70,7 @@ func SendMerchantExchangeMessage(ctx context.Context, data code_data.Provider, i
// and will have merchant payments appear in the verified merchant
// chat.
chatTitle = *destinationAccountInfoRecord.RelationshipTo
isVerified = true
isVerifiedChat = true
verbByMessageReceiver[intentRecord.SendPrivatePaymentMetadata.DestinationOwnerAccount] = chatpb.ExchangeDataContent_DEPOSITED
}
}
Expand All @@ -84,11 +86,24 @@ func SendMerchantExchangeMessage(ctx context.Context, data code_data.Provider, i
// and will have merchant payments appear in the verified merchant
// chat.
chatTitle = *destinationAccountInfoRecord.RelationshipTo
isVerified = true
isVerifiedChat = true
verbByMessageReceiver[intentRecord.SendPublicPaymentMetadata.DestinationOwnerAccount] = chatpb.ExchangeDataContent_DEPOSITED
}
}
}
case intent.ExternalDeposit:
messageId = strings.Split(messageId, "-")[0]
destinationAccountInfoRecord, err := data.GetAccountInfoByTokenAddress(ctx, intentRecord.ExternalDepositMetadata.DestinationTokenAccount)
if err != nil {
return err
} else if destinationAccountInfoRecord.AccountType == commonpb.AccountType_RELATIONSHIP {
// Relationship accounts only exist against verified merchants,
// and will have merchant payments appear in the verified merchant
// chat.
chatTitle = *destinationAccountInfoRecord.RelationshipTo
isVerifiedChat = true
verbByMessageReceiver[intentRecord.ExternalDepositMetadata.DestinationOwnerAccount] = chatpb.ExchangeDataContent_DEPOSITED
}
default:
return nil
}
Expand Down Expand Up @@ -121,7 +136,7 @@ func SendMerchantExchangeMessage(ctx context.Context, data code_data.Provider, i
data,
chatTitle,
chatType,
isVerified,
isVerifiedChat,
receiver,
protoMessage,
true,
Expand Down
34 changes: 32 additions & 2 deletions pkg/code/server/grpc/transaction/v2/history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,17 @@ func TestPaymentHistory_HappyPath(t *testing.T) {
sendingPhone.resetConfig()
receivingPhone.resetConfig()

// [Cash Transactions] sendingPhone WITHDREW $32.1 USD of Kin
// [Verified Merchant] receivingPhone DEPOSITED $32.1 USD of Kin
sendingPhone.publiclyWithdraw777KinToCodeUserBetweenRelationshipAccounts(t, merchantDomain, receivingPhone).requireSuccess(t)

// [Cash Transactions] sendingPhone WITHDREW $32.1 USD of Kin
// [Verified Merchant] receivingPhone DEPOSITED $32.1 USD of Kin
sendingPhone.privatelyWithdraw321KinToCodeUserRelationshipAccount(t, receivingPhone, merchantDomain).requireSuccess(t)

// [Verified Merchant] receivingPhone DEPOSITED 12,345 Kin
server.simulateExternalDepositHistoryItem(t, receivingPhone.parentAccount, getTimelockVault(t, receivingPhone.getAuthorityForRelationshipAccount(t, merchantDomain)), kin.ToQuarks(12_345))

//
// New chat assertions below
//
Expand Down Expand Up @@ -326,7 +334,7 @@ func TestPaymentHistory_HappyPath(t *testing.T) {

chatMessageRecords, err = server.data.GetAllChatMessages(server.ctx, chat.GetChatId("example.com", receivingPhone.parentAccount.PublicKey().ToBase58(), true))
require.NoError(t, err)
require.Len(t, chatMessageRecords, 4)
require.Len(t, chatMessageRecords, 5)

protoChatMessage = getProtoChatMessage(t, chatMessageRecords[0])
require.Len(t, protoChatMessage.Content, 1)
Expand Down Expand Up @@ -364,6 +372,15 @@ func TestPaymentHistory_HappyPath(t *testing.T) {
assert.Equal(t, 32.1, protoChatMessage.Content[0].GetExchangeData().GetExact().NativeAmount)
assert.Equal(t, kin.ToQuarks(321), protoChatMessage.Content[0].GetExchangeData().GetExact().Quarks)

protoChatMessage = getProtoChatMessage(t, chatMessageRecords[4])
require.Len(t, protoChatMessage.Content, 1)
require.NotNil(t, protoChatMessage.Content[0].GetExchangeData())
assert.Equal(t, chatpb.ExchangeDataContent_DEPOSITED, protoChatMessage.Content[0].GetExchangeData().Verb)
assert.EqualValues(t, currency_lib.KIN, protoChatMessage.Content[0].GetExchangeData().GetExact().Currency)
assert.Equal(t, 1.0, protoChatMessage.Content[0].GetExchangeData().GetExact().ExchangeRate)
assert.Equal(t, 12_345.0, protoChatMessage.Content[0].GetExchangeData().GetExact().NativeAmount)
assert.Equal(t, kin.ToQuarks(12_345), protoChatMessage.Content[0].GetExchangeData().GetExact().Quarks)

chatMessageRecords, err = server.data.GetAllChatMessages(server.ctx, chat.GetChatId("example.com", receivingPhone.parentAccount.PublicKey().ToBase58(), false))
require.NoError(t, err)
require.Len(t, chatMessageRecords, 1)
Expand Down Expand Up @@ -567,7 +584,7 @@ func TestPaymentHistory_HappyPath(t *testing.T) {
assert.False(t, items[13].IsMicroPayment)

items = receivingPhone.getPaymentHistory(t)
require.Len(t, items, 13)
require.Len(t, items, 14)

assert.Equal(t, transactionpb.PaymentHistoryItem_RECEIVE, items[0].PaymentType)
assert.Equal(t, kin.ToQuarks(42), items[0].ExchangeData.Quarks)
Expand Down Expand Up @@ -737,6 +754,19 @@ func TestPaymentHistory_HappyPath(t *testing.T) {
assert.False(t, items[12].IsAirdrop)
assert.Equal(t, transactionpb.AirdropType_UNKNOWN, items[12].AirdropType)
assert.False(t, items[12].IsMicroPayment)

assert.Equal(t, transactionpb.PaymentHistoryItem_RECEIVE, items[13].PaymentType)
assert.Equal(t, kin.ToQuarks(12_345), items[13].ExchangeData.Quarks)
assert.EqualValues(t, currency_lib.KIN, items[13].ExchangeData.Currency)
assert.EqualValues(t, 1.0, items[13].ExchangeData.ExchangeRate)
assert.EqualValues(t, 12_345.0, items[13].ExchangeData.NativeAmount)
assert.False(t, items[13].IsWithdraw)
assert.True(t, items[13].IsDeposit)
assert.False(t, items[13].IsRemoteSend)
assert.False(t, items[13].IsReturned)
assert.False(t, items[13].IsAirdrop)
assert.Equal(t, transactionpb.AirdropType_UNKNOWN, items[13].AirdropType)
assert.False(t, items[13].IsMicroPayment)
}

func TestGetPaymentHistory_NoPayments(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions pkg/code/server/grpc/transaction/v2/testutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,7 @@ func (s *serverTestEnv) simulateExternalDepositHistoryItem(t *testing.T, owner,
require.NoError(t, s.data.SaveIntent(s.ctx, intentRecord))

require.NoError(t, chat_util.SendCashTransactionsExchangeMessage(s.ctx, s.data, intentRecord))
require.NoError(t, chat_util.SendMerchantExchangeMessage(s.ctx, s.data, intentRecord))
}

func (s *serverTestEnv) simulateAllCommitmentsUpgraded(t *testing.T) {
Expand Down

0 comments on commit 21f1ccc

Please sign in to comment.