Skip to content

Commit 8dc3d7b

Browse files
authored
Merge pull request #475 from WatchItDev/app/next/fixes
feat: added new events to transactions table ('locked', 'claimed', 'reserved', 'collected', 'released')
2 parents c10e1c1 + b9e81e5 commit 8dc3d7b

File tree

8 files changed

+426
-244
lines changed

8 files changed

+426
-244
lines changed

src/App.tsx

Lines changed: 88 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,17 @@ import { AuthProvider } from '@src/auth/context/web3Auth';
4545
import { ResponsiveOverlay } from '@src/components/responsive-overlay';
4646

4747
import { Buffer } from 'buffer';
48-
import { Provider } from 'react-redux';
48+
import {Provider, useDispatch, useSelector} from 'react-redux';
4949
import { MetaMaskProvider } from '@metamask/sdk-react';
50+
import {useNotifications} from "@src/hooks/use-notifications.ts";
51+
import {useSnackbar} from "notistack";
52+
import {useEffect} from "react";
53+
import {setGlobalNotifier} from "@notifications/internal-notifications.ts";
54+
import {publicClientWebSocket} from "@src/clients/viem/publicClient.ts";
55+
import {GLOBAL_CONSTANTS} from "@src/config-global.ts";
56+
import LedgerVaultAbi from "@src/config/abi/LedgerVault.json";
57+
import {setBlockchainEvents} from "@redux/blockchain-events";
58+
import {subscribeToNotifications} from "@src/utils/subscribe-notifications-supabase.ts";
5059

5160
window.Buffer = Buffer;
5261

@@ -103,10 +112,7 @@ export default function App() {
103112
<ThemeProvider>
104113
<MotionLazy>
105114
<SnackbarProvider>
106-
<SettingsDrawer />
107-
<ProgressBar />
108-
<Router />
109-
<ResponsiveOverlay />
115+
<AppContent />
110116
</SnackbarProvider>
111117
</MotionLazy>
112118
</ThemeProvider>
@@ -117,3 +123,80 @@ export default function App() {
117123
</MetaMaskProvider>
118124
);
119125
}
126+
127+
128+
interface EventArgs {
129+
recipient?: string;
130+
origin?: string;
131+
}
132+
const AppContent = () => {
133+
const dispatch = useDispatch();
134+
const sessionData = useSelector((state: any) => state.auth.session);
135+
const { getNotifications } = useNotifications();
136+
const { enqueueSnackbar } = useSnackbar();
137+
138+
useEffect(() => {
139+
// Set the global reference so we can call notify(...) anywhere.
140+
setGlobalNotifier(enqueueSnackbar);
141+
}, [enqueueSnackbar]);
142+
143+
const watchEvent = (eventName: string, args: EventArgs, logText: string) => {
144+
const results = publicClientWebSocket.watchContractEvent({
145+
address: GLOBAL_CONSTANTS.LEDGER_VAULT_ADDRESS,
146+
abi: LedgerVaultAbi.abi,
147+
eventName,
148+
args,
149+
onLogs: (logs) => {
150+
console.log(logText, logs);
151+
dispatch(setBlockchainEvents(logs));
152+
},
153+
});
154+
155+
console.log('Watching', eventName, 'events');
156+
console.log('Results', results);
157+
158+
return results;
159+
};
160+
161+
useEffect(() => {
162+
if (!sessionData?.address) return;
163+
164+
const events = [
165+
{ name: 'FundsDeposited', args: { recipient: sessionData?.address }, logText: 'New deposit (user as recipient):' },
166+
{ name: 'FundsWithdrawn', args: { origin: sessionData?.address }, logText: 'New withdraw (user as origin):' },
167+
{ name: 'FundsTransferred', args: { origin: sessionData?.address }, logText: 'New transfer from me:' },
168+
{ name: 'FundsTransferred', args: { recipient: sessionData?.address }, logText: 'New transfer to me:' },
169+
{ name: 'FundsLocked', args: { account: sessionData?.address }, logText: 'New funds locked:' },
170+
{ name: 'FundsClaimed', args: { claimer: sessionData?.address }, logText: 'New funds claimed:' },
171+
{ name: 'FundsReserved', args: { from: sessionData?.address }, logText: 'New funds reserved:' },
172+
{ name: 'FundsCollected', args: { from: sessionData?.address }, logText: 'New funds collected:' },
173+
{ name: 'FundsReleased', args: { to: sessionData?.address }, logText: 'New funds released:' },
174+
];
175+
176+
const unwatchers = events.map(event => watchEvent(event.name, event.args, event.logText));
177+
178+
return () => {
179+
unwatchers.forEach(unwatch => unwatch());
180+
};
181+
}, [sessionData?.address]);
182+
183+
184+
useEffect(() => {
185+
if (sessionData?.profile?.id) {
186+
// Subscribe to notifications channel
187+
subscribeToNotifications(sessionData?.profile?.id, dispatch, ['notifications']);
188+
189+
// Set the notifications in first render
190+
getNotifications(sessionData?.profile?.id).then(() => {});
191+
}
192+
}, [sessionData?.profile?.id, dispatch]);
193+
194+
return (
195+
<>
196+
<SettingsDrawer />
197+
<ProgressBar />
198+
<Router />
199+
<ResponsiveOverlay />
200+
</>
201+
)
202+
}

0 commit comments

Comments
 (0)