我如何在不支付 gas 费的情况下计算 solidity 合约的结果?

how do i compute the result of a solidity contract without paying gas fee?

假设我有这个 solidity 合约,我想计算 minAmountOut2 的结果,而不用实际支付 gas gas 费用。有可能实现吗?我认为这在理论上应该是可行的,但我不确定如何实际实现它......谢谢!

pragma solidity >=0.7.0 <0.9.0;

contract Storage {
    
    constructor()  payable {
        // uint256 number;
        address wbnb_addres = 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c; 
        address pancake_swap_v2 = 0x10ED43C718714eb63d5aA57B78B54704E256024E; 
        uint amount = msg.value ;
        address target_token_address = 0x0E09FaBB73Bd3Ade0a17ECC321fD13a19e81cE82;
        address sender_address = tx.origin;
        address[] memory address_input = new address[](2);
        address_input[0] = wbnb_addres;
        address_input[1] = target_token_address;

        uint[] memory result = IPancakeRouter02(pancake_swap_v2).getAmountsOut(amount,address_input);
        uint minAmountOut = result[1];
        uint deadline = 1e30;

        address[] memory address_output2 = new address[](2);
        address_output2[0] = target_token_address;
        address_output2[1] = wbnb_addres;

        uint[] memory result2 = IPancakeRouter02(pancake_swap_v2).getAmountsOut(minAmountOut,address_output2);
        uint minAmountOut2 = result2[1];

        


    }    

    

}

一种可能的方法是将结果转换为字符串并触发恢复函数....然后当我在 web3 中 运行 estimatgas 函数时,我使用错误和异常处理得到了结果。

据我了解estimateGas is the only such function. estimateGas should be almost equal to web3.eth.sendTransaction,它们之间唯一的区别是estimateGas实际上并没有将交易发送出去,如果调用函数时操作失败,则estimateGas无法获得有效结果.

您现在拥有构造函数中的所有代码。您可能希望将代码拆分为更小的函数,然后调用 estimateGas。