Skip to content

Commit

Permalink
wip 2
Browse files Browse the repository at this point in the history
  • Loading branch information
dianasavvatina committed Oct 25, 2024
1 parent 886a7e1 commit 77af87f
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
10 changes: 4 additions & 6 deletions apps/web/src/components/WalletConnect/WalletConnectProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { type NetworkType } from "@airgap/beacon-wallet";
import { useToast } from "@chakra-ui/react";
import { type WalletKitTypes } from "@reown/walletkit";
import { useQueryClient } from "@tanstack/react-query";
import { useDynamicModalContext } from "@umami/components";
import {
type State,
createWalletKit,
useAsyncActionHandler,
useAvailableNetworks,
useGetAllWcTopicToAcc,
useRemoveWcConnection,
useRemoveWcTopicToAcc,
useWcPeers,
walletKit,
} from "@umami/state";
Expand All @@ -30,8 +29,7 @@ export const WalletConnectProvider = ({ children }: PropsWithChildren) => {
const availableNetworks: Network[] = useAvailableNetworks();

const handleWcRequest = useHandleWcRequest();
const removeWcPeer = useRemoveWcConnection();
const queryClient = useQueryClient();
const removeWcTopicToAcc = useRemoveWcTopicToAcc();

const slice = useGetAllWcTopicToAcc();
const { peers, refresh } = useWcPeers();
Expand Down Expand Up @@ -77,7 +75,7 @@ export const WalletConnectProvider = ({ children }: PropsWithChildren) => {
} else {
console.error(`Session deleted by dApp but not known locally. Topic: ${topic}`);
}
removeWcPeer(topic);
await removeWcTopicToAcc(topic);

// update peer list in the UI
await refresh();
Expand All @@ -100,7 +98,7 @@ export const WalletConnectProvider = ({ children }: PropsWithChildren) => {
status: "info",
});
}
await handleWcRequest(event, peers);
await handleWcRequest(event, peers, sliceTopicToAcc);
}).catch(async error => {
const { id, topic } = event;
console.error("WalletConnect session request failed", event, peers, error);
Expand Down
10 changes: 5 additions & 5 deletions apps/web/src/components/WalletConnect/useHandleWcRequest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { useToast } from "@chakra-ui/react";
import { useDynamicModalContext } from "@umami/components";
import { type ImplicitAccount, estimate, toAccountOperations } from "@umami/core";
import {
type State,
useAsyncActionHandler,
useFindNetwork,
useGetAllWcTopicToAcc,
useGetOwnedAccountSafe,
walletKit,
} from "@umami/state";
Expand All @@ -28,7 +28,6 @@ export const useHandleWcRequest = () => {
const getAccount = useGetOwnedAccountSafe();
const findNetwork = useFindNetwork();
// const { peers } = useWcPeers();
const state = useGetAllWcTopicToAcc();
const toast = useToast();

return async (
Expand All @@ -42,7 +41,8 @@ export const useHandleWcRequest = () => {
};
chainId: string;
}>,
peers: Record<string, SessionTypes.Struct>
peers: Record<string, SessionTypes.Struct>,
sliceTopicToAcc: State
) => {
await handleAsyncActionUnsafe(
async () => {
Expand Down Expand Up @@ -74,10 +74,10 @@ export const useHandleWcRequest = () => {
if (!request.params.account) {
throw new Error("Missing account in request");
}
if (!(topic in state)) {
if (!(topic in sliceTopicToAcc)) {
throw new Error(`Unknown dapp: ${topic}`);
}
const dappInfo = state[topic];
const dappInfo = sliceTopicToAcc[topic];
if (request.params.account !== dappInfo.accountPkh) {
throw new Error(`Unknown account: ${request.params.account}, topic: ${topic}`);
}
Expand Down
10 changes: 6 additions & 4 deletions packages/state/src/hooks/WalletConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,21 +58,23 @@ export const useAddWcTopicToAcc = () => {
};

// remove from slice
export const useRemoveWcConnection = () => {
export const useRemoveWcTopicToAcc = () => {
const dispatch = useDispatch();
return (topic: string) => dispatch(wcActions.removeConnection(topic));
return async (topic: string) => {
console.log("removing connection from sliceTopicToAcc", topic);
dispatch(wcActions.removeConnection(topic));}
};

// remove from WC and slice
export const useRemoveWcPeer = () => {
const { refresh } = useWcPeers();
const removeConnectionFromWcSlice = useRemoveWcConnection();
const removeWcTopicToAcc = useRemoveWcTopicToAcc();

return async (params: { topic: string; reason: ErrorResponse }) => {
console.log("disconnecting WC session", params);
await walletKit
.disconnectSession(params)
.then(() => removeConnectionFromWcSlice(params.topic))
.then(() => removeWcTopicToAcc(params.topic))
.then(async () => await refresh());
};
};
Expand Down

0 comments on commit 77af87f

Please sign in to comment.