简体   繁体   English

是否可以在不消耗Gas的情况下从另一个合同调用合同功能?

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

I wrote very basic Concatenation and StringUtil contracts . 我写了非常基本的Concatenation和StringUtil合同 I tried to apply composition approach with using Concatenation contract with creating an instance inside of StringUtil. 我尝试通过在StringUtil内创建实例的情况下使用并置协定来应用组合方法。 However, Solidity compiler warned me to add payable keyword to concatenate function in order to consume it from StringUtil contract. 但是,Solidity编译器警告我添加payable关键字来连接函数,以便从StringUtil合约中使用它。 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. 将整个串联合同代码复制到StringUtil中并直接在其中使用。 However, it is not a good approach. 但是,这不是一个好方法。

Extending StringUtil from Concatenation(contract StringUtil is Concatenation). 从Concatenation扩展StringUtil(合同StringUtil是Concatenation)。 However, i need to write more functionalities inside of newly created contract lets say Comparer and use it as same approach. 但是,我需要在新创建的合约中编写更多功能,比如说Comparer并将其用作相同的方法。 This is also not good. 这也不好。 Because, StringUtil is aldready extended from Concatenation. 因为,StringUtil已从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 Solidity编译器警告我添加payable关键字以连接函数,以便从StringUtil合约中使用它

This is because you have marked the concatenate function as payable. 这是因为您已将concatenate功能标记为应付款。 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. 运行非view函数或pure函数将始终产生一定的耗气量,与完成的工作量成比例。 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. 至于可升级性,除非添加了call ,并且设置了一个接受函数签名和参数的方法,否则通常需要在添加新方法时重新部署两个合同。 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. 我在函数上同时应用了payable和view关键字。 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 不使用view关键字的调用功能
  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. 最后,我了解到将payable关键字添加到从另一个合同调用的函数中并不是强制性的事情。 We can manage that called function whether consumes gas or not from another contract . 我们可以管理被调用的函数是否消耗其他合同中的天然气。 Is it correct? 这是正确的吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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