defi-wonderland / smock

The Solidity mocking library
MIT License
319 stars 40 forks source link

Add funds to a wallet? #80

Closed JamesEarle closed 2 years ago

JamesEarle commented 2 years ago

I'm writing a test for a contract with hardhat that is forking mainnet. I'm deploying that contract and then creating a fake of it with smock.

const signers = await ethers.getSigners();
const myContractFactory = new MyContract__factory(signers[0]);
const contract: MyContract = await MyContractFactory.deploy();

mockContract = await smock.fake(contract);

Then I'm using this mock to send a function.

await mockContract.executeFunction(...)

But this doesn't work because the function is not signed, so we also have to a signer for that transaction like the smock wallet, turning the code into

await mockContract.connect(mockContract.wallet).executeFunction(...)

But now this is creating an issue because that wallet has no funds.

InvalidInputError: sender doesn't have enough funds to send tx. The max upfront cost is: 68604170304050 and the sender's account only has: 0

Am I able to add funds to this account? And if so, how? I don't see examples on doing this in your docs.

I am using Windows 19043.1237 with smock version 2.0.6

0xGorilla commented 2 years ago

Why would you like to call a function of a fake on your own? Fakes don't store any logic inside the functions, so it would just return the default value (false in case of a bool for example). Maybe you are trying to use mocks?

Extract from the docs:

Mocks are extensions to smart contracts that have all of the functionality of a fake with some extra goodies. Behind every mock is a real smart contract (with actual Solidity code!) of your choosing. You can modify the behavior of functions like a fake, or you can leave the functions alone and calls will pass through to your actual contract code. And, with a little bit of smock magic, you can even modify the value of variables within your contract! 🥳

Anyhow, as you saw, you can access the wallet of a fake or a mock by doing myFakeOrMock.wallet. If you need funds there, you could either transfer some ETH from a default hardhat signer by doing something like:

...
import { ethers } from 'hardhat';
import { utils } from 'ethers';

const [whale]  = await ethers.getSigners();
await whale.sendTransaction({ value: utils.parseUnits('10'), to: myFakeOrMock.address });

or by using hardhat setBalance:

await network.provider.send('hardhat_setBalance', [
  myFakeOrMock.address,
  '0x1000',
]);

Does this help? If so, feel free to close the issue

JamesEarle commented 2 years ago

A mistake on my part using the smock incorrectly. Thanks for your help