V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
cxh116
V2EX  ›  以太坊

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

  •  
  •   cxh116 · 2022-11-04 16:19:11 +08:00 · 923 次点击
    这是一个创建于 510 天前的主题,其中的信息可能已经有所发展或是发生改变。
    Eth 合约函数写操作(非 view 非 pure)时调用外部只读(view)合约函数时, 外部 view 函数调用会算 Gas 吗?

    假如外部 view 调用也算 Gas 的话,我把外部合约同样的代码,复制到自己的合约里,直接在合约内部调用.
    在忽略首次部署的 Gas 成本时,哪种调用方式更省 Gas ? 还是一样多?
    2 条回复    2022-11-04 18:03:17 +08:00
    aggron
        1
    aggron  
       2022-11-04 17:37:39 +08:00   ❤️ 1
    1. 会算 gas
    2. 复制到自己合约内调用,比直接调用外部合约省 gas ,外部合约初始化
    cxh116
        2
    cxh116  
    OP
       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
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3738 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 10:37 · PVG 18:37 · LAX 03:37 · JFK 06:37
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.