2023-03-01 09:45:27 +01:00
|
|
|
import { useCallback } from 'react';
|
|
|
|
import { useSelector } from 'react-redux';
|
2023-05-02 14:36:24 +02:00
|
|
|
import type { Hex } from '@metamask/utils';
|
2023-03-01 09:45:27 +01:00
|
|
|
import { ChainId, CHAIN_IDS } from '../../../shared/constants/network';
|
2023-06-02 14:19:03 +02:00
|
|
|
import { getCurrentChainId, getMetaMetricsId } from '../../selectors';
|
2023-03-01 09:45:27 +01:00
|
|
|
|
|
|
|
interface IUseRamps {
|
|
|
|
openBuyCryptoInPdapp: VoidFunction;
|
|
|
|
getBuyURI: (chainId: ChainId) => string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const portfolioUrl = process.env.PORTFOLIO_URL;
|
|
|
|
|
|
|
|
const useRamps = (): IUseRamps => {
|
|
|
|
const chainId = useSelector(getCurrentChainId);
|
2023-06-02 14:19:03 +02:00
|
|
|
const metaMetricsId = useSelector(getMetaMetricsId);
|
2023-03-01 09:45:27 +01:00
|
|
|
|
2023-05-02 14:36:24 +02:00
|
|
|
const getBuyURI = useCallback((_chainId: Hex) => {
|
2023-03-01 09:45:27 +01:00
|
|
|
switch (_chainId) {
|
|
|
|
case CHAIN_IDS.SEPOLIA:
|
|
|
|
return 'https://faucet.sepolia.dev/';
|
2023-06-02 14:19:03 +02:00
|
|
|
default: {
|
|
|
|
const params = new URLSearchParams();
|
|
|
|
params.set('metamaskEntry', 'ext_buy_button');
|
|
|
|
if (metaMetricsId) {
|
|
|
|
params.set('metametricsId', metaMetricsId);
|
|
|
|
}
|
|
|
|
return `${portfolioUrl}/buy?${params.toString()}`;
|
|
|
|
}
|
2023-03-01 09:45:27 +01:00
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
const openBuyCryptoInPdapp = useCallback(() => {
|
|
|
|
const buyUrl = getBuyURI(chainId);
|
|
|
|
global.platform.openTab({
|
|
|
|
url: buyUrl,
|
|
|
|
});
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return { openBuyCryptoInPdapp, getBuyURI };
|
|
|
|
};
|
|
|
|
|
|
|
|
export default useRamps;
|