I want to deploy a FlashLoan smart contract using hardhat, and deploy the contract on hardhat network that fork by mainnet. But when I run the deploy script, keep showing Error: cannot estimate gas; transaction may fail or may require manual gas limit. I find some solution on network like adding allowUnlimitedContractSize: true in hardhat config but this is not working for me. Does anyone know how can I fix this error?
The flash loan smart contract is from this YouTube tutorial: https://www.youtube.com/watch?v=PtMs8FZJhkU&t=919s&ab_channel=BlockExplorer
The following code is my hardhat.config.js, deploy.js and the smart contract.
// hardhat.config.js
require('@nomiclabs/hardhat-waffle');
require('dotenv').config()
/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
defaultNetwork: "hardhat",
networks: {
hardhat: {
gas: 12000000,
allowUnlimitedContractSize: true,
chainId: 1,
mining: {
auto: false,
interval: 30000,
},
forking: {
url: process.env.PRC_URL_MAIN,
blockNumber: 15870000,
},
},
goerli: {
url: process.env.RPC_URL,
},
},
solidity: {
version: "0.8.10",
settings: {
optimizer: {
enabled: true,
runs: 200
}
}
},
mocha: {
timeout: 600000
},
};
// deploy.js
const { network, ethers } = require("hardhat");
const { provider} = require("./test/src/constants.js");
const deploy = async () => {
const accounts = await ethers.getSigners();
const Attacker = accounts[0].address;
console.log("Deploying contracts with the account:", Attacker);
const FlashLoan = await ethers.getContractFactory("FlashLoan");
const flashLoan = await FlashLoan.deploy("0xa97684ead0e402dC232d5A977953DF7ECBaB3CDb");
await flashLoan.deployed();
}
deploy()
// contracts/FlashLoan.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.0;
import "../node_modules/@aave/core-v3/contracts/flashloan/base/FlashLoanSimpleReceiverBase.sol";
import "../node_modules/@aave/core-v3/contracts/interfaces/IPoolAddressesProvider.sol";
import "./IERC20.sol";
contract FlashLoan is FlashLoanSimpleReceiverBase {
address payable owner;
constructor(address _addressProvider)
FlashLoanSimpleReceiverBase(IPoolAddressesProvider(_addressProvider))
{
owner = payable(msg.sender);
}
/**
This function is called after your contract has received the flash loaned amount
*/
function executeOperation(
address asset,
uint256 amount,
uint256 premium,
address initiator,
bytes calldata params
) external override returns (bool) {
// Approve the Pool contract allowance to *pull* the owed amount
uint256 amountOwed = amount + premium;
IERC20(asset).approve(address(POOL), amountOwed);
return true;
}
function requestFlashLoan(address _token, uint256 _amount) public {
address receiverAddress = address(this);
address asset = _token;
uint256 amount = _amount;
bytes memory params = "";
uint16 referralCode = 0;
POOL.flashLoanSimple(
receiverAddress,
asset,
amount,
params,
referralCode
);
}
modifier onlyOwner() {
require(
msg.sender == owner,
"Only the contract owner can call this function"
);
_;
}
receive() external payable {}
}
the error code is below
Deploying contracts with the account: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266
Account balance: 10000000000000000000000
/Users/Demo/node_modules/@ethersproject/logger/src.ts/index.ts:269
const error: any = new Error(message);
^
Error: cannot estimate gas; transaction may fail or may require manual gas limit [ See: https://links.ethers.org/v5-errors-UNPREDICTABLE_GAS_LIMIT ] (reason="Transaction reverted without a reason string", method="estimateGas", transaction={"from":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266","data":"0x60c060405234801561001057600080fd5b506040516105a33803806105a383398101604081905261002f916100d1565b80806001600160a01b03166080816001600160a01b031681525050806001600160a01b031663026b1d5f6040518163ffffffff1660e01b8152600401602060405180830381865afa158015610088573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100ac91906100d1565b6001600160a01b031660a0525050600080546001600160a01b03191633179055610101565b6000602082840312156100e357600080fd5b81516001600160a01b03811681146100fa57600080fd5b9392505050565b60805160a0516104716101326000396000818160e20152818161014a015261020901526000606101526104716000f3fe6080604052600436106100435760003560e01c80630542975c1461004f5780631b11d0ff146100a05780637535d246146100d0578063d54ba9541461010457600080fd5b3661004a57005b600080fd5b34801561005b57600080fd5b506100837f000000000000000000000000000000000000000000000000000000000000000081565b6040516001600160a01b0390911681526020015b60405180910390f35b3480156100ac57600080fd5b506100c06100bb36600461029d565b610126565b6040519015158152602001610097565b3480156100dc57600080fd5b506100837f000000000000000000000000000000000000000000000000000000000000000081565b34801561011057600080fd5b5061012461011f366004610343565b6101db565b005b600080610133868861036d565b60405163095ea7b360e01b81526001600160a01b037f000000000000000000000000000000000000000000000000000000000000000081166004830152602482018390529192509089169063095ea7b3906044016020604051808303816000875af11580156101a6573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906101ca9190610393565b5060019150505b9695505050505050565b60408051602081018252600080825291516310ac2ddf60e21b81523092859285929091906001600160a01b037f000000000000000000000000000000000000000000000000000000000000000016906342b0b77c9061024690889088908890889088906004016103bc565b600060405180830381600087803b15801561026057600080fd5b505af1158015610274573d6000803e3d6000fd5b5050505050505050505050565b80356001600160a01b038116811461029857600080fd5b919050565b60008060008060008060a087890312156102b657600080fd5b6102bf87610281565b955060208701359450604087013593506102db60608801610281565b9250608087013567ffffffffffffffff808211156102f857600080fd5b818901915089601f83011261030c57600080fd5b81358181111561031b57600080fd5b8a602082850101111561032d57600080fd5b6020830194508093505050509295509295509295565b6000806040838503121561035657600080fd5b61035f83610281565b946020939093013593505050565b6000821982111561038e57634e487b7160e01b600052601160045260246000fd5b500190565b6000602082840312156103a557600080fd5b815180151581146103b557600080fd5b9392505050565b600060018060a01b03808816835260208188168185015286604085015260a06060850152855191508160a085015260005b828110156104095786810182015185820160c0015281016103ed565b8281111561041b57600060c084870101525b5050601f01601f1916820160c00190506101d1608083018461ffff16905256fea2646970667358221220c102ef19c5d6a4cf011f9447ca79903380d6c1eb7d605ad7b618de67b8bc19f064736f6c634300080a0033000000000000000000000000a97684ead0e402dc232d5a977953df7ecbab3cdb","accessList":null}, error={"stackTrace":[{"type":17,"message":{"value":{"type":"Buffer","data":[]}},"isInvalidOpcodeError":false}],"data":"0x"}, code=UNPREDICTABLE_GAS_LIMIT, version=providers/5.7.2)