How to interact with interfaces through HardHat?

Viewed 855

how can I interact with interfaces through hardhat.

For example in brownie you can just call function from an interface like this:

def main():
    weth = interface.IWeth("weth_address")
    tx = weth.deposit({"from": account, "value": "value"})

and that's it.

Is there a way to do the same in hardhat? I've been siting on this problem for couple of hours and for the life of me I can't figure out how to do this.

If it's not possible how do I get weth through solidity.

2 Answers

Yes, you can do it with ethers.getContractAt.

import { ethers } from "hardhat";
...
// Assume `ISetup` is an interface of the contract.
const setup = await ethers.getContractAt("ISetup", contractAddress);

In hardhat, you can achieve it using the attachment.

  1. Go to hardhat console: npx hardhat console
  2. Get the contract factory: factory = await ethers.getContractFactory('YourContractName')
  3. Attach to address weth = await factory.attach('weth_address')
  4. Do your function call e.g:
signer = await ethers.getSigner();
await signer.sendTransaction({ to: tokenAddress, value: ethers.utils.parseEther("5.0") });
Related