3

I am trying to understand how to set a dynamic fee in Solidity. I have the following code

contract Test{
uint fee;

function Test(uint _feeNumerator, uint _feeDenominator){
    fee = _feeNumerator/_feeDenominator;
}

function updateFee(uint _numerator, uint _denominator){
    fee = _numerator / _denominator;
}

function testPayable(string _doesNothing) payable {
    require(msg.value == fee * (10**18));

}

function getFee() returns (uint fee){
    return fee;
}

} Which generates a pre-compiled error stating "ParseError: Expected token Comma got 'SubEther'.

I had previously tried the same functionality, but instead using (_numerator**18)/10. When I previously ran that code it worked, but it is now giving me an "Error: gas required exceeds allowance or always failing transaction" whenever I attempt to run testPayable.

My two questions are, is there a limit to how much I can charge for a user to execute a function? And how would I properly create a dynamic fee similar to what's attempted in the first part of the question?

  • The issue from this version of the question was due to me not entering the proper amount of Ether in the "Value" field in the top section under the "Run" tab. The code as pictured should work. – Min Mar 12 '18 at 0:06
0

Q1: Is there a limit to how much I can charge for a user to execute a function?

Answer: No there is no limit (for example you can set it to be 0.00001 ether or 10000 ether)

Q2: How would I properly create a dynamic fee similar to what's attempted in the first part of the question?

To resolve the error you have to use:

    //Just multiply by 10^18 instead
    // Because, units like ether can only be used with integer literals at compile time. 
    require(msg.value == fee * 10**18);

By the way, generally speaking, to set a dynamic fee you have to first define your equation. It will depend on your choice based on your business needs. So, you may use in your equation some of the global variables like block.number or now (check http://solidity.readthedocs.io/en/develop/units-and-global-variables.html#block-and-transaction-properties for reference).

However, to set an efficient algorithm for dynamically changed fees, you may need some input from outside solidity (outside the Ethereum blockchain scope). In this case you may use the concept Oracle (this is a concept has nothing to do with the well known Oracle database company). If you need an Oracle, I recommend this article as a start: https://kndrck.co/posts/ethereum_oracles_a_simple_guide/

  • I have tried this method. This is what caused my original error... transact to Test.testPayable errored: Error: gas required exceeds allowance or always failing transaction – Min Mar 11 '18 at 0:06
  • I have also updated the question to reflect what's currently in my contract. – Min Mar 11 '18 at 0:07
  • How do you call the function? Do you use remix, Truffle, geth command line or what exactly? How do you specify the ammount of ether?... – Muhammad Altabba Mar 11 '18 at 0:11
  • I am using remix. If I set both the numerator and denominator to 1, then when calling test payable, shouldn't I be charging a fee of 1 ether? Instead, I am getting the error transact to Test.testPayable errored: Error: gas required exceeds allowance or always failing transaction – Min Mar 11 '18 at 0:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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