请教一个 Eth 合约调用 Gas 的计算问题.

2022-11-04 16:19:11 +08:00
 cxh116
Eth 合约函数写操作(非 view 非 pure)时调用外部只读(view)合约函数时, 外部 view 函数调用会算 Gas 吗?

假如外部 view 调用也算 Gas 的话,我把外部合约同样的代码,复制到自己的合约里,直接在合约内部调用.
在忽略首次部署的 Gas 成本时,哪种调用方式更省 Gas ? 还是一样多?
966 次点击
所在节点    以太坊
2 条回复
aggron
2022-11-04 17:37:39 +08:00
1. 会算 gas
2. 复制到自己合约内调用,比直接调用外部合约省 gas ,外部合约初始化
cxh116
2022-11-04 18:03:17 +08:00
@aggron 感谢感谢.刚实测了也确实是复制到自己合约里更省.


test1 gas 25,448
test2 gas 23,055


// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import './HelloWorld.sol';

contract TestCall {
function test1(string memory p1) external returns (string memory ret) {
ret = IHelloWorld(0x93e3EC5b5087bFcaBCcA95Fce30a094dEaEaA13B).get();
}
function test2(string memory p1) external returns (string memory ret) {
ret = get();
}

function get() view public returns(string memory greeting){
return greeting = "Hello, world!";
}
}


test1
https://testnet.bscscan.com/tx/0x823f72c8f901eb2b85c011281895a17c9d04d09296941079362e682d0d633209
https://testnet.bscscan.com/tx/0x2a61bd695d17754f8e1a15eb050d86f98874ce026e3ddaec19773a1b4cb50d20

test2
https://testnet.bscscan.com/tx/0x93cd7e582013f0e1f397455665fd54b5c983489a58434a6c5bc732792437b027
https://testnet.bscscan.com/tx/0x1dd8d168baafb8a8b94d50355350432c275ca510ff1846f57e367f9cfe7e39b9

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/892701

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX