Call Us 786-865-0767

Here is an article based on the information provided:

Ethereum Hardhat does not recognize assigned errors from legacy contracts

Ethereum: Hardhat doesn't recognize custom errors from inherited Contracts

When developing and testing smart contracts on the Ethereum network using Hardhat, it is not uncommon to encounter issues with handling custom errors. In this case, we will examine a common issue that occurs when trying to test the TimelockController contract from OpenZeppelin.

Problem: Assigned errors in legacy contracts

To use custom errors in contracts, you need to import them from an external module (in this case, Address.sol) and extend their functionality. However, Hardhat has limitations when it comes to recognizing these custom errors.

Specifically, the TimelockController contract imports FailedCall, which is defined in the Address.sol module. Unfortunately, Hardhat does not recognize these custom errors.

Sprendimas: sukurkite atskirą klaidų modulį

Norėdami išspręsti šią problemą, galime sukurti atskirą modulį, kuris apibrėžia pasirinktinę klaidą, pavadintą „FailedCall“. Tada importuosime ir pratęsime ją pagal „TimelockController“ sutartį.

Štai pavyzdys, kaip galite tai padaryti:

// failedcall.sol (naujas modulis)

klaida FailedCall {

msg = "Nepavyko iškviesti funkcijos";

}

Tada „TimelockController“ sutartyje:

// timelockcontroller.sol (atnaujinta sutartis)

import { Address } from "@openzeppelin/contracts/token/ERC20/SafeToken.sol";

contract TimelockController is SafeToken {

// esamas kodas...

function timelock() public onlyOwner {

pabandyk {

// bandoma iškviesti laiko užrakto funkciją

_timelock();

} catch (error: FailedCall) {

// handle custom error

console.log("Failed to call timelock: ", error.msg);

}

}

function _timelock() public onlyOwner {

// implementing timelock logic...

}

}

Testing with Hardhat

Now that we have defined our failedcall.sol module, we can import it and test our custom TimelockController contract error using Hardhat.

Here is an example of how you can do this:

// Hardhat configuration

const hre = require("hardhat");

const TimelockContract = await hre.getContractFactory("TimelockController");

const timelockContract = await TimelockContract.deploy();

async function main() {

// deploy the contract

const timelockAddress = await timelockContract.address;

try {

// call the timelock function to check for a custom error

await timelockContract.timelock();

} catch (error: FailedCall) {

console.log("Failed to call timelock: ", error.msg);

}

}

By creating a separate error module and extending it with the TimelockController contract, we were able to use custom errors in our test. This should help you resolve the issue where Hardhat is not recognizing these errors.

Hope this helps! Let me know if you have any questions or need further assistance.

Bitcoin Message With Using