Skip to content

Commit

Permalink
Merge pull request #294 from trilitech/refactor-signer-config
Browse files Browse the repository at this point in the history
Refactor signer config
  • Loading branch information
serjonya-trili authored Jul 26, 2023
2 parents 4dbcc25 + 885bbac commit 0a00b0a
Show file tree
Hide file tree
Showing 45 changed files with 689 additions and 806 deletions.
12 changes: 0 additions & 12 deletions src/components/AccountCard/ApproveExecuteForm/types.tsx

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,76 +1,80 @@
import { mockImplicitAccount } from "../../../../mocks/factories";
import { mockImplicitAccount, mockMultisigAccount } from "../../../../mocks/factories";
import { render, screen } from "../../../../mocks/testUtils";

import store from "../../../../utils/redux/store";
import MultisigActionButton from "./MultisigSignerTile";
import accountsSlice from "../../../../utils/redux/slices/accountsSlice";
import { pendingOps } from "../../../../mocks/multisig";

const { add } = accountsSlice.actions;
const account = mockImplicitAccount(0);

describe("<ActionButton/>", () => {
it("should display execute for non-pending operation with signer included in the owned account", () => {
const account = mockImplicitAccount(0);
store.dispatch(add([account]));
render(
<MultisigActionButton
signer={account.address}
approvers={[]}
signerAddress={account.address}
pendingApprovals={0}
onClickApproveOrExecute={() => {}}
openSignModal={_ => {}}
operation={pendingOps[0]}
account={mockMultisigAccount(0)}
/>
);
expect(screen.getByTestId("multisig-signer-button")).toHaveTextContent("Execute");
});

it("should display approve for pending operation with signer included in the owned account", () => {
const account = mockImplicitAccount(0);
store.dispatch(add([account]));
render(
<MultisigActionButton
signer={account.address}
approvers={[]}
signerAddress={account.address}
pendingApprovals={1}
onClickApproveOrExecute={() => {}}
openSignModal={_ => {}}
operation={pendingOps[0]}
account={mockMultisigAccount(0)}
/>
);
expect(screen.getByTestId("multisig-signer-button")).toHaveTextContent("Approve");
});

it("should show approved for pending operation with signers included in the account that already approved", () => {
const account = mockImplicitAccount(0);
store.dispatch(add([account]));
const operation = { ...pendingOps[0], approvals: [account.address] };
render(
<MultisigActionButton
signer={account.address}
approvers={[account.address]}
signerAddress={account.address}
pendingApprovals={1}
onClickApproveOrExecute={() => {}}
openSignModal={_ => {}}
operation={operation}
account={mockMultisigAccount(0)}
/>
);
expect(screen.getByTestId("multisig-signer-approved")).toHaveTextContent("Approved");
});

it("should show approved for operation with signers not in the account", () => {
const account = mockImplicitAccount(0);
const operation = { ...pendingOps[0], approvals: [account.address] };
render(
<MultisigActionButton
signer={account.address}
approvers={[account.address]}
signerAddress={account.address}
pendingApprovals={1}
onClickApproveOrExecute={() => {}}
openSignModal={_ => {}}
operation={operation}
account={mockMultisigAccount(0)}
/>
);
expect(screen.getByTestId("multisig-signer-approved-or-waiting")).toHaveTextContent("Approved");
});

it("should show Awaiting approval for operation with signers not owned by the user account that hasn't approved", () => {
const account = mockImplicitAccount(0);
render(
<MultisigActionButton
signer={account.address}
approvers={[]}
signerAddress={account.address}
pendingApprovals={1}
onClickApproveOrExecute={() => {}}
openSignModal={_ => {}}
operation={pendingOps[0]}
account={mockMultisigAccount(0)}
/>
);
expect(screen.getByTestId("multisig-signer-approved-or-waiting")).toHaveTextContent(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@ import { RxCheckCircled } from "react-icons/rx";
import colors from "../../../../style/colors";
import { ImplicitAddress } from "../../../../types/Address";
import { useGetImplicitAccount } from "../../../../utils/hooks/accountHooks";
import { ApproveOrExecute } from "../../../../utils/tezos/types";
import { useSelectedNetwork } from "../../../../utils/hooks/assetsHooks";
import { estimateMultisigApproveOrExecute } from "../../../../utils/tezos";
import { IconAndTextBtn } from "../../../IconAndTextBtn";
import { ParamsWithFee } from "../../../ApproveExecuteForm/types";
import { MultisigOperation } from "../../../../utils/multisig/types";
import { MultisigAccount } from "../../../../types/Account";

export const MultisigActionButton: React.FC<{
signer: ImplicitAddress; // TODO: change to ImplicitAccount
approvers: ImplicitAddress[]; // TODO: change to ImplicitAccount
signerAddress: ImplicitAddress;
pendingApprovals: number;
onClickApproveOrExecute: (a: ApproveOrExecute) => void;
isLoading?: boolean;
}> = ({ signer, approvers, pendingApprovals, onClickApproveOrExecute, isLoading = false }) => {
operation: MultisigOperation;
account: MultisigAccount;
openSignModal: (params: ParamsWithFee) => void;
}> = ({
signerAddress,
account: { address: multisigAddress },
operation,
pendingApprovals,
openSignModal,
}) => {
const getImplicitAccount = useGetImplicitAccount();
const network = useSelectedNetwork();
const signer = getImplicitAccount(signerAddress.pkh);
const signerInOwnedAccounts = !!signer;

const signerInOwnedAccounts = !!getImplicitAccount(signer.pkh);
const approvedBySigner = !!approvers.find(approver => approver === signer);
const approvedBySigner = !!operation.approvals.find(approver => approver === signerAddress);
const operationIsExecutable = pendingApprovals === 0;

if (!signerInOwnedAccounts) {
Expand Down Expand Up @@ -47,13 +59,28 @@ export const MultisigActionButton: React.FC<{
);
}

const onButtonClick = async () => {
const actionType = operationIsExecutable ? "execute" : "approve";
const { suggestedFeeMutez } = await estimateMultisigApproveOrExecute(
{
type: actionType,
contract: multisigAddress,
operationId: operation.id,
},
signer,
network
);
openSignModal({
type: actionType,
operation: operation,
multisigAddress,
signer,
suggestedFeeMutez,
});
};

return (
<Button
isLoading={isLoading}
bg={colors.blue}
data-testid="multisig-signer-button"
onClick={() => onClickApproveOrExecute(operationIsExecutable ? "execute" : "approve")}
>
<Button bg={colors.blue} data-testid="multisig-signer-button" onClick={onButtonClick}>
{operationIsExecutable ? "Execute" : "Approve"}
</Button>
);
Expand Down
Loading

0 comments on commit 0a00b0a

Please sign in to comment.