// SPDX-License-Identifier: MIT pragma solidity ^0.8.1; import {GelatoRelayBase} from "./base/GelatoRelayBase.sol"; import {TokenUtils} from "./lib/TokenUtils.sol"; uint256 constant _FEE_COLLECTOR_START = 72; // offset: address + address + uint256 uint256 constant _FEE_TOKEN_START = 52; // offset: address + uint256 uint256 constant _FEE_START = 32; // offset: uint256 // WARNING: Do not use this free fn by itself, always inherit GelatoRelayContext // solhint-disable-next-line func-visibility, private-vars-leading-underscore function _getFeeCollectorRelayContext() pure returns (address feeCollector) { assembly { feeCollector := shr( 96, calldataload(sub(calldatasize(), _FEE_COLLECTOR_START)) ) } } // WARNING: Do not use this free fn by itself, always inherit GelatoRelayContext // solhint-disable-next-line func-visibility, private-vars-leading-underscore function _getFeeTokenRelayContext() pure returns (address feeToken) { assembly { feeToken := shr(96, calldataload(sub(calldatasize(), _FEE_TOKEN_START))) } } // WARNING: Do not use this free fn by itself, always inherit GelatoRelayContext // solhint-disable-next-line func-visibility, private-vars-leading-underscore function _getFeeRelayContext() pure returns (uint256 fee) { assembly { fee := calldataload(sub(calldatasize(), _FEE_START)) } } /** * @dev Context variant with feeCollector, feeToken and fee appended to msg.data * Expects calldata encoding: * abi.encodePacked( _data, * _feeCollector, * _feeToken, * _fee); * Therefore, we're expecting 20 + 20 + 32 = 72 bytes to be appended to normal msgData * 32bytes start offsets from calldatasize: * feeCollector: - 72 bytes * feeToken: - 52 bytes * fee: - 32 bytes */ /// @dev Do not use with GelatoRelayFeeCollector - pick only one abstract contract GelatoRelayContext is GelatoRelayBase { using TokenUtils for address; // DANGER! Only use with onlyGelatoRelay `_isGelatoRelay` before transferring function _transferRelayFee() internal { _getFeeToken().transfer(_getFeeCollector(), _getFee()); } // DANGER! Only use with onlyGelatoRelay `_isGelatoRelay` before transferring function _transferRelayFeeCapped(uint256 _maxFee) internal { uint256 fee = _getFee(); require( fee <= _maxFee, "GelatoRelayContext._transferRelayFeeCapped: maxFee" ); _getFeeToken().transfer(_getFeeCollector(), fee); } function _getMsgData() internal view returns (bytes calldata) { return _isGelatoRelay(msg.sender) ? msg.data[:msg.data.length - _FEE_COLLECTOR_START] : msg.data; } // Only use with GelatoRelayBase onlyGelatoRelay or `_isGelatoRelay` checks function _getFeeCollector() internal pure returns (address) { return _getFeeCollectorRelayContext(); } // Only use with previous onlyGelatoRelay or `_isGelatoRelay` checks function _getFeeToken() internal pure returns (address) { return _getFeeTokenRelayContext(); } // Only use with previous onlyGelatoRelay or `_isGelatoRelay` checks function _getFee() internal pure returns (uint256) { return _getFeeRelayContext(); } }