From 5bc6506cecc4374d242ea2fcbd524e8c718dd1b6 Mon Sep 17 00:00:00 2001 From: port <108868128+portdeveloper@users.noreply.github.com> Date: Wed, 13 Mar 2024 19:05:47 +0300 Subject: [PATCH 1/2] Use uid's when adding/removing methods (#70) --- .../Contract/ContractReadMethods.tsx | 9 +-- .../scaffold-eth/Contract/ContractUI.tsx | 64 +++++++++++-------- .../Contract/ContractWriteMethods.tsx | 7 +- .../scaffold-eth/Contract/MethodSelector.tsx | 43 ++++++------- 4 files changed, 64 insertions(+), 59 deletions(-) diff --git a/packages/nextjs/components/scaffold-eth/Contract/ContractReadMethods.tsx b/packages/nextjs/components/scaffold-eth/Contract/ContractReadMethods.tsx index a5a87f84..95c43aff 100644 --- a/packages/nextjs/components/scaffold-eth/Contract/ContractReadMethods.tsx +++ b/packages/nextjs/components/scaffold-eth/Contract/ContractReadMethods.tsx @@ -1,5 +1,6 @@ +import { AugmentedAbiFunction } from "./ContractUI"; import { ReadOnlyFunctionForm } from "./ReadOnlyFunctionForm"; -import { Abi, AbiFunction } from "abitype"; +import { Abi } from "abitype"; import { XMarkIcon } from "@heroicons/react/24/outline"; import { Contract, ContractName, GenericContract, InheritedFunctions } from "~~/utils/scaffold-eth/contract"; @@ -15,7 +16,7 @@ export const ContractReadMethods = ({ } const functionsToDisplay = ( - ((deployedContractData.abi || []) as Abi).filter(part => part.type === "function") as AbiFunction[] + ((deployedContractData.abi || []) as Abi).filter(part => part.type === "function") as AugmentedAbiFunction[] ) .filter(fn => { const isQueryableWithParams = @@ -41,7 +42,7 @@ export const ContractReadMethods = ({ return ( <> {functionsToDisplay.map(({ fn, inheritedFrom }) => ( -
+
@@ -100,15 +95,15 @@ export const MethodSelector = ({
From bbf8460d8ad44211f0c7f7cec5a91bb18451b2fb Mon Sep 17 00:00:00 2001 From: port <108868128+portdeveloper@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:28:08 +0300 Subject: [PATCH 2/2] Only add uids to overloaded functions (#73) --- .../scaffold-eth/Contract/ContractUI.tsx | 40 +++++++++++++++---- 1 file changed, 33 insertions(+), 7 deletions(-) diff --git a/packages/nextjs/components/scaffold-eth/Contract/ContractUI.tsx b/packages/nextjs/components/scaffold-eth/Contract/ContractUI.tsx index 9e29f587..592bab93 100644 --- a/packages/nextjs/components/scaffold-eth/Contract/ContractUI.tsx +++ b/packages/nextjs/components/scaffold-eth/Contract/ContractUI.tsx @@ -22,6 +22,39 @@ export interface AugmentedAbiFunction extends AbiFunction { uid: string; } +const augmentMethodsWithUid = (methods: AbiFunction[]): AugmentedAbiFunction[] => { + // Group methods by their name to identify overloaded functions + const methodsByName: Record = {}; + methods.forEach(method => { + if (!methodsByName[method.name]) { + methodsByName[method.name] = []; + } + methodsByName[method.name].push(method); + }); + + // Process each method, adding UID with index only for overloaded functions + const augmentedMethods: AugmentedAbiFunction[] = []; + Object.entries(methodsByName).forEach(([, group]) => { + if (group.length > 1) { + // overloaded methods + group.forEach((method, index) => { + augmentedMethods.push({ + ...method, + uid: `${method.name}_${index}`, + }); + }); + } else { + // regular methods + augmentedMethods.push({ + ...group[0], + uid: group[0].name, + }); + } + }); + + return augmentedMethods; +}; + const mainNetworks = getTargetNetworks(); /** @@ -47,13 +80,6 @@ export const ContractUI = ({ className = "", initialContractData }: ContractUIPr router.push({ pathname: newPath, query: currentQuery.toString() }, undefined, { shallow: true }); }; - const augmentMethodsWithUid = (methods: AbiFunction[]): AugmentedAbiFunction[] => { - return methods.map((method, index) => ({ - ...method, - uid: `${method.name}_${index}`, // Simple UID based on index - })); - }; - const readMethodsWithInputsAndWriteMethods = useMemo(() => { return augmentMethodsWithUid( initialContractData.abi.filter((method): method is AbiFunction => {