Skip to content

Commit

Permalink
Merge pull request #29 from vechainfoundation/fix-no-wallet-error
Browse files Browse the repository at this point in the history
fix: show message when wallet is not configured
  • Loading branch information
davidecarpini authored Oct 27, 2023
2 parents 6110238 + 5249b19 commit f1044af
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 19 deletions.
46 changes: 30 additions & 16 deletions apps/sample-react-app/src/Hooks/useCounter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,15 @@ interface UseCounter {
increment: () => Promise<void>;
status: IncrementStatus;
address: string;
error: string | null;
}

export const useCounter = (): UseCounter => {
const { thor } = useConnex();

const [count, setCount] = useState<number>(0);
const [status, setStatus] = useState<IncrementStatus>('idle');
const [error, setError] = useState<string | null>(null);

const contract = useMemo(
() => thor.account('0x8384738c995d49c5b692560ae688fc8b51af1059'),
Expand All @@ -55,22 +57,34 @@ export const useCounter = (): UseCounter => {
}, [setValue]);

const increment = useCallback(async (): Promise<void> => {
setStatus('in-wallet');

await contract
.method(_increment)
.transact()
.delegate('https://sponsor-testnet.vechain.energy/by/90')
.request();

setStatus('pending');

await thor.ticker().next();

await setValue()
.then(() => setStatus('idle'))
.catch(() => setStatus('error'));
setError(null);

try {
setStatus('in-wallet');

await contract
.method(_increment)
.transact()
.delegate('https://sponsor-testnet.vechain.energy/by/90')
.request();

setStatus('pending');

await thor.ticker().next();

await setValue()
.then(() => setStatus('idle'))
.catch(() => setStatus('error'));
} catch (e) {
setStatus('error');
if (e instanceof Error) {
setError(e.message);
} else {
setError('Unknown error');
}
throw e;
}
}, [thor, contract, setValue]);

return { count, increment, status, address: contract.address };
return { count, increment, status, address: contract.address, error };
};
6 changes: 3 additions & 3 deletions apps/sample-react-app/src/Screens/components/Counter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { AddressButton } from '../../Components';

export const Counter = (): JSX.Element => {
const { accountState } = useWallet();
const { count, increment, status, address } = useCounter();
const { count, increment, status, address, error } = useCounter();
const toast = useToast();

const incrementCounter = useCallback(() => {
Expand Down Expand Up @@ -65,7 +65,7 @@ export const Counter = (): JSX.Element => {
return (
<Alert status="error">
<AlertIcon />
There was an unexpected error
{error ? error : 'Unknown error'}
</Alert>
);
case 'pending':
Expand All @@ -78,7 +78,7 @@ export const Counter = (): JSX.Element => {
case 'idle':
return null;
}
}, [status]);
}, [error, status]);

return (
<StyledCard h={['auto', 'auto', 'full']} p={4}>
Expand Down
2 changes: 2 additions & 0 deletions packages/wallet-kit/src/signer-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class SignerManager implements ConnexSigner {
if (signer) {
await signer.disconnect?.();
}

this.currentSource = undefined;
}

async signTx(
Expand Down

0 comments on commit f1044af

Please sign in to comment.