From 0c5cd863ccee5df74169890722983469fa7bc11c Mon Sep 17 00:00:00 2001 From: Polybius93 <99192647+Polybius93@users.noreply.github.com> Date: Thu, 4 Apr 2024 15:50:50 +0200 Subject: [PATCH] feat: modify observer (#73) --- .env template | 4 ++++ src/app/hooks/use-ethereum-contracts.ts | 18 ++++++++++++++++++ src/app/hooks/use-ethereum-observer.ts | 18 ++++++++---------- src/app/hooks/use-ethereum.ts | 2 +- .../providers/ethereum-context-provider.tsx | 4 ++++ 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/.env template b/.env template index d0ad74f8..5c8ad20c 100644 --- a/.env template +++ b/.env template @@ -25,6 +25,7 @@ # VITE_ETHEREUM_DEPLOYMENT_BRANCH=dev # VITE_ETHEREUM_DEPLOYMENT_VERSION=1 # VITE_ENABLED_ETHEREUM_NETWORKS='11155111' +; VITE_ETHEREUM_ARB_SEPOLIA_OBSERVER_NODE= # VITE_ATTESTOR_API_URLS= # VITE_BITCOIN_NETWORK=regtest # VITE_BITCOIN_EXPLORER_API_URL=https://devnet.dlc.link/electrs @@ -37,6 +38,7 @@ # VITE_ETHEREUM_DEPLOYMENT_BRANCH=dev # VITE_ETHEREUM_DEPLOYMENT_VERSION=1 # VITE_ENABLED_ETHEREUM_NETWORKS='11155111' +; VITE_ETHEREUM_ARB_SEPOLIA_OBSERVER_NODE= # VITE_ATTESTOR_API_URLS= # VITE_BITCOIN_NETWORK=regtest # VITE_BITCOIN_EXPLORER_API_URL=https://devnet.dlc.link/electrs @@ -49,6 +51,7 @@ # VITE_ETHEREUM_DEPLOYMENT_BRANCH=testnet # VITE_ETHEREUM_DEPLOYMENT_VERSION=1 # VITE_ENABLED_ETHEREUM_NETWORKS='11155111' +; VITE_ETHEREUM_ARB_SEPOLIA_OBSERVER_NODE= # VITE_ATTESTOR_API_URLS= # VITE_BITCOIN_NETWORK=testnet # VITE_BITCOIN_EXPLORER_API_URL=https://mempool.space/testnet @@ -61,6 +64,7 @@ # VITE_ETHEREUM_DEPLOYMENT_BRANCH=mainnet # VITE_ETHEREUM_DEPLOYMENT_VERSION=1 # VITE_ENABLED_ETHEREUM_NETWORKS='11155111' +; VITE_ETHEREUM_ARB_SEPOLIA_OBSERVER_NODE= # VITE_ATTESTOR_API_URLS= # VITE_BITCOIN_NETWORK=mainnet # VITE_BITCOIN_EXPLORER_API_URL=https://mempool.space diff --git a/src/app/hooks/use-ethereum-contracts.ts b/src/app/hooks/use-ethereum-contracts.ts index 69336d44..55794cb8 100644 --- a/src/app/hooks/use-ethereum-contracts.ts +++ b/src/app/hooks/use-ethereum-contracts.ts @@ -11,6 +11,7 @@ interface UseEthereumContractsReturnType { ethereumNetwork: EthereumNetwork ) => Promise; protocolContract: Contract | undefined; + observerProtocolContract: Contract | undefined; dlcManagerContract: Contract | undefined; dlcBTCContract: Contract | undefined; contractsLoaded: boolean; @@ -18,6 +19,7 @@ interface UseEthereumContractsReturnType { export function useEthereumContracts(): UseEthereumContractsReturnType { const protocolContract = useRef(undefined); + const observerProtocolContract = useRef(undefined); const dlcManagerContract = useRef(undefined); const dlcBTCContract = useRef(undefined); @@ -42,6 +44,21 @@ export function useEthereumContracts(): UseEthereumContractsReturnType { protocolContract.current = contract; } + if (!observerProtocolContract.current) { + const observerProtocolContractData = await fetchEthereumDeploymentPlan( + 'TokenManager', + ethereumNetwork + ); + const contract = new ethers.Contract( + observerProtocolContractData.contract.address, + observerProtocolContractData.contract.abi, + new ethers.providers.WebSocketProvider( + import.meta.env.VITE_ETHEREUM_ARB_SEPOLIA_OBSERVER_NODE + ) + ); + observerProtocolContract.current = contract; + } + if (!dlcManagerContract.current) { const dlcManagerContractData = await fetchEthereumDeploymentPlan( 'DLCManager', @@ -95,6 +112,7 @@ export function useEthereumContracts(): UseEthereumContractsReturnType { return { getEthereumContracts, protocolContract: protocolContract.current, + observerProtocolContract: observerProtocolContract.current, dlcManagerContract: dlcManagerContract.current, dlcBTCContract: dlcBTCContract.current, contractsLoaded, diff --git a/src/app/hooks/use-ethereum-observer.ts b/src/app/hooks/use-ethereum-observer.ts index 7ed66e6f..a3103049 100644 --- a/src/app/hooks/use-ethereum-observer.ts +++ b/src/app/hooks/use-ethereum-observer.ts @@ -13,19 +13,18 @@ import { useEthereumContext } from './use-ethereum-context'; export function useEthereumObserver(): void { const dispatch = useDispatch(); - const { protocolContract, dlcBTCContract } = useEthereumContext(); + const { observerProtocolContract } = useEthereumContext(); const { getVault } = useEthereum(); const { address, network } = useSelector((state: RootState) => state.account); useEffect(() => { - if (!protocolContract || !dlcBTCContract) return; + if (!observerProtocolContract) return; console.log(`Listening to [${network?.name}]`); - console.log(`Listening to [${protocolContract.address}]`); - console.log(`Listening to [${dlcBTCContract.address}]`); + console.log(`Listening to [${observerProtocolContract.address}]`); - protocolContract.on('SetupVault', async (...args: any[]) => { + observerProtocolContract.on('SetupVault', async (...args: any[]) => { const vaultOwner: string = args[2]; if (vaultOwner.toLowerCase() !== address) return; @@ -39,7 +38,7 @@ export function useEthereumObserver(): void { }); }); - protocolContract.on('CloseVault', async (...args: any[]) => { + observerProtocolContract.on('CloseVault', async (...args: any[]) => { const vaultOwner: string = args[1]; if (vaultOwner.toLowerCase() !== address) return; @@ -53,8 +52,7 @@ export function useEthereumObserver(): void { }); }); - protocolContract.on('SetStatusFunded', async (...args: any[]) => { - console.log('SetStatusFunded', args); + observerProtocolContract.on('SetStatusFunded', async (...args: any[]) => { const vaultOwner = args[2]; if (vaultOwner.toLowerCase() !== address) return; @@ -74,7 +72,7 @@ export function useEthereumObserver(): void { }); }); - protocolContract.on('PostCloseDLCHandler', async (...args: any[]) => { + observerProtocolContract.on('PostCloseDLCHandler', async (...args: any[]) => { const vaultOwner = args[2]; if (vaultOwner.toLowerCase() !== address) return; @@ -94,5 +92,5 @@ export function useEthereumObserver(): void { }); }); // eslint-disable-next-line react-hooks/exhaustive-deps - }, [protocolContract, dlcBTCContract, network]); + }, [observerProtocolContract, network]); } diff --git a/src/app/hooks/use-ethereum.ts b/src/app/hooks/use-ethereum.ts index 698fde76..1ee992ff 100644 --- a/src/app/hooks/use-ethereum.ts +++ b/src/app/hooks/use-ethereum.ts @@ -191,7 +191,7 @@ export function useEthereum(): UseEthereumReturnType { return; } catch (error) { // eslint-disable-next-line no-console - console.log(`Error fetching vault ${vaultUUID}. Retrying...`); + console.log(`Vault with uuid: ${vaultUUID} is not yet updated. Retrying...`); } await new Promise(resolve => setTimeout(resolve, retryInterval)); } diff --git a/src/app/providers/ethereum-context-provider.tsx b/src/app/providers/ethereum-context-provider.tsx index 0b096a71..f2c173bc 100644 --- a/src/app/providers/ethereum-context-provider.tsx +++ b/src/app/providers/ethereum-context-provider.tsx @@ -7,6 +7,7 @@ import { ethers } from 'ethers'; export interface EthereumContextProviderType { protocolContract: any; + observerProtocolContract: any; dlcManagerContract: any; dlcBTCContract: any; getEthereumContracts: ( @@ -18,6 +19,7 @@ export interface EthereumContextProviderType { export const EthereumContext = createContext({ protocolContract: undefined, + observerProtocolContract: undefined, dlcManagerContract: undefined, dlcBTCContract: undefined, getEthereumContracts: async ( @@ -32,6 +34,7 @@ export const EthereumContext = createContext({ export function EthereumContextProvider({ children }: HasChildren): React.JSX.Element { const { protocolContract, + observerProtocolContract, dlcManagerContract, dlcBTCContract, getEthereumContracts, @@ -42,6 +45,7 @@ export function EthereumContextProvider({ children }: HasChildren): React.JSX.El