|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.13; |
| 3 | + |
| 4 | +import "./interfaces/IExecutorQuoter.sol"; |
| 5 | + |
| 6 | +string constant executorQuoterVersion = "Executor-Quoter-0.0.1"; |
| 7 | + |
| 8 | +contract ExecutorQuoter is IExecutorQuoter { |
| 9 | + string public constant EXECUTOR_QUOTER_VERSION = executorQuoterVersion; |
| 10 | + uint8 private constant QUOTE_DECIMALS = 10; |
| 11 | + uint8 private constant DECIMAL_RESOLUTION = 18; |
| 12 | + |
| 13 | + address public immutable quoterAddress; |
| 14 | + address public immutable updaterAddress; |
| 15 | + uint8 public immutable srcTokenDecimals; |
| 16 | + bytes32 public immutable payeeAddress; |
| 17 | + |
| 18 | + /// This is the same as an EQ01 quote body |
| 19 | + /// It fits into a single bytes32 storage slot |
| 20 | + struct OnChainQuoteBody { |
| 21 | + /// The base fee, in sourceChain native currency, required by the quoter to perform an execution on the destination chain |
| 22 | + uint64 baseFee; |
| 23 | + /// The current gas price on the destination chain |
| 24 | + uint64 dstGasPrice; |
| 25 | + /// The USD price, in 10^10, of the sourceChain native currency |
| 26 | + uint64 srcPrice; |
| 27 | + /// The USD price, in 10^10, of the destinationChain native currency |
| 28 | + uint64 dstPrice; |
| 29 | + } |
| 30 | + |
| 31 | + struct ChainDecimals { |
| 32 | + bool enabled; |
| 33 | + uint8 gasPriceDecimals; |
| 34 | + uint8 nativeDecimals; |
| 35 | + } |
| 36 | + |
| 37 | + struct QuoteUpdate { |
| 38 | + uint16 chainId; |
| 39 | + OnChainQuoteBody quote; |
| 40 | + } |
| 41 | + |
| 42 | + struct DecimalsUpdate { |
| 43 | + uint16 chainId; |
| 44 | + ChainDecimals decimals; |
| 45 | + } |
| 46 | + |
| 47 | + mapping(uint16 => OnChainQuoteBody) public quoteByDstChain; |
| 48 | + mapping(uint16 => ChainDecimals) public decimalsByDstChain; |
| 49 | + |
| 50 | + /// @dev Selector 0x40788bb5. |
| 51 | + error InvalidUpdater(address sender, address expected); |
| 52 | + /// @dev Selector 0x4dc2c273. |
| 53 | + error ChainDisabled(uint16 chainId); |
| 54 | + /// @dev Selector 0x0d2e6713. |
| 55 | + error UnsupportedInstruction(uint8 ixType); |
| 56 | + /// @dev Selector 0x3a5a1720. |
| 57 | + error MoreThanOneDropOff(); |
| 58 | + |
| 59 | + constructor(address _quoterAddress, address _updaterAddress, uint8 _srcTokenDecimals, bytes32 _payeeAddress) { |
| 60 | + quoterAddress = _quoterAddress; |
| 61 | + updaterAddress = _updaterAddress; |
| 62 | + srcTokenDecimals = _srcTokenDecimals; |
| 63 | + payeeAddress = _payeeAddress; |
| 64 | + } |
| 65 | + |
| 66 | + function decimalsUpdate(DecimalsUpdate[] calldata updates) public { |
| 67 | + if (msg.sender != updaterAddress) { |
| 68 | + revert InvalidUpdater(msg.sender, updaterAddress); |
| 69 | + } |
| 70 | + uint256 updatesLength = updates.length; |
| 71 | + for (uint256 i = 0; i < updatesLength;) { |
| 72 | + DecimalsUpdate memory update = updates[i]; |
| 73 | + decimalsByDstChain[update.chainId] = update.decimals; |
| 74 | + unchecked { |
| 75 | + i += 1; |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + function quoteUpdate(QuoteUpdate[] calldata updates) public { |
| 81 | + if (msg.sender != updaterAddress) { |
| 82 | + revert InvalidUpdater(msg.sender, updaterAddress); |
| 83 | + } |
| 84 | + uint256 updatesLength = updates.length; |
| 85 | + for (uint256 i = 0; i < updatesLength;) { |
| 86 | + QuoteUpdate memory update = updates[i]; |
| 87 | + quoteByDstChain[update.chainId] = update.quote; |
| 88 | + unchecked { |
| 89 | + i += 1; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + function normalize(uint256 amount, uint8 from, uint8 to) internal pure returns (uint256) { |
| 95 | + if (from > to) { |
| 96 | + return amount / 10 ** uint256(from - to); |
| 97 | + } else if (from < to) { |
| 98 | + return amount * 10 ** uint256(to - from); |
| 99 | + } |
| 100 | + return amount; |
| 101 | + } |
| 102 | + |
| 103 | + function mul(uint256 a, uint256 b, uint8 decimals) internal pure returns (uint256) { |
| 104 | + return (a * b) / 10 ** uint256(decimals); |
| 105 | + } |
| 106 | + |
| 107 | + function div(uint256 a, uint256 b, uint8 decimals) internal pure returns (uint256) { |
| 108 | + return (a * 10 ** uint256(decimals)) / b; |
| 109 | + } |
| 110 | + |
| 111 | + /// Calculates the total gas limit and total message value from a set of relay instructions. |
| 112 | + /// Each relay instruction can be either a `GasInstruction` or a `GasDropOffInstruction`. |
| 113 | + /// - `GasInstruction` contributes to both `gasLimit` and `msgValue`. |
| 114 | + /// - `GasDropOffInstruction` contributes only to `msgValue`. |
| 115 | + /// Throws If an unsupported instruction type is encountered. |
| 116 | + function totalGasLimitAndMsgValue(bytes calldata relayInstructions) |
| 117 | + internal |
| 118 | + pure |
| 119 | + returns (uint256 gasLimit, uint256 msgValue) |
| 120 | + { |
| 121 | + uint256 offset = 0; |
| 122 | + uint8 ixType; |
| 123 | + bool hasDropOff = false; |
| 124 | + uint256 relayInstructionsLength = relayInstructions.length; |
| 125 | + while (offset < relayInstructionsLength) { |
| 126 | + assembly { |
| 127 | + ixType := shr(248, calldataload(add(relayInstructions.offset, offset))) |
| 128 | + offset := add(offset, 1) |
| 129 | + } |
| 130 | + if (ixType == 1) { |
| 131 | + assembly { |
| 132 | + gasLimit := shr(128, calldataload(add(relayInstructions.offset, offset))) |
| 133 | + offset := add(offset, 16) |
| 134 | + msgValue := shr(128, calldataload(add(relayInstructions.offset, offset))) |
| 135 | + offset := add(offset, 16) |
| 136 | + } |
| 137 | + } else if (ixType == 2) { |
| 138 | + if (hasDropOff) { |
| 139 | + revert MoreThanOneDropOff(); |
| 140 | + } |
| 141 | + hasDropOff = true; |
| 142 | + assembly { |
| 143 | + msgValue := shr(128, calldataload(add(relayInstructions.offset, offset))) |
| 144 | + offset := add(offset, 48) |
| 145 | + } |
| 146 | + } else { |
| 147 | + revert UnsupportedInstruction(ixType); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + |
| 152 | + function estimateQuote( |
| 153 | + OnChainQuoteBody storage quote, |
| 154 | + ChainDecimals storage dstChainDecimals, |
| 155 | + uint256 gasLimit, |
| 156 | + uint256 msgValue |
| 157 | + ) internal view returns (uint256) { |
| 158 | + uint256 srcChainValueForBaseFee = normalize(quote.baseFee, QUOTE_DECIMALS, srcTokenDecimals); |
| 159 | + |
| 160 | + uint256 nSrcPrice = normalize(quote.srcPrice, QUOTE_DECIMALS, DECIMAL_RESOLUTION); |
| 161 | + uint256 nDstPrice = normalize(quote.dstPrice, QUOTE_DECIMALS, DECIMAL_RESOLUTION); |
| 162 | + uint256 scaledConversion = div(nDstPrice, nSrcPrice, DECIMAL_RESOLUTION); |
| 163 | + |
| 164 | + uint256 nGasLimitCost = |
| 165 | + normalize(gasLimit * quote.dstGasPrice, dstChainDecimals.gasPriceDecimals, DECIMAL_RESOLUTION); |
| 166 | + |
| 167 | + uint256 srcChainValueForGasLimit = |
| 168 | + normalize(mul(nGasLimitCost, scaledConversion, DECIMAL_RESOLUTION), DECIMAL_RESOLUTION, srcTokenDecimals); |
| 169 | + |
| 170 | + uint256 nMsgValue = normalize(msgValue, dstChainDecimals.nativeDecimals, DECIMAL_RESOLUTION); |
| 171 | + uint256 srcChainValueForMsgValue = |
| 172 | + normalize(mul(nMsgValue, scaledConversion, DECIMAL_RESOLUTION), DECIMAL_RESOLUTION, srcTokenDecimals); |
| 173 | + return srcChainValueForBaseFee + srcChainValueForGasLimit + srcChainValueForMsgValue; |
| 174 | + } |
| 175 | + |
| 176 | + function requestQuote( |
| 177 | + uint16 dstChain, |
| 178 | + bytes32, //dstAddr, |
| 179 | + address, //refundAddr, |
| 180 | + bytes calldata, //requestBytes, |
| 181 | + bytes calldata relayInstructions |
| 182 | + ) public view returns (bytes32, uint256) { |
| 183 | + ChainDecimals storage dstChainDecimals = decimalsByDstChain[dstChain]; |
| 184 | + if (!dstChainDecimals.enabled) { |
| 185 | + revert ChainDisabled(dstChain); |
| 186 | + } |
| 187 | + OnChainQuoteBody storage quote = quoteByDstChain[dstChain]; |
| 188 | + (uint256 gasLimit, uint256 msgValue) = totalGasLimitAndMsgValue(relayInstructions); |
| 189 | + // NOTE: this does not include any maxGasLimit or maxMsgValue checks |
| 190 | + uint256 requiredPayment = estimateQuote(quote, dstChainDecimals, gasLimit, msgValue); |
| 191 | + |
| 192 | + return (payeeAddress, requiredPayment); |
| 193 | + } |
| 194 | +} |
0 commit comments