Skip to content
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 transfer.success event to v2 Redeem view #2696

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 1 addition & 1 deletion wormhole-connect/src/hooks/useUSDamountGetter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const useUSDamountGetter = (): ((args: {
const tokenPrice = Number(getTokenPrice(prices, config.tokens[token]));
const USDAmount = tokenPrice * numericAmount;

return isNaN(USDAmount) ? undefined : USDAmount;
return isNaN(USDAmount) ? undefined : parseFloat(USDAmount.toFixed(2));
},
[data],
);
Expand Down
23 changes: 22 additions & 1 deletion wormhole-connect/src/telemetry/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import config from 'config';

import { TokenDetails } from './types';
import { TokenDetails, TransferDetails } from './types';
import { Chain } from '@wormhole-foundation/sdk';

export function getTokenDetails(token: string): TokenDetails {
const tokenConfig = config.tokens[token]!;
Expand All @@ -11,3 +12,23 @@ export function getTokenDetails(token: string): TokenDetails {
tokenId: tokenId ?? 'native',
};
}

export function getTransferDetails(
route: string,
sourceToken: string,
destToken: string,
sourceChain: Chain,
destChain: Chain,
amount: string,
getUSDAmount: (args: { token: string; amount: string }) => number | undefined,
): TransferDetails {
return {
route,
fromToken: getTokenDetails(sourceToken),
toToken: getTokenDetails(destToken),
fromChain: sourceChain,
toChain: destChain,
amount: Number(amount),
USDAmount: getUSDAmount({ token: sourceToken, amount }),
};
}
19 changes: 9 additions & 10 deletions wormhole-connect/src/views/v2/Bridge/ReviewTransaction/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Stack from '@mui/material/Stack';
import Typography from '@mui/material/Typography';
import ChevronLeft from '@mui/icons-material/ChevronLeft';
import IconButton from '@mui/material/IconButton';
import { getTokenDetails } from 'telemetry';
import { getTransferDetails } from 'telemetry';
import { Context } from 'sdklegacy';

import Button from 'components/v2/Button';
Expand Down Expand Up @@ -139,16 +139,15 @@ const ReviewTransaction = (props: Props) => {
return;
}

// Details for config.dispatchEvent events
const transferDetails = {
const transferDetails = getTransferDetails(
route,
fromToken: getTokenDetails(sourceToken),
toToken: getTokenDetails(destToken),
fromChain: sourceChain,
toChain: destChain,
amount: Number(amount),
USDAmount: getUSDAmount({ token: sourceToken, amount }),
};
sourceToken,
destToken,
sourceChain,
destChain,
amount,
getUSDAmount,
);

// Handle custom transfer validation (if provided by integrator)
if (config.validateTransfer) {
Expand Down
29 changes: 28 additions & 1 deletion wormhole-connect/src/views/v2/Redeem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
routes,
TransferState,
} from '@wormhole-foundation/sdk';
import { getTokenDetails } from 'telemetry';
import { getTokenDetails, getTransferDetails } from 'telemetry';
import { makeStyles } from 'tss-react/mui';
import { Context } from 'sdklegacy';

Expand All @@ -27,6 +27,7 @@ import useTrackTransfer from 'hooks/useTrackTransfer';
import PoweredByIcon from 'icons/PoweredBy';
import { SDKv2Signer } from 'routes/sdkv2/signer';
import { setRoute } from 'store/router';
import { useUSDamountGetter } from 'hooks/useUSDamountGetter';
import { displayAddress, getDisplayName, millisToHumanString } from 'utils';
import { interpretTransferError } from 'utils/errors';
import { joinClass } from 'utils/style';
Expand Down Expand Up @@ -101,6 +102,8 @@ const Redeem = () => {

const [claimError, setClaimError] = useState('');
const [isClaimInProgress, setIsClaimInProgress] = useState(false);
const [transferSuccessEventFired, setTransferSuccessEventFired] =
useState(false);
const [etaExpired, setEtaExpired] = useState(false);

const [isWalletSidebarOpen, setIsWalletSidebarOpen] = useState(false);
Expand Down Expand Up @@ -130,10 +133,34 @@ const Redeem = () => {
fromChain,
tokenKey,
receivedTokenKey,
amount,
receiveAmount,
eta = 0,
} = txData!;

const getUSDAmount = useUSDamountGetter();

useEffect(() => {
// When we see the transfer was complete for the first time,
// fire a transfer.success telemetry event.
if (isTxComplete && !transferSuccessEventFired) {
Copy link
Collaborator

@kev1n-peters kev1n-peters Sep 23, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to include transferSuccessEventFired in the dep array? I guess isTxComplete will only change once (from false to true) so might not matter

setTransferSuccessEventFired(true);

config.triggerEvent({
type: 'transfer.success',
details: getTransferDetails(
routeName!,
tokenKey,
receivedTokenKey,
fromChain,
toChain,
amount,
getUSDAmount,
),
});
}
}, [isTxComplete]);

const receivingWallet = useSelector(
(state: RootState) => state.wallet.receiving,
);
Expand Down
Loading