Skip to content

Commit

Permalink
Merge pull request #4252 from JoinColony/feat/4245-arbitrary-contract…
Browse files Browse the repository at this point in the history
…-warning

Arbitrary txs: show warning instead of error if contract address is not verified
  • Loading branch information
Nortsova authored Feb 6, 2025
2 parents e3e1350 + 76ac973 commit 1837f46
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,26 +1,36 @@
import React, { useState } from 'react';

import { formatText } from '~utils/intl.ts';

import { ContractAddressInput } from './ContractAddressInput.tsx';
import { DynamicInputs } from './DynamicInputs.tsx';
import { JsonAbiInput } from './JsonAbiInput.tsx';
import { MSG } from './translation.ts';
import { useGenerateABI } from './useGenerateABI.ts';

export const AddTransactionForm = () => {
const [contractAbiLoading, setContractAbiLoading] = useState(false);

const {
serverError,
contractAddressServerError,
isJsonAbiFormatted,
showJsonAbiEditWarning,
toggleJsonFormat,
toggleShowJsonAbiEditWarningOff,
} = useGenerateABI({ setContractAbiLoading });
return (
<>
<ContractAddressInput
contractAbiLoading={contractAbiLoading}
serverError={serverError}
/>
{contractAddressServerError && (
<div className="flex rounded-md border border-warning-400 bg-warning-100 px-4 py-4.5 text-warning-400 break-word">
<span className="text-sm">
<span className="font-medium">
{formatText(MSG.contractAddressServerErrorNote)}{' '}
</span>
{contractAddressServerError}
</span>
</div>
)}
<ContractAddressInput contractAbiLoading={contractAbiLoading} />
<JsonAbiInput
loading={contractAbiLoading}
toggleJsonFormat={toggleJsonFormat}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,8 @@ import { MSG } from './translation.ts';

interface ContractAddressInputProps {
contractAbiLoading?: boolean;
serverError?: string;
}
export const ContractAddressInput: FC<ContractAddressInputProps> = ({
serverError,
contractAbiLoading,
}) => {
return (
Expand All @@ -20,7 +18,6 @@ export const ContractAddressInput: FC<ContractAddressInputProps> = ({
name="contractAddress"
label={formatText(MSG.contractAddressField)}
placeholder={formatText(MSG.contractAddressPlaceholder)}
error={serverError || undefined}
loading={contractAbiLoading}
registerOptions={{
validate: validateContractAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ export const MSG = defineMessages({
id: `${displayName}.contractAddressError`,
defaultMessage: 'Please enter a valid contract address.',
},
contractAddressServerError: {
id: `${displayName}.contractAddressServerError`,
defaultMessage:
'We were unable to verify this contract. Please proceed only if you fully understand its functionality and trust its source.',
},
contractAddressServerErrorNote: {
id: `${displayName}.contractAddressServerErrorNote`,
defaultMessage: 'Note:',
},
jsonAbiField: {
id: `${displayName}.jsonAbiField`,
defaultMessage: 'ABI/JSON',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@ import useGetCurrentNetwork from '~hooks/useGetCurrentNetwork.ts';
import { formatText } from '~utils/intl.ts';
import { fetchContractABI } from '~utils/safes/index.ts';

import { MSG } from './translation.ts';

export const useGenerateABI = ({ setContractAbiLoading }) => {
const { watch, setValue, trigger } = useFormContext();

const [serverError, setServerError] = useState('');
const [contractAddressServerError, setContractAddressServerError] =
useState('');
const networkInfo = useGetCurrentNetwork();

const [isJsonAbiFormatted, setIsJsonAbiFormatted] = useState(false);
Expand All @@ -29,18 +32,27 @@ export const useGenerateABI = ({ setContractAbiLoading }) => {

setContractAbiLoading(false);
if (response.status === '1') {
setServerError('');
setContractAddressServerError('');
setValue('jsonAbi', response.result);
trigger('jsonAbi'); // Trigger validation to clear previous errors
setShowJsonAbiEditWarning(true);
} else {
setServerError(
response?.result ?? formatText({ id: 'error.message' }),
);
const errorMessage = response?.result;
if (errorMessage) {
if (errorMessage.toLowerCase().includes('not verified')) {
setContractAddressServerError(
formatText(MSG.contractAddressServerError),
);
} else {
setContractAddressServerError(errorMessage);
}
} else {
setContractAddressServerError(formatText({ id: 'error.message' }));
}
}
} catch (e) {
setContractAbiLoading(false);
setServerError(e.message);
setContractAddressServerError(e.message);
// eslint-disable-next-line no-console
console.log(e);
}
Expand Down Expand Up @@ -110,7 +122,7 @@ export const useGenerateABI = ({ setContractAbiLoading }) => {
};

return {
serverError,
contractAddressServerError,
isJsonAbiFormatted,
showJsonAbiEditWarning,
toggleJsonFormat,
Expand Down

0 comments on commit 1837f46

Please sign in to comment.