Skip to content

Commit

Permalink
Merge pull request #475 from WatchItDev/app/next/fixes
Browse files Browse the repository at this point in the history
feat: added new events to transactions table ('locked', 'claimed', 'reserved', 'collected', 'released')
  • Loading branch information
geolffreym authored Jan 22, 2025
2 parents c10e1c1 + b9e81e5 commit 8dc3d7b
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 244 deletions.
93 changes: 88 additions & 5 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,17 @@ import { AuthProvider } from '@src/auth/context/web3Auth';
import { ResponsiveOverlay } from '@src/components/responsive-overlay';

import { Buffer } from 'buffer';
import { Provider } from 'react-redux';
import {Provider, useDispatch, useSelector} from 'react-redux';
import { MetaMaskProvider } from '@metamask/sdk-react';
import {useNotifications} from "@src/hooks/use-notifications.ts";
import {useSnackbar} from "notistack";
import {useEffect} from "react";
import {setGlobalNotifier} from "@notifications/internal-notifications.ts";
import {publicClientWebSocket} from "@src/clients/viem/publicClient.ts";
import {GLOBAL_CONSTANTS} from "@src/config-global.ts";
import LedgerVaultAbi from "@src/config/abi/LedgerVault.json";
import {setBlockchainEvents} from "@redux/blockchain-events";
import {subscribeToNotifications} from "@src/utils/subscribe-notifications-supabase.ts";

window.Buffer = Buffer;

Expand Down Expand Up @@ -103,10 +112,7 @@ export default function App() {
<ThemeProvider>
<MotionLazy>
<SnackbarProvider>
<SettingsDrawer />
<ProgressBar />
<Router />
<ResponsiveOverlay />
<AppContent />
</SnackbarProvider>
</MotionLazy>
</ThemeProvider>
Expand All @@ -117,3 +123,80 @@ export default function App() {
</MetaMaskProvider>
);
}


interface EventArgs {
recipient?: string;
origin?: string;
}
const AppContent = () => {
const dispatch = useDispatch();
const sessionData = useSelector((state: any) => state.auth.session);
const { getNotifications } = useNotifications();
const { enqueueSnackbar } = useSnackbar();

useEffect(() => {
// Set the global reference so we can call notify(...) anywhere.
setGlobalNotifier(enqueueSnackbar);
}, [enqueueSnackbar]);

const watchEvent = (eventName: string, args: EventArgs, logText: string) => {
const results = publicClientWebSocket.watchContractEvent({
address: GLOBAL_CONSTANTS.LEDGER_VAULT_ADDRESS,
abi: LedgerVaultAbi.abi,
eventName,
args,
onLogs: (logs) => {
console.log(logText, logs);
dispatch(setBlockchainEvents(logs));
},
});

console.log('Watching', eventName, 'events');
console.log('Results', results);

return results;
};

useEffect(() => {
if (!sessionData?.address) return;

const events = [
{ name: 'FundsDeposited', args: { recipient: sessionData?.address }, logText: 'New deposit (user as recipient):' },
{ name: 'FundsWithdrawn', args: { origin: sessionData?.address }, logText: 'New withdraw (user as origin):' },
{ name: 'FundsTransferred', args: { origin: sessionData?.address }, logText: 'New transfer from me:' },
{ name: 'FundsTransferred', args: { recipient: sessionData?.address }, logText: 'New transfer to me:' },
{ name: 'FundsLocked', args: { account: sessionData?.address }, logText: 'New funds locked:' },
{ name: 'FundsClaimed', args: { claimer: sessionData?.address }, logText: 'New funds claimed:' },
{ name: 'FundsReserved', args: { from: sessionData?.address }, logText: 'New funds reserved:' },
{ name: 'FundsCollected', args: { from: sessionData?.address }, logText: 'New funds collected:' },
{ name: 'FundsReleased', args: { to: sessionData?.address }, logText: 'New funds released:' },
];

const unwatchers = events.map(event => watchEvent(event.name, event.args, event.logText));

return () => {
unwatchers.forEach(unwatch => unwatch());
};
}, [sessionData?.address]);


useEffect(() => {
if (sessionData?.profile?.id) {
// Subscribe to notifications channel
subscribeToNotifications(sessionData?.profile?.id, dispatch, ['notifications']);

// Set the notifications in first render
getNotifications(sessionData?.profile?.id).then(() => {});
}
}, [sessionData?.profile?.id, dispatch]);

return (
<>
<SettingsDrawer />
<ProgressBar />
<Router />
<ResponsiveOverlay />
</>
)
}
Loading

0 comments on commit 8dc3d7b

Please sign in to comment.