Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show error if contract method has invalid data structure #2087

Merged
merged 3 commits into from
Jul 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion icons/block_countdown.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion mocks/contract/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,6 @@ export const write: Array<SmartContractMethodWrite> = [
payable: false,
stateMutability: 'nonpayable',
type: 'function',
method_id: '0x06',
is_invalid: true,
},
];
22 changes: 13 additions & 9 deletions ui/address/contract/methods/ContractAbiItem.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AccordionButton, AccordionIcon, AccordionItem, AccordionPanel, Box, Tooltip, useClipboard, useDisclosure } from '@chakra-ui/react';
import { AccordionButton, AccordionIcon, AccordionItem, AccordionPanel, Alert, Box, Tooltip, useClipboard, useDisclosure } from '@chakra-ui/react';
import React from 'react';
import { Element } from 'react-scroll';

Expand Down Expand Up @@ -110,14 +110,18 @@ const ContractAbiItem = ({ data, index, id, addressHash, tab, onSubmit }: Props)
</AccordionButton>
</Element>
<AccordionPanel pb={ 4 } pr={ 0 } pl="28px" w="calc(100% - 6px)">
<ContractMethodForm
key={ id + '_' + index + '_' + attempt }
data={ data }
attempt={ attempt }
onSubmit={ onSubmit }
onReset={ handleReset }
isOpen={ isExpanded }
/>
{ 'is_invalid' in data && data.is_invalid ? (
<Alert status="warning">An error occurred while parsing the method signature.</Alert>
) : (
<ContractMethodForm
key={ id + '_' + index + '_' + attempt }
data={ data }
attempt={ attempt }
onSubmit={ onSubmit }
onReset={ handleReset }
isOpen={ isExpanded }
/>
) }
</AccordionPanel>
</>
) }
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ interface Props {

const ItemTuple = ({ abiParameter, data, mode, level }: Props) => {
return (
<p>
<div>
<p>
<span>{ printRowOffset(level) }</span>
<chakra.span fontWeight={ 500 }>{ abiParameter.name || abiParameter.internalType }</chakra.span>
Expand All @@ -36,7 +36,7 @@ const ItemTuple = ({ abiParameter, data, mode, level }: Props) => {
);
}) }
<p>{ printRowOffset(level) }{ '}' }</p>
</p>
</div>
);
};

Expand Down
5 changes: 3 additions & 2 deletions ui/address/contract/methods/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ export type MethodType = 'read' | 'write';
export type MethodCallStrategy = 'read' | 'write' | 'simulate';
export type ResultViewMode = 'preview' | 'result';

export type SmartContractMethodRead = AbiFunction & { method_id: string };
export type SmartContractMethodWrite = AbiFunction & { method_id: string } | AbiFallback | AbiReceive;
export type SmartContractMethodCustomFields = { method_id: string } | { is_invalid: boolean };
export type SmartContractMethodRead = AbiFunction & SmartContractMethodCustomFields;
export type SmartContractMethodWrite = AbiFunction & SmartContractMethodCustomFields | AbiFallback | AbiReceive;
export type SmartContractMethod = SmartContractMethodRead | SmartContractMethodWrite;

export interface FormSubmitResultPublicClient {
Expand Down
11 changes: 7 additions & 4 deletions ui/address/contract/methods/useCallMethodPublicClient.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from 'react';
import { getAddress } from 'viem';
import { usePublicClient } from 'wagmi';

import type { FormSubmitResult, MethodCallStrategy, SmartContractMethod } from './types';
Expand All @@ -15,7 +16,7 @@ interface Params {

export default function useCallMethodPublicClient(): (params: Params) => Promise<FormSubmitResult> {
const publicClient = usePublicClient({ chainId: Number(config.chain.id) });
const { address } = useAccount();
const { address: account } = useAccount();

return React.useCallback(async({ args, item, addressHash, strategy }) => {
if (!('name' in item)) {
Expand All @@ -26,12 +27,14 @@ export default function useCallMethodPublicClient(): (params: Params) => Promise
throw new Error('Public Client is not defined');
}

const address = getAddress(addressHash);

const params = {
abi: [ item ],
functionName: item.name,
args: args,
address: addressHash as `0x${ string }`,
account: address,
address,
account,
};

const result = strategy === 'read' ? await publicClient.readContract(params) : await publicClient.simulateContract(params);
Expand All @@ -40,5 +43,5 @@ export default function useCallMethodPublicClient(): (params: Params) => Promise
data: strategy === 'read' ? result : result.result,
};

}, [ address, publicClient ]);
}, [ account, publicClient ]);
}
19 changes: 16 additions & 3 deletions ui/address/contract/methods/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Abi } from 'abitype';
import type { AbiFunction } from 'viem';
import { toFunctionSelector } from 'viem';

import type { SmartContractMethodRead, SmartContractMethodWrite } from './types';
import type { SmartContractMethodCustomFields, SmartContractMethodRead, SmartContractMethodWrite } from './types';

export const getNativeCoinValue = (value: unknown) => {
if (typeof value !== 'string') {
Expand All @@ -25,13 +26,25 @@ export const isWriteMethod = (method: Abi[number]): method is SmartContractMetho
(method.type === 'function' || method.type === 'fallback' || method.type === 'receive') &&
!isReadMethod(method);

const enrichWithMethodId = (method: AbiFunction): SmartContractMethodCustomFields => {
try {
return {
method_id: toFunctionSelector(method).slice(2),
};
} catch (error) {
return {
is_invalid: true,
};
}
};

export function divideAbiIntoMethodTypes(abi: Abi): DividedAbi {
return {
read: abi
.filter(isReadMethod)
.map((method) => ({
...method,
method_id: toFunctionSelector(method).slice(2),
...enrichWithMethodId(method),
})),
write: abi
.filter(isWriteMethod)
Expand All @@ -43,7 +56,7 @@ export function divideAbiIntoMethodTypes(abi: Abi): DividedAbi {

return {
...method,
method_id: toFunctionSelector(method).slice(2),
...enrichWithMethodId(method),
};
}),
};
Expand Down
Loading