2

if user sent the 1.5 ETH then accepted the 1 ETH, but 0.5 ETH should be refunded to sender. I tried like msg.sender.transfer(0.5)

and tried like msg.sender.transfer(500000000000000000)

but nothing happened. I am not sure about that msg.sender.transfer is a right function.

is it possible do such way in Contracts?

1
  • which version of solidity are you using?
    – wandos
    Jul 14, 2019 at 10:40

4 Answers 4

1

Change 0.5 to 0.5 ether or to 500000000000000000.

3
  • tried msg.sender.transfer(500000000000000000) but its not refund the ether.
    – aumkii
    Nov 21, 2018 at 14:05
  • @aumkii did you check the actual balances? The transaction will still show as sending 1.5 eth, but the balance will only be 1 eth less after the transaction
    – natewelch_
    Nov 21, 2018 at 14:30
  • Thank you for your reply. sending 1.5, so deducted 1.5 ETH from my account and credited to another 1.5 ETH account. owner.transfer(1.5 ether) ; its working fine. but msg.sender.transfer(500000000000000000) is not working.. its not credit 0.5 ether in my account.
    – aumkii
    Nov 22, 2018 at 7:33
1

Just to add a bit more complete answer: Solidity (and Ethereum) handle only full integers and no decimals in Ether values. All transactions happen in Wei which is the smallest unit of Ether (the cryptocurrency).

One Ether equals to 1000000000000000000 Weis - that is 10^18 Weis.

Therefore, whenever you want to transfer value withing the Ethereum blockchain, you use Weis as the amount and calculate the correct amount. So, in your case, 0.5 Ethers = 500000000000000000 Weis.

0

The transfer function takes as parameter to transfer in wei, so in order to send 0.5 ether you will have to transfer 500000000000000000 wei. And the parameter has to be an interger.

0

msg.sender.transfer(0.5 ether); may not be worked in modern versions of solidity.

transfer function can only be called in payable addresses. So following code will work. payable(address(msg.sender)).transfer(0.5 ether);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.