Skip to content
24 changes: 23 additions & 1 deletion cosmwasm/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1073,12 +1073,34 @@ const encodeSubmitProposal = (content, config, options, proposer) => {
};
};

// Retries sign-and-broadcast on transient RPC socket closures
const signAndBroadcastWithRetry = async (client, signerAddress, msgs, fee, memo = '', maxAttempts = 3) => {
let lastError;
for (let attempt = 0; attempt < maxAttempts; attempt++) {
try {
return await client.signAndBroadcast(signerAddress, msgs, fee, memo);
} catch (error) {
lastError = error;
const code = error?.cause?.code || error?.code;
const message = error?.message || '';

// Confirm err is socket error
const isTransient = code === 'UND_ERR_SOCKET' || /fetch failed/i.test(message);
if (!isTransient || attempt === maxAttempts - 1) {
throw error;
}

printInfo('Retrying proposal submission..... 🔄');
}
}
};

const submitProposal = async (client, config, options, content, fee) => {
const [account] = client.accounts;

const submitProposalMsg = encodeSubmitProposal(content, config, options, account.address);

const { events } = await client.signAndBroadcast(account.address, [submitProposalMsg], fee, '');
const { events } = await signAndBroadcastWithRetry(client, account.address, [submitProposalMsg], fee, '');

return events.find(({ type }) => type === 'submit_proposal').attributes.find(({ key }) => key === 'proposal_id').value;
};
Expand Down