Bug Found in transfer behavior on Local env testing #2961
Unanswered
BradMyrick
asked this question in
Troubleshooting
Replies: 1 comment
-
Hi @BradMyrick, trying to reproduce this with the snippets you shared will be a bit hard. Can you please create a minimal reproducible example if you have the time? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I'll do a formal bug submission if anyone else can verify this. While designing and testing a contract I found a bug when using a msg.sender.transfer(address(this).balance) in a withdraw function. The eth gets removed from the msg.sender and added to the contracts eth balance. Running the exact same code in Remix gets the desired result of removing the funds from the contract and depositing the eth to the msg.sender.
Test
it("Should withdraw the correct amount of tokens", async function () { await tacvue721a.connect(owner).saleActiveSwitch(); await tacvue721a.connect(addr1).mint(1, {value: ethers.utils.parseEther("1")}); // balance correctly updates to 1 eth below expect(await ethers.provider.getBalance(tacvue721a.address)).to.equal(ethers.utils.parseEther("1")); await tacvue721a.connect(owner).withdraw(); // balance incorrectly updates to 2 eth below expect(await ethers.provider.getBalance(tacvue721a.address)).to.equal(ethers.utils.parseEther("0")); } );
Contract Code
function withdraw() external onlyOwner nonReentrant { require(address(this).balance > 100 wei, "Not enough Avax to withdraw"); uint256 fee = address(this).balance * 2 / 100; payable(feeCollector).transfer(fee); emit FeeCollected(feeCollector, fee); payable(msg.sender).transfer(address(this).balance - fee); emit Withdrawal(msg.sender, address(this).balance - fee); }
Pictures attached with correct behavior working on Remix (top account is owner, second is fee collector) and incorrectly updating balance in Hardhat
Beta Was this translation helpful? Give feedback.
All reactions