Search code examples
blockchainethereumsoliditysmartcontracts

Ether stuck in smart contract


I have a smart contract which can hold ETH.

receive() external payable whenNotPaused {
    if (msg.value == 0) revert AmountZero();
    emit EthDeposit(msg.sender, msg.value);
}

function withdraw(uint256 amount) external onlyOwner nonReentrant whenNotPaused {
    if (amount == 0) revert AmountZero();
    if (address(this).balance < amount) {
        revert InsufficientBalance(amount, address(this).balance);
    }
    (bool success, ) = payable(owner()).call{value: amount}("");
    if (!success) revert TransferFailed();
    emit EthWithdrawal(owner(), amount);
}

I am able to send eth to the smart contract successfully (via Metamask for example).

When I try to call withdraw, the transaction succeeds (with any amount, even if it exceeds the amount sent to it) but the amount of eth in the smart contract doesn't change.

I have checked the blockexplorer and the arguments that are passed to withdraw are correct but is says the value is 0, for example:

Value: 0 ETH

Function called: withdraw(uint256 amount = 20)

Gas Price: 1.599151031 Gwei

I am using Scaffold-Eth 2 to interact with my smart contract.


Solution

  • I wasn't specifying the correct number of decimals.

    For example if I wanted to withdraw 2 ETH:

    Correct: 2_000_000_000_000_000_000

    Incorrect: 2