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

feat: add external flag to external contracts #647

Merged
merged 10 commits into from
Dec 27, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export const ContractUI = ({ contractName, className = "" }: ContractUIProps) =>
<div className="flex">
<div className="flex flex-col gap-1">
<span className="font-bold">{contractName}</span>
{deployedContractData.external && <span>external</span>}
<Address address={deployedContractData.address} />
<div className="flex gap-1 items-center">
<span className="font-bold text-sm">Balance:</span>
Expand Down
26 changes: 20 additions & 6 deletions packages/nextjs/utils/scaffold-eth/contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,30 @@ import deployedContractsData from "~~/contracts/deployedContracts";
import externalContractsData from "~~/contracts/externalContracts";
import scaffoldConfig from "~~/scaffold.config";

const deepMergeContracts = <D extends Record<PropertyKey, any>, S extends Record<PropertyKey, any>>(
destination: D,
source: S,
type AddExternalFlag<T> = {
[ChainId in keyof T]: {
[ContractName in keyof T[ChainId]]: T[ChainId][ContractName] & { external?: true };
};
};

const deepMergeContracts = <L extends Record<PropertyKey, any>, E extends Record<PropertyKey, any>>(
local: L,
external: E,
) => {
const result: Record<PropertyKey, any> = {};
const allKeys = Array.from(new Set([...Object.keys(source), ...Object.keys(destination)]));
const allKeys = Array.from(new Set([...Object.keys(external), ...Object.keys(local)]));
for (const key of allKeys) {
result[key] = { ...destination[key], ...source[key] };
const amendedExternal = Object.fromEntries(
Object.entries(external[key] as Record<string, Record<string, unknown>>).map(([contractName, declaration]) => [
sverps marked this conversation as resolved.
Show resolved Hide resolved
contractName,
{ ...declaration, external: true },
]),
);
result[key] = { ...local[key], ...amendedExternal };
}
return result as MergeDeepRecord<D, S, { arrayMergeMode: "replace" }>;
return result as MergeDeepRecord<AddExternalFlag<L>, AddExternalFlag<E>, { arrayMergeMode: "replace" }>;
};

const contractsData = deepMergeContracts(deployedContractsData, externalContractsData);

export type InheritedFunctions = { readonly [key: string]: string };
Expand All @@ -42,6 +55,7 @@ export type GenericContract = {
address: Address;
abi: Abi;
inheritedFunctions?: InheritedFunctions;
external?: true;
};

export type GenericContractsDeclaration = {
Expand Down