0

I am trying to code in solidity a pay fee function like so:

contract GetFee {

    address payable public feeCollector = 0x59c0f4Bea65c99C281A0107C86beE309b20b3B49;

    function payFee() public payable {
        feeCollector.transfer(100000000000000000);
    }
}

However when I click it in solidity I get the following error:

Note: The called function should be payable if you send value and the value you send should be less than your current balance.

If I do however insert any amount in the deploy & run, like 1 ETH for example, it works, but I don't want to do that.

I want a function that when clicked it simply send that specific amount to that specific address, without the user having to insert anything else.

How to do that?

1
  • The contract must have the funds to do the transfer. The reason why it works with 1 ETH is beacese that will give the contract enough funds to do so. So the problem you are facing is not a problem just the code following the logic it has
    – Julissa DC
    Dec 29, 2021 at 12:46

2 Answers 2

1

Its easy. Instead of hard coding the amount, you just put a variable in it. Take a look at code below which takes an address and amount from user, and then sends the amount to the given address:

function payFromContract (address payable _recipient, uint _amount) external {
    _recipient.transfer(_amount);
}
0

It can not be done, as any funds transfer must be initiated by the user. There is a workaround in web3 and js.

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.