wighawag / hardhat-deploy

hardhat deployment plugin
MIT License
1.19k stars 293 forks source link

Cannot deploy contract using a namedAccount (impersonating) #152

Open 0xdavinchee opened 3 years ago

0xdavinchee commented 3 years ago

I am attempting to impersonate an account via the namedAccounts property of the HardhatUserConfig object like so:

  namedAccounts: {
    deployer: 0,
    whale: WHALE_TEST_ADDRESS
  },

However, I end up getting an error when I try deploying like so:

  await deploy("Contract", {
    from: whale,
    args: [ACTUAL_ADDRESS],
    log: true,
  });

The error is this jumble of chaos:

 ERROR processing /Users/0xsquanch/Documents/solidity/farmer/deploy/001_deploy_sushi_farmer.ts:
Error: Unknown Signer for account: 0x84D34f4f83a87596Cd3FB6887cFf8F17Bf5A7B83 Trying to execute the following::
 {
  "from": "0x84D34f4f83a87596Cd3FB6887cFf8F17Bf5A7B83",
  "gasLimit": 1980989,
  "nonce": 551,
  "data": "***bunch of letters and numbers***"
}
    at _deploy (/Users/0xsquanch/Documents/solidity/farmer/node_modules/hardhat-deploy/src/helpers.ts:547:13)
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at _deployOne (/Users/0xsquanch/Documents/solidity/farmer/node_modules/hardhat-deploy/src/helpers.ts:768:16)
    at Object.func (/Users/0xsquanch/Documents/solidity/farmer/deploy/001_deploy_sushi_farmer.ts:9:3)
    at DeploymentsManager.executeDeployScripts (/Users/0xsquanch/Documents/solidity/farmer/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1049:22)
    at DeploymentsManager.runDeploy (/Users/0xsquanch/Documents/solidity/farmer/node_modules/hardhat-deploy/src/DeploymentsManager.ts:885:5)
    at Object.fixture (/Users/0xsquanch/Documents/solidity/farmer/node_modules/hardhat-deploy/src/DeploymentsManager.ts:307:9)
    at setup (/Users/0xsquanch/Documents/solidity/farmer/test/farmer-tests.test.ts:55:3)
    at Context.<anonymous> (/Users/0xsquanch/Documents/solidity/farmer/test/farmer-tests.test.ts:101:7)
  Error: ERROR processing /Users/0xsquanch/Documents/solidity/farmer/deploy/001_deploy_sushi_farmer.ts:
  Error: Unknown Signer for account: 0x84D34f4f83a87596Cd3FB6887cFf8F17Bf5A7B83 Trying to execute the following::
   {
    "from": "0x84D34f4f83a87596Cd3FB6887cFf8F17Bf5A7B83",
    "gasLimit": 1980989,
    "nonce": 551,
    "data": "***bunch of letters and numbers***"
  }
      at _deploy (node_modules/hardhat-deploy/src/helpers.ts:547:13)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)
      at _deployOne (node_modules/hardhat-deploy/src/helpers.ts:768:16)
      at Object.func (deploy/001_deploy_sushi_farmer.ts:9:3)
      at DeploymentsManager.executeDeployScripts (node_modules/hardhat-deploy/src/DeploymentsManager.ts:1049:22)
      at DeploymentsManager.runDeploy (node_modules/hardhat-deploy/src/DeploymentsManager.ts:885:5)
      at Object.fixture (node_modules/hardhat-deploy/src/DeploymentsManager.ts:307:9)
      at setup (test/farmer-tests.test.ts:55:3)
      at Context.<anonymous> (test/farmer-tests.test.ts:101:7)
      at DeploymentsManager.executeDeployScripts (node_modules/hardhat-deploy/src/DeploymentsManager.ts:1052:19)
      at processTicksAndRejections (internal/process/task_queues.js:93:5)
      at DeploymentsManager.runDeploy (node_modules/hardhat-deploy/src/DeploymentsManager.ts:885:5)
      at Object.fixture (node_modules/hardhat-deploy/src/DeploymentsManager.ts:307:9)
      at setup (test/farmer-tests.test.ts:55:3)
      at Context.<anonymous> (test/farmer-tests.test.ts:101:7)
0xdavinchee commented 3 years ago

I have been playing around w/ the code and I wanted to add a little more detail to this bug, I am actually able to run the deployment script by passing in a named account address when running npx hardhat node, but this doesn't work when trying to run tests, maybe it is something to do with fixtures? @wighawag

xenoliss commented 3 years ago

Hello there, facing the same problem, I got an Error: Unknown Signer for account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 when deploying from tests script through a fixture.

xenoliss commented 3 years ago

Turned out I found my problem quickly after posting maybe you are in the same situation as I am.

While the docs says await deployments.fixture(); // ensure you start from a fresh deployments it did not seem to work in my case (got the unknown signer error). When starting from a "fresh" fixture using await deployments.fixture(""); // notice the quotes worked for me. Hope it helps.

EDIT: In the end even though using await deployments.fixture(""); made the problem disappears I think the base problem came from the fact I was calling the ficture in a describe block instead of an it one.

wighawag commented 3 years ago

Hi all, thanks for the reports. does anyone have a reproduction repo with the issue ?

yiteng-guo commented 2 years ago

Hi, I'd like to add more details to this issue. The minimum reproduction is as simple as

await hre.network.provider.request({
    method: "hardhat_impersonateAccount",
    params: [address],
});
const signer = await hre.ethers.getSigner(address);
await hre.deployments.deploy(contractName, {
    args: deployArgs,
    from: signer.address,
    log: true
})

I think the root cause is that hardhat-deploy saves all availableAccounts during the initialization (https://github.com/wighawag/hardhat-deploy/blob/master/src/helpers.ts#L290) while impersonated accounts are added after the initialization. Therefore, we set the unknown = true here https://github.com/wighawag/hardhat-deploy/blob/master/src/helpers.ts#L1693 and an exception is thrown here https://github.com/wighawag/hardhat-deploy/blob/master/src/helpers.ts#L595.

I don't understand why unknown is checked and an exception is thrown if unknown is True. Can't we defer that to signer.sendTransaction and re-raise UnknownSignerError if ethers.js throws us an error? We don't need to keep track of availableAccounts if we do it in this way.

Magicking commented 2 years ago

Encountered the same error as describe above, I added a simple availableAccounts["ADDRESS".toLowerCase()] = true; after the mentioned line above and it fixed the issue.

This issue might need to be re-opened.

wighawag commented 2 years ago

Hey, sorry hard to find time these days, @yiteng-guo the links you provided are unfortunately not permalink so can't use them as the code changed since then @Magicking similarly not sure what line you are referring to

jainkunal commented 2 years ago

Permalinks for what @yiteng-guo was mentioning.

availableAccounts during initialization: https://github.com/wighawag/hardhat-deploy/blob/f85305ba6ffef2750d64ade7509e7fd65a05bc27/src/helpers.ts#L287

unknown = true set here if not present in availableAccounts: https://github.com/wighawag/hardhat-deploy/blob/f85305ba6ffef2750d64ade7509e7fd65a05bc27/src/helpers.ts#L1769

Exception is thrown here: https://github.com/wighawag/hardhat-deploy/blob/f85305ba6ffef2750d64ade7509e7fd65a05bc27/src/helpers.ts#L442

The problem is that even when ethSigners has a valid value (because of the impersonation), the exception gets thrown.

Also now sure how @Magicking was able to add to availableAccounts when importing hardhat-deploy package.

marcelomorgado commented 1 year ago

This may help you guys, I was trying to run deployments against forked mainnet node (i.e. localhost network) and was able to make it work by adding autoImpersonate: true option on the localhost network section:

...
networks: {
  localhost: {
    ...
    autoImpersonate: true,
    ...
  }
}
...

Have had to call hardhat_impersonateAccount somewhere before running deployment scripts

AlissonRS commented 8 months ago

I had this issue while trying to use extendProvider to add custom logic to request() method.

I fixed this by adding accounts: "remote" on the hardhat.config.ts as per below:

goerli: {
  accounts: "remote",
  chainId: 5,
  url: `https://rpc.ankr.com/eth_goerli`,
},