I am currently in the process of creating tests for a smart contract that I have developed. Below is a simplified version of the smart contract:
Please Note: I have hard-coded the value to ensure accuracy regarding the amount leaving the contract.
function withdraw(uint256 tokenId, address to, address owner) external returns (uint256 amount) {
amount = _positionManager.withdraw(tokenId, owner);
_wrapper.withdraw(amount);
//(bool success, ) = to.call{value: amount }("");
(bool success, ) = to.call{value: 100000000000000000 }("");
// failed transfer...
require(success, "3FM:FT");
emit Withdraw(to, amount);
}
The test code I have written looks like this:
const provider = ethers.getDefaultProvider();
const oldBalance = await provider.getBalance(owner.address);
console.log('Old Balance:' + oldBalance);
await fundManager.withdraw(defaultTokenId, owner.address, owner.address);
const newBalance = await provider.getBalance(owner.address);
const expectedAmount = oldBalance.add(BigNumber.from("100000000000000000"));
console.log('New Balance:' + newBalance);
expect(newBalance).to.equal(expectedAmount);
After running the test, I noticed that the console output indicates the new balance is the same as the old balance. However, my smart contract does not throw an error.
I suspect that this issue may be related to how hardhat handles values. How can I confirm that the balance is being sent correctly?
Additional Information:
I have confirmed that my hardhat configuration does not fail silently, by deliberately setting the success variable to false.
Below are the logs generated from running the test:
Old Balance:276014338387200 New Balance:276014338387200
- I suspect that this might be a hardhat-specific issue, as regardless of the initial deposit amount made from the same account, the balance remains unchanged.