Skip to content

Commit

Permalink
chore: add estimated USD value to swap analytics events (#4060)
Browse files Browse the repository at this point in the history
### Test plan

Verify properties in analytics backends.
  • Loading branch information
silasbw authored Aug 11, 2023
1 parent 829fde8 commit f706891
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/analytics/Properties.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1257,13 +1257,17 @@ interface SwapEventsProperties {
fromTokenBalance: string
swapExecuteTxId: string
swapApproveTxId: string
estimatedSellTokenUsdValue?: number
estimatedBuyTokenUsdValue?: number
}
[SwapEvents.swap_execute_error]: SwapQuoteEvent &
SwapTimeMetrics & {
error: string
fromTokenBalance: string
swapExecuteTxId: string
swapApproveTxId: string
estimatedSellTokenUsdValue?: number
estimatedBuyTokenUsdValue?: number
}
[SwapEvents.swap_learn_more]: undefined
[SwapEvents.swap_price_impact_warning_displayed]: SwapEvent & {
Expand Down
6 changes: 6 additions & 0 deletions src/swap/saga.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ jest.mock('src/transactions/send', () => ({

const mockSwapTransaction = {
buyAmount: '10000000000000000',
sellAmount: '10000000000000000',
buyTokenAddress: mockCeloAddress,
sellTokenAddress: mockCeurAddress,
price: '1',
Expand Down Expand Up @@ -89,6 +90,7 @@ describe(swapSubmitSaga, () => {
[
{
...mockTokenBalances[mockCeurAddress],
usdPrice: new BigNumber('1'),
balance: new BigNumber('10'),
},
],
Expand Down Expand Up @@ -129,6 +131,8 @@ describe(swapSubmitSaga, () => {
swapExecuteTxId: 'a uuid',
quoteToUserConfirmsSwapElapsedTimeInMs: 2500,
quoteToTransactionElapsedTimeInMs: 10000,
estimatedBuyTokenUsdValue: undefined,
estimatedSellTokenUsdValue: 0.01,
})
})

Expand Down Expand Up @@ -159,6 +163,8 @@ describe(swapSubmitSaga, () => {
swapExecuteTxId: 'a uuid',
quoteToUserConfirmsSwapElapsedTimeInMs: 30000,
quoteToTransactionElapsedTimeInMs: undefined,
estimatedBuyTokenUsdValue: undefined,
estimatedSellTokenUsdValue: 0.01,
})
})

Expand Down
26 changes: 26 additions & 0 deletions src/swap/saga.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SwapInfo>) {
const swapSubmittedAt = Date.now()
const {
Expand All @@ -74,10 +89,19 @@ export function* swapSubmitSaga(action: PayloadAction<SwapInfo>) {
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')
Expand All @@ -94,6 +118,8 @@ export function* swapSubmitSaga(action: PayloadAction<SwapInfo>) {
fromTokenBalance,
swapExecuteTxId: swapExecuteContext.id,
swapApproveTxId: swapApproveContext.id,
estimatedSellTokenUsdValue,
estimatedBuyTokenUsdValue,
}

let quoteToTransactionElapsedTimeInMs: number | undefined
Expand Down

0 comments on commit f706891

Please sign in to comment.