Skip to content

Commit

Permalink
feat: Implement offchain sequence management for multisig (#1462)
Browse files Browse the repository at this point in the history
  • Loading branch information
charymalloju authored Nov 12, 2024
2 parents 7422df3 + 98590af commit fc0a410
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import React, { useEffect } from 'react';
import { FAILED_TO_BROADCAST_ERROR } from '@/utils/errors';
import useVerifyAccount from '@/custom-hooks/useVerifyAccount';
import CustomButton from '@/components/common/CustomButton';
import { useRouter } from 'next/navigation';

interface BroadCastTxnProps {
txn: Txn;
Expand All @@ -20,20 +21,33 @@ interface BroadCastTxnProps {
pubKeys: MultisigAddressPubkey[];
chainID: string;
isMember: boolean;
disableBroadcast?: boolean;
isOverview?: boolean;
}

const BroadCastTxn: React.FC<BroadCastTxnProps> = (props) => {
const { txn, multisigAddress, pubKeys, threshold, chainID, isMember } = props;
const {
txn,
multisigAddress,
pubKeys,
threshold,
chainID,
isMember,
disableBroadcast,
isOverview,
} = props;
const dispatch = useAppDispatch();
const { getChainInfo } = useGetChainInfo();
const {
address: walletAddress,
restURLs: baseURLs,
rpcURLs,
chainName,
} = getChainInfo(chainID);
const { isAccountVerified } = useVerifyAccount({
address: walletAddress,
});
const router = useRouter();

const updateTxnRes = useAppSelector(
(state: RootState) => state.multisig.updateTxnRes
Expand Down Expand Up @@ -78,9 +92,13 @@ const BroadCastTxn: React.FC<BroadCastTxnProps> = (props) => {
<CustomButton
btnText="Broadcast"
btnOnClick={() => {
broadcastTxn();
if (isOverview) {
router.push(`/multisig/${chainName}/${multisigAddress}`);
} else {
broadcastTxn();
}
}}
btnDisabled={!isMember}
btnDisabled={!isMember || disableBroadcast}
btnStyles="w-[115px]"
/>
);
Expand Down
33 changes: 28 additions & 5 deletions frontend/src/app/(routes)/multisig/components/common/SignTxn.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useAppDispatch } from '@/custom-hooks/StateHooks';
import { useAppDispatch, useAppSelector } from '@/custom-hooks/StateHooks';
import useGetChainInfo from '@/custom-hooks/useGetChainInfo';
import {
setVerifyDialogOpen,
Expand All @@ -8,23 +8,41 @@ import { Txn } from '@/types/multisig';
import React from 'react';
import useVerifyAccount from '@/custom-hooks/useVerifyAccount';
import CustomButton from '@/components/common/CustomButton';
import { useRouter } from 'next/navigation';

interface SignTxnProps {
address: string;
txId: number;
unSignedTxn: Txn;
isMember: boolean;
chainID: string;
isOverview?: boolean;
}

const SignTxn: React.FC<SignTxnProps> = (props) => {
const { address, isMember, unSignedTxn, chainID } = props;
const { address, isMember, unSignedTxn, chainID, isOverview } = props;
const dispatch = useAppDispatch();
const { getChainInfo } = useGetChainInfo();
const { address: walletAddress, rpcURLs } = getChainInfo(chainID);
const { address: walletAddress, rpcURLs, chainName } = getChainInfo(chainID);
const { isAccountVerified } = useVerifyAccount({
address: walletAddress,
});
const router = useRouter();

const txnsCount = useAppSelector((state) => state.multisig.txns.Count);
const getCount = (option: string) => {
let count = 0;
/* eslint-disable @typescript-eslint/no-explicit-any */
txnsCount &&
txnsCount.forEach((t: any) => {
if (t?.computed_status?.toLowerCase() === option.toLowerCase()) {
count = t?.count;
}
});

return count;
};
const toBeBroadcastedCount = getCount('to-broadcast');

const signTheTx = async () => {
if (!isAccountVerified()) {
Expand All @@ -37,7 +55,8 @@ const SignTxn: React.FC<SignTxnProps> = (props) => {
multisigAddress: address,
unSignedTxn,
walletAddress,
rpcURLs
rpcURLs,
toBeBroadcastedCount,
})
);
};
Expand All @@ -47,7 +66,11 @@ const SignTxn: React.FC<SignTxnProps> = (props) => {
btnText="Sign"
btnDisabled={!isMember}
btnOnClick={() => {
signTheTx();
if (isOverview) {
router.push(`/multisig/${chainName}/${address}`);
} else {
signTheTx();
}
}}
btnStyles="w-[115px]"
/>
Expand Down
16 changes: 14 additions & 2 deletions frontend/src/app/(routes)/multisig/components/common/TxnsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export const TxnsCard = ({
isHistory,
onViewError,
allowRepeat,
disableBroadcast,
isOverview,
}: {
txn: Txn;
currency: Currency;
Expand All @@ -49,6 +51,8 @@ export const TxnsCard = ({
isHistory: boolean;
onViewError?: (errMsg: string) => void;
allowRepeat?: boolean;
disableBroadcast?: boolean;
isOverview?: boolean;
}) => {
const dispatch = useAppDispatch();
const { getChainInfo } = useGetChainInfo();
Expand Down Expand Up @@ -208,7 +212,7 @@ export const TxnsCard = ({
<div className="flex gap-[2px] items-end">
<span className="text-b1">
{isHistory || isReadyToBroadcast()
? getTimeDifferenceToFutureDate(txn.last_updated, true)
? getTimeDifferenceToFutureDate(txn.signed_at, true)
: getTimeDifferenceToFutureDate(txn.created_at, true)}
</span>
</div>
Expand Down Expand Up @@ -268,6 +272,8 @@ export const TxnsCard = ({
threshold={threshold}
chainID={chainID}
isMember={isMember}
disableBroadcast={disableBroadcast}
isOverview={isOverview}
/>
) : (
<SignTxn
Expand All @@ -276,6 +282,7 @@ export const TxnsCard = ({
isMember={isMember}
txId={txn.id}
unSignedTxn={txn}
isOverview={isOverview}
/>
)}
</>
Expand Down Expand Up @@ -303,7 +310,12 @@ export const TxnsCard = ({
open={deleteDialogOpen}
onClose={() => setDeleteDialogOpen(false)}
title="Delete Transaction"
description=" Are you sure you want to delete the transaction ?"
description={
'Are you sure you want to delete the transaction?' +
(isReadyToBroadcast()
? ' This action will require re-signing any already signed subsequent transactions.'
: '')
}
onDelete={onDeleteTxn}
loading={loading === TxStatus.PENDING}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const DialogConfirmDelete = ({
/>
<div className="flex items-center flex-col gap-2">
<div className="text-h2 !font-bold">{title}</div>
<div className="text-b1-light">{description}</div>
<div className="text-b1-light text-center">{description}</div>
</div>
</div>
<CustomButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ const Transactions = ({
dispatch(getTxns({ address: multisigAddress, status: status }));
};

const txnsCount = useAppSelector((state) => state.multisig.txns.Count)
const txnsCount = useAppSelector((state) => state.multisig.txns.Count);
const txnsStatus = useAppSelector((state) => state.multisig.txns.status);
const deleteTxnRes = useAppSelector((state) => state.multisig.deleteTxnRes);
const signTxStatus = useAppSelector(
Expand Down Expand Up @@ -172,21 +172,21 @@ const TransactionsFilters = ({
}: {
txnsType: string;
handleTxnsTypeChange: (type: string) => void;
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
txnsCount: any;
}) => {

const getCount = (option: string) => {
let count = 0;
/* eslint-disable-next-line @typescript-eslint/no-explicit-any */
txnsCount && txnsCount.forEach((t: any) => {
if (t?.computed_status?.toLowerCase() === option.toLowerCase()) {
count = t?.count
}
})
/* eslint-disable @typescript-eslint/no-explicit-any */
txnsCount &&
txnsCount.forEach((t: any) => {
if (t?.computed_status?.toLowerCase() === option.toLowerCase()) {
count = t?.count;
}
});

return count
}
return count;
};

return (
<div className="flex gap-4 flex-wrap">
Expand Down Expand Up @@ -244,9 +244,19 @@ const TransactionsList = ({
setErrMsg(errMsg);
setViewErrorDialogOpen(true);
};
const sortedTxns = [...txns].sort((a, b) => {
const dateA = new Date(
txnsType === 'to-broadcast' ? a.signed_at : a.created_at
).getTime();
const dateB = new Date(
txnsType === 'to-broadcast' ? b.signed_at : b.created_at
).getTime();
return txnsType === 'to-broadcast' ? dateA - dateB : dateB - dateA;
});

return (
<div className="space-y-4">
{txns.map((txn, index) => (
{sortedTxns.map((txn, index) => (
<TxnsCard
key={index}
txn={txn}
Expand All @@ -257,6 +267,7 @@ const TransactionsList = ({
isHistory={isHistory}
onViewError={onViewError}
allowRepeat={txnsType === 'completed'}
disableBroadcast={txnsType === 'to-broadcast' && index > 0}
/>
))}
<DialogTxnFailed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,14 +111,17 @@ const MultisigAccountRecentTxns = ({
multisigAddress={multisigAddress}
chainID={chainID}
isHistory={false}
isOverview={true}
/>
))}
</div>
<div className="flex justify-end">
<button onClick={handleToggleView} className="secondary-btn">
{showAllTxns ? 'View Less' : 'View More'}
</button>
</div>
{txns?.length > 1 ? (
<div className="flex justify-end">
<button onClick={handleToggleView} className="secondary-btn">
{showAllTxns ? 'View Less' : 'View More'}
</button>
</div>
) : null}
</div>
);
};
5 changes: 3 additions & 2 deletions frontend/src/app/(routes)/multisig/utils/multisigSigning.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ const signTransaction = async (
multisigAddress: string,
unSignedTxn: Txn,
walletAddress: string,
rpcURLs: string[]
rpcURLs: string[],
toBeBroadcastedCount: number
) => {
try {
window.wallet.defaultOptions = {
Expand All @@ -47,7 +48,7 @@ const signTransaction = async (

const signerData = {
accountNumber: multisigAcc?.accountNumber,
sequence: multisigAcc?.sequence,
sequence: multisigAcc?.sequence + toBeBroadcastedCount,
chainId: chainID,
};

Expand Down
2 changes: 1 addition & 1 deletion frontend/src/components/common/CustomButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const CustomButton = ({
}: CustomButtonProps) => {
return (
<button
className={`${btnStyles} ${isDelete ? 'delete-btn' : 'primary-btn'}`}
className={`${btnStyles} ${isDelete ? 'delete-btn' : 'primary-btn'} ${btnDisabled ? 'cursor-not-allowed opacity-50' : ''}`}
disabled={btnDisabled}
type={btnType || 'button'}
onClick={btnOnClick}
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/store/features/multisig/multisigSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,7 @@ export const signTransaction = createAsyncThunk(
unSignedTxn: Txn;
walletAddress: string;
rpcURLs: string[];
toBeBroadcastedCount: number;
},
{ rejectWithValue, dispatch }
) => {
Expand All @@ -473,7 +474,8 @@ export const signTransaction = createAsyncThunk(
data.multisigAddress,
data.unSignedTxn,
data.walletAddress,
data.rpcURLs
data.rpcURLs,
data.toBeBroadcastedCount
);
const authToken = getAuthToken(COSMOS_CHAIN_ID);

Expand Down
1 change: 1 addition & 0 deletions frontend/src/types/multisig.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ interface Txn {
signatures: Signature[];
last_updated: string;
created_at: string;
signed_at: string;
pubkeys?: MultisigAddressPubkey[];
threshold?: number;
}
Expand Down

0 comments on commit fc0a410

Please sign in to comment.