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.