Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 79 additions & 22 deletions pkg/code/async/geyser/external_deposit.go
Original file line number Diff line number Diff line change
Expand Up @@ -327,28 +327,16 @@ func processPotentialExternalDeposit(ctx context.Context, conf *conf, data code_
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")
}
go delayedUsdcDepositProcessing(
ctx,
conf,
data,
pusher,
chatMessageReceiver,
tokenAccount,
signature,
blockTime,
)

syncedDepositCache.Insert(cacheKey, true, 1)

Expand Down Expand Up @@ -647,6 +635,75 @@ func getPurchasesFromSwap(
return purchases, nil
}

func delayedUsdcDepositProcessing(
ctx context.Context,
conf *conf,
data code_data.Provider,
pusher push_lib.Provider,
ownerAccount *common.Account,
tokenAccount *common.Account,
signature string,
blockTime time.Time,
) {
// todo: configurable
time.Sleep(2 * time.Minute)

history, err := data.GetBlockchainHistory(ctx, tokenAccount.PublicKey().ToBase58(), solana.CommitmentFinalized, query.WithLimit(32))
if err != nil {
return
}

var historyToCheck []*solana.TransactionSignature
for _, historyItem := range history {
if base58.Encode(historyItem.Signature[:]) == signature {
break
}

if historyItem.Err == nil {
historyToCheck = append(historyToCheck, historyItem)
}
}

for _, historyItem := range historyToCheck {
tokenBalances, err := data.GetBlockchainTransactionTokenBalances(ctx, base58.Encode(historyItem.Signature[:]))
if err != nil {
continue
}

isCodeSwap, _, _, err := getCodeSwapMetadata(ctx, conf, tokenBalances)
if err != nil {
continue
}

if isCodeSwap {
return
}
}

chatMessage, err := chat_util.ToUsdcDepositedMessage(signature, blockTime)
if err != nil {
return
}

canPush, err := chat_util.SendCodeTeamMessage(ctx, data, ownerAccount, chatMessage)
switch err {
case nil:
if canPush {
push.SendChatMessagePushNotification(
ctx,
data,
pusher,
chat_util.CodeTeamName,
ownerAccount,
chatMessage,
)
}
case chat.ErrMessageAlreadyExists:
default:
return
}
}

// 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.
Expand Down
22 changes: 1 addition & 21 deletions pkg/code/chat/message_code_team.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func ToReferralBonusMessage(intentRecord *intent.Record) (*chatpb.ChatMessage, e

// ToUsdcDepositedMessage turns details of a USDC deposit transaction into a chat
// message to be inserted into the Code Team chat.
func ToUsdcDepositedMessage(signature string, quarks uint64, ts time.Time) (*chatpb.ChatMessage, error) {
func ToUsdcDepositedMessage(signature string, ts time.Time) (*chatpb.ChatMessage, error) {
// todo: Don't have a way of propagating quarks, but that's probably ok since
// this is a temporary message for testing swaps.
content := []*chatpb.Content{
Expand All @@ -59,26 +59,6 @@ func ToUsdcDepositedMessage(signature string, quarks uint64, ts time.Time) (*cha
return newProtoChatMessage(signature, content, ts)
}

// NewUsdcBeingConvertedMessage generates a new message generated upon initiating
// a USDC swap to be inserted into the Code Team chat.
func NewUsdcBeingConvertedMessage() (*chatpb.ChatMessage, error) {
messageId, err := common.NewRandomAccount()
if err != nil {
return nil, err
}

content := []*chatpb.Content{
{
Type: &chatpb.Content_Localized{
Localized: &chatpb.LocalizedContent{
KeyOrText: localization.ChatMessageUsdcBeingConverted,
},
},
},
}
return newProtoChatMessage(messageId.PublicKey().ToBase58(), content, time.Now())
}

// ToKinAvailableForUseMessage turns details of a USDC swap transaction into a
// chat message to be inserted into the Code Team chat.
func ToKinAvailableForUseMessage(signature string, ts time.Time, purchases ...*transactionpb.ExchangeDataWithoutRate) (*chatpb.ChatMessage, error) {
Expand Down
41 changes: 0 additions & 41 deletions pkg/code/server/grpc/transaction/v2/swap.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ import (
transactionpb "github.com/code-payments/code-protobuf-api/generated/go/transaction/v2"

"github.com/code-payments/code-server/pkg/code/balance"
chat_util "github.com/code-payments/code-server/pkg/code/chat"
"github.com/code-payments/code-server/pkg/code/common"
"github.com/code-payments/code-server/pkg/code/data/account"
push_util "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/grpc/client"
"github.com/code-payments/code-server/pkg/jupiter"
Expand All @@ -30,10 +28,6 @@ import (
"github.com/code-payments/code-server/pkg/usdc"
)

var (
swapNotificationTimeByOwner = make(map[string]time.Time)
)

func (s *transactionServer) Swap(streamer transactionpb.Transaction_SwapServer) error {
ctx, cancel := context.WithTimeout(streamer.Context(), s.conf.swapTimeout.Get(streamer.Context()))
defer cancel()
Expand Down Expand Up @@ -370,8 +364,6 @@ func (s *transactionServer) Swap(streamer transactionpb.Transaction_SwapServer)

log.Debug("submitted transaction")

s.bestEffortNotifyUserOfSwapInProgress(ctx, owner)

if !initiateReq.WaitForBlockchainStatus {
err = streamer.Send(&transactionpb.SwapResponse{
Response: &transactionpb.SwapResponse_Success_{
Expand Down Expand Up @@ -417,39 +409,6 @@ func (s *transactionServer) Swap(streamer transactionpb.Transaction_SwapServer)
}
}

// Temporary for manual USDC deposit flow
func (s *transactionServer) bestEffortNotifyUserOfSwapInProgress(ctx context.Context, owner *common.Account) {
// Avoid spamming users chat messages due to retries of the Swap RPC within
// small periods of time. Implementation isn't perfect, but we'll be updating
// notifications later anyways.
lastNotificationTs, ok := swapNotificationTimeByOwner[owner.PublicKey().ToBase58()]
if ok && time.Since(lastNotificationTs) < time.Minute {
return
}
swapNotificationTimeByOwner[owner.PublicKey().ToBase58()] = time.Now()

chatMessage, err := chat_util.NewUsdcBeingConvertedMessage()
if err != nil {
return
}

canPush, err := chat_util.SendCodeTeamMessage(ctx, s.data, owner, chatMessage)
if err != nil {
return
}

if canPush {
push_util.SendChatMessagePushNotification(
ctx,
s.data,
s.pusher,
chat_util.CodeTeamName,
owner,
chatMessage,
)
}
}

func (s *transactionServer) validateSwap(
ctx context.Context,
amountToSwap uint64,
Expand Down