Skip to content

Commit

Permalink
Moved uniswap logic to dedicated hook
Browse files Browse the repository at this point in the history
  • Loading branch information
mzparacha committed Dec 9, 2024
1 parent 71e8a1e commit 82c4bcb
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
import { ChainBase } from '@hicommonwealth/shared';
import { SwapWidget, Theme } from '@uniswap/widgets';
import '@uniswap/widgets/fonts.css';
import WebWalletController from 'client/scripts/controllers/app/web_wallets';
import { ethers } from 'ethers';
import React, { useEffect, useState } from 'react';
import React from 'react';
import { CWText } from 'views/components/component_kit/cw_text';
import {
CWModal,
Expand All @@ -14,6 +11,7 @@ import {
import TokenIcon from '../TokenIcon';
import { TradeTokenModalProps } from '../types';
import './UniswapTradeModal.scss';
import useUniswapTradeModal from './useUniswapTradeModal';

// By default the widget uses https://gateway.ipfs.io/ipns/tokens.uniswap.org for tokens
// list, but it doesn't work (DNS_PROBE_FINISHED_NXDOMAIN) for me (@malik). The original
Expand All @@ -40,21 +38,7 @@ const UniswapTradeModal = ({
onModalClose,
tradeConfig,
}: TradeTokenModalProps) => {
const [provider, setProvider] = useState<any>();
useEffect(() => {
const handleAsync = async () => {
const wallet = WebWalletController.Instance.availableWallets(
ChainBase.Ethereum,
);
const selectedWallet = wallet[0];
await selectedWallet.enable('8453'); // TODO: make dynamic
const tempProvider = new ethers.providers.Web3Provider(
selectedWallet.api.givenProvider,
);
setProvider(tempProvider);
};
handleAsync();
}, []);
const { uniswapProvider } = useUniswapTradeModal({ tradeConfig });

return (
<CWModal
Expand Down Expand Up @@ -84,7 +68,7 @@ const UniswapTradeModal = ({
defaultInputTokenAddress="NATIVE"
defaultOutputTokenAddress={tradeConfig.token.token_address}
hideConnectionUI={true}
{...(provider && { provider })}
{...(uniswapProvider && { provider: uniswapProvider })}
/>
</div>
</CWModalBody>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { TradingConfig } from '../types';

export type UseUniswapTradeModalProps = {
tradeConfig: TradingConfig;
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { commonProtocol } from '@hicommonwealth/evm-protocols';
import { ChainBase } from '@hicommonwealth/shared';
import WebWalletController from 'controllers/app/web_wallets';
import { ethers } from 'ethers';
import useRunOnceOnCondition from 'hooks/useRunOnceOnCondition';
import NodeInfo from 'models/NodeInfo';
import { useState } from 'react';
import { fetchCachedNodes } from 'state/api/nodes';
import { UseUniswapTradeModalProps } from './types';

const useUniswapTradeModal = ({ tradeConfig }: UseUniswapTradeModalProps) => {
const [uniswapProvider, setUniswapProvider] =
useState<ethers.providers.Web3Provider>();

// base chain node info
const nodes = fetchCachedNodes();
const baseNode = nodes?.find(
(n) => n.ethChainId === commonProtocol.ValidChains.SepoliaBase,
) as NodeInfo; // this is expected to exist

useRunOnceOnCondition({
callback: () => {
const handleAsync = async () => {
// adding this to avoid ts issues
if (!baseNode?.ethChainId) return;

const wallet = WebWalletController.Instance.availableWallets(
ChainBase.Ethereum,
);
const selectedWallet = wallet[0];
await selectedWallet.enable(`${baseNode.ethChainId}`);
const tempProvider = new ethers.providers.Web3Provider(
selectedWallet.api.givenProvider,
);
setUniswapProvider(tempProvider);
};
handleAsync().catch(console.error);
},
shouldRun: !!baseNode.ethChainId,
});

return {
uniswapProvider,
// TODO: only export what's needed
tradeConfig,
};
};

export default useUniswapTradeModal;

0 comments on commit 82c4bcb

Please sign in to comment.