Skip to content

Commit

Permalink
chore: use a single signing state across the app (#273)
Browse files Browse the repository at this point in the history
This makes a lot of state keeping simpler regarding signing. Now
the components just use "isSigning" from "useTx" and they will all
sync up if there is a signature going on.

This is preliminary work for #240
  • Loading branch information
hansl authored Feb 13, 2025
1 parent 6644a84 commit 18e9acb
Show file tree
Hide file tree
Showing 19 changed files with 34 additions and 97 deletions.
6 changes: 1 addition & 5 deletions components/admins/modals/cancelUpgradeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ export function CancelUpgradeModal({
}: BaseModalProps) {
const { cancelUpgrade } = cosmos.upgrade.v1beta1.MessageComposer.withTypeUrl;
const { submitProposal } = cosmos.group.v1.MessageComposer.withTypeUrl;
const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { estimateFee } = useFeeEstimation(env.chain);

const handleCancelUpgrade = async () => {
setIsSigning(true);
try {
const msgUpgrade = cancelUpgrade({
authority: admin,
Expand All @@ -68,15 +67,12 @@ export function CancelUpgradeModal({
await tx([groupProposalMsg], {
fee,
onSuccess: () => {
setIsSigning(false);
onClose();
refetchPlan();
},
});
} catch (error) {
console.error('Error canceling upgrade:', error);
} finally {
setIsSigning(false);
}
};

Expand Down
5 changes: 1 addition & 4 deletions components/admins/modals/multiMfxBurnModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const MultiBurnSchema = Yup.object().shape({

export function MultiBurnModal({ isOpen, onClose, admin, address, denom }: MultiBurnModalProps) {
const [burnPairs, setBurnPairs] = useState([{ address: admin, amount: '' }]);
const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { estimateFee } = useFeeEstimation(env.chain);
const { burnHeldBalance } = liftedinit.manifest.v1.MessageComposer.withTypeUrl;
const { submitProposal } = cosmos.group.v1.MessageComposer.withTypeUrl;
Expand All @@ -72,7 +72,6 @@ export function MultiBurnModal({ isOpen, onClose, admin, address, denom }: Multi
};

const handleMultiBurn = async (values: { burnPairs: BurnPair[] }) => {
setIsSigning(true);
try {
const messages = values.burnPairs.map(pair =>
burnHeldBalance({
Expand Down Expand Up @@ -112,8 +111,6 @@ export function MultiBurnModal({ isOpen, onClose, admin, address, denom }: Multi
});
} catch (error) {
console.error('Error during multi-burn:', error);
} finally {
setIsSigning(false);
}
};

Expand Down
6 changes: 1 addition & 5 deletions components/admins/modals/multiMfxMintModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const MultiMintSchema = Yup.object().shape({

export function MultiMintModal({ isOpen, onClose, admin, address, denom }: MultiMintModalProps) {
const [payoutPairs, setPayoutPairs] = useState([{ address: '', amount: '' }]);
const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { estimateFee } = useFeeEstimation(env.chain);
const { payout } = liftedinit.manifest.v1.MessageComposer.withTypeUrl;
const { submitProposal } = cosmos.group.v1.MessageComposer.withTypeUrl;
Expand All @@ -72,8 +72,6 @@ export function MultiMintModal({ isOpen, onClose, admin, address, denom }: Multi
};

const handleMultiMint = async (values: { payoutPairs: PayoutPair[] }) => {
setIsSigning(true);

try {
const exponent = denom?.denom_units?.[1]?.exponent ?? 6;
const payoutMsg = payout({
Expand Down Expand Up @@ -111,8 +109,6 @@ export function MultiMintModal({ isOpen, onClose, admin, address, denom }: Multi
});
} catch (error) {
console.error('Error during multi-mint:', error);
} finally {
setIsSigning(false);
}
};

Expand Down
6 changes: 1 addition & 5 deletions components/admins/modals/upgradeModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ export function UpgradeModal({ isOpen, onClose, admin, address, refetchPlan }: B

const { softwareUpgrade } = cosmos.upgrade.v1beta1.MessageComposer.withTypeUrl;
const { submitProposal } = cosmos.group.v1.MessageComposer.withTypeUrl;
const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { estimateFee } = useFeeEstimation(env.chain);

const handleUpgrade = async (values: {
Expand All @@ -104,8 +104,6 @@ export function UpgradeModal({ isOpen, onClose, admin, address, refetchPlan }: B
info: string;
selectedVersion: (GitHubRelease & { upgradeInfo?: UpgradeInfo | null }) | null;
}) => {
setIsSigning(true);

const selectedRelease = values.selectedVersion;
const binaryLinks: { [key: string]: string } = {};

Expand Down Expand Up @@ -156,11 +154,9 @@ export function UpgradeModal({ isOpen, onClose, admin, address, refetchPlan }: B
await tx([groupProposalMsg], {
fee,
onSuccess: () => {
setIsSigning(false);
refetchPlan();
},
});
setIsSigning(false);
};

const initialValues = {
Expand Down
8 changes: 2 additions & 6 deletions components/admins/modals/validatorModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export function ValidatorDetailsModal({

const [power, setPowerInput] = useState(validator?.consensus_power?.toString() || '');

const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { estimateFee } = useFeeEstimation(env.chain);
const { address: userAddress } = useChain(env.chain);

Expand All @@ -66,7 +66,6 @@ export function ValidatorDetailsModal({
};

const handleUpdate = async (values: { power: string }) => {
setIsSigning(true);
// The minimum power is 1_000_000
const realPower = BigInt(values.power) * BigInt(10 ** 6);
const msgSetPower = setPower({
Expand Down Expand Up @@ -94,11 +93,8 @@ export function ValidatorDetailsModal({
const fee = await estimateFee(userAddress ?? '', [groupProposalMsg]);
await tx([groupProposalMsg], {
fee,
onSuccess: () => {
setIsSigning(false);
},
onSuccess: () => {},
});
setIsSigning(false);
};

return (
Expand Down
8 changes: 2 additions & 6 deletions components/admins/modals/warningModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@ export function WarningModal({
openWarningModal,
setOpenWarningModal,
}: Readonly<WarningModalProps>) {
const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { estimateFee } = useFeeEstimation(env.chain);
const { address: userAddress } = useChain(env.chain);
const { removePending, removeValidator } =
strangelove_ventures.poa.v1.MessageComposer.withTypeUrl;
const { submitProposal } = cosmos.group.v1.MessageComposer.withTypeUrl;

const handleAccept = async () => {
setIsSigning(true);
const msgRemoveActive = removeValidator({
sender: admin ?? '',
validatorAddress: address,
Expand Down Expand Up @@ -67,11 +66,8 @@ export function WarningModal({
const fee = await estimateFee(userAddress ?? '', [groupProposalMsg]);
await tx([groupProposalMsg], {
fee,
onSuccess: () => {
setIsSigning(false);
},
onSuccess: () => {},
});
setIsSigning(false);
};

const handleClose = () => setOpenWarningModal(false);
Expand Down
9 changes: 2 additions & 7 deletions components/factory/forms/BurnForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default function BurnForm({

const [isContactsOpen, setIsContactsOpen] = useState(false);

const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { estimateFee } = useFeeEstimation(env.chain);
const { burn } = osmosis.tokenfactory.v1beta1.MessageComposer.withTypeUrl;
const { burnHeldBalance } = liftedinit.manifest.v1.MessageComposer.withTypeUrl;
Expand Down Expand Up @@ -86,7 +86,6 @@ export default function BurnForm({
if (!amount || Number.isNaN(Number(amount))) {
return;
}
setIsSigning(true);
try {
const amountInBaseUnits = parseNumberToBigInt(amount, exponent).toString();
let msg;
Expand Down Expand Up @@ -153,8 +152,6 @@ export default function BurnForm({
});
} catch (error) {
console.error('Error during burning:', error);
} finally {
setIsSigning(false);
}
};

Expand All @@ -168,7 +165,7 @@ export default function BurnForm({
});
return;
}
setIsSigning(true);

try {
const burnMsg = burnHeldBalance({
authority: admin ?? '',
Expand Down Expand Up @@ -202,8 +199,6 @@ export default function BurnForm({
});
} catch (error) {
console.error('Error during multi-burning:', error);
} finally {
setIsSigning(false);
}
};

Expand Down
6 changes: 1 addition & 5 deletions components/factory/forms/ConfirmationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export default function ConfirmationForm({
formData: TokenFormData;
address: string;
}>) {
const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { estimateFee } = useFeeEstimation(env.chain);
const { setDenomMetadata, createDenom } =
osmosis.tokenfactory.v1beta1.MessageComposer.withTypeUrl;
Expand All @@ -40,8 +40,6 @@ export default function ConfirmationForm({
const { prefixedSubdenom, symbol, fullDenom } = getDenomInfo(formData.subdenom);

const handleConfirm = async () => {
setIsSigning(true);

const createAsGroup = async () => {
const msg = submitProposal({
groupPolicyAddress: formData.groupPolicyAddress || '',
Expand Down Expand Up @@ -149,8 +147,6 @@ export default function ConfirmationForm({
formData.isGroup ? await createAsGroup() : await createAsUser();
} catch (error) {
console.error('Error during transaction setup:', error);
} finally {
setIsSigning(false);
}
};

Expand Down
6 changes: 2 additions & 4 deletions components/factory/forms/MintForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function MintForm({
const [recipient, setRecipient] = useState(address || '');
const [isContactsOpen, setIsContactsOpen] = useState(false);

const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { estimateFee } = useFeeEstimation(env.chain);
const { mint } = osmosis.tokenfactory.v1beta1.MessageComposer.withTypeUrl;
const { submitProposal } = cosmos.group.v1.MessageComposer.withTypeUrl;
Expand All @@ -52,7 +52,7 @@ export default function MintForm({
if (!amount || Number.isNaN(Number(amount))) {
return;
}
setIsSigning(true);

try {
const amountInBaseUnits = parseNumberToBigInt(amount).toString();
let msg;
Expand Down Expand Up @@ -100,8 +100,6 @@ export default function MintForm({
});
} catch (error) {
console.error('Error during minting:', error);
} finally {
setIsSigning(false);
}
};

Expand Down
5 changes: 1 addition & 4 deletions components/factory/modals/TransferModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,12 @@ export default function TransferModal({
newAdmin: '',
};

const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { estimateFee } = useFeeEstimation(env.chain);
const { changeAdmin } = osmosis.tokenfactory.v1beta1.MessageComposer.withTypeUrl;
const { submitProposal } = cosmos.group.v1.MessageComposer.withTypeUrl;

const handleTransfer = async (values: FormikValues, resetForm: () => void) => {
setIsSigning(true);
try {
const msg = isGroup
? submitProposal({
Expand Down Expand Up @@ -110,8 +109,6 @@ export default function TransferModal({
bgColor: '#e74c3c',
});
throw error;
} finally {
setIsSigning(false);
}
};

Expand Down
5 changes: 1 addition & 4 deletions components/factory/modals/updateDenomMetadata.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,12 @@ export default function UpdateDenomMetadataModal({
exponent: '6',
label: fullDenom || '',
};
const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { estimateFee } = useFeeEstimation(env.chain);
const { setDenomMetadata } = osmosis.tokenfactory.v1beta1.MessageComposer.withTypeUrl;
const { submitProposal } = cosmos.group.v1.MessageComposer.withTypeUrl;

const handleUpdate = async (values: TokenFormData, resetForm: () => void) => {
setIsSigning(true);
const symbol = values.display.toUpperCase();
try {
const msg = isGroup
Expand Down Expand Up @@ -138,8 +137,6 @@ export default function UpdateDenomMetadataModal({
});
} catch (error) {
console.error('Error during transaction setup:', error);
} finally {
setIsSigning(false);
}
};

Expand Down
6 changes: 1 addition & 5 deletions components/groups/forms/groups/ConfirmationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export default function ConfirmationForm({
console.error('Failed to serialize group metadata:', error);
throw new Error('Invalid group metadata format');
}
const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { estimateFee } = useFeeEstimation(env.chain);

const minExecutionPeriod: Duration = {
Expand All @@ -59,7 +59,6 @@ export default function ConfirmationForm({
const typeUrl = cosmos.group.v1.ThresholdDecisionPolicy.typeUrl;

const handleConfirm = async () => {
setIsSigning(true);
try {
const msg = createGroupWithPolicy({
admin: address ?? '',
Expand Down Expand Up @@ -87,10 +86,7 @@ export default function ConfirmationForm({
},
});
} catch (error) {
setIsSigning(false);
console.error('Error during transaction setup:', error);
} finally {
setIsSigning(false);
}
};

Expand Down
7 changes: 1 addition & 6 deletions components/groups/modals/groupInfo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export function GroupInfo({
setShowInfoModal,
}: GroupInfoProps) {
const [showUpdateModal, setShowUpdateModal] = useState(false);
const { tx, isSigning, setIsSigning } = useTx(env.chain);
const { tx, isSigning } = useTx(env.chain);
const { leaveGroup } = cosmos.group.v1.MessageComposer.withTypeUrl;
const { estimateFee } = useFeeEstimation(env.chain);
if (!group || !group.policies || group.policies.length === 0) return null;
Expand Down Expand Up @@ -132,8 +132,6 @@ export function GroupInfo({
}

const handleLeave = async () => {
setIsSigning(true);

try {
const msg = leaveGroup({
address: address,
Expand All @@ -146,7 +144,6 @@ export function GroupInfo({
{
fee,
onSuccess: () => {
setIsSigning(false);
onUpdate();
setShowInfoModal(false);
},
Expand All @@ -155,8 +152,6 @@ export function GroupInfo({
);
} catch (error) {
console.error('Error leaving group:', error);
} finally {
setIsSigning(false);
}
};

Expand Down
Loading

0 comments on commit 18e9acb

Please sign in to comment.