@@ -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+
494512func 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 }
0 commit comments