简体   繁体   中英

Is it possible to call contract function from another contract without consuming Gas?

I wrote very basic Concatenation and StringUtil contracts . I tried to apply composition approach with using Concatenation contract with creating an instance inside of StringUtil. However, Solidity compiler warned me to add payable keyword to concatenate function in order to consume it from StringUtil contract. I thought that concatenate had not be a payable operation. =) Is there a way to use that function without consuming gas? Some ideas that i write below come on my mind but they are not good.

Copying whole Concatenation contract code into StringUtil and directly use in it. However, it is not a good approach.

Extending StringUtil from Concatenation(contract StringUtil is Concatenation). However, i need to write more functionalities inside of newly created contract lets say Comparer and use it as same approach. This is also not good. Because, StringUtil is aldready extended from Concatenation.

What are your thoughts? Do you know best practices on this subject?

Thank you.

Solidity compiler warned me to add payable keyword to concatenate function in order to consume it from StringUtil contract

This is because you have marked the concatenate function as payable. It doesn't seem to be doing any ether transfers, so it seems unnecessary.

Is there a way to use that function without consuming gas?

Running a function that is not view or pure will always incur some gas cost, proportional to the amount of work that is done. Since your function modifies storage, there is no way to use it without spending gas.

As for upgradeability, you would generally need to redeploy both contracts when new methods are added, unless you make use of call , and set up a method to accept function signatures and parameters. You can research proxy contracts to see how something like that is implemented.

I did very simple experiment below. I applied both payable and view keyword on functions. This time complier did not warn me. I figured out that i wrongly call payable function when i posted first post.

  1. Calling view keyword added function
  2. Calling function without using view keyword
  3. Wrongly Calling payable function
  4. Correctly Calling payable function as a result there is no error or warning

Finally, i understand that it is not mandatory thing to add payable keyword to functions that are called from another contract. We can manage that called function whether consumes gas or not from another contract . Is it correct?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM