Skip to content

Commit

Permalink
fix: Add tokens events (#2371)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiyounglee authored Nov 7, 2024
1 parent 9fd70ee commit c79b34f
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export type AddTokensSuccess = {
};

/**
* Type representing a add tokens failure
* Type representing add tokens failure
* @property {string} reason
* @property {number} timestamp
*/
Expand All @@ -34,7 +34,7 @@ export type AddTokensFailed = {
};

/**
* Type representing a successfull provider connection
* Type representing a successful provider connection
* @property {Web3Provider} provider
* @property {EIP6963ProviderInfo} providerInfo
* @property {'from' | 'to'} providerType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export enum CommerceSuccessEventType {
BRIDGE_SUCCESS = 'BRIDGE_SUCCESS',
BRIDGE_CLAIM_WITHDRAWAL_SUCCESS = 'BRIDGE_CLAIM_WITHDRAWAL_SUCCESS',
ADD_TOKENS_SUCCESS = 'ADD_TOKENS_SUCCESS',
ADD_TOKENS_CONNECT_SUCCESS = 'ADD_TOKENS_CONNECT_SUCCESS',
}

export enum CommerceFailureEventType {
Expand Down Expand Up @@ -107,7 +108,7 @@ export type CommerceAddTokensSuccessEvent = {
};

export type CommerceAddTokensConnectSuccessEvent = {
type: CommerceSuccessEventType.CONNECT_SUCCESS;
type: CommerceSuccessEventType.ADD_TOKENS_CONNECT_SUCCESS;
data: AddTokensConnectSuccess;
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
import { Web3Provider } from '@ethersproject/providers';
import { useContext } from 'react';
import { RouteResponse } from '@0xsquid/squid-types';
import { Squid } from '@0xsquid/sdk';
import { ethers } from 'ethers';
import { Environment } from '@imtbl/config';
import { isSquidNativeToken } from '../functions/isSquidNativeToken';
import { useError } from './useError';
import { AddTokensError, AddTokensErrorTypes } from '../types';
import { EventTargetContext } from '../../../context/event-target-context/EventTargetContext';
import { sendAddTokensFailedEvent } from '../AddTokensWidgetEvents';

export const useExecute = (environment: Environment) => {
const { showErrorHandover } = useError(environment);
const {
eventTargetState: { eventTarget },
} = useContext(EventTargetContext);

const handleTransactionError = (err: unknown) => {
const reason = `${
Expand Down Expand Up @@ -37,6 +43,7 @@ export const useExecute = (environment: Environment) => {
|| reason.includes('transaction failed')
) {
errorType = AddTokensErrorTypes.TRANSACTION_FAILED;
sendAddTokensFailedEvent(eventTarget, errorType);
}

if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ import {
import { convertToNetworkChangeableProvider } from '../functions/convertToNetworkChangeableProvider';
import { SquidFooter } from '../components/SquidFooter';
import { useError } from '../hooks/useError';
import { sendAddTokensSuccessEvent } from '../AddTokensWidgetEvents';
import { EventTargetContext } from '../../../context/event-target-context/EventTargetContext';

interface ReviewProps {
data: AddTokensReviewData;
Expand Down Expand Up @@ -104,6 +106,8 @@ export function Review({
},
} = useProvidersContext();

const { eventTargetState: { eventTarget } } = useContext(EventTargetContext);

const [route, setRoute] = useState<RouteResponse | undefined>();
const [proceedDisabled, setProceedDisabled] = useState(true);
const [showFeeBreakdown, setShowFeeBreakdown] = useState(false);
Expand Down Expand Up @@ -328,6 +332,8 @@ export function Review({
},
});

sendAddTokensSuccessEvent(eventTarget, executeTxnReceipt.transactionHash);

showHandover(
EXECUTE_TXN_ANIMATION,
RiveStateMachineInput.PROCESSING,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,35 @@ function mapAddTokensWidgetEvent(
const { type } = event.detail;

switch (type) {
case AddTokensEventType.SUCCESS:
return {
type: CommerceEventType.SUCCESS,
data: {
type: CommerceSuccessEventType.ADD_TOKENS_SUCCESS,
data: event.detail.data,
},
};
case AddTokensEventType.CONNECT_SUCCESS:
return {
type: CommerceEventType.SUCCESS,
data: {
type: CommerceSuccessEventType.ADD_TOKENS_CONNECT_SUCCESS,
data: event.detail.data,
},
};
case AddTokensEventType.FAILURE:
return {
type: CommerceEventType.FAILURE,
data: {
type: CommerceFailureEventType.ADD_TOKENS_FAILED,
data: event.detail.data,
},
};
case AddTokensEventType.CLOSE_WIDGET:
return {
type: CommerceEventType.CLOSE,
data: {},
};
default:
throw new Error(`Unknown add tokens event type "${event.detail.type}"`);
}
Expand Down

0 comments on commit c79b34f

Please sign in to comment.