Commit ba54c461 authored by tom's avatar tom

defatul value for evm version field

parent 23b1163a
...@@ -92,7 +92,7 @@ test.describe('sourcify', () => { ...@@ -92,7 +92,7 @@ test.describe('sourcify', () => {
}); });
testWithSocket.describe.configure({ mode: 'serial', timeout: 20_000 }); testWithSocket.describe.configure({ mode: 'serial', timeout: 20_000 });
testWithSocket('with multiple contracts +@mobile', async({ mount, page, createSocket }) => { testWithSocket('with multiple contracts', async({ mount, page, createSocket }) => {
const component = await mount( const component = await mount(
<TestApp withSocket> <TestApp withSocket>
<ContractVerificationForm config={ formConfig } hash={ hash }/> <ContractVerificationForm config={ formConfig } hash={ hash }/>
......
import { Button, chakra, useUpdateEffect } from '@chakra-ui/react'; import { Button, chakra, useUpdateEffect } from '@chakra-ui/react';
import { useRouter } from 'next/router'; import { route } from 'nextjs-routes';
import React from 'react'; import React from 'react';
import type { SubmitHandler } from 'react-hook-form'; import type { SubmitHandler } from 'react-hook-form';
import { useForm, FormProvider } from 'react-hook-form'; import { useForm, FormProvider } from 'react-hook-form';
...@@ -20,7 +20,7 @@ import ContractVerificationSourcify from './methods/ContractVerificationSourcify ...@@ -20,7 +20,7 @@ import ContractVerificationSourcify from './methods/ContractVerificationSourcify
import ContractVerificationStandardInput from './methods/ContractVerificationStandardInput'; import ContractVerificationStandardInput from './methods/ContractVerificationStandardInput';
import ContractVerificationVyperContract from './methods/ContractVerificationVyperContract'; import ContractVerificationVyperContract from './methods/ContractVerificationVyperContract';
import ContractVerificationVyperMultiPartFile from './methods/ContractVerificationVyperMultiPartFile'; import ContractVerificationVyperMultiPartFile from './methods/ContractVerificationVyperMultiPartFile';
import { prepareRequestBody, formatSocketErrors, DEFAULT_VALUES } from './utils'; import { prepareRequestBody, formatSocketErrors, getDefaultValues } from './utils';
const METHOD_COMPONENTS = { const METHOD_COMPONENTS = {
'flattened-code': <ContractVerificationFlattenSourceCode/>, 'flattened-code': <ContractVerificationFlattenSourceCode/>,
...@@ -40,14 +40,13 @@ interface Props { ...@@ -40,14 +40,13 @@ interface Props {
const ContractVerificationForm = ({ method: methodFromQuery, config, hash }: Props) => { const ContractVerificationForm = ({ method: methodFromQuery, config, hash }: Props) => {
const formApi = useForm<FormFields>({ const formApi = useForm<FormFields>({
mode: 'onBlur', mode: 'onBlur',
defaultValues: methodFromQuery ? DEFAULT_VALUES[methodFromQuery] : undefined, defaultValues: methodFromQuery ? getDefaultValues(methodFromQuery, config) : undefined,
}); });
const { control, handleSubmit, watch, formState, setError, reset } = formApi; const { control, handleSubmit, watch, formState, setError, reset } = formApi;
const submitPromiseResolver = React.useRef<(value: unknown) => void>(); const submitPromiseResolver = React.useRef<(value: unknown) => void>();
const apiFetch = useApiFetch(); const apiFetch = useApiFetch();
const toast = useToast(); const toast = useToast();
const router = useRouter();
const onFormSubmit: SubmitHandler<FormFields> = React.useCallback(async(data) => { const onFormSubmit: SubmitHandler<FormFields> = React.useCallback(async(data) => {
const body = prepareRequestBody(data); const body = prepareRequestBody(data);
...@@ -86,8 +85,8 @@ const ContractVerificationForm = ({ method: methodFromQuery, config, hash }: Pro ...@@ -86,8 +85,8 @@ const ContractVerificationForm = ({ method: methodFromQuery, config, hash }: Pro
isClosable: true, isClosable: true,
}); });
router.push({ pathname: '/address/[hash]', query: { hash, tab: 'contract' } }, undefined, { shallow: false }); window.location.assign(route({ pathname: '/address/[hash]', query: { hash, tab: 'contract' } }));
}, [ hash, router, setError, toast ]); }, [ hash, setError, toast ]);
const handleSocketError = React.useCallback(() => { const handleSocketError = React.useCallback(() => {
if (!formState.isSubmitting) { if (!formState.isSubmitting) {
...@@ -129,7 +128,7 @@ const ContractVerificationForm = ({ method: methodFromQuery, config, hash }: Pro ...@@ -129,7 +128,7 @@ const ContractVerificationForm = ({ method: methodFromQuery, config, hash }: Pro
useUpdateEffect(() => { useUpdateEffect(() => {
if (methodValue) { if (methodValue) {
reset(DEFAULT_VALUES[methodValue]); reset(getDefaultValues(methodValue, config));
} }
// !!! should run only when method is changed // !!! should run only when method is changed
}, [ methodValue ]); }, [ methodValue ]);
......
...@@ -60,14 +60,14 @@ const ContractVerificationFieldMethod = ({ control, isDisabled, methods }: Props ...@@ -60,14 +60,14 @@ const ContractVerificationFieldMethod = ({ control, isDisabled, methods }: Props
const renderPopoverListItem = React.useCallback((method: SmartContractVerificationMethod) => { const renderPopoverListItem = React.useCallback((method: SmartContractVerificationMethod) => {
switch (method) { switch (method) {
case 'flattened-code': case 'flattened-code':
return <ListItem>Verification through flattened source code.</ListItem>; return <ListItem key={ method }>Verification through flattened source code.</ListItem>;
case 'multi-part': case 'multi-part':
return <ListItem>Verification of multi-part Solidity files.</ListItem>; return <ListItem key={ method }>Verification of multi-part Solidity files.</ListItem>;
case 'sourcify': case 'sourcify':
return <ListItem>Verification through <Link href="https://sourcify.dev/" target="_blank">Sourcify</Link>.</ListItem>; return <ListItem key={ method }>Verification through <Link href="https://sourcify.dev/" target="_blank">Sourcify</Link>.</ListItem>;
case 'standard-input': case 'standard-input':
return ( return (
<ListItem> <ListItem key={ method }>
<span>Verification using </span> <span>Verification using </span>
<Link <Link
href="https://docs.soliditylang.org/en/latest/using-the-compiler.html#input-description" href="https://docs.soliditylang.org/en/latest/using-the-compiler.html#input-description"
...@@ -79,9 +79,9 @@ const ContractVerificationFieldMethod = ({ control, isDisabled, methods }: Props ...@@ -79,9 +79,9 @@ const ContractVerificationFieldMethod = ({ control, isDisabled, methods }: Props
</ListItem> </ListItem>
); );
case 'vyper-code': case 'vyper-code':
return <ListItem>Verification of Vyper contract.</ListItem>; return <ListItem key={ method }>Verification of Vyper contract.</ListItem>;
case 'vyper-multi-part': case 'vyper-multi-part':
return <ListItem>Verification of multi-part Vyper files.</ListItem>; return <ListItem key={ method }>Verification of multi-part Vyper files.</ListItem>;
} }
}, []); }, []);
......
...@@ -15,8 +15,8 @@ export interface FormFieldsFlattenSourceCode { ...@@ -15,8 +15,8 @@ export interface FormFieldsFlattenSourceCode {
method: MethodOption; method: MethodOption;
is_yul: boolean; is_yul: boolean;
name: string; name: string;
compiler: Option; compiler: Option | null;
evm_version: Option; evm_version: Option | null;
is_optimization_enabled: boolean; is_optimization_enabled: boolean;
optimization_runs: string; optimization_runs: string;
code: string; code: string;
...@@ -28,7 +28,7 @@ export interface FormFieldsFlattenSourceCode { ...@@ -28,7 +28,7 @@ export interface FormFieldsFlattenSourceCode {
export interface FormFieldsStandardInput { export interface FormFieldsStandardInput {
method: MethodOption; method: MethodOption;
name: string; name: string;
compiler: Option; compiler: Option | null;
sources: Array<File>; sources: Array<File>;
autodetect_constructor_args: boolean; autodetect_constructor_args: boolean;
constructor_args: string; constructor_args: string;
...@@ -42,8 +42,8 @@ export interface FormFieldsSourcify { ...@@ -42,8 +42,8 @@ export interface FormFieldsSourcify {
export interface FormFieldsMultiPartFile { export interface FormFieldsMultiPartFile {
method: MethodOption; method: MethodOption;
compiler: Option; compiler: Option | null;
evm_version: Option; evm_version: Option | null;
is_optimization_enabled: boolean; is_optimization_enabled: boolean;
optimization_runs: string; optimization_runs: string;
sources: Array<File>; sources: Array<File>;
...@@ -53,15 +53,15 @@ export interface FormFieldsMultiPartFile { ...@@ -53,15 +53,15 @@ export interface FormFieldsMultiPartFile {
export interface FormFieldsVyperContract { export interface FormFieldsVyperContract {
method: MethodOption; method: MethodOption;
name: string; name: string;
compiler: Option; compiler: Option | null;
code: string; code: string;
constructor_args: string; constructor_args: string;
} }
export interface FormFieldsVyperMultiPartFile { export interface FormFieldsVyperMultiPartFile {
method: MethodOption; method: MethodOption;
compiler: Option; compiler: Option | null;
evm_version: Option; evm_version: Option | null;
sources: Array<File>; sources: Array<File>;
} }
......
...@@ -10,7 +10,7 @@ import type { ...@@ -10,7 +10,7 @@ import type {
FormFieldsVyperContract, FormFieldsVyperContract,
FormFieldsVyperMultiPartFile, FormFieldsVyperMultiPartFile,
} from './types'; } from './types';
import type { SmartContractVerificationMethod, SmartContractVerificationError } from 'types/api/contract'; import type { SmartContractVerificationMethod, SmartContractVerificationError, SmartContractVerificationConfig } from 'types/api/contract';
import type { Params as FetchParams } from 'lib/hooks/useFetch'; import type { Params as FetchParams } from 'lib/hooks/useFetch';
...@@ -32,7 +32,7 @@ export const METHOD_LABELS: Record<SmartContractVerificationMethod, string> = { ...@@ -32,7 +32,7 @@ export const METHOD_LABELS: Record<SmartContractVerificationMethod, string> = {
'vyper-multi-part': 'Vyper (Multi-part files)', 'vyper-multi-part': 'Vyper (Multi-part files)',
}; };
export const DEFAULT_VALUES = { export const DEFAULT_VALUES: Record<SmartContractVerificationMethod, FormFields> = {
'flattened-code': { 'flattened-code': {
method: { method: {
value: 'flattened-code' as const, value: 'flattened-code' as const,
...@@ -75,7 +75,7 @@ export const DEFAULT_VALUES = { ...@@ -75,7 +75,7 @@ export const DEFAULT_VALUES = {
compiler: null, compiler: null,
evm_version: null, evm_version: null,
is_optimization_enabled: true, is_optimization_enabled: true,
optimization_runs: 200, optimization_runs: '200',
sources: [], sources: [],
libraries: [], libraries: [],
}, },
...@@ -100,6 +100,22 @@ export const DEFAULT_VALUES = { ...@@ -100,6 +100,22 @@ export const DEFAULT_VALUES = {
}, },
}; };
export function getDefaultValues(method: SmartContractVerificationMethod, config: SmartContractVerificationConfig) {
const defaultValues = DEFAULT_VALUES[method];
if ('evm_version' in defaultValues) {
if (method === 'flattened-code' || method === 'multi-part') {
defaultValues.evm_version = config.solidity_evm_versions.find((value) => value === 'default') ? { label: 'default', value: 'default' } : null;
}
if (method === 'vyper-multi-part') {
defaultValues.evm_version = config.vyper_evm_versions.find((value) => value === 'default') ? { label: 'default', value: 'default' } : null;
}
}
return defaultValues;
}
export function isValidVerificationMethod(method?: string): method is SmartContractVerificationMethod { export function isValidVerificationMethod(method?: string): method is SmartContractVerificationMethod {
return method && SUPPORTED_VERIFICATION_METHODS.includes(method) ? true : false; return method && SUPPORTED_VERIFICATION_METHODS.includes(method) ? true : false;
} }
...@@ -141,7 +157,7 @@ export function prepareRequestBody(data: FormFields): FetchParams['body'] { ...@@ -141,7 +157,7 @@ export function prepareRequestBody(data: FormFields): FetchParams['body'] {
const _data = data as FormFieldsStandardInput; const _data = data as FormFieldsStandardInput;
const body = new FormData(); const body = new FormData();
body.set('compiler_version', _data.compiler?.value); _data.compiler && body.set('compiler_version', _data.compiler.value);
body.set('contract_name', _data.name); body.set('contract_name', _data.name);
body.set('autodetect_constructor_args', String(Boolean(_data.autodetect_constructor_args))); body.set('autodetect_constructor_args', String(Boolean(_data.autodetect_constructor_args)));
body.set('constructor_args', _data.constructor_args); body.set('constructor_args', _data.constructor_args);
...@@ -163,8 +179,8 @@ export function prepareRequestBody(data: FormFields): FetchParams['body'] { ...@@ -163,8 +179,8 @@ export function prepareRequestBody(data: FormFields): FetchParams['body'] {
const _data = data as FormFieldsMultiPartFile; const _data = data as FormFieldsMultiPartFile;
const body = new FormData(); const body = new FormData();
body.set('compiler_version', _data.compiler?.value); _data.compiler && body.set('compiler_version', _data.compiler.value);
body.set('evm_version', _data.evm_version?.value); _data.evm_version && body.set('evm_version', _data.evm_version.value);
body.set('is_optimization_enabled', String(Boolean(_data.is_optimization_enabled))); body.set('is_optimization_enabled', String(Boolean(_data.is_optimization_enabled)));
_data.is_optimization_enabled && body.set('optimization_runs', _data.optimization_runs); _data.is_optimization_enabled && body.set('optimization_runs', _data.optimization_runs);
...@@ -190,8 +206,8 @@ export function prepareRequestBody(data: FormFields): FetchParams['body'] { ...@@ -190,8 +206,8 @@ export function prepareRequestBody(data: FormFields): FetchParams['body'] {
const _data = data as FormFieldsVyperMultiPartFile; const _data = data as FormFieldsVyperMultiPartFile;
const body = new FormData(); const body = new FormData();
body.set('compiler_version', _data.compiler?.value); _data.compiler && body.set('compiler_version', _data.compiler.value);
body.set('evm_version', _data.evm_version?.value); _data.evm_version && body.set('evm_version', _data.evm_version.value);
addFilesToFormData(body, _data.sources); addFilesToFormData(body, _data.sources);
return body; return body;
......
...@@ -25,7 +25,7 @@ interface AsyncSelectProps extends AsyncProps<Option, boolean, GroupBase<Option> ...@@ -25,7 +25,7 @@ interface AsyncSelectProps extends AsyncProps<Option, boolean, GroupBase<Option>
type Props = RegularSelectProps | AsyncSelectProps; type Props = RegularSelectProps | AsyncSelectProps;
const FancySelect = (props: Props) => { const FancySelect = (props: Props, ref: React.LegacyRef<HTMLDivElement>) => {
const menuZIndex = useToken('zIndices', 'dropdown'); const menuZIndex = useToken('zIndices', 'dropdown');
const { colorMode } = useColorMode(); const { colorMode } = useColorMode();
...@@ -42,6 +42,7 @@ const FancySelect = (props: Props) => { ...@@ -42,6 +42,7 @@ const FancySelect = (props: Props) => {
variant="floating" variant="floating"
size={ props.size || 'md' } size={ props.size || 'md' }
isRequired={ props.isRequired } isRequired={ props.isRequired }
ref={ ref }
{ ...(props.error ? { 'aria-invalid': true } : {}) } { ...(props.error ? { 'aria-invalid': true } : {}) }
{ ...(props.isDisabled ? { 'aria-disabled': true } : {}) } { ...(props.isDisabled ? { 'aria-disabled': true } : {}) }
{ ...(props.value ? { 'data-active': true } : {}) } { ...(props.value ? { 'data-active': true } : {}) }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment