Skip to content

Commit

Permalink
Use vocdoni SDK errors
Browse files Browse the repository at this point in the history
  • Loading branch information
selankon committed Jan 12, 2024
1 parent b3dfa00 commit 2bcf76a
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 46 deletions.
41 changes: 3 additions & 38 deletions src/context/createGaslessProposal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
UnpublishedElection,
AccountData,
ErrNotFoundToken,
ErrAPI,
ErrFaucetAlreadyFunded,
} from '@vocdoni/sdk';
import {VoteValues} from '@aragon/sdk-client';
import {useClient} from '@vocdoni/react-providers';
Expand Down Expand Up @@ -113,16 +113,8 @@ const useCreateGaslessProposal = ({
balance = (await vocdoniClient.collectFaucetTokens()).balance;
} catch (e) {
// Wallet already funded
if (
e instanceof ErrAPI &&
e.message &&
e.message.includes('Error: 402')
) {
let dateStr = '';
const dates = findDatesInString(e.message);
if (dates !== null && dates.length > 0) {
dateStr = `(until ${dates[0].toLocaleDateString()})`;
}
if (e instanceof ErrFaucetAlreadyFunded) {
const dateStr = `(until ${e.untilDate.toLocaleDateString()})`;
throw Error(
`This wallet has reached the maximum allocation of Vocdoni tokens for this period ${dateStr}. ` +
'For additional tokens, please visit https://onvote.app/faucet and retry after acquiring more.'
Expand Down Expand Up @@ -293,31 +285,4 @@ const useCreateGaslessProposal = ({
return {steps, globalState, createProposal};
};

/**
* Function used to find the date on a string.
* @param inputString
*/
function findDatesInString(inputString: string): Date[] | null {
const dateRegex = /\b(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} [+-]\d{4} UTC)\b/g;
const matches = inputString.match(dateRegex);
const validDates: Date[] = [];

if (matches) {
matches.forEach(match => {
try {
const transformedDate = match.replace(' +0000 UTC', 'Z');
const date = new Date(transformedDate);
// Check if the date is valid
if (!isNaN(date.getTime())) {
validDates.push(date);
}
} catch (e) {
return null;
}
});
return validDates.length > 0 ? validDates : null;
}
return null;
}

export {useCreateGaslessProposal};
10 changes: 2 additions & 8 deletions src/context/useGaslessVoting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
} from '@vocdoni/react-providers';
import {useCallback, useEffect, useMemo, useState} from 'react';
import {VoteProposalParams} from '@aragon/sdk-client';
import {ErrAPI, Vote} from '@vocdoni/sdk';
import {ErrElectionFinished, Vote} from '@vocdoni/sdk';
import {
StepsMap,
StepStatus,
Expand Down Expand Up @@ -67,13 +67,7 @@ const useGaslessVoting = () => {
try {
return vocdoniClient.submitVote(vocVote);
} catch (e) {
if (
e instanceof ErrAPI &&
e.message &&
e.message.includes('SendTx failed') &&
e.message.includes('finished at height') &&
e.message.includes('current height is')
) {
if (e instanceof ErrElectionFinished) {
throw new Error('The election has finished');
}
throw e;
Expand Down

0 comments on commit 2bcf76a

Please sign in to comment.