
Subscribe to Derked
Share Dialog
Share Dialog
<100 subscribers
<100 subscribers
Last night I spent 6 hours pulling my hair out trying to figure out why after compiling my contracts with hardhat, the generated factory in typechain/factories contained no bytecode or deploy export. This post is meant to explain how I solved this issue and help other developers from losing their minds. If you are stumbling upon this post after hours of searching the internet in terms of a fix, hopefully this helps.
This would happen seemingly at random. I would code a contract, compile and everything be as expected. A few minutes later after working on a separate contract, I would compile again via hardhat (npx hardhat compile) and the factory for the first contract would contain no bytecode or ability to deploy it!
It turns out, this all had to do with naming collisions and the way typechain and hardhat typechain generates types from solidity contracts. Let’s say I have a contract which we will creatively title ContractOne.
contract ContractOne {
//beautiful contract with lots of functions and stuff
}
Now I code a separate contract which we will creatively title ContractTwo. ContractTwo uses some basic functionality of ContractOne, but not everything and does not inherit it. To make my life easier I write a basic interface for the functionality I want from ContractOne. My ContractTwo.sol file looks something like this:
interface ContractOne {
//a few function signatures
}
contract ContractTwo {
//beautiful contract with lots of functions and stuff
}
Can you spot the problem? It turns out, when compiling ContractTwo, typechain will generate types for ContractOne and ContractTwo, but since both the interface and the contract share the same name, depending on the order of your edits, you could end up with either type and all our written tests, deployments, etc. will fail.
The fix is quite simple and a best practice seen in a lot of contracts. It turns out using this best practice can help us avoid this extremely bothersome naming collision issue. We simply append an I to the beginning of the interface name.
interface IContractOne {
//a few function signatures
}
contract ContractTwo {
//beautiful contracts with lots of functions and stuff
}
Now when we run npx hardhat compile, typechain will generate types for both the interface AND the contract. Exactly what we want and the original expected behavior!
Last night I spent 6 hours pulling my hair out trying to figure out why after compiling my contracts with hardhat, the generated factory in typechain/factories contained no bytecode or deploy export. This post is meant to explain how I solved this issue and help other developers from losing their minds. If you are stumbling upon this post after hours of searching the internet in terms of a fix, hopefully this helps.
This would happen seemingly at random. I would code a contract, compile and everything be as expected. A few minutes later after working on a separate contract, I would compile again via hardhat (npx hardhat compile) and the factory for the first contract would contain no bytecode or ability to deploy it!
It turns out, this all had to do with naming collisions and the way typechain and hardhat typechain generates types from solidity contracts. Let’s say I have a contract which we will creatively title ContractOne.
contract ContractOne {
//beautiful contract with lots of functions and stuff
}
Now I code a separate contract which we will creatively title ContractTwo. ContractTwo uses some basic functionality of ContractOne, but not everything and does not inherit it. To make my life easier I write a basic interface for the functionality I want from ContractOne. My ContractTwo.sol file looks something like this:
interface ContractOne {
//a few function signatures
}
contract ContractTwo {
//beautiful contract with lots of functions and stuff
}
Can you spot the problem? It turns out, when compiling ContractTwo, typechain will generate types for ContractOne and ContractTwo, but since both the interface and the contract share the same name, depending on the order of your edits, you could end up with either type and all our written tests, deployments, etc. will fail.
The fix is quite simple and a best practice seen in a lot of contracts. It turns out using this best practice can help us avoid this extremely bothersome naming collision issue. We simply append an I to the beginning of the interface name.
interface IContractOne {
//a few function signatures
}
contract ContractTwo {
//beautiful contracts with lots of functions and stuff
}
Now when we run npx hardhat compile, typechain will generate types for both the interface AND the contract. Exactly what we want and the original expected behavior!
No activity yet