Skip to content

Commit

Permalink
broken_again
Browse files Browse the repository at this point in the history
  • Loading branch information
dianasavvatina committed Oct 25, 2024
1 parent a08b56d commit 886a7e1
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 109 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export const useSignWithBeacon = ({
fee: totalFee(form.watch("executeParams")),
isSigning,
onSign,
network: headerProps.network,
form,
network: headerProps.network
};
};
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export const useSignWithWalletConnect = ({
isSigning: false,
onSign: async () => {},
network: null,
form,
};
}

Expand All @@ -50,6 +49,5 @@ export const useSignWithWalletConnect = ({
isSigning,
onSign,
network: headerProps.network,
form,
};
};
5 changes: 3 additions & 2 deletions apps/web/src/components/SendFlow/sdk/ContractCallSignPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
ModalFooter,
} from "@chakra-ui/react";
import { type ContractCall } from "@umami/core";
import { FormProvider } from "react-hook-form";
import { FormProvider, useForm } from "react-hook-form";

import { Header } from "./Header";
import { useColor } from "../../../styles/useColor";
Expand All @@ -35,7 +35,8 @@ export const ContractCallSignPage = (
args,
} = operation.operations[0] as ContractCall;
const color = useColor();
const { isSigning, onSign, network, fee, form } = calculatedSignProps;
const { isSigning, onSign, network, fee } = calculatedSignProps;
const form = useForm({ defaultValues: { executeParams: operation.estimates } });

return (
<FormProvider {...form}>
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/components/SendFlow/sdk/DelegationSignPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Flex, FormLabel, ModalBody, ModalContent, ModalFooter } from "@chakra-ui/react";
import { type Delegation } from "@umami/core";
import { FormProvider } from "react-hook-form";
import { FormProvider, useForm } from "react-hook-form";

import { Header } from "./Header";
import { AddressTile } from "../../AddressTile/AddressTile";
Expand All @@ -15,7 +15,8 @@ export const DelegationSignPage = (
) => {
const { recipient } = operation.operations[0] as Delegation;

const { isSigning, onSign, network, fee, form } = calculatedSignProps;
const { isSigning, onSign, network, fee } = calculatedSignProps;
const form = useForm({ defaultValues: { executeParams: operation.estimates } });

return (
<FormProvider {...form}>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Flex, FormLabel, ModalBody, ModalContent, ModalFooter } from "@chakra-ui/react";
import { useAccountTotalFinalizableUnstakeAmount } from "@umami/state";
import { FormProvider } from "react-hook-form";
import { FormProvider, useForm } from "react-hook-form";

import { Header } from "./Header";
import { AddressTile } from "../../AddressTile/AddressTile";
Expand All @@ -13,7 +13,8 @@ export const FinalizeUnstakeSignPage = (
{ operation, headerProps }: SdkSignPageProps,
calculatedSignProps: CalculatedSignProps
) => {
const { isSigning, onSign, network, fee, form } = calculatedSignProps;
const { isSigning, onSign, network, fee } = calculatedSignProps;
const form = useForm({ defaultValues: { executeParams: operation.estimates } });
const totalFinalizableAmount = useAccountTotalFinalizableUnstakeAmount(
operation.signer.address.pkh
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import {
} from "@chakra-ui/react";
import { type ContractOrigination } from "@umami/core";
import { capitalize } from "lodash";
import { FormProvider } from "react-hook-form";
import { FormProvider, useForm } from "react-hook-form";

import { CodeSandboxIcon } from "../../../assets/icons";
import { useColor } from "../../../styles/useColor";
Expand All @@ -31,9 +31,10 @@ export const OriginationOperationSignPage = (
{ operation, headerProps }: SdkSignPageProps,
calculatedSignProps: CalculatedSignProps
) => {
const { isSigning, onSign, network, form, fee } = calculatedSignProps;
const { isSigning, onSign, network, fee } = calculatedSignProps;
const color = useColor();
const { code, storage } = operation.operations[0] as ContractOrigination;
const form = useForm({ defaultValues: { executeParams: operation.estimates } });

return (
<FormProvider {...form}>
Expand All @@ -48,7 +49,7 @@ export const OriginationOperationSignPage = (
Network:
</Heading>
<Text color={color("700")} fontWeight="400" size="sm">
{capitalize(headerProps.networkName)}
{capitalize(headerProps.network.name)}
</Text>
</Flex>
</ModalHeader>
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/components/SendFlow/sdk/StakeSignPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Flex, FormLabel, ModalBody, ModalContent, ModalFooter } from "@chakra-ui/react";
import { type Stake } from "@umami/core";
import { FormProvider } from "react-hook-form";
import { FormProvider, useForm } from "react-hook-form";

import { Header } from "./Header";
import { AddressTile } from "../../AddressTile/AddressTile";
Expand All @@ -15,7 +15,8 @@ export const StakeSignPage = (
) => {
const { amount: mutezAmount } = operation.operations[0] as Stake;

const { isSigning, onSign, network, fee, form } = calculatedSignProps;
const { isSigning, onSign, network, fee } = calculatedSignProps;
const form = useForm({ defaultValues: { executeParams: operation.estimates } });

return (
<FormProvider {...form}>
Expand Down
2 changes: 2 additions & 0 deletions apps/web/src/components/SendFlow/sdk/TezSignPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ export const TezSignPage = (
calculatedSignProps: CalculatedSignProps
) => {
const { amount: mutezAmount, recipient } = operation.operations[0] as TezTransfer;
console.log("TezSignPage", calculatedSignProps);

const { isSigning, onSign, network, fee } = calculatedSignProps;
const form = useForm({ defaultValues: { executeParams: operation.estimates } });


return (
<FormProvider {...form}>
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/components/SendFlow/sdk/UndelegationSignPage.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Flex, FormLabel, ModalBody, ModalContent, ModalFooter } from "@chakra-ui/react";
import { FormProvider } from "react-hook-form";
import { FormProvider, useForm } from "react-hook-form";

import { Header } from "./Header";
import { AddressTile } from "../../AddressTile/AddressTile";
Expand All @@ -12,7 +12,8 @@ export const UndelegationSignPage = (
{ operation, headerProps }: SdkSignPageProps,
calculatedSignProps: CalculatedSignProps
) => {
const { isSigning, onSign, network, form, fee } = calculatedSignProps;
const { isSigning, onSign, network, fee } = calculatedSignProps;
const form = useForm({ defaultValues: { executeParams: operation.estimates } });

return (
<FormProvider {...form}>
Expand Down
5 changes: 3 additions & 2 deletions apps/web/src/components/SendFlow/sdk/UnstakeSignPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Flex, FormLabel, ModalBody, ModalContent, ModalFooter } from "@chakra-ui/react";
import { type Unstake } from "@umami/core";
import { FormProvider } from "react-hook-form";
import { FormProvider, useForm } from "react-hook-form";

import { Header } from "./Header";
import { AddressTile } from "../../AddressTile/AddressTile";
Expand All @@ -15,7 +15,8 @@ export const UnstakeSignPage = (
) => {
const { amount: mutezAmount } = operation.operations[0] as Unstake;

const { isSigning, onSign, network, fee, form } = calculatedSignProps;
const { isSigning, onSign, network, fee } = calculatedSignProps;
const form = useForm({ defaultValues: { executeParams: operation.estimates } });

return (
<FormProvider {...form}>
Expand Down
1 change: 0 additions & 1 deletion apps/web/src/components/SendFlow/utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export type CalculatedSignProps = {
isSigning: boolean;
onSign: (tezosToolkit: TezosToolkit) => Promise<void>;
network: any;
form: any;
};

export type sdkType = "beacon" | "walletconnect";
Expand Down
155 changes: 66 additions & 89 deletions apps/web/src/components/WalletConnect/WalletConnectProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,88 +23,20 @@ import { SessionProposalModal } from "./SessionProposalModal";
import { useHandleWcRequest } from "./useHandleWcRequest";

export const WalletConnectProvider = ({ children }: PropsWithChildren) => {
const onSessionProposal = useOnSessionProposal();
const onSessionDelete = useOnSessionDelete();
// const onSessionRequest = useOnSessionRequest();
const slice = useGetAllWcTopicToAcc();

const queryClient = useQueryClient();
// const peers = queryClient.getQueryData(["wcPeers"]);
// console.log("WalletConnectProvider, wcPeers from query", peers);

// const onSessionRequest = () => {
const { handleAsyncActionUnsafe } = useAsyncActionHandler();
const { peers } = useWcPeers();

const handleWcRequest = useHandleWcRequest();
const toast = useToast();

// return (event: WalletKitTypes.SessionRequest) =>
const onSessionRequest = (event: WalletKitTypes.SessionRequest, peers: Record<string, SessionTypes.Struct> | undefined, sliceTopicToAcc: State) =>
handleAsyncActionUnsafe(async () => {
if (!peers) {
throw new Error("No peers found in the state");
}
console.log("Session request, sliceTopicToAcc, wcPeers", event, sliceTopicToAcc, peers);
if (event.topic in sliceTopicToAcc) {
console.log("Session request from dApp", sliceTopicToAcc[event.topic], peers[event.topic]);
toast({
description: `Session request from dApp ${peers[event.topic].peer.metadata.name}`,
status: "info",
});
}
await handleWcRequest(event, peers);
}).catch(async error => {
const { id, topic } = event;
const peers: Record<string, SessionTypes.Struct> | undefined = queryClient.getQueryData(["wcPeers"]);
if (!peers) {
throw new Error("No peers found in the state");
}
console.error("WalletConnect session request failed", event, peers, error);
if (event.topic in peers) {
toast({
description: `Session request for dApp ${peers[topic].peer.metadata.name} failed. It was rejected.`,
status: "error",
});
} else {
toast({
description: `Session request for dApp ${topic} failed. It was rejected. Peer not found by topic.`,
status: "error",
});
}
// dApp is waiting so we need to notify it
const response = formatJsonRpcError(id, getSdkError("INVALID_METHOD").message);
await walletKit.respondSessionRequest({ topic, response });
});
// };


useEffect(() => {
// const peers: Record<string, SessionTypes.Struct> | undefined = queryClient.getQueryData(["wcPeers"]);

const initializeWallet = async () => {
await createWalletKit();
walletKit.on("session_proposal", event => void onSessionProposal(event));
walletKit.on("session_request", event => void onSessionRequest(event, peers, slice));
walletKit.on("session_delete", event => void onSessionDelete(event));
};
void initializeWallet();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() =>{
console.log("WalletConnectProvider useEffect(), PEERS and SLICE", peers, slice);
}, [peers, slice])

return children;
};

const useOnSessionProposal = () => {
const { handleAsyncActionUnsafe } = useAsyncActionHandler();
const { handleAsyncAction, handleAsyncActionUnsafe } = useAsyncActionHandler();
const { openWith } = useDynamicModalContext();
const toast = useToast();

const availableNetworks: Network[] = useAvailableNetworks();

return (proposal: WalletKitTypes.SessionProposal) =>
const handleWcRequest = useHandleWcRequest();
const removeWcPeer = useRemoveWcConnection();
const queryClient = useQueryClient();

const slice = useGetAllWcTopicToAcc();
const { peers, refresh } = useWcPeers();

const onSessionProposal = (proposal: WalletKitTypes.SessionProposal) =>
handleAsyncActionUnsafe(async () => {
// dApp sends in the session proposal the required networks and the optional networks.
// The response must contain all the required networks but Umami supports just one per request.
Expand Down Expand Up @@ -133,17 +65,8 @@ const useOnSessionProposal = () => {
// dApp is waiting so we need to notify it
await walletKit.rejectSession({ id: proposal.id, reason: getSdkError("UNSUPPORTED_CHAINS") });
});
};



const useOnSessionDelete = () => {
const { handleAsyncAction } = useAsyncActionHandler();
const { peers, refresh } = useWcPeers();
const removeWcPeer = useRemoveWcConnection();
const toast = useToast();

return (event: WalletKitTypes.SessionDelete) =>
const onSessionDelete = (event: WalletKitTypes.SessionDelete) =>
handleAsyncAction(async () => {
const { topic } = event;
if (topic in peers) {
Expand All @@ -159,6 +82,60 @@ const useOnSessionDelete = () => {
// update peer list in the UI
await refresh();
});

const onSessionRequest = (
event: WalletKitTypes.SessionRequest,
peers: Record<string, SessionTypes.Struct> | undefined,
sliceTopicToAcc: State
) =>
handleAsyncActionUnsafe(async () => {
if (!peers) {
throw new Error("No peers found in the state");
}
console.log("Session request, sliceTopicToAcc, wcPeers", event, sliceTopicToAcc, peers);
if (event.topic in sliceTopicToAcc) {
console.log("Session request from dApp", sliceTopicToAcc[event.topic], peers[event.topic]);
toast({
description: `Session request from dApp ${peers[event.topic].peer.metadata.name}`,
status: "info",
});
}
await handleWcRequest(event, peers);
}).catch(async error => {
const { id, topic } = event;
console.error("WalletConnect session request failed", event, peers, error);
if (peers && event.topic in peers) {
toast({
description: `Session request for dApp ${peers[topic].peer.metadata.name} failed. It was rejected.`,
status: "error",
});
} else {
toast({
description: `Session request for dApp ${topic} failed. It was rejected. Peer not found by topic.`,
status: "error",
});
}
// dApp is waiting so we need to notify it
const response = formatJsonRpcError(id, getSdkError("INVALID_METHOD").message);
await walletKit.respondSessionRequest({ topic, response });
});

useEffect(() => {
const initializeWallet = async () => {
await createWalletKit();
walletKit.on("session_proposal", event => void onSessionProposal(event));
walletKit.on("session_request", event => void onSessionRequest(event, peers, slice));
walletKit.on("session_delete", event => void onSessionDelete(event));
};
void initializeWallet();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
console.log("WalletConnectProvider useEffect(), PEERS and SLICE", peers, slice);
}, [peers, slice]);

return children;
};

export const useOnWalletConnect = () => {
Expand Down

0 comments on commit 886a7e1

Please sign in to comment.