Skip to content

Commit

Permalink
update across the board ownership changes, url updates
Browse files Browse the repository at this point in the history
  • Loading branch information
iainnash committed Apr 21, 2024
1 parent 29f1d2f commit 3cda71d
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 50 deletions.
46 changes: 28 additions & 18 deletions src/app/NewSafeProposal.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import { Field, FieldArray, Formik } from "formik";
import { SafeInformation } from "../components/SafeInformation";
import { Card, View, Text, Button, useToast } from "reshaped";
import { SyntheticEvent, useCallback, useEffect, useState } from "react";
import {
SyntheticEvent,
createContext,
useCallback,
useEffect,
useState,
} from "react";
import { Address, Hex, formatEther } from "viem";
import { validateAddress, validateETH } from "../utils/validators";
import { GenericField } from "../components/GenericField";
Expand Down Expand Up @@ -407,6 +413,8 @@ const EditProposal = ({
);
};

export const ProposalContext = createContext<Proposal | undefined>(undefined);

export const NewSafeProposal = () => {
const [proposal, setProposal] = useState<undefined | Proposal>(
DEFAULT_PROPOSAL,
Expand All @@ -431,22 +439,24 @@ export const NewSafeProposal = () => {
);

return (
<View paddingTop={4} paddingBottom={8} gap={8}>
<SafeInformation>
{isEditing && (
<EditProposal
proposal={proposal}
setProposal={setProposal}
setIsEditing={setIsEditing}
/>
)}
{!isEditing && proposal && (
<ViewProposal
proposal={proposal}
handleEditClicked={handleEditClicked}
/>
)}
</SafeInformation>
</View>
<ProposalContext.Provider value={proposal}>
<View paddingTop={4} paddingBottom={8} gap={8}>
<SafeInformation>
{isEditing && (
<EditProposal
proposal={proposal}
setProposal={setProposal}
setIsEditing={setIsEditing}
/>
)}
{!isEditing && proposal && (
<ViewProposal
proposal={proposal}
handleEditClicked={handleEditClicked}
/>
)}
</SafeInformation>
</View>
</ProposalContext.Provider>
);
};
2 changes: 1 addition & 1 deletion src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Object.keys(contractNetworks).map((network) => {
return;
}
const viemChain = Object.values(chains).find(
(chain) => chain.id.toString() === network
(chain) => chain.id.toString() === network,
);

if (!viemChain) {
Expand Down
65 changes: 36 additions & 29 deletions src/components/SetOwnerModal.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { SyntheticEvent } from "react";
import { SyntheticEvent, useContext } from "react";
import { Button, Modal, Text, View, useToast } from "reshaped";
import { AddressView } from "./AddressView";
import { Address } from "viem";
import { Field, Form, Formik } from "formik";
import { GenericField } from "./GenericField";
import { yupAddress } from "../utils/validators";
import { number, object } from "yup";
import { useOutletContext, useSearchParams } from "react-router-dom";
import { useOutletContext } from "react-router-dom";
import { SafeContext } from "./Contexts";
import { ProposalContext } from "../app/NewSafeProposal";
import { useUpdateProposalViaQuery } from "../hooks/useUpdateProposalViaQuery";

export type OwnerAction =
| undefined
Expand Down Expand Up @@ -43,7 +45,9 @@ const ButtonPanel = ({
const AddOwnerModalContent = ({ onClose }: { onClose: () => void }) => {
const { safeInformation } = useOutletContext<SafeContext>();
const toast = useToast();
const [, setSearchParams] = useSearchParams();
const updateProposalQuery = useUpdateProposalViaQuery();
const currentProposal = useContext(ProposalContext);

return (
<Formik
initialValues={{ address: "0x", threshold: safeInformation?.threshold }}
Expand All @@ -58,16 +62,10 @@ const AddOwnerModalContent = ({ onClose }: { onClose: () => void }) => {
ownerAddress: address,
threshold: threshold,
});
setSearchParams({
proposal: JSON.stringify({
actions: [
{
data: addOwnerTx.data.data,
value: 0,
to: safeInformation.address,
},
],
}),
updateProposalQuery({
data: addOwnerTx.data.data,
value: "0",
to: safeInformation.address,
});
} catch (err: any) {
toast.show({ title: "Error Updating Safe", text: err.toString() });
Expand All @@ -76,7 +74,7 @@ const AddOwnerModalContent = ({ onClose }: { onClose: () => void }) => {
}}
>
<Form>
<Text variant="featured-2">Add Owner</Text>
<Text variant="featured-2">Add Signer</Text>
<Field name="address">
{GenericField({ label: "New User Address" })}
</Field>
Expand All @@ -102,25 +100,34 @@ const RemoveOwnerModalContent = ({
onClose: () => void;
target: string;
}) => {
const [_, setParams] = useSearchParams();
const { safeInformation } = useOutletContext<SafeContext>();
const updateProposalViaQuery = useUpdateProposalViaQuery();
const toaster = useToast();

const onSubmitClick = async ({ threshold }: any) => {
const removeOwnerTx = await safeInformation?.safeSdk.createRemoveOwnerTx({
ownerAddress: safeInformation!.address,
threshold: threshold,
});
if (!removeOwnerTx || !safeInformation) {
return;
try {
const removeOwnerTx = await safeInformation?.safeSdk2.createRemoveOwnerTx(
{
ownerAddress: target,
threshold: threshold,
},
);
if (!removeOwnerTx || !safeInformation) {
return;
}

updateProposalViaQuery({
data: removeOwnerTx.data.data,
value: "0",
to: safeInformation.address,
});
onClose();
} catch (err: any) {
toaster.show({
title: "Error Removing Owner",
text: `Error setting up transaction: ${err.toString()}`,
});
}
setParams({
proposal: JSON.stringify({
actions: [
{ data: removeOwnerTx.data, value: "0", to: safeInformation.address },
],
}),
});
onClose();
};
return (
<Formik
Expand Down
4 changes: 4 additions & 0 deletions src/hooks/useLoadProposalFromQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ export const useLoadProposalFromQuery = () => {
const [params] = useSearchParams();

useEffect(() => {
console.log("parsing params", params);

const targets = params.get("targets")?.split("|");
const calldatas = params.get("calldatas")?.split("|");
const values = params.get("values")?.split("|");
Expand All @@ -26,9 +28,11 @@ export const useLoadProposalFromQuery = () => {
data: calldatas[index]!,
value: (values && values[index]) || "0",
}));
console.log({ actions, txt: "setting proposal" });
setProposal({ actions });
}
}, [params, setProposal]);

console.log({ proposal });
return proposal;
};
8 changes: 6 additions & 2 deletions src/hooks/useSetParamsFromQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ export const useSetParamsFromQuery = () => {
if (!proposal.actions?.length) {
return;
}
console.log("setting params", proposal.actions);
setParams({
targets: proposal.actions!.map((action) => action.to).join("|"),
data: proposal.actions!.map((action) => action.data).join("|"),
calldatas: proposal.actions!.map((action) => action.data).join("|"),
value: proposal.actions!.map((action) => action.value).join("|"),
...(proposal.nonce
? {
nonce: proposal.nonce.toString(),
}
: {}),
});
},
[setParams],
Expand Down
19 changes: 19 additions & 0 deletions src/hooks/useUpdateProposalViaQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { useCallback, useContext } from "react";
import { Proposal } from "../schemas/proposal";
import { ProposalContext } from "../app/NewSafeProposal";
import { useSetParamsFromQuery } from "./useSetParamsFromQuery";

export const useUpdateProposalViaQuery = () => {
const setParams = useSetParamsFromQuery();
const proposal = useContext(ProposalContext);

return useCallback(
(newAction: NonNullable<Proposal["actions"]>[0]) => {
setParams({
actions: [...(proposal?.actions || []), newAction],
nonce: proposal?.nonce,
});
},
[proposal, setParams],
);
};

0 comments on commit 3cda71d

Please sign in to comment.