-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add input_amount to events #451
base: staging
Are you sure you want to change the base?
Changes from all commits
3c5dbae
977f579
af08900
7c32c67
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
import Big from 'big.js'; | ||
import { useEffect, useMemo, useRef, useState, useCallback, FormEvent } from 'react'; | ||
import { useEffect, useMemo, useRef, useState, useCallback, FormEvent, useDeferredValue } from 'react'; | ||
import { ApiPromise } from '@polkadot/api'; | ||
import { motion } from 'framer-motion'; | ||
|
||
|
@@ -71,6 +71,7 @@ import satoshipayLogo from '../../assets/logo/satoshipay.svg'; | |
export const SwapPage = () => { | ||
const formRef = useRef<HTMLDivElement | null>(null); | ||
const feeComparisonRef = useRef<FeeComparisonRef>(null); | ||
const fromAmountRef = useRef<Big | undefined>(undefined); | ||
const pendulumNode = usePendulumNode(); | ||
const trackQuote = useRef(false); | ||
const [api, setApi] = useState<ApiPromise | null>(null); | ||
|
@@ -268,6 +269,10 @@ export const SwapPage = () => { | |
[toToken.fiat.assetIcon, toToken.fiat.symbol, form, tokenOutAmount.isLoading, openTokenSelectModal], | ||
); | ||
|
||
useEffect(() => { | ||
fromAmountRef.current = fromAmount; | ||
}, [fromAmount]); | ||
|
||
const WithdrawNumericInput = useMemo( | ||
() => ( | ||
<> | ||
|
@@ -278,7 +283,12 @@ export const SwapPage = () => { | |
onClick={() => openTokenSelectModal('from')} | ||
onChange={() => { | ||
// User interacted with the input field | ||
trackEvent({ event: 'amount_type' }); | ||
setTimeout(() => { | ||
trackEvent({ | ||
event: 'amount_type', | ||
input_amount: fromAmountRef.current ? fromAmountRef.current.toString() : '0', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any reason we are using a ref, and especially using it only here? Alternatively, we could change the onChange={(newValue: string) => {
trackEvent({...})
}} |
||
}); | ||
}, 3000); | ||
// This also enables the quote tracking events | ||
trackQuote.current = true; | ||
}} | ||
|
@@ -287,15 +297,19 @@ export const SwapPage = () => { | |
<UserBalance token={fromToken} onClick={(amount: string) => form.setValue('fromAmount', amount)} /> | ||
</> | ||
), | ||
[form, fromToken, openTokenSelectModal, trackEvent], | ||
[form, fromAmount, fromToken, openTokenSelectModal, trackEvent], | ||
); | ||
|
||
function getCurrentErrorMessage() { | ||
if (isDisconnected) return; | ||
|
||
if (typeof userInputTokenBalance === 'string') { | ||
if (Big(userInputTokenBalance).lt(fromAmount ?? 0) && walletAccount) { | ||
trackEvent({ event: 'form_error', error_message: 'insufficient_balance' }); | ||
trackEvent({ | ||
event: 'form_error', | ||
error_message: 'insufficient_balance', | ||
input_amount: fromAmount ? fromAmount.toString() : '0', | ||
}); | ||
return `Insufficient balance. Your balance is ${userInputTokenBalance} ${fromToken?.assetSymbol}.`; | ||
} | ||
} | ||
|
@@ -307,14 +321,22 @@ export const SwapPage = () => { | |
const minAmountUnits = multiplyByPowerOfTen(Big(toToken.minWithdrawalAmountRaw), -toToken.decimals); | ||
|
||
if (maxAmountUnits.lt(amountOut)) { | ||
trackEvent({ event: 'form_error', error_message: 'more_than_maximum_withdrawal' }); | ||
trackEvent({ | ||
event: 'form_error', | ||
error_message: 'more_than_maximum_withdrawal', | ||
input_amount: fromAmount ? fromAmount.toString() : '0', | ||
}); | ||
return `Maximum withdrawal amount is ${stringifyBigWithSignificantDecimals(maxAmountUnits, 2)} ${ | ||
toToken.fiat.symbol | ||
}.`; | ||
} | ||
|
||
if (!config.test.overwriteMinimumTransferAmount && minAmountUnits.gt(amountOut)) { | ||
trackEvent({ event: 'form_error', error_message: 'less_than_minimum_withdrawal' }); | ||
trackEvent({ | ||
event: 'form_error', | ||
error_message: 'less_than_minimum_withdrawal', | ||
input_amount: fromAmount ? fromAmount.toString() : '0', | ||
}); | ||
return `Minimum withdrawal amount is ${stringifyBigWithSignificantDecimals(minAmountUnits, 2)} ${ | ||
toToken.fiat.symbol | ||
}.`; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { create } from 'zustand'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @pendulum-chain/devs let me know if you like the addition of this state, if you prefer it to be more minimal (only Although it adds extra code, I found it less confusing than passing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm fine with keeping it the way you implemented it 👍 |
||
import Big from 'big.js'; | ||
import { InputTokenDetails, OutputTokenDetails } from '../constants/tokenConfig'; | ||
|
||
interface FormState { | ||
fromAmount?: Big; | ||
fromToken?: InputTokenDetails; | ||
toToken?: OutputTokenDetails; | ||
} | ||
|
||
interface FormStoreActions { | ||
setFromAmount: (amount?: Big) => void; | ||
setFromToken: (token?: InputTokenDetails) => void; | ||
setToToken: (token?: OutputTokenDetails) => void; | ||
} | ||
|
||
type FormStore = FormState & { | ||
actions: FormStoreActions; | ||
}; | ||
|
||
const useFormStore = create<FormStore>((set) => ({ | ||
fromAmount: undefined, | ||
fromToken: undefined, | ||
toToken: undefined, | ||
|
||
actions: { | ||
setFromAmount: (amount?: Big) => set({ fromAmount: amount }), | ||
setFromToken: (token?: InputTokenDetails) => set({ fromToken: token }), | ||
setToToken: (token?: OutputTokenDetails) => set({ toToken: token }), | ||
}, | ||
})); | ||
|
||
export const useFromAmount = () => useFormStore((state) => state.fromAmount); | ||
export const useFromToken = () => useFormStore((state) => state.fromToken); | ||
export const useToToken = () => useFormStore((state) => state.toToken); | ||
|
||
export const useFormStoreActions = () => useFormStore((state) => state.actions); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You added the import but we are not actually using this. It's not a requirement in the ticket. I'm fine if we just send a new event for every user interaction without debouncing it.