Skip to content

Commit

Permalink
Fixed a bug when the default signer was used instead of the actual si…
Browse files Browse the repository at this point in the history
…gner (#76)
  • Loading branch information
KyrylR authored Mar 22, 2024
1 parent 78f3024 commit c0d2771
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 13 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## Version 2.1.6

* Fixed a bug when the default signer was used instead of the actual signer

## Version 2.1.5

* Fixed behaviour of connect method in Ethers Adapter.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@solarity/hardhat-migrate",
"version": "2.1.5",
"version": "2.1.6",
"description": "Automatic deployment and verification of smart contracts",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
Expand Down
6 changes: 4 additions & 2 deletions src/deployer/adapters/AbstractEthersAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export abstract class AbstractEthersAdapter extends Adapter {

const oldMethod: BaseContractMethod = (contract as any)[methodName];

const newMethod = this._wrapOldMethod(contractName, methodName, methodFragments, oldMethod);
const newMethod = this._wrapOldMethod(contractName, methodName, methodFragments, oldMethod, contract.runner!);

defineProperties<any>(newMethod, {
name: oldMethod.name,
Expand Down Expand Up @@ -134,9 +134,12 @@ export abstract class AbstractEthersAdapter extends Adapter {
methodName: string,
methodFragments: FunctionFragment,
oldMethod: BaseContractMethod,
runner: ContractRunner,
): (...args: any[]) => Promise<ContractTransactionResponse> {
return async (...args: any[]): Promise<ContractTransactionResponse> => {
const tx = await oldMethod.populateTransaction(...args);
tx.from = (runner as any).address;

await fillParameters(tx);

const methodString = getMethodString(contractName, methodName, methodFragments, args);
Expand Down Expand Up @@ -199,7 +202,6 @@ export abstract class AbstractEthersAdapter extends Adapter {
} as unknown as ContractTransactionResponse;
}

// TODO: run normal migrations in tests.
private _getKeyFieldsFromTransaction(tx: ContractTransaction): KeyTransactionFields {
return {
name: this._getTransactionName(tx),
Expand Down
5 changes: 5 additions & 0 deletions src/deployer/adapters/TruffleAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ export class TruffleAdapter extends Adapter {
let contractMethod = ethersBaseContract.getFunction(methodName);

// Build transaction. Under the hood, ethers handle overrides.
// In Truffle to specify different signer the particular `{ from: 0x... }` override should be used.
const tx = await contractMethod.populateTransaction(...args);

// In case if the `from` field is not specified, it should be filled with the default signer.
tx.from = tx.from ?? (await getSignerHelper()).address;

await fillParameters(tx);

const keyFields = this._getKeyFieldsFromTransaction(tx, contractMethod, args);
Expand Down
6 changes: 5 additions & 1 deletion src/tools/reporters/Reporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ class BaseReporter {
}

public reportWarnings() {
if (this._warningsToPrint.size === 0) {
if (this.getWarningsCount() === 0) {
return;
}

Expand All @@ -276,6 +276,10 @@ class BaseReporter {
console.log("");
}

public getWarningsCount(): number {
return this._warningsToPrint.size;
}

private _printContractCollision(output: string, oldData: ContractFieldsToSave, dataToSave: ContractFieldsToSave) {
output += `\n> Contract: ${oldData.contractKeyData?.name || dataToSave.contractKeyData?.name}`;
output += `\n> Previous Collision Details: `;
Expand Down
6 changes: 0 additions & 6 deletions src/utils/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@ import { deepCopy, toJSON } from "./common";
import { MigrateError } from "../errors";
import { UNKNOWN_CONTRACT_NAME } from "../constants";

import { networkManager } from "../tools/network/NetworkManager";

import { Bytecode } from "../types/deployer";
import { KeyDeploymentFields, KeyTransactionFields } from "../types/tools";

export async function fillParameters(parameters: Overrides): Promise<Overrides> {
if (parameters.from === undefined) {
parameters.from = await (await networkManager!.provider.getSigner()).getAddress();
}

if (parameters.chainId === undefined) {
parameters.chainId = await getChainId();
}
Expand Down
4 changes: 3 additions & 1 deletion test/integration/deployer/base-contract-interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
import { Deployer } from "../../../src/deployer/Deployer";
import { Migrator } from "../../../src/migrator/Migrator";

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

Expand Down Expand Up @@ -82,10 +83,11 @@ describe("deployer", () => {

expect(receipt!.from).to.equal(signer1.address);

tx = await contract.connect(signer2).pay({ value: 110n });
tx = await contract.connect(signer2).pay({ value: 100n });
receipt = await tx.wait();

expect(receipt!.from).to.equal(signer2.address);
expect(Reporter?.getWarningsCount()).to.be.equal(0);
});
});
});

0 comments on commit c0d2771

Please sign in to comment.