0

I am trying to call a function with one parameter I have used these methods all didn't work: //z=addmul(total); //this.addmul(total); This is the function created:

function addmul(uint256 a, uint256 b) internal pure returns (uint256 c  ) {


        require(c >= a && c>=b, "SafeMath: overflow encountered");

        return c;

    }

This is how it was called, given unexpected result:

//multiplication operation
    function multiplication(uint256 x, uint y) public  {
    total=x+y;
    total.addmult;  // this one runs, but it does not give desired result
    //z=addmul(total);
    //this.addmul(total);
}

I want the addmull function to effect the computed value of total variable. The main essence of the function is to protect an overflow when x and y are added or multiplied together.

2
  • Solidity is a pretty challenging language to start learning programming, I'd suggest to first try learning javascript which solidity is close in syntaxis.
    – Ismael
    Jul 7 '19 at 17:45
  • Thank you so much.
    – Mansur
    Jul 7 '19 at 21:40
0

Your addmul function has two issues.

  1. it does not use the second argument (b)
  2. it requires c to be bigger than a, but c being a return value is initialized as 0 at function start, so your code makes this function systematically revert the transaction unless a=0.

Now your question has an issue as well: Your intention is not clear at all, so it is hard to help you.

I'll try and answer the explicit question in your post though: Regarding calling a function with fewer parameters than it expects, you just can't; or rather, if you do the EVM will not find the correct signature and execute the fallback function instead.

What you can do, is feed it dummy values for the arguments you don't care about (like in this case, since b is ignored in the function anyway).

4
  • I have made some changes to the function by adding b argument too. I have also added one function calls that run correctly but does not give desired result. In the end I have added some more points.
    – Mansur
    Jul 7 '19 at 17:19
  • What is the desired result ?
    – blackscale
    Jul 7 '19 at 19:13
  • I want to the function to test that a and b should be always less than c for addition and multiplication.
    – Mansur
    Jul 7 '19 at 21:30
  • c is zero in the code you've shown, it seems pointless. Maybe if you formulate your objective with real words instead of faceless variables (a, b, c, x, y), it would help you write code that actually achieves it.
    – blackscale
    Jul 8 '19 at 18:42
0

I have to agree with Ismael, you seem to lack some basic skills that a good javascript tutorial should fill. And anyway js is a crucial tool to work with smart contracts, starting with testing.

Nevertheless, it just dawned on me that maybe you are asking about how to use the pattern that OpenZeppelin applies on its SafeMath library:

import "./libs/SafeMath.sol";

contract test {
  using SafeMath for uint256;

  uint256 a = 4;
  uint256 b = a.add(a); // equivalent to SafeMath.add(a, a)
}

Notice SafeMath is a library, not a contract. cf. OpenZeppelin code

You can call functions that have two parameters on variables that have the same type as the first, this means you only need to specify the second parameter now (cf code example above).

4
  • It is of course from the SafeMath. I am trying to merge the addition and multiplication functions to be one one function aim at lowering the gas cost.
    – Mansur
    Jul 7 '19 at 21:32
  • Ok. I get it. You will not achieve that by grouping 2 features into a single function. Gas usage optimization usually comes from building more specific functions (i.e. with less moving parts). The typical process usually involves splitting a function into 2, not the contrary. Moreover, trying to find a single rule to detect overflow on both multiplication and addition risks being only partially efficient for at least one of the operations, putting your code at risk. And using complex logic will make it spend even more gas than standard SafeMath operations.
    – blackscale
    Jul 8 '19 at 18:50
  • Thank you so much for this great useful insights.
    – Mansur
    Jul 8 '19 at 20:45
  • I will definitely make it specific for each function (add and mul separately). My challenge is the function call I did, no syntax error, but it is not given what I intend. You now the OpenZeppelin has the operations inside the library. I have taken it out of the library to the contract where the library is called and then focus on checking the resulting output( c) for overflow not the inputs (a and b). See the code: total=x*y; total.mul;
    – Mansur
    Jul 8 '19 at 21:08

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.