in

Withdraw from contract exact amount of ether

I know, it’s a noob question, but I’ve failed to google solution. Suppose I have a contract:

contract mycontract {
constructor() payable {}

function withdraw(uint256 amount) public {
// either
//payable(address(msg.sender)).transfer(amount);
// or
(bool success, ) = msg.sender.call{value: amount}(“”);
require(success, “Transfer failed.”);
}
}

When I call `withdraw(100000000000000)`, I got my ether balance increased by 99999999973576 or so wei instead of 100000000000000. Is it because of transaction cost? How can I fix this function to receive exactly 100000000000000 wei?

What do you think?

10 Points
Upvote Downvote

Leave a Reply

Your email address will not be published. Required fields are marked *

GIPHY App Key not set. Please check settings

2 Comments

  1. From what I remember, I think there is a fixed limit of 2300 gas for .send() and .transfer() calls. .call() has a settable limit. So that will get deducted from the amount you are sending.

Speculation: How will PoS affect ETH price action?

Discovery Inc. (DISCK) is an upward trend