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

Add deployment arguments validation before the contract deployment #71

Closed
wants to merge 10 commits into from
Prev Previous commit
Next Next commit
Added ability to retrieve the class name from the contract instance.
  • Loading branch information
KyrylR committed Feb 7, 2024
commit ffc727477f5a353023d51f30e78ee8abe1104fb8
7 changes: 4 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@

## Version 2.1.1

* Fixed a bug related to the shallow copying and mutation of arguments within the `getMethodString` function
* Fixed a bug related to the shallow copying and mutation of arguments within the `getMethodString` function.
* Added ability to retrieve the class name from the contract instance.

## Version 2.1.0

* Updated packages to their latest versions (updated `@nomicfoundation/hardhat-verify` to version `2.0.4`)
* Added a `save` function to the Deployer class to enable saving the contract to storage without deploying it
* Updated packages to their latest versions (updated `@nomicfoundation/hardhat-verify` to version `2.0.4`).
* Added a `save` function to the Deployer class to enable saving the contract to storage without deploying it.

## Version 2.0.1

Expand Down
6 changes: 2 additions & 4 deletions src/deployer/adapters/EthersContractAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import { BaseContract, ContractRunner, Interface } from "ethers";

import { AbstractEthersAdapter } from "./AbstractEthersAdapter";

import { catchError, getSignerHelper } from "../../utils";

import { UNKNOWN_CONTRACT_NAME } from "../../constants";
import { catchError, getInstanceNameFromClass, getSignerHelper } from "../../utils";

import { EthersContract } from "../../types/adapter";
import { OverridesAndName } from "../../types/deployer";
Expand Down Expand Up @@ -33,7 +31,7 @@ export class EthersContractAdapter extends AbstractEthersAdapter {
return (instance as any).contractName;
}

return UNKNOWN_CONTRACT_NAME;
return getInstanceNameFromClass(instance);
}
}

Expand Down
6 changes: 2 additions & 4 deletions src/deployer/adapters/EthersFactoryAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { AbstractEthersAdapter } from "./AbstractEthersAdapter";

import { OverridesAndName } from "../../types/deployer";

import { catchError } from "../../utils";

import { UNKNOWN_CONTRACT_NAME } from "../../constants";
import { catchError, getInstanceNameFromClass } from "../../utils";

import { ArtifactProcessor } from "../../tools/storage/ArtifactProcessor";

Expand All @@ -32,7 +30,7 @@ export class EthersFactoryAdapter extends AbstractEthersAdapter {
return (instance as any).contractName;
}

return UNKNOWN_CONTRACT_NAME;
return getInstanceNameFromClass(instance);
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/deployer/adapters/TruffleAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@ import { Adapter } from "./Adapter";

import { MinimalContract } from "../MinimalContract";

import { bytecodeToString, catchError, fillParameters, getMethodString, getSignerHelper } from "../../utils";
import {
bytecodeToString,
catchError,
fillParameters,
getInstanceNameFromClass,
getMethodString,
getSignerHelper,
} from "../../utils";

import { UNKNOWN_CONTRACT_NAME, UNKNOWN_TRANSACTION_NAME } from "../../constants";
import { UNKNOWN_TRANSACTION_NAME } from "../../constants";

import { KeyTransactionFields, MigrationMetadata } from "../../types/tools";
import { EthersContract, Instance, TruffleFactory } from "../../types/adapter";
Expand Down Expand Up @@ -82,7 +89,7 @@ export class TruffleAdapter extends Adapter {
return this._getFullyQualifiedName(instance as any) || (instance as any).contractName;
}

return UNKNOWN_CONTRACT_NAME;
return getInstanceNameFromClass(instance);
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/utils/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { join } from "path";
import { realpathSync, existsSync } from "fs";
import { UNKNOWN_CONTRACT_NAME } from "../constants";

export function resolvePathToFile(path: string, file: string = ""): string {
if (!existsSync(path)) {
Expand All @@ -9,6 +10,24 @@ export function resolvePathToFile(path: string, file: string = ""): string {
return join(realpathSync(path), file);
}

export function getInstanceNameFromClass(instance: any): string {
const className = parseClassName(instance.toString());

return className === undefined ? UNKNOWN_CONTRACT_NAME : className.replace("__factory", "");
}

function parseClassName(classDefinitionString: string) {
// Regular expression to match the class name
const classNameRegex = /class\s+([^\s]+)\s*\{/;

const match = classDefinitionString.match(classNameRegex);
if (match && match.length > 1) {
return match[1];
}

return undefined;
}

export function deepCopy<T>(obj: T): T {
return JSON.parse(JSON.stringify(obj));
}
Expand Down
Loading