Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
11fc709
init commit
Jul 6, 2022
0864ee4
add amplitude ts sdk to package.json
Jul 6, 2022
f9f013d
add more comments and documentation
Jul 6, 2022
561aadf
respond to vm comments
Jul 6, 2022
682d713
respond to cmcewen comments
Jul 7, 2022
4d3f629
fix: remove unused constants
Jul 7, 2022
6e5119d
init commit
Jul 7, 2022
8f185b3
adapt to web
Jul 8, 2022
02b927c
add optional event properties to trace
Jul 8, 2022
f5ad7f1
correct telemetry to analytics
Jul 8, 2022
4fe3aa2
init commit
Jul 8, 2022
2c40799
change telemetry to analytics in doc
Jul 8, 2022
84e9683
init commit
Jul 8, 2022
42fddee
fix: respond to cmcewen comments + initialize analytics in app.tsx + …
Jul 8, 2022
40b148b
Merge branch 'amplitude_sdk_initial' into analytics_trace_framework
Jul 8, 2022
3f01dd7
Merge branch 'analytics_trace_framework' into WEB-214-implement-the-m…
Jul 8, 2022
5ee155d
add element name constant
Jul 8, 2022
caa99fc
merge
Jul 13, 2022
5fad981
Merge branch 'main' into WEB-214-implement-the-main-submit-swap-event
Jul 18, 2022
f40e223
Merge branch 'main' into WEB-214-implement-the-main-submit-swap-event
Jul 19, 2022
9232948
init commit
Jul 20, 2022
8072ae2
Merge branch 'main' into WEB-214-implement-the-main-submit-swap-event
Jul 20, 2022
8db5a4c
correct price_impact calculation
Jul 20, 2022
3cc77be
resolve vm comments
Jul 20, 2022
23ab7af
fixes in response to comments
Jul 20, 2022
d314f77
respond to vm
Jul 20, 2022
ec51b9a
use ALL significant digits for token amounts
Jul 20, 2022
519b1cb
init commit
Jul 21, 2022
3ed403c
Merge branch 'main' into WEB-214-implement-the-main-submit-swap-event
Jul 21, 2022
39f6073
merge main
Jul 21, 2022
d36824d
Merge branch 'WEB-214-implement-the-main-submit-swap-event' into swap…
Jul 21, 2022
0e6c7df
logged all properties
Jul 21, 2022
dc6fa77
create helper function getPriceImpactPercentageNumber
Jul 21, 2022
34a72c0
4 decimal points for percentages
Jul 21, 2022
d4f7352
Merge branch 'WEB-214-implement-the-main-submit-swap-event' into swap…
Jul 21, 2022
cf83cea
price percentage fn
Jul 21, 2022
a4ddbbf
percentage to basis points units
Jul 21, 2022
4c18c39
only log event on FIRST price fetch
Jul 21, 2022
8b222ea
respond to cmcewen comments
Jul 22, 2022
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
2 changes: 2 additions & 0 deletions src/components/AmplitudeAnalytics/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export enum EventName {
SWAP_AUTOROUTER_VISUALIZATION_EXPANDED = 'Swap Autorouter Visualization Expanded',
SWAP_DETAILS_EXPANDED = 'Swap Details Expanded',
SWAP_MAX_TOKEN_AMOUNT_SELECTED = 'Swap Max Token Amount Selected',
SWAP_QUOTE_RECEIVED = 'Swap Quote Received',
SWAP_SUBMITTED = 'Swap Submitted',
SWAP_TOKENS_REVERSED = 'Swap Tokens Reversed',
TOKEN_IMPORTED = 'Token Imported',
Expand Down Expand Up @@ -68,6 +69,7 @@ export const enum ElementName {
SWAP_BUTTON = 'swap-button',
SWAP_DETAILS_DROPDOWN = 'swap-details-dropdown',
SWAP_TOKENS_REVERSE_ARROW_BUTTON = 'swap-tokens-reverse-arrow-button',
SWAP_TRADE_PRICE_ROW = 'swap-trade-price-row',
TOKEN_SELECTOR_ROW = 'token-selector-row',
WALLET_TYPE_OPTION = 'wallet-type-option',
// alphabetize additional element names.
Expand Down
13 changes: 13 additions & 0 deletions src/components/AmplitudeAnalytics/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Currency, CurrencyAmount, Percent, Token } from '@uniswap/sdk-core'

export const getDurationTillTimestampSinceEpoch = (futureTimestampSinceEpoch?: number): number | undefined => {
if (!futureTimestampSinceEpoch) return undefined
return futureTimestampSinceEpoch - new Date().getTime() / 1000
}

export const getNumberFormattedToDecimalPlace = (
intialNumberObject: Percent | CurrencyAmount<Token | Currency>,
decimalPlace: number
): number => parseFloat(intialNumberObject.toFixed(decimalPlace))

export const formatPercentInBasisPointsNumber = (percent: Percent): number => parseFloat(percent.toFixed(2)) * 100
51 changes: 45 additions & 6 deletions src/components/swap/SwapDetailsDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ import { Trans } from '@lingui/macro'
import { Currency, Percent, TradeType } from '@uniswap/sdk-core'
import { useWeb3React } from '@web3-react/core'
import { ElementName, Event, EventName } from 'components/AmplitudeAnalytics/constants'
import { Trace } from 'components/AmplitudeAnalytics/Trace'
import { TraceEvent } from 'components/AmplitudeAnalytics/TraceEvent'
import { formatPercentInBasisPointsNumber, getNumberFormattedToDecimalPlace } from 'components/AmplitudeAnalytics/utils'
import AnimatedDropdown from 'components/AnimatedDropdown'
import Card, { OutlineCard } from 'components/Card'
import { AutoColumn } from 'components/Column'
Expand All @@ -11,13 +13,15 @@ import Row, { RowBetween, RowFixed } from 'components/Row'
import { MouseoverTooltipContent } from 'components/Tooltip'
import { SUPPORTED_GAS_ESTIMATE_CHAIN_IDS } from 'constants/chains'
import { darken } from 'polished'
import { useState } from 'react'
import { useEffect, useState } from 'react'
import { ChevronDown, Info } from 'react-feather'
import { InterfaceTrade } from 'state/routing/types'
import styled, { keyframes, useTheme } from 'styled-components/macro'
import { HideSmall, ThemedText } from 'theme'
import { computeRealizedLPFeePercent } from 'utils/prices'

import { AdvancedSwapDetails } from './AdvancedSwapDetails'
import { getPriceImpactPercent } from './AdvancedSwapDetails'
import GasEstimateBadge from './GasEstimateBadge'
import { ResponsiveTooltipContainer } from './styleds'
import SwapRoute from './SwapRoute'
Expand Down Expand Up @@ -120,6 +124,29 @@ interface SwapDetailsInlineProps {
allowedSlippage: Percent
}

const formatAnalyticsEventProperties = (trade: InterfaceTrade<Currency, Currency, TradeType>) => {
const lpFeePercent = trade ? computeRealizedLPFeePercent(trade) : undefined
return {
token_in_symbol: trade.inputAmount.currency.symbol,
token_out_symbol: trade.outputAmount.currency.symbol,
token_in_address: trade.inputAmount.currency.isToken ? trade.inputAmount.currency.address : undefined,
token_out_address: trade.outputAmount.currency.isToken ? trade.outputAmount.currency.address : undefined,
price_impact_basis_points: lpFeePercent
? formatPercentInBasisPointsNumber(getPriceImpactPercent(lpFeePercent, trade))
: undefined,
estimated_network_fee_usd: trade.gasUseEstimateUSD
? getNumberFormattedToDecimalPlace(trade.gasUseEstimateUSD, 2)
: undefined,
chain_id:
trade.inputAmount.currency.chainId === trade.outputAmount.currency.chainId
? trade.inputAmount.currency.chainId
: undefined,
token_in_amount: getNumberFormattedToDecimalPlace(trade.inputAmount, trade.inputAmount.currency.decimals),
token_out_amount: getNumberFormattedToDecimalPlace(trade.outputAmount, trade.outputAmount.currency.decimals),
// TODO(lynnshaoyu): Implement quote_latency_milliseconds.
}
}

export default function SwapDetailsDropdown({
trade,
syncing,
Expand All @@ -131,6 +158,11 @@ export default function SwapDetailsDropdown({
const theme = useTheme()
const { chainId } = useWeb3React()
const [showDetails, setShowDetails] = useState(false)
const [isFirstPriceFetch, setIsFirstPriceFetch] = useState(true)

useEffect(() => {
Comment thread
lynnshaoyu marked this conversation as resolved.
if (isFirstPriceFetch && syncing) setIsFirstPriceFetch(false)
}, [isFirstPriceFetch, syncing])

return (
<Wrapper>
Expand Down Expand Up @@ -174,11 +206,18 @@ export default function SwapDetailsDropdown({
)}
{trade ? (
<LoadingOpacityContainer $loading={syncing}>
<TradePrice
price={trade.executionPrice}
showInverted={showInverted}
setShowInverted={setShowInverted}
/>
<Trace
name={EventName.SWAP_QUOTE_RECEIVED}
element={ElementName.SWAP_TRADE_PRICE_ROW}
properties={formatAnalyticsEventProperties(trade)}
shouldLogImpression={!loading && !syncing && isFirstPriceFetch}
>
<TradePrice
price={trade.executionPrice}
showInverted={showInverted}
setShowInverted={setShowInverted}
/>
</Trace>
</LoadingOpacityContainer>
) : loading || syncing ? (
<ThemedText.Main fontSize={14}>
Expand Down
19 changes: 6 additions & 13 deletions src/components/swap/SwapModalFooter.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
import { Trans } from '@lingui/macro'
import { Currency, CurrencyAmount, Percent, Token, TradeType } from '@uniswap/sdk-core'
import { Currency, Percent, TradeType } from '@uniswap/sdk-core'
import { ElementName, EventName } from 'components/AmplitudeAnalytics/constants'
import { Event } from 'components/AmplitudeAnalytics/constants'
import { TraceEvent } from 'components/AmplitudeAnalytics/TraceEvent'
import {
formatPercentInBasisPointsNumber,
getDurationTillTimestampSinceEpoch,
getNumberFormattedToDecimalPlace,
} from 'components/AmplitudeAnalytics/utils'
import { useStablecoinValue } from 'hooks/useStablecoinPrice'
import useTransactionDeadline from 'hooks/useTransactionDeadline'
import { ReactNode } from 'react'
Expand All @@ -16,18 +21,6 @@ import { AutoRow } from '../Row'
import { getPriceImpactPercent } from './AdvancedSwapDetails'
import { SwapCallbackError } from './styleds'

function getDurationTillTimestampSinceEpoch(futureTimestampSinceEpoch?: number): number | undefined {
if (!futureTimestampSinceEpoch) return undefined
return futureTimestampSinceEpoch - new Date().getTime() / 1000
}

const getNumberFormattedToDecimalPlace = (
intialNumberObject: Percent | CurrencyAmount<Token | Currency>,
decimalPlace: number
): number => parseFloat(intialNumberObject.toFixed(decimalPlace))

const formatPercentInBasisPointsNumber = (percent: Percent): number => parseFloat(percent.toFixed(2)) * 100

interface AnalyticsEventProps {
trade: InterfaceTrade<Currency, Currency, TradeType>
txHash: string | undefined
Expand Down