---
# System prepended metadata

title: Gas Experiment

---

# Gas Experiment

```solidity
// SPDX-License-Identifier: GPL-3.0-or-later

pragma solidity 0.8.11;

import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "../sushiXswap/interfaces/ISushiXSwap.sol";

contract GasProfiler {
    using SafeERC20 for IERC20;

    // Custom Error
    error NotStargateRouter();
    
    event StargateSushiXSwapDst(bytes32 indexed srcContext, bool failed);

    IERC20 public stargateRouter = IERC20(0x8731d54E9D02c286767d56ac03e8037C07e01e98);

    uint public gas0;
    uint public gas1;
    uint public gas2;

    function setStargateRouter (address sgr) public {
        stargateRouter = IERC20(sgr);
    }

    function profile(
        uint16,
        bytes memory,
        uint256,
        address _token,
        uint256 amountLD,
        bytes memory payload
    ) external {
        gas0 = gasleft();

     if (msg.sender != address(stargateRouter)) revert NotStargateRouter();

        (
            address to,
            uint8[] memory actions,
            uint256[] memory values,
            bytes[] memory datas,
            bytes32 srcContext
        ) = abi.decode(payload, (address, uint8[], uint256[], bytes[], bytes32));

        uint256 limit = gasleft() - 200000;
        bool failed;
        /// @dev incase the actions fail, transfer bridge token to the to address
        try
            ISushiXSwap(payable(address(this))).cook{gas: limit}(
                actions,
                values,
                datas
            )
        {} catch (bytes memory) {
            gas1 = gasleft();
            IERC20(_token).safeTransfer(to, amountLD);
            failed = true;
        }

        /// @dev transfer any native token received as dust to the to address
        if (address(this).balance > 0)
            to.call{value: (address(this).balance)}("");

        emit StargateSushiXSwapDst(srcContext, failed);

        gas2 = gasleft();

    }

    function cook(
        uint8[] memory actions,
        uint256[] memory values,
        bytes[] memory datas
    ) public payable  {
        revert();
    }

    /// @notice Allows the contract to receive Native tokens
    receive() external payable {}
}

```

## Results
Running on forked mainnets:

Entry Gas: gas0-gas1
Exit Gas: gas1-gas2

**Eth Mainnet USDT**
Entry Gas: 28142
Exit Gas: 66143

**Eth Mainnet USDC**
Entry Gas: 28142
Exit Gas: 67581

**Polygon USDT**
Entry Gas: 28142
Exit Gas: 63762

**Polygon USDC**
Entry Gas: 28142
Exit Gas: 66108