Withdraw failed with “transaction revert” error solidity compiler ^0.8.4

ethers.jshardhatsmart-contract-walletssolidity

I am writing my first smart contract, it's very barebone with two functions deposit and withdraw. Testing using hardhat and etherjs

contract test {
mapping(address => uint256) balances;

function deposit(uint256 amount) external payable {
    balances[msg.sender] += amount;
}

function withdraw() external {
    uint256 amount = balances[msg.sender];
    balances[msg.sender] = 0;
    (bool success, ) = msg.sender.call{value: amount}("");
    require(success, "Transfer failed.");
}

function balanceOf(address account) public view virtual returns (uint256) {
    return balances[account];
}
}

Testing it with etherjs + hardhat and transactions getting reverted, pls help, what I am missing here.

below is the test case

  it("TEST CONTRACT", async function () {

Contract= await ethers.getContractFactory("test");
testContract= await Contract.deploy();

await expect(testContract.connect(add1).deposit(100));
expect(await testContract.connect(add1).balanceOf(player1.address)).to.equal(100);
expect(await testContract.connect(add1).withdraw()); // Basically transaction revert from here
console.log('Balance ', add1.address, (await testContract.balanceOf(add1.address)).toNumber())
});

Best Answer

Well your deposit function doesnt ensure the amount added to the user's balance is the amount that is actually transferred. One could for example call this function with amount = 100 ETH and actually send only 1 wei to the contract, its balance within the contract would be set to 100 eth regardless (which would naturally lead to a revert when you call the withdraw function in that case, and imagining the contract lives on mainnet and is actually used, would lead to exploits). You'd want to use msg.value instead of amount here balances[msg.sender] += amount;

Related Topic