Skip to content

Commit

Permalink
Enable prefilled token experiment on Add Tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
luads committed Dec 5, 2024
1 parent d164641 commit 34f72cd
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,7 @@ export type AddTokensWidgetParams = {

/** The destination wallet provider, when requiring to lock destination of funds */
toProvider?: Web3Provider;

/** Flags to control experiments within the widget */
experiments?: Record<string, string>;
};
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ export class AddTokens extends Base<WidgetType.ADD_TOKENS> {
showSwapOption={this.parameters.showSwapOption}
showOnrampOption={this.parameters.showOnrampOption}
showBackButton={this.parameters.showBackButton}
experiments={this.parameters.experiments}
/>
</Suspense>
</ProvidersContextProvider>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { getRemoteImage } from '../../lib/utils';
import { isValidAddress } from '../../lib/validations/widgetValidators';
import { amountInputValidation } from '../../lib/validations/amountInputValidations';
import { useError } from './hooks/useError';
import { AddTokensErrorTypes } from './types';
import { AddTokensErrorTypes, AddTokensExperiments } from './types';
import { ServiceUnavailableErrorView } from '../../views/error/ServiceUnavailableErrorView';

export type AddTokensWidgetInputs = Omit<AddTokensWidgetParams, 'toProvider'> & {
Expand All @@ -51,10 +51,11 @@ export default function AddTokensWidget({
showOnrampOption = true,
showSwapOption = true,
showBridgeOption = true,
toTokenAddress,
toTokenAddress: toTokenAddressParameter,
toAmount,
showBackButton,
config,
experiments,
}: AddTokensWidgetInputs) {
const fetchingBalances = useRef(false);
const { base: { colorMode } } = useTheme();
Expand Down Expand Up @@ -104,8 +105,29 @@ export default function AddTokensWidget({
id: uuidv4(),
},
});

if (experiments) {
addTokensDispatch({
payload: {
type: AddTokensActions.SET_EXPERIMENTS,
experiments,
},
});
}
}, []);

const toTokenAddress = useMemo(() => {
if (toTokenAddressParameter) {
return toTokenAddressParameter;
}

if (experiments?.[AddTokensExperiments.PRESELECTED_TOKEN]) {
return experiments[AddTokensExperiments.PRESELECTED_TOKEN];
}

return undefined;
}, [experiments, toTokenAddressParameter]);

useEffect(() => {
if (config.environment !== Environment.PRODUCTION) {
showErrorHandover(AddTokensErrorTypes.ENVIRONMENT_ERROR, { contextId: id });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export interface AddTokensState {
selectedToken: TokenInfo | undefined;
selectedAmount: string;
isSwapAvailable: boolean | undefined;
experiments: Record<string, string> | undefined;
}

export const initialAddTokensState: AddTokensState = {
Expand All @@ -30,6 +31,7 @@ export const initialAddTokensState: AddTokensState = {
selectedToken: undefined,
selectedAmount: '',
isSwapAvailable: undefined,
experiments: undefined,
};

export interface AddTokensContextState {
Expand All @@ -52,7 +54,8 @@ type ActionPayload =
| SetSelectedRouteData
| SetSelectedToken
| SetSelectedAmount
| SetIsSwapAvailable;
| SetIsSwapAvailable
| SetExperiments;

export enum AddTokensActions {
SET_ID = 'SET_ID',
Expand All @@ -66,6 +69,7 @@ export enum AddTokensActions {
SET_SELECTED_TOKEN = 'SET_SELECTED_TOKEN',
SET_SELECTED_AMOUNT = 'SET_SELECTED_AMOUNT',
SET_IS_SWAP_AVAILABLE = 'SET_IS_SWAP_AVAILABLE',
SET_EXPERIMENTS = 'SET_EXPERIMENTS',
}

export interface SetId {
Expand Down Expand Up @@ -123,6 +127,11 @@ export interface SetIsSwapAvailable {
isSwapAvailable: boolean;
}

export interface SetExperiments {
type: AddTokensActions.SET_EXPERIMENTS;
experiments: Record<string, string>;
}

// eslint-disable-next-line @typescript-eslint/naming-convention
export const AddTokensContext = createContext<AddTokensContextState>({
addTokensState: initialAddTokensState,
Expand Down Expand Up @@ -193,6 +202,11 @@ export const addTokensReducer: Reducer<AddTokensState, AddTokensAction> = (
...state,
isSwapAvailable: action.payload.isSwapAvailable,
};
case AddTokensActions.SET_EXPERIMENTS:
return {
...state,
experiments: action.payload.experiments,
};
default:
return state;
}
Expand Down
4 changes: 4 additions & 0 deletions packages/checkout/widgets-lib/src/widgets/add-tokens/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@ export enum AddTokensErrorTypes {
WALLET_POPUP_BLOCKED = 'WALLET_POPUP_BLOCKED',
ENVIRONMENT_ERROR = 'ENVIRONMENT_ERROR',
}

export enum AddTokensExperiments {
PRESELECTED_TOKEN = 'add-tokens-preselected-token',
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ import {
import type { StrongCheckoutWidgetsConfig } from '../../../lib/withDefaultWidgetConfig';
import { useRoutes } from '../../../lib/squid/hooks/useRoutes';
import { AddTokensWidgetViews } from '../../../context/view-context/AddTokensViewContextTypes';
import { AddTokensErrorTypes } from '../types';
import { AddTokensErrorTypes, AddTokensExperiments } from '../types';
import { SelectedRouteOption } from '../components/SelectedRouteOption';
import { SelectedWallet } from '../components/SelectedWallet';
import { DeliverToWalletDrawer } from '../../../components/WalletDrawer/DeliverToWalletDrawer';
Expand Down Expand Up @@ -118,6 +118,7 @@ export function AddTokens({
selectedRouteData,
selectedToken,
isSwapAvailable,
experiments,
} = addTokensState;

const {
Expand Down Expand Up @@ -580,6 +581,20 @@ export function AddTokens({
return undefined;
};

useEffect(() => {
if (!id || !experiments) return;

track({
userJourney: UserJourney.ADD_TOKENS,
screen: 'Experiments',
action: 'Started',
extras: {
contextId: id,
preselectedToken: experiments[AddTokensExperiments.PRESELECTED_TOKEN] || 'none',
},
});
}, [id, experiments]);

return (
<SimpleLayout
containerSx={{ bg: 'transparent' }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { passport } from "./passport";
import { Web3Provider } from "@ethersproject/providers";

const ADD_TOKENS_TARGET_ID = "add-tokens-widget-target";
const FLAG_PRESELECTED_TOKEN = 'add-tokens-preselected-token';

function AddTokensUI() {
const checkout = useMemo(
Expand Down Expand Up @@ -51,6 +52,7 @@ function AddTokensUI() {

const [toTokenAddress, setToTokenAddress] = useState<string | undefined>(undefined);
const [toAmount, setToAmount] = useState<string | undefined>(undefined);
const [experiments, setExperiments] = useState<Record<string, string> | undefined>(undefined);

const addTokens = useMemo(
() =>
Expand All @@ -74,6 +76,7 @@ function AddTokensUI() {
toProvider,
toTokenAddress,
toAmount,
experiments,
});
};

Expand Down Expand Up @@ -213,7 +216,15 @@ function AddTokensUI() {
<button onClick={() => setPresetToProvider(prev => !prev)}>
{presetToProvider ? 'Disconnect destination wallet' : 'Connect destination wallet'}
</button>

<br />
<br />
<b>Experiments</b><br />
Preselected token{' '}
<input type="text"
name={FLAG_PRESELECTED_TOKEN}
value={experiments?.[FLAG_PRESELECTED_TOKEN] ?? ''}
onChange={(e) => setExperiments({ ...experiments, [FLAG_PRESELECTED_TOKEN]: e.target.value })}
placeholder="0x1234" />
</div>
);
}
Expand Down

0 comments on commit 34f72cd

Please sign in to comment.