Skip to content

Commit 5baebbf

Browse files
author
jeffyanta
authored
Capture more buy module metrics even when nonce isn't available (#126)
1 parent d29e93e commit 5baebbf

2 files changed

Lines changed: 55 additions & 28 deletions

File tree

pkg/code/async/geyser/external_deposit.go

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,18 @@ func processPotentialExternalDeposit(ctx context.Context, conf *conf, data code_
270270
return errors.Wrap(err, "error getting swap purchases")
271271
}
272272

273-
chatMessage, err := chat_util.ToKinAvailableForUseMessage(signature, blockTime, purchases...)
273+
var protoPurchases []*transactionpb.ExchangeDataWithoutRate
274+
for _, purchase := range purchases {
275+
protoPurchases = append(protoPurchases, purchase.protoExchangeData)
276+
recordBuyModulePurchaseCompletedEvent(
277+
ctx,
278+
purchase.deviceType,
279+
purchase.purchaseInitiationTime,
280+
purchase.usdcDepositTime,
281+
)
282+
}
283+
284+
chatMessage, err := chat_util.ToKinAvailableForUseMessage(signature, blockTime, protoPurchases...)
274285
if err != nil {
275286
return errors.Wrap(err, "error creating chat message")
276287
}
@@ -491,14 +502,21 @@ func getCodeSwapMetadata(ctx context.Context, conf *conf, tokenBalances *solana.
491502
return true, usdcAccount, usdcPaid, nil
492503
}
493504

505+
type purchaseWithMetrics struct {
506+
protoExchangeData *transactionpb.ExchangeDataWithoutRate
507+
deviceType client.DeviceType
508+
purchaseInitiationTime *time.Time
509+
usdcDepositTime *time.Time
510+
}
511+
494512
func getPurchasesFromSwap(
495513
ctx context.Context,
496514
conf *conf,
497515
data code_data.Provider,
498516
signature string,
499517
usdcSwapAccount *common.Account,
500518
usdcQuarksSwapped uint64,
501-
) ([]*transactionpb.ExchangeDataWithoutRate, error) {
519+
) ([]*purchaseWithMetrics, error) {
502520
accountInfoRecord, err := data.GetAccountInfoByTokenAddress(ctx, usdcSwapAccount.PublicKey().ToBase58())
503521
if err != nil {
504522
return nil, errors.Wrap(err, "error getting account info record")
@@ -523,8 +541,8 @@ func getPurchasesFromSwap(
523541
return nil, errors.Wrap(err, "error getting transaction history")
524542
}
525543

526-
purchases, err := func() ([]*transactionpb.ExchangeDataWithoutRate, error) {
527-
var res []*transactionpb.ExchangeDataWithoutRate
544+
purchases, err := func() ([]*purchaseWithMetrics, error) {
545+
var res []*purchaseWithMetrics
528546
var usdcDeposited uint64
529547
for _, historyItem := range history {
530548
if historyItem.Err != nil {
@@ -580,14 +598,18 @@ func getPurchasesFromSwap(
580598
continue
581599
}
582600

583-
rawUsdPurchase := &transactionpb.ExchangeDataWithoutRate{
584-
Currency: "usd",
585-
NativeAmount: math.Round(usdAmount), // Round to nearest $1 since we don't support decimals in app yet
601+
rawUsdPurchase := &purchaseWithMetrics{
602+
protoExchangeData: &transactionpb.ExchangeDataWithoutRate{
603+
Currency: "usd",
604+
NativeAmount: math.Round(usdAmount), // Round to nearest $1 since we don't support decimals in app yet
605+
},
606+
deviceType: client.DeviceTypeUnknown,
607+
usdcDepositTime: historyItem.BlockTime,
586608
}
587609

588610
// There is no memo for a blockchain message
589611
if historyItem.Memo == nil {
590-
res = append([]*transactionpb.ExchangeDataWithoutRate{rawUsdPurchase}, res...)
612+
res = append([]*purchaseWithMetrics{rawUsdPurchase}, res...)
591613
continue
592614
}
593615

@@ -597,21 +619,24 @@ func getPurchasesFromSwap(
597619
memoMessage := memoParts[len(memoParts)-1]
598620
blockchainMessage, err := thirdparty.DecodeFiatOnrampPurchaseMessage([]byte(memoMessage))
599621
if err != nil {
600-
res = append([]*transactionpb.ExchangeDataWithoutRate{rawUsdPurchase}, res...)
622+
res = append([]*purchaseWithMetrics{rawUsdPurchase}, res...)
601623
continue
602624
}
603625
onrampRecord, err := data.GetFiatOnrampPurchase(ctx, blockchainMessage.Nonce)
604626
if err == onramp.ErrPurchaseNotFound {
605-
res = append([]*transactionpb.ExchangeDataWithoutRate{rawUsdPurchase}, res...)
627+
res = append([]*purchaseWithMetrics{rawUsdPurchase}, res...)
606628
continue
607629
} else if err != nil {
608630
return nil, errors.Wrap(err, "error getting onramp record")
609631
}
610632

633+
rawUsdPurchase.deviceType = client.DeviceType(onrampRecord.Platform)
634+
rawUsdPurchase.purchaseInitiationTime = &onrampRecord.CreatedAt
635+
611636
// This nonce is not associated with the owner account linked to the
612637
// fiat purchase.
613638
if onrampRecord.Owner != accountInfoRecord.OwnerAccount {
614-
res = append([]*transactionpb.ExchangeDataWithoutRate{rawUsdPurchase}, res...)
639+
res = append([]*purchaseWithMetrics{rawUsdPurchase}, res...)
615640
continue
616641
}
617642

@@ -631,22 +656,20 @@ func getPurchasesFromSwap(
631656
fxRate := otherCurrencyRate / usdRate
632657
pctDiff := math.Abs(usdAmount*fxRate-onrampRecord.Amount) / onrampRecord.Amount
633658
if pctDiff > 0.1 {
634-
res = append([]*transactionpb.ExchangeDataWithoutRate{rawUsdPurchase}, res...)
659+
res = append([]*purchaseWithMetrics{rawUsdPurchase}, res...)
635660
continue
636661
}
637662
}
638663

639-
recordBuyModulePurchaseCompletedEvent(
640-
ctx,
641-
client.DeviceType(onrampRecord.Platform),
642-
onrampRecord.CreatedAt,
643-
historyItem.BlockTime,
644-
)
645-
646-
res = append([]*transactionpb.ExchangeDataWithoutRate{
664+
res = append([]*purchaseWithMetrics{
647665
{
648-
Currency: onrampRecord.Currency,
649-
NativeAmount: onrampRecord.Amount,
666+
protoExchangeData: &transactionpb.ExchangeDataWithoutRate{
667+
Currency: onrampRecord.Currency,
668+
NativeAmount: onrampRecord.Amount,
669+
},
670+
deviceType: client.DeviceType(onrampRecord.Platform),
671+
purchaseInitiationTime: &onrampRecord.CreatedAt,
672+
usdcDepositTime: historyItem.BlockTime,
650673
},
651674
}, res...)
652675
}
@@ -667,10 +690,12 @@ func getPurchasesFromSwap(
667690

668691
if len(purchases) == 0 {
669692
// No purchases were returned, so defer back to the USDC amount swapped
670-
return []*transactionpb.ExchangeDataWithoutRate{
693+
return []*purchaseWithMetrics{
671694
{
672-
Currency: "usd",
673-
NativeAmount: float64(usdcQuarksSwapped) / float64(usdc.QuarksPerUsdc),
695+
protoExchangeData: &transactionpb.ExchangeDataWithoutRate{
696+
Currency: "usd",
697+
NativeAmount: float64(usdcQuarksSwapped) / float64(usdc.QuarksPerUsdc),
698+
},
674699
},
675700
}, nil
676701
}

pkg/code/async/geyser/metrics.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,12 @@ func (p *service) recordBackupQueueStatusPollingEvent(ctx context.Context) {
139139
})
140140
}
141141

142-
func recordBuyModulePurchaseCompletedEvent(ctx context.Context, deviceType client.DeviceType, purchaseInitiationTime time.Time, usdcDepositTime *time.Time) {
142+
func recordBuyModulePurchaseCompletedEvent(ctx context.Context, deviceType client.DeviceType, purchaseInitiationTime *time.Time, usdcDepositTime *time.Time) {
143143
kvs := map[string]interface{}{
144-
"total_latency": int(time.Since(purchaseInitiationTime) / time.Millisecond),
145-
"platform": deviceType.String(),
144+
"platform": deviceType.String(),
145+
}
146+
if purchaseInitiationTime != nil {
147+
kvs["total_latency"] = int(time.Since(*purchaseInitiationTime) / time.Millisecond)
146148
}
147149
if usdcDepositTime != nil {
148150
kvs["swap_latency"] = int(time.Since(*usdcDepositTime) / time.Millisecond)

0 commit comments

Comments
 (0)