From 4e9f770da61a91e80157481dd8b3a394d9ab4321 Mon Sep 17 00:00:00 2001 From: ppsimatikas Date: Tue, 27 Aug 2024 14:34:24 +0300 Subject: [PATCH] Add error handling on very large comment during voting. (#2413) --- actions/castVote.ts | 10 +++++----- actions/chat/postMessage.ts | 24 ++++++++++++++++++++---- components/VoteCommentModal.tsx | 8 ++++---- components/chat/DiscussionForm.tsx | 4 ++++ hooks/useSubmitVote.ts | 3 +++ 5 files changed, 36 insertions(+), 13 deletions(-) diff --git a/actions/castVote.ts b/actions/castVote.ts index 0ec6c663ba..71aa202a1f 100644 --- a/actions/castVote.ts +++ b/actions/castVote.ts @@ -27,7 +27,6 @@ import { withCastVote } from '@solana/spl-governance' import { VotingClient } from '@utils/uiTypes/VotePlugin' import { chunks } from '@utils/helpers' import { - sendTransactionsV3, SequenceType, txBatchesToInstructionSetWithSigners, } from '@utils/sendTransactions' @@ -43,6 +42,7 @@ import { fetchProgramVersion } from '@hooks/queries/useProgramVersionQuery' import { fetchVoteRecordByPubkey } from '@hooks/queries/voteRecord' import { findPluginName } from '@constants/plugins' import { BN } from '@coral-xyz/anchor' +import { postComment } from './chat/postMessage' const getVetoTokenMint = ( proposal: ProgramAccount, @@ -358,7 +358,7 @@ export async function castVote( sequenceType: SequenceType.Parallel, })) - await sendTransactionsV3({ + await postComment({ connection, wallet, transactionInstructions: actions, @@ -396,7 +396,7 @@ export async function castVote( } }) - await sendTransactionsV3({ + await postComment({ connection, wallet, transactionInstructions: ixsChunks, @@ -470,10 +470,10 @@ export async function castVote( connection ) if (!hasEnoughSol) { - return + throw new Error('Not enough SOL.') } - await sendTransactionsV3({ + await postComment({ connection, wallet, transactionInstructions: instructionsChunks, diff --git a/actions/chat/postMessage.ts b/actions/chat/postMessage.ts index 3fe17ef76c..cbea1c6d47 100644 --- a/actions/chat/postMessage.ts +++ b/actions/chat/postMessage.ts @@ -21,6 +21,7 @@ import { sendTransactionsV3, txBatchesToInstructionSetWithSigners, } from '@utils/sendTransactions' +import { sendSignAndConfirmTransactionsProps } from '@blockworks-foundation/mangolana/lib/transactions' export async function postChatMessage( { connection, wallet, programId, walletPubkey }: RpcContext, @@ -89,14 +90,29 @@ export async function postChatMessage( }), ] - await sendTransactionsV3({ + await postComment({ connection, wallet, transactionInstructions: instructionsChunks, callbacks: undefined, }) +} - // const transaction = new Transaction() - // transaction.add(...instructions) - // await sendTransaction({ transaction, wallet, connection, signers }) +export async function postComment( + transactionProps: sendSignAndConfirmTransactionsProps & { + lookupTableAccounts?: any + autoFee?: boolean +}) { + try { + await sendTransactionsV3(transactionProps) + } catch (e) { + if (e.message.indexOf('Transaction too large:') !== -1) { + const numbers = e.message.match(/\d+/g) + const [size, maxSize] = numbers ? numbers.map(Number) : [0, 0] + if (size > maxSize) { + throw new Error(`You must reduce your comment by ${size - maxSize} character(s).`) + } + } + throw e + } } diff --git a/components/VoteCommentModal.tsx b/components/VoteCommentModal.tsx index e5ea1e1c9d..6f0e337782 100644 --- a/components/VoteCommentModal.tsx +++ b/components/VoteCommentModal.tsx @@ -30,7 +30,7 @@ const VoteCommentModal: FunctionComponent = ({ isMulti, }) => { const [comment, setComment] = useState('') - const { submitting, submitVote } = useSubmitVote() + const { submitting, submitVote , error } = useSubmitVote() const voteString = VOTE_STRINGS[vote] @@ -39,9 +39,8 @@ const VoteCommentModal: FunctionComponent = ({ vote, comment, voteWeights: isMulti, - }) - - onClose() + }).then(() => onClose()) + .catch(console.log) } return ( @@ -62,6 +61,7 @@ const VoteCommentModal: FunctionComponent = ({ onChange={(e) => setComment(e.target.value)} // placeholder={`Let the DAO know why you vote '${voteString}'`} /> + {error &&

{error.message}

}
diff --git a/components/chat/DiscussionForm.tsx b/components/chat/DiscussionForm.tsx index c4f8efb585..966f385287 100644 --- a/components/chat/DiscussionForm.tsx +++ b/components/chat/DiscussionForm.tsx @@ -29,6 +29,7 @@ const DiscussionForm = () => { const { realmInfo } = useRealm() const votingClients = useVotingClients(); const [submitting, setSubmitting] = useState(false) + const [error, setError] = useState('') const wallet = useWalletOnePointOh() const connected = !!wallet?.connected @@ -43,6 +44,7 @@ const DiscussionForm = () => { const votingClient = votingClients(tokenRole ?? 'community');// default to community if no role is provided const submitComment = async () => { setSubmitting(true) + setError('') if ( !realm || !proposal || @@ -79,6 +81,7 @@ const DiscussionForm = () => { setComment('') } catch (ex) { console.error("Can't post chat message", ex) + setError(ex.message); //TODO: How do we present transaction errors to users? Just the notification? } finally { setSubmitting(false) @@ -118,6 +121,7 @@ const DiscussionForm = () => {
+ {error &&

{error}

} ) } diff --git a/hooks/useSubmitVote.ts b/hooks/useSubmitVote.ts index 9cbaba0179..2571fe2a50 100644 --- a/hooks/useSubmitVote.ts +++ b/hooks/useSubmitVote.ts @@ -167,6 +167,9 @@ export const useSubmitVote = () => { } catch (e) { console.error(e) notify({ type: 'error', message: e.message }) + if (msg) { + throw e + } } finally { if (isNftPlugin) { closeNftVotingCountingModal(