Files
explore/ui/address/contract/methods/utils.ts
T
tom goriunovandGitHub 434aae92bb Call contract read methods from the client (#2032)
* display methods accordion

* add method_id to methods

* query read method via public client

* query read methods without inputs at accordion section open

* add custom abi tabs

* add proxy tabs

* custom wagmi config if wc is not configured

* remove ContractAbiItemConstant component

* display tuple properties in method outputs

* display read result

* clean up

* support nested arrays in the read result

* re-write test for contract methods

* change result view for address output

* refactoring

* fix tests

* remove web3 provider fallback

* fix tab loading

* refactoring

* add implementation contract selector

* add "copy to clipboard" for address, remove top-level square brackets

* add preview mode to contract method result

* tests for read result

* fix tests

* change styles for result preview

* review fix
2024-06-24 19:56:06 +02:00

51 lines
1.3 KiB
TypeScript

import type { Abi } from 'abitype';
import { toFunctionSelector } from 'viem';
import type { SmartContractMethodRead, SmartContractMethodWrite } from './types';
export const getNativeCoinValue = (value: unknown) => {
if (typeof value !== 'string') {
return BigInt(0);
}
return BigInt(value);
};
interface DividedAbi {
read: Array<SmartContractMethodRead>;
write: Array<SmartContractMethodWrite>;
}
export const isReadMethod = (method: Abi[number]): method is SmartContractMethodRead =>
method.type === 'function' && (
method.constant || method.stateMutability === 'view' || method.stateMutability === 'pure'
);
export const isWriteMethod = (method: Abi[number]): method is SmartContractMethodWrite =>
(method.type === 'function' || method.type === 'fallback' || method.type === 'receive') &&
!isReadMethod(method);
export function divideAbiIntoMethodTypes(abi: Abi): DividedAbi {
return {
read: abi
.filter(isReadMethod)
.map((method) => ({
...method,
method_id: toFunctionSelector(method).slice(2),
})),
write: abi
.filter(isWriteMethod)
.map((method) => {
if (method.type !== 'function') {
return method;
}
return {
...method,
method_id: toFunctionSelector(method).slice(2),
};
}),
};
}