Skip to content

Commit

Permalink
Merge pull request #425 from reservoirprotocol/pedro/relay-6670-add-q…
Browse files Browse the repository at this point in the history
…uote_error-analytics-to-posthog

Add quote error analytics to posthog
  • Loading branch information
pedromcunha authored Jan 8, 2025
2 parents a140e71 + 16034fc commit 70dbc2d
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 17 deletions.
6 changes: 6 additions & 0 deletions .changeset/clever-geese-fly.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@reservoir0x/relay-kit-hooks': patch
'@reservoir0x/relay-kit-ui': patch
---

Add quote_error analytics event
14 changes: 10 additions & 4 deletions packages/hooks/src/hooks/useQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ export default function (
options?: QuoteBody,
onRequest?: () => void,
onResponse?: (data: Execute) => void,
queryOptions?: Partial<QueryOptions>
queryOptions?: Partial<QueryOptions>,
onError?: (e: any) => void
) {
const queryKey = ['useQuote', options]
const response = (useQuery as QueryType)({
Expand All @@ -73,9 +74,14 @@ export default function (
options.referrer = client.source
}
const promise = queryQuote(client?.baseApiUrl, options)
promise.then((response: any) => {
onResponse?.(response)
})
promise
.then((response: any) => {
onResponse?.(response)
})
.catch((e) => {
onError?.(e)
throw e
})
return promise
},
enabled: client !== undefined && options !== undefined,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,25 @@ export const DepositAddressModalRenderer: FC<Props> = ({
const providerOptionsContext = useContext(ProviderOptionsContext)
const { connector } = useAccount()
const deadAddress = getDeadAddress(fromChain?.vmType, fromChain?.id)

const _quoteData =
fromToken && toToken
? {
user: deadAddress,
originChainId: fromToken.chainId,
destinationChainId: toToken.chainId,
originCurrency: fromToken.address,
destinationCurrency: toToken.address,
recipient: recipient as string,
tradeType: 'EXACT_INPUT',
appFees: providerOptionsContext.appFees,
amount: parseUnits(
debouncedInputAmountValue,
fromToken.decimals
).toString(),
referrer: relayClient?.source ?? undefined,
useDepositAddress: true
}
: undefined
const {
data: quoteData,
isLoading: isFetchingQuote,
Expand Down Expand Up @@ -168,6 +186,16 @@ export const DepositAddressModalRenderer: FC<Props> = ({
refetchOnMount: false,
retryOnMount: false,
staleTime: Infinity
},
(e: any) => {
const errorMessage = e?.response?.data?.message
? new Error(e?.response?.data?.message)
: e
onAnalyticEvent?.(EventNames.QUOTE_ERROR, {
wallet_connector: connector?.name,
error_message: errorMessage,
parameters: _quoteData
})
}
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,17 +154,7 @@ export const TransactionModalRenderer: FC<Props> = ({
const walletClient = useWalletClient()
const { connector } = useAccount()
const deadAddress = getDeadAddress(fromChain?.vmType, fromChain?.id)

const {
data: quote,
isLoading: isFetchingQuote,
isRefetching: isRefetchingQuote,
executeQuote: executeSwap,
error: quoteError,
dataUpdatedAt: quoteUpdatedAt
} = useQuote(
relayClient ? relayClient : undefined,
wallet ?? walletClient.data,
const _quoteData =
fromToken && toToken
? {
user: address ?? deadAddress,
Expand All @@ -188,7 +178,18 @@ export const TransactionModalRenderer: FC<Props> = ({
referrer: relayClient?.source ?? undefined,
useExternalLiquidity
}
: undefined,
: undefined
const {
data: quote,
isLoading: isFetchingQuote,
isRefetching: isRefetchingQuote,
executeQuote: executeSwap,
error: quoteError,
dataUpdatedAt: quoteUpdatedAt
} = useQuote(
relayClient ? relayClient : undefined,
wallet ?? walletClient.data,
_quoteData,
() => {},
({ steps, details }) => {
onAnalyticEvent?.(EventNames.SWAP_EXECUTE_QUOTE_RECEIVED, {
Expand Down Expand Up @@ -227,6 +228,16 @@ export const TransactionModalRenderer: FC<Props> = ({
debouncedOutputAmountValue === amountOutputValue
? 30000
: undefined
},
(e: any) => {
const errorMessage = e?.response?.data?.message
? new Error(e?.response?.data?.message)
: e
onAnalyticEvent?.(EventNames.QUOTE_ERROR, {
wallet_connector: connector?.name,
error_message: errorMessage,
parameters: _quoteData
})
}
)

Expand Down
10 changes: 10 additions & 0 deletions packages/ui/src/components/widgets/SwapWidgetRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -488,6 +488,16 @@ const SwapWidgetRenderer: FC<SwapWidgetRendererProps> = ({
debouncedOutputAmountValue === amountOutputValue
? 12000
: undefined
},
(e: any) => {
const errorMessage = e?.response?.data?.message
? new Error(e?.response?.data?.message)
: e
onAnalyticEvent?.(EventNames.QUOTE_ERROR, {
wallet_connector: connector?.name,
error_message: errorMessage,
parameters: _quoteData
})
}
)

Expand Down
1 change: 1 addition & 0 deletions packages/ui/src/constants/events.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export const EventNames = {
SWAP_BUTTON_CLICKED: 'SWAP_BUTTON_CLICKED',
SWAP_CTA_CLICKED: 'SWAP_CTA_CLICKED',
QUOTE_ERROR: 'QUOTE_ERROR',
SWAP_ERROR: 'SWAP_ERROR',
SWAP_INPUT_FOCUSED: 'SWAP_INPUT_FOCUSED',
SWAP_OUTPUT_FOCUSED: 'SWAP_OUTPUT_FOCUSED',
Expand Down

0 comments on commit 70dbc2d

Please sign in to comment.