Let the user pay a fee to use a function

Hi!
I don’t understand how to use “payable” functions.
How can I ask that the value sent is equals to an amount I choose?
How can the user know how much to send to this function?
Thank you.

Hi,

There are several ways to do it depending on your dapp design or how you choose to interact with the contract, one way could be this:

contract A {
 uint fee;
 constructor(uint _fee) public {
  fee = _fee;
 }
 function payableFunctionName() payable external {
  require(msg.value >= fee);
  //...Logic
 }
}

How the user would know how much it needs to tranfers you could expose a function to get the fee, or if you are using a dapp tell the user over there reading the fee variable of the contract:

function howMuch() external returns(uint) {
 return fee;
}
1 Like

Thank you really much!