-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moved uniswap logic to dedicated hook
- Loading branch information
Showing
3 changed files
with
58 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
packages/commonwealth/client/scripts/views/modals/TradeTokenModel/UniswapTradeModal/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
import { TradingConfig } from '../types'; | ||
|
||
export type UseUniswapTradeModalProps = { | ||
tradeConfig: TradingConfig; | ||
}; |
49 changes: 49 additions & 0 deletions
49
...lth/client/scripts/views/modals/TradeTokenModel/UniswapTradeModal/useUniswapTradeModal.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |