From f706891bcc231a520cda2dfc00aea811c676915e Mon Sep 17 00:00:00 2001 From: Silas Boyd-Wickizer Date: Fri, 11 Aug 2023 11:47:23 -0700 Subject: [PATCH] chore: add estimated USD value to swap analytics events (#4060) ### Test plan Verify properties in analytics backends. --- src/analytics/Properties.tsx | 4 ++++ src/swap/saga.test.ts | 6 ++++++ src/swap/saga.ts | 26 ++++++++++++++++++++++++++ 3 files changed, 36 insertions(+) diff --git a/src/analytics/Properties.tsx b/src/analytics/Properties.tsx index 12be2ed8b14..6c24c660758 100644 --- a/src/analytics/Properties.tsx +++ b/src/analytics/Properties.tsx @@ -1257,6 +1257,8 @@ interface SwapEventsProperties { fromTokenBalance: string swapExecuteTxId: string swapApproveTxId: string + estimatedSellTokenUsdValue?: number + estimatedBuyTokenUsdValue?: number } [SwapEvents.swap_execute_error]: SwapQuoteEvent & SwapTimeMetrics & { @@ -1264,6 +1266,8 @@ interface SwapEventsProperties { fromTokenBalance: string swapExecuteTxId: string swapApproveTxId: string + estimatedSellTokenUsdValue?: number + estimatedBuyTokenUsdValue?: number } [SwapEvents.swap_learn_more]: undefined [SwapEvents.swap_price_impact_warning_displayed]: SwapEvent & { diff --git a/src/swap/saga.test.ts b/src/swap/saga.test.ts index 75d6a979092..fe8203c80e5 100644 --- a/src/swap/saga.test.ts +++ b/src/swap/saga.test.ts @@ -40,6 +40,7 @@ jest.mock('src/transactions/send', () => ({ const mockSwapTransaction = { buyAmount: '10000000000000000', + sellAmount: '10000000000000000', buyTokenAddress: mockCeloAddress, sellTokenAddress: mockCeurAddress, price: '1', @@ -89,6 +90,7 @@ describe(swapSubmitSaga, () => { [ { ...mockTokenBalances[mockCeurAddress], + usdPrice: new BigNumber('1'), balance: new BigNumber('10'), }, ], @@ -129,6 +131,8 @@ describe(swapSubmitSaga, () => { swapExecuteTxId: 'a uuid', quoteToUserConfirmsSwapElapsedTimeInMs: 2500, quoteToTransactionElapsedTimeInMs: 10000, + estimatedBuyTokenUsdValue: undefined, + estimatedSellTokenUsdValue: 0.01, }) }) @@ -159,6 +163,8 @@ describe(swapSubmitSaga, () => { swapExecuteTxId: 'a uuid', quoteToUserConfirmsSwapElapsedTimeInMs: 30000, quoteToTransactionElapsedTimeInMs: undefined, + estimatedBuyTokenUsdValue: undefined, + estimatedSellTokenUsdValue: 0.01, }) }) diff --git a/src/swap/saga.ts b/src/swap/saga.ts index 2e2049685b5..481c43d0095 100644 --- a/src/swap/saga.ts +++ b/src/swap/saga.ts @@ -54,6 +54,21 @@ function* handleSendSwapTransaction( yield* call(sendTransaction, txo, walletAddress, transactionContext) } +function calculateEstimatedUsdValue({ + tokenInfo, + tokenAmount, +}: { + tokenInfo: TokenBalance + tokenAmount: string +}): number | undefined { + if (!tokenInfo.usdPrice) { + return undefined + } + + const amount = valueToBigNumber(tokenAmount) + return tokenInfo.usdPrice.times(amount.shiftedBy(-tokenInfo.decimals)).toNumber() +} + export function* swapSubmitSaga(action: PayloadAction) { const swapSubmittedAt = Date.now() const { @@ -74,10 +89,19 @@ export function* swapSubmitSaga(action: PayloadAction) { const { quoteReceivedAt } = action.payload const tokenBalances: TokenBalance[] = yield* select(swappableTokensSelector) + const fromToken = tokenBalances.find((token) => token.address === sellTokenAddress) const fromTokenBalance = fromToken ? fromToken.balance.shiftedBy(fromToken.decimals).toString() : '' + const estimatedSellTokenUsdValue = fromToken + ? calculateEstimatedUsdValue({ tokenInfo: fromToken, tokenAmount: sellAmount }) + : undefined + + const toToken = tokenBalances.find((token) => token.address === buyTokenAddress) + const estimatedBuyTokenUsdValue = toToken + ? calculateEstimatedUsdValue({ tokenInfo: toToken, tokenAmount: buyAmount }) + : undefined const swapApproveContext = newTransactionContext(TAG, 'Swap/Approve') const swapExecuteContext = newTransactionContext(TAG, 'Swap/Execute') @@ -94,6 +118,8 @@ export function* swapSubmitSaga(action: PayloadAction) { fromTokenBalance, swapExecuteTxId: swapExecuteContext.id, swapApproveTxId: swapApproveContext.id, + estimatedSellTokenUsdValue, + estimatedBuyTokenUsdValue, } let quoteToTransactionElapsedTimeInMs: number | undefined