Skip to content

Commit

Permalink
fix: requires selected account for interaction to be available (#552)
Browse files Browse the repository at this point in the history
  • Loading branch information
peetzweg authored Apr 12, 2024
1 parent f1d8c8e commit a3a81e9
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
35 changes: 20 additions & 15 deletions src/ui/components/contract/Interact.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
// Copyright 2022-2024 @paritytech/contracts-ui authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import { useEffect, useState, useRef, useMemo } from 'react';
import { useEffect, useMemo, useRef, useState } from 'react';
import { ResultsOutput } from './ResultsOutput';
import { BN_ZERO } from 'lib/bn';
import {
decodeStorageDeposit,
getGasLimit,
getStorageDepositLimit,
transformUserInput,
} from 'lib/callOptions';
import { getDecodedOutput } from 'lib/output';
import {
AbiMessage,
Balance,
CallResult,
ContractExecResult,
ContractOptions,
ContractSubmittableResult,
CallResult,
SubmittableResult,
ContractOptions,
Balance,
UIContract,
} from 'types';
import { AccountSelect } from 'ui/components/account';
import { Dropdown, Button, Buttons } from 'ui/components/common';
import { Button, Buttons, Dropdown } from 'ui/components/common';
import { ArgumentForm, Form, FormField, OptionsForm } from 'ui/components/form';
import { BN_ZERO } from 'lib/bn';
import { useApi, useTransactions } from 'ui/contexts';
import { useWeight, useBalance, useArgValues } from 'ui/hooks';
import { useArgValues, useBalance, useWeight } from 'ui/hooks';
import { useAccountAvailable } from 'ui/hooks/useAccountAvailable';
import { useStorageDepositLimit } from 'ui/hooks/useStorageDepositLimit';
import { createMessageOptions } from 'ui/util/dropdown';
import {
decodeStorageDeposit,
getGasLimit,
getStorageDepositLimit,
transformUserInput,
} from 'lib/callOptions';
import { getDecodedOutput } from 'lib/output';

interface Props {
contract: UIContract;
Expand Down Expand Up @@ -57,6 +58,7 @@ export const InteractTab = ({
const proofSize = useWeight(outcome?.gasRequired.proofSize.toBn());
const timeoutId = useRef<NodeJS.Timeout | null>(null);
const isCustom = refTime.mode === 'custom' || proofSize.mode === 'custom';
const isAccountAvailable = useAccountAvailable(accountId);

useEffect((): void => {
if (!accounts || accounts.length === 0) return;
Expand Down Expand Up @@ -181,7 +183,8 @@ export const InteractTab = ({
!proofSize.isValid ||
txs[txId]?.status === 'processing' ||
!!outcome?.result.isErr ||
!!decodedOutput?.isError;
!!decodedOutput?.isError ||
isAccountAvailable === false;

const isDispatchable = message?.isMutating || message?.isPayable;

Expand All @@ -193,7 +196,9 @@ export const InteractTab = ({
className="caller mb-8"
help="The sending account for this interaction. Any transaction fees will be deducted from this account."
id="accountId"
isError={isAccountAvailable === false}
label="Caller"
message="Selected Account is not available to sign extrinsics."
>
<AccountSelect
className="mb-2"
Expand Down
17 changes: 13 additions & 4 deletions src/ui/components/instantiate/Step1.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@

import { useEffect, useState } from 'react';
import { useParams } from 'react-router';
import { AccountSelect } from '../account';
import { Button, Buttons } from '../common/Button';
import { Input, InputFile, Form, FormField, useMetadataField, getValidation } from '../form';
import { Loader } from '../common/Loader';
import { AccountSelect } from '../account';
import { Form, FormField, Input, InputFile, getValidation, useMetadataField } from '../form';
import { MessageDocs } from '../message';
import { Metadata } from '../metadata';
import { CodeHash } from './CodeHash';
import { useNonEmptyString } from 'ui/hooks/useNonEmptyString';
import { useApi, useDatabase, useInstantiate } from 'ui/contexts';
import { useDbQuery } from 'ui/hooks';
import { useNonEmptyString } from 'ui/hooks/useNonEmptyString';

import { fileToFileState } from 'lib/fileToFileState';
import { getContractFromPatron } from 'lib/getContractFromPatron';
import { useAccountAvailable } from 'ui/hooks/useAccountAvailable';

export function Step1() {
const { codeHash: codeHashUrlParam } = useParams<{ codeHash: string }>();
Expand All @@ -29,6 +30,7 @@ export function Step1() {
const { setStep, setData, data, step } = useInstantiate();

const [accountId, setAccountId] = useState('');
const isAccountAvailable = useAccountAvailable(accountId);
const { value: name, onChange: setName, ...nameValidation } = useNonEmptyString();

const {
Expand Down Expand Up @@ -90,6 +92,8 @@ export function Step1() {
help="The account to use for this instantiation. The fees and storage deposit will be deducted from this account."
id="accountId"
label="Account"
isError={isAccountAvailable === false}
message="Selected Account is not available to sign extrinsics."
>
<AccountSelect
className="mb-2"
Expand Down Expand Up @@ -185,7 +189,12 @@ export function Step1() {
<Buttons>
<Button
data-cy="next-btn"
isDisabled={!metadata || !nameValidation.isValid || !metadataValidation.isValid}
isDisabled={
!metadata ||
!nameValidation.isValid ||
!metadataValidation.isValid ||
isAccountAvailable === false
}
onClick={submitStep1}
variant="primary"
>
Expand Down
16 changes: 16 additions & 0 deletions src/ui/hooks/useAccountAvailable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Copyright 2022-2024 @paritytech/contracts-ui authors & contributors
// SPDX-License-Identifier: GPL-3.0-only

import { keyring } from '@polkadot/ui-keyring';
import { useMemo } from 'react';

export const useAccountAvailable = (accountId?: string): boolean | undefined =>
useMemo(() => {
if (accountId === '' || accountId === undefined) return undefined;
try {
keyring.getPair(accountId);
return true;
} catch {
return false;
}
}, [accountId]);

0 comments on commit a3a81e9

Please sign in to comment.