Skip to content

Commit

Permalink
added Handler inception to contracts factory (#41)
Browse files Browse the repository at this point in the history
* added Handler inception to contracts factory

* fixed naming

* implemented class AbstractEthersAdapter

* added features

* changed own function to libs

* fixed intergration of Reporter to Truffle objects
  • Loading branch information
RuslanProgrammer authored Oct 27, 2023
1 parent 775a6b5 commit 9882f20
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 166 deletions.
31 changes: 28 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@
"@nomicfoundation/hardhat-verify": "1.1.1",
"@nomiclabs/hardhat-truffle5": "2.0.7",
"axios": "1.5.0",
"bignumber.js": "9.1.2",
"ora": "5.4.1"
},
"peerDependencies": {
Expand All @@ -69,7 +68,7 @@
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-prettier": "^5.0.0",
"eslint-plugin-promise": "^6.1.1",
"ethers": "^6.7.1",
"ethers": "^6.8.0",
"hardhat": "^2.17.0",
"mocha": "^10.0.0",
"pinst": "^3.0.0",
Expand Down
23 changes: 18 additions & 5 deletions src/deployer/Deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ import { Signer } from "ethers";

import { HardhatRuntimeEnvironment } from "hardhat/types";

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

import { MigrateError } from "../errors";

import { Adapter } from "./adapters/Adapter";
import { PureAdapter } from "./adapters/PureAdapter";
import { EthersAdapter } from "./adapters/EthersAdapter";
import { TruffleAdapter } from "./adapters/TruffleAdapter";
import { PureAdapter } from "./adapters/PureAdapter";
import { PureEthersAdapter } from "./adapters/PureEthersAdapter";
import { TruffleAdapter } from "./adapters/TruffleAdapter";

import { OverridesAndLibs } from "../types/deployer";
import { Instance, TypedArgs } from "../types/adapter";
import { OverridesAndLibs } from "../types/deployer";
import { isContractFactory, isEthersFactory, isPureFactory, isTruffleFactory } from "../types/type-checks";

import { TransactionProcessor } from "../tools/storage/TransactionProcessor";
Expand All @@ -36,7 +36,20 @@ export class Deployer {
return adapter.toInstance(contract, contractAddress, parameters);
}

public async deployed<A, I>(contract: Instance<A, I>): Promise<I> {
public setAsDeployed<T, A = T, I = any>(
contract: Instance<A, I> | (T extends Truffle.Contract<I> ? T : never),
address: string,
): void {
const adapter = this._resolveAdapter(contract);

const contractName = adapter.getContractName(contract);

TransactionProcessor.saveDeploymentTransactionWithContractName(contractName, address);
}

public async deployed<T, A = T, I = any>(
contract: Instance<A, I> | (T extends Truffle.Contract<I> ? T : never),
): Promise<I> {
const adapter = this._resolveAdapter(contract);

const contractName = adapter.getContractName(contract);
Expand Down
2 changes: 2 additions & 0 deletions src/deployer/Linker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ export class Linker {
} catch {
const artifact = this._mustGetLibraryArtifact(libraryName);

// https://github.com/ethers-io/ethers.js/issues/2431
// https://github.com/ethers-io/ethers.js/issues/1126
const core = new MinimalContract(hre, artifact.bytecode, Interface.from(artifact.abi), libraryName);

Reporter.notifyDeploymentOfMissingLibrary(libraryName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
BaseContract,
BaseContractMethod,
ContractFactory,
ContractTransaction,
ContractTransactionResponse,
defineProperties,
Expand All @@ -10,19 +11,53 @@ import {

import { HardhatRuntimeEnvironment } from "hardhat/types";

import { fillParameters, getMethodString } from "../../utils";
import { Adapter } from "./Adapter";

import { MigrateConfig } from "../../types/migrations";
import { bytecodeToString, fillParameters, getMethodString, getSignerHelper } from "../../utils";

import { EthersFactory, PureFactory } from "../../types/adapter";
import { OverridesAndLibs } from "../../types/deployer";

import { Reporter } from "../../tools/reporters/Reporter";
import { TransactionProcessor } from "../../tools/storage/TransactionProcessor";
import { MinimalContract } from "../MinimalContract";

type Factory<A, I> = EthersFactory<A, I> | PureFactory | ContractFactory;

export class EthersInjectHelper {
protected _config: MigrateConfig;
export abstract class AbstractEthersAdapter extends Adapter {
private static _processedClasses = new Set<string>();

constructor(private _hre: HardhatRuntimeEnvironment) {
this._config = _hre.config.migrate;
constructor(_hre: HardhatRuntimeEnvironment) {
super(_hre);
}

public getRawBytecode<A, I>(instance: Factory<A, I>): string {
return bytecodeToString(instance.bytecode);
}

public async fromInstance<A, I>(instance: Factory<A, I>): Promise<MinimalContract> {
return new MinimalContract(
this._hre,
this.getRawBytecode(instance),
this.getInterface(instance),
this.getContractName(instance),
);
}

public async toInstance<A, I>(instance: Factory<A, I>, address: string, parameters: OverridesAndLibs): Promise<I> {
const signer = await getSignerHelper(this._hre, parameters.from);

const contract = new BaseContract(address, this.getInterface(instance), signer);

const contractName = this.getContractName(instance);

if (!AbstractEthersAdapter._processedClasses.has(contractName)) {
AbstractEthersAdapter._processedClasses.add(contractName);

this.overrideConnectMethod(instance, contractName);
}

return this.insertHandlers(contract, contractName, parameters) as unknown as I;
}

public insertHandlers(contract: BaseContract, contractName: string, parameters: OverridesAndLibs): BaseContract {
Expand Down Expand Up @@ -65,6 +100,8 @@ export class EthersInjectHelper {
return contract;
}

public abstract overrideConnectMethod<A, I>(instance: Factory<A, I>, contractName: string): Promise<void>;

private _getContractFunctionFragments(contractInterface: Interface): FunctionFragment[] {
const result: FunctionFragment[] = [];

Expand All @@ -76,8 +113,8 @@ export class EthersInjectHelper {
}

private _wrapOldMethod(
methodName: string,
contractName: string,
methodName: string,
methodFragments: FunctionFragment,
oldMethod: BaseContractMethod,
parameters: OverridesAndLibs,
Expand Down
Loading

0 comments on commit 9882f20

Please sign in to comment.