0

I tried writing a contract so that we can send eth without inputting value manually in this field in remix.

enter image description here

So, we just need to click the sendEth button below ( no need to input anything) enter image description here

The following is the code:

// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.18;

contract Payment{
   address  payable receiver = payable(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2);
   uint price = 5 ether;

   function sendEth() public payable {
        (bool sent, ) = receiver.call{value: price}("");
        require(sent, "Fail to send");
    }
    
}

Since, the suggested method to send eth is by call, so I try using the call method. However, when I deploy it and click the sendEth button, it did not work, and the following is the error: enter image description here

Eth can be sent if I input the value manually, however in my case I hope that the payment can be done automatically. Could it possibly be done? Any explanation would be appreciated!

2 Answers 2

2

Payment contract does not have 5 ether balance that is the reason why this is reverted. either have a receive(){} fallback to deposit eth to your contract, or send 5 ethers when sendEth() is called.

0
0

This won't work the way you want it to.

When you call the sendEth function, it basically sends the ether from the payment contract to the receiver address, but for that, it's pretty obvious that the payment contract needs to have that much of an amount. So it takes you back to the original problem of manually sending those 5 ETH while calling the function.

You can avoid this by sending a big chunk of ETH one time and then sending 5 ETH every time you call the sendEth function, for example, you sent 100 ETH once and then you call the sendEth function 20 times. For this you will need to include a receive or fallback function in your contract.

As long as you are using remix, you'll need to manually enter the ETH amount, either one time for big amount or multiple times with a small amount.

2
  • I see. So, in the case of sending eth from the contract, the contract needs to have some balance. What about sending directly from an account to another account in remix, is this possible?
    – tony
    Oct 31, 2023 at 9:51
  • Nope, it's not. This would be possible in a wallet like metamas etc. or by writing a simple JS/TS script. Remix doesn't have an interface to work with wallets in general, it's made to work with contracts only. Oct 31, 2023 at 12:31

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.