Skip to content

Commit

Permalink
Added ability to save address for contract (#69)
Browse files Browse the repository at this point in the history
* Added ability to save address for contract

* Updated version

* Updated packages

* Updated ora version

* Wrote tests. Cleaned up a project

* Updated CHANGELOG.md
  • Loading branch information
KyrylR authored Feb 1, 2024
1 parent 82d5d23 commit 0d95688
Show file tree
Hide file tree
Showing 11 changed files with 409 additions and 547 deletions.
1 change: 1 addition & 0 deletions .mocharc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"file": ["test/setup.ts"],
"require": "ts-node/register/files",
"ignore": ["test/fixture-projects/**/*"],
"timeout": 10000
Expand Down
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## 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

## Version 2.0.1

* Fixed a bug where an instance of a storage object overwrote the state of another object in the file.
Expand Down
792 changes: 283 additions & 509 deletions package-lock.json

Large diffs are not rendered by default.

43 changes: 23 additions & 20 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solarity/hardhat-migrate",
"version": "2.0.1",
"version": "2.1.0",
"description": "Automatic deployment and verification of smart contracts",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down Expand Up @@ -47,48 +47,51 @@
"publish-to-npm": "npm run compile && npm run lint-fix && npm publish ./ --access public"
},
"nyc": {
"reporter": ["html", "text"]
"reporter": [
"html",
"text"
]
},
"dependencies": {
"@nomicfoundation/hardhat-ethers": "3.0.5",
"@nomicfoundation/hardhat-verify": "1.1.1",
"@nomicfoundation/hardhat-verify": "2.0.4",
"@nomiclabs/hardhat-truffle5": "2.0.7",
"axios": "1.5.0",
"ethers": "6.9.0",
"axios": "1.6.7",
"ethers": "6.10.0",
"ora": "5.4.1"
},
"peerDependencies": {
"hardhat": "^2.14.0",
"typechain": "^8.3.1"
},
"devDependencies": {
"@nomicfoundation/hardhat-ethers": "^3.0.4",
"@nomiclabs/hardhat-truffle5": "^2.0.0",
"@nomicfoundation/hardhat-ethers": "^3.0.5",
"@nomiclabs/hardhat-truffle5": "^2.0.7",
"@openzeppelin/contracts": "^5.0.1",
"@typechain/ethers-v6": "^0.5.0",
"@typechain/ethers-v6": "^0.5.1",
"@typechain/hardhat": "^9.1.0",
"@typechain/truffle-v5": "^8.0.7",
"@types/chai": "^4.3.11",
"@types/chai-as-promised": "^7.1.8",
"@types/mocha": "^10.0.6",
"@types/ora": "^3.2.0",
"@typescript-eslint/eslint-plugin": "^6.6.0",
"@typescript-eslint/parser": "^6.6.0",
"chai": "^4.3.10",
"@typescript-eslint/eslint-plugin": "^6.20.0",
"@typescript-eslint/parser": "^6.20.0",
"chai": "^4.4.1",
"chai-as-promised": "^7.1.1",
"eslint": "^8.48.0",
"eslint-plugin-import": "^2.28.1",
"eslint-plugin-prettier": "^5.0.0",
"eslint": "^8.56.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-prettier": "^5.1.3",
"eslint-plugin-promise": "^6.1.1",
"ethers": "^6.8.0",
"hardhat": "^2.17.0",
"ethers": "^6.10.0",
"hardhat": "^2.19.5",
"hardhat-abi-exporter": "^2.10.1",
"mocha": "^10.2.0",
"nyc": "^15.1.0",
"pinst": "^3.0.0",
"prettier": "^3.0.3",
"ts-node": "^8.1.0",
"typechain": "^8.3.1",
"typescript": "^5.2.2"
"prettier": "^3.2.4",
"ts-node": "^8.10.2",
"typechain": "^8.3.2",
"typescript": "^5.3.3"
}
}
24 changes: 24 additions & 0 deletions src/deployer/Deployer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,30 @@ export class Deployer {
return adapter.toInstance(contract, contractAddress!, {});
}

public async save<T, A = T, I = any>(
contract: Instance<A, I> | (T extends Truffle.Contract<I> ? T : never) | string,
contractAddress: string,
) {
if (!(await isDeployedContractAddress(contractAddress))) {
throw new MigrateError(`Contract with address '${contractAddress}' is not deployed`);
}

const saveMetadata: MigrationMetadata = {
migrationNumber: Stats.currentMigration,
};

if (typeof contract === "string") {
TransactionProcessor?.saveContractAddress(contract, contractAddress, saveMetadata);

return;
}

const adapter = Deployer.resolveAdapter(this._hre, contract);
const defaultContractName = adapter.getContractName(contract, {});

TransactionProcessor?.saveContractAddress(defaultContractName, contractAddress, saveMetadata);
}

public async sendNative(
to: string,
value: bigint,
Expand Down
4 changes: 2 additions & 2 deletions src/tools/reporters/Reporter.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable no-console */
import ora from "ora";
import ora, { Ora } from "ora";

import { Network, TransactionResponse, formatEther, formatUnits, TransactionReceipt, id } from "ethers";

Expand All @@ -22,7 +22,7 @@ class BaseReporter {
private _config: MigrateConfig = {} as any;
private _network: Network = {} as any;

private _spinner: ora.Ora | null = null;
private _spinner: Ora | null = null;
private _spinnerMessage: string | null = null;
private _spinnerInterval: NodeJS.Timeout | null = null;
private _spinnerState: string[] = [];
Expand Down
9 changes: 9 additions & 0 deletions src/tools/storage/TransactionProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ export class BaseTransactionProcessor {
);
}

public saveContractAddress(contractName: string, contractAddress: string, metadata: MigrationMetadata) {
const dataToSave: ContractFieldsToSave = {
contractAddress,
metadata,
};

this._saveContractByName(contractName, dataToSave);
}

@validateKeyDeploymentFields
public async tryRestoreContractAddressByKeyFields(key: ContractDeployTxWithName): Promise<string> {
const restoredData = this._tryGetDataFromStorage(
Expand Down
8 changes: 1 addition & 7 deletions test/integration/deployer/base-contract-interaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import chai, { expect } from "chai";
import chaiAsPromised from "chai-as-promised";

chai.use(chaiAsPromised);
import { expect } from "chai";

import { useEnvironment } from "../../helpers";

Expand All @@ -16,9 +13,6 @@ import { Migrator } from "../../../src/migrator/Migrator";
import { ethersProvider } from "../../../src/tools/network/EthersProvider";
import { TransactionStorage } from "../../../src/tools/storage/MigrateStorage";

import { resetTransactionRunner } from "../../../src/tools/runners/TransactionRunner";
import { resetReporter } from "../../../src/tools/reporters/Reporter";

describe("deployer", () => {
let deployer: Deployer;

Expand Down
60 changes: 60 additions & 0 deletions test/integration/deployer/save-and-recover.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { expect } from "chai";
import { ethers } from "ethers";

import { useEnvironment } from "../../helpers";

import {
ConstructorWithArguments,
ConstructorWithArguments__factory,
} from "../../fixture-projects/hardhat-project-typechain-ethers/typechain-types";

import { Deployer } from "../../../src/deployer/Deployer";
import { Migrator } from "../../../src/migrator/Migrator";

import { ethersProvider } from "../../../src/tools/network/EthersProvider";
import { TransactionStorage } from "../../../src/tools/storage/MigrateStorage";

describe("deployer", () => {
let deployer: Deployer;

describe("default interaction with contracts", () => {
useEnvironment("typechain-ethers");

let contract: ConstructorWithArguments;

beforeEach("setup", async function () {
await Migrator.buildMigrateTaskDeps(this.hre);

deployer = new Deployer(this.hre);

const ContractFactory = new ConstructorWithArguments__factory(await ethersProvider!.getSigner());
contract = await ContractFactory.deploy(2);

TransactionStorage.clear();
});

it("should save and recover contract by name", async function () {
const contractName = "ConstructorWithArguments";

await deployer.save(contractName, await contract.getAddress());

const contractInstance = await deployer.deployed(ConstructorWithArguments__factory, contractName);

expect(await contractInstance.getAddress()).to.equal(await contract.getAddress());
});

it("should save and recover contract by instance", async function () {
await deployer.save(ConstructorWithArguments__factory, await contract.getAddress());

const contractInstance = await deployer.deployed(ConstructorWithArguments__factory);

expect(await contractInstance.getAddress()).to.equal(await contract.getAddress());
});

it("should throw error if contract is not deployed", async function () {
await expect(deployer.save(ConstructorWithArguments__factory, ethers.ZeroAddress)).to.be.rejectedWith(
"Deployer.save(): Contract with address '0x0000000000000000000000000000000000000000' is not deployed",
);
});
});
});
3 changes: 0 additions & 3 deletions test/integration/migration/typechain-truffle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { HardhatRuntimeEnvironment } from "hardhat/types";
import { runWithContinue, runWithoutContinue } from "./helper";

import { useEnvironment } from "../../helpers";
import { resetReporter } from "../../../src/tools/reporters/Reporter";
import { resetTransactionRunner } from "../../../src/tools/runners/TransactionRunner";
import { resetNetworkManager } from "../../../src/tools/network/NetworkManager";

describe("typechain-truffle", () => {
useEnvironment("typechain-truffle");
Expand Down
7 changes: 1 addition & 6 deletions test/integration/tools/transaction-storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import chai, { assert, expect } from "chai";
import chaiAsPromised from "chai-as-promised";

chai.use(chaiAsPromised);
import { assert, expect } from "chai";

import { ContractFactory, ZeroAddress } from "ethers";

Expand All @@ -20,8 +17,6 @@ import { ContractDeployTxWithName } from "../../../src/types/deployer";

import { TransactionStorage } from "../../../src/tools/storage/MigrateStorage";
import { TransactionProcessor } from "../../../src/tools/storage/TransactionProcessor";
import { resetReporter } from "../../../src/tools/reporters/Reporter";
import { resetTransactionRunner } from "../../../src/tools/runners/TransactionRunner";

describe("TransactionStorage", async () => {
useEnvironment("typechain-ethers");
Expand Down

0 comments on commit 0d95688

Please sign in to comment.