Skip to content

Commit

Permalink
feat: modify observer (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 authored Apr 4, 2024
1 parent 55da48b commit 0c5cd86
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 11 deletions.
4 changes: 4 additions & 0 deletions .env template
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/app/hooks/use-ethereum-contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ interface UseEthereumContractsReturnType {
ethereumNetwork: EthereumNetwork
) => Promise<void>;
protocolContract: Contract | undefined;
observerProtocolContract: Contract | undefined;
dlcManagerContract: Contract | undefined;
dlcBTCContract: Contract | undefined;
contractsLoaded: boolean;
}

export function useEthereumContracts(): UseEthereumContractsReturnType {
const protocolContract = useRef<Contract | undefined>(undefined);
const observerProtocolContract = useRef<Contract | undefined>(undefined);
const dlcManagerContract = useRef<Contract | undefined>(undefined);
const dlcBTCContract = useRef<Contract | undefined>(undefined);

Expand All @@ -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',
Expand Down Expand Up @@ -95,6 +112,7 @@ export function useEthereumContracts(): UseEthereumContractsReturnType {
return {
getEthereumContracts,
protocolContract: protocolContract.current,
observerProtocolContract: observerProtocolContract.current,
dlcManagerContract: dlcManagerContract.current,
dlcBTCContract: dlcBTCContract.current,
contractsLoaded,
Expand Down
18 changes: 8 additions & 10 deletions src/app/hooks/use-ethereum-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -94,5 +92,5 @@ export function useEthereumObserver(): void {
});
});
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [protocolContract, dlcBTCContract, network]);
}, [observerProtocolContract, network]);
}
2 changes: 1 addition & 1 deletion src/app/hooks/use-ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
4 changes: 4 additions & 0 deletions src/app/providers/ethereum-context-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { ethers } from 'ethers';

export interface EthereumContextProviderType {
protocolContract: any;
observerProtocolContract: any;
dlcManagerContract: any;
dlcBTCContract: any;
getEthereumContracts: (
Expand All @@ -18,6 +19,7 @@ export interface EthereumContextProviderType {

export const EthereumContext = createContext<any>({
protocolContract: undefined,
observerProtocolContract: undefined,
dlcManagerContract: undefined,
dlcBTCContract: undefined,
getEthereumContracts: async (
Expand All @@ -32,6 +34,7 @@ export const EthereumContext = createContext<any>({
export function EthereumContextProvider({ children }: HasChildren): React.JSX.Element {
const {
protocolContract,
observerProtocolContract,
dlcManagerContract,
dlcBTCContract,
getEthereumContracts,
Expand All @@ -42,6 +45,7 @@ export function EthereumContextProvider({ children }: HasChildren): React.JSX.El
<EthereumContext.Provider
value={{
protocolContract,
observerProtocolContract,
dlcManagerContract,
dlcBTCContract,
getEthereumContracts,
Expand Down

0 comments on commit 0c5cd86

Please sign in to comment.