wighawag / hardhat-deploy-ethers

MIT License
69 stars 25 forks source link

Allow passing in a typechain type to getContract functions #4

Closed TomAFrench closed 3 years ago

TomAFrench commented 3 years ago

I find that quite often when I try to load a contract with the correct typechain type I have to do a cast which looks something like

const collateral = (await ethers.getContract("Collateral")) as Cash;

This seems quite ugly to me and would prefer this function being a generic where we can pass in any subtype of ethers.Contract and get that subtype out. This would change the above to something like

const collateral = await ethers.getContract<Cash>("Collateral");

This is much more readable in my opinion compared to having to await then cast. Of course if no type is provided then we get out an ethers.Contract type so it's backwards compatible for those who don't use this feature.

We could do something similar to the getContractFactory function but I wanted to gauge your opinion first.

wighawag commented 3 years ago

Thanks @TomAFrench agree it is better this way.

I think it make sense to have it for getContractFactory.

I was also thinking to add a deploy function that behave like hardhat-deploy deploy function but return an instantiated ethers contract and we could also use generic type parameter there

TomAFrench commented 3 years ago

Cool, just chucked in a new PR for the factories.

re deploy function: I'm not entirely sure what behaviour you're wanting to bring over. I've been seeing a fair number of projects implementing helper functions similar to https://github.com/balancer-labs/balancer-core-v2/blob/master/lib/helpers/deploy.ts

Do you mean this or more along the lines of stuff like deterministic deployments, etc?

wighawag commented 3 years ago

Basically have the equivalent of deployments.deploy (which will save the deployment on disk) but that returns an instantiated ethers contract. This way in your deploy script you can do:

const MyContract = await ethers.deployContract<MyContract>("MyContract", {from: deployer, args: [...]});
const tx = await MyContract.test();
await tx.wait();