gas编程实战

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

contract Gas {
    
    
    uint public i = 0;
    uint public remained;
 
    function forever() public {
    
    
        while (true) {
    
    
            if(i > 100)
                return;
            if(i == 10)
                remained = gasleft();
            i += 1;
        }
    }
}
contract GasCaller{
    
    
    address gas;
    constructor(address _gas) public{
    
    
        gas = _gas;

    }
    function callForever() public {
    
    
        bytes memory cd = abi.encodeWithSignature("forever()");
        (bool suc, bytes memory data) = gas.call{
    
    gas: 100000}(cd);
        if(!suc){
    
    
            revert("gas not enough");
        }
    }
}

在这里插入图片描述
超出gas
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_37117521/article/details/139708779
gas