1

A very noob question, I guess, but I can't wrap my mind around it. What are the ways of calling a private smart contract function? It's not possible from Remix. For example, there is a private function that gets two uint256 values and return a third uint256, which is the sum of the two, and the function is private.

Could you please explain the possible options of calling it?

  • Private methods called only by public methods available in the contract. – Kaviyarasu Arasu Feb 23 '18 at 4:24
1

That's the point of private functions! You can't call them from an external source, they're meant to be called by functions within the contract.

You can read more about permissions here http://solidity.readthedocs.io/en/develop/contracts.html

  • Thanks for the answer. But what's the point then? If I have a private function that only I want to call, and then attach a public function then anybody is able to call it if I deploy my contract on Etherscan, for example? – Ruham Feb 23 '18 at 4:52
  • Say you have a game that takes in entrants addresses and then deletes them from the array after the game is over and you're using a function to delete the players. You will want to be able to call this function only when the game is over, if someone was able to call the function externally when the game was still in progress(deleting the people that have entered) this would cause problems for you. The use is private is meant for applications similar to this. – JAG Feb 23 '18 at 4:58
  • I both understand and don't understand it. For example, I need to manually call a function every day, but only I should have access to that function. If the code is deployed to Etherscan and is available to anyone, since the function is private it's not visible there, but if I make that function public to call it from Etherscan, then everybody will be able to do that. If I leave the function private, then how do I call it? From where? TestRPC? Making a modifier with only my address? – Ruham Feb 23 '18 at 5:05
  • You can make create modifier so only the contract owner (you) can call the function. Use this code modifier onlyOwner {require(msg.senderowner);_;}, you will need to correctly space it out. and owner = msg.sender; this before it in a function. Use the onlyOwner modifier in the function you want to make so only you can call – JAG Feb 23 '18 at 5:08
1

Private functions are only visible for the contract they are defined in and not in derived contracts.

       pragma solidity ^0.4.0;

       contract C {
               function f(uint a) private returns(uint b) { 
                             return a + 1;
               }

      }

     contract D {
              function readData() public {
                       C c = new C();
                       uint local = c.f(7); // error: member `f` is not visible

             }
    }

Here contract D is not able to call the private function f.Its not possible to call private functions in derived contracts.

You have the only option to call the private functions within the public methods of the same contract.

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.