Verified contractExactMIT
TrailsRouter
Address0xf8a739b9f24e297a98b7aba7a9cdfdbd457f6ff8
- Balance
0 ETH
- Transactions
0
- Compiler
solc 0.8.30
- Same code
Only this contract
Details
- Compiler
- solc 0.8.30+commit.73712a01
- Language
- solidity
- Optimization
- Yes, 4,294,967,295 runs
- EVM version
- prague
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 23,893,956
- Creation transaction
- 0x69620fe2c3…34fd82547e
77 files
The source code is large: one file is shown at a time. Choose a file in the list.
- src/TrailsRouter.solmain16.26 KB
- lib/erc2470-libs/script/SingletonDeployer.s.sol2.39 KB
- lib/erc2470-libs/src/ISingletonFactory.sol1.22 KB
- lib/forge-std/src/Base.sol2.23 KB
- lib/forge-std/src/Script.sol881 B
- lib/forge-std/src/StdAssertions.sol25.39 KB
- lib/forge-std/src/StdChains.sol14.27 KB
- lib/forge-std/src/StdCheats.sol31.81 KB
- lib/forge-std/src/StdConstants.sol1.87 KB
- lib/forge-std/src/StdError.sol1.01 KB
- lib/forge-std/src/StdInvariant.sol3.98 KB
- lib/forge-std/src/StdJson.sol9.7 KB
- lib/forge-std/src/StdMath.sol1.6 KB
- lib/forge-std/src/StdStorage.sol17.84 KB
- lib/forge-std/src/StdStyle.sol10.46 KB
- lib/forge-std/src/StdToml.sol9.7 KB
- lib/forge-std/src/StdUtils.sol9.9 KB
- lib/forge-std/src/Test.sol1.1 KB
- lib/forge-std/src/Vm.sol131.78 KB
- lib/forge-std/src/console.sol69.27 KB
- lib/forge-std/src/console2.sol117 B
- lib/forge-std/src/interfaces/IMulticall3.sol2.19 KB
- lib/forge-std/src/safeconsole.sol427.54 KB
- lib/openzeppelin-contracts/contracts/interfaces/IERC1363.sol4.45 KB
- lib/openzeppelin-contracts/contracts/interfaces/IERC165.sol193 B
- lib/openzeppelin-contracts/contracts/interfaces/IERC20.sol182 B
- lib/openzeppelin-contracts/contracts/interfaces/IERC5267.sol740 B
- lib/openzeppelin-contracts/contracts/interfaces/draft-IERC6093.sol6.54 KB
- lib/openzeppelin-contracts/contracts/mocks/token/ERC20Mock.sol392 B
- lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol10.8 KB
- lib/openzeppelin-contracts/contracts/token/ERC20/IERC20.sol2.78 KB
- lib/openzeppelin-contracts/contracts/token/ERC20/extensions/ERC20Permit.sol2.72 KB
- lib/openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Metadata.sol684 B
- lib/openzeppelin-contracts/contracts/token/ERC20/extensions/IERC20Permit.sol3.81 KB
- lib/openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol13.31 KB
- lib/openzeppelin-contracts/contracts/utils/Bytes.sol11.34 KB
- lib/openzeppelin-contracts/contracts/utils/Context.sol963 B
- lib/openzeppelin-contracts/contracts/utils/Nonces.sol1.49 KB
- lib/openzeppelin-contracts/contracts/utils/Panic.sol1.97 KB
- lib/openzeppelin-contracts/contracts/utils/ReentrancyGuard.sol4.77 KB
- lib/openzeppelin-contracts/contracts/utils/ShortStrings.sol4.31 KB
- lib/openzeppelin-contracts/contracts/utils/StorageSlot.sol4.18 KB
- lib/openzeppelin-contracts/contracts/utils/Strings.sol19.88 KB
- lib/openzeppelin-contracts/contracts/utils/cryptography/ECDSA.sol11.91 KB
- lib/openzeppelin-contracts/contracts/utils/cryptography/EIP712.sol6.7 KB
- lib/openzeppelin-contracts/contracts/utils/cryptography/MessageHashUtils.sol4.25 KB
- lib/openzeppelin-contracts/contracts/utils/introspection/IERC165.sol871 B
- lib/openzeppelin-contracts/contracts/utils/math/Math.sol32.36 KB
- lib/openzeppelin-contracts/contracts/utils/math/SafeCast.sol34.94 KB
- lib/openzeppelin-contracts/contracts/utils/math/SignedMath.sol2.61 KB
- lib/tstorish/src/Tstorish.sol9.76 KB
- lib/wallet-contracts-v3/src/modules/interfaces/IDelegatedExtension.sol665 B
- script/TrailsIntentEntrypoint.s.sol1.02 KB
- script/TrailsRouter.s.sol1.34 KB
- script/TrailsRouterShim.s.sol1.62 KB
- src/TrailsIntentEntrypoint.sol8.39 KB
- src/TrailsRouterShim.sol3.19 KB
- src/guards/DelegatecallGuard.sol1.5 KB
- src/interfaces/IMulticall3.sol1.16 KB
- src/interfaces/ITrailsIntentEntrypoint.sol4.68 KB
- src/interfaces/ITrailsRouter.sol6.52 KB
- src/interfaces/ITrailsRouterShim.sol767 B
- src/libraries/TrailsSentinelLib.sol1.13 KB
- test/TrailsIntentEntrypoint.t.sol79.03 KB
- test/TrailsRouter.t.sol59.68 KB
- test/TrailsRouterShim.t.sol24.99 KB
- test/guards/DelegatecallGuard.t.sol6.58 KB
- test/libraries/TrailsSentinelLib.t.sol6.47 KB
- test/mocks/MockERC20.sol689 B
- test/mocks/MockMulticall3.sol1.96 KB
- test/mocks/MockNonStandardERC20.sol2.55 KB
- test/mocks/MockSenderGetter.sol360 B
- test/script/TrailsIntentEntrypoint.s.t.sol4.76 KB
- test/script/TrailsRouter.s.t.sol4.37 KB
- test/script/TrailsRouterShim.s.t.sol5.96 KB
- test/utils/Create2Utils.sol1.46 KB
- test/utils/TstoreUtils.sol3.32 KB
src/TrailsRouter.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.30;
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import {IDelegatedExtension} from "wallet-contracts-v3/modules/interfaces/IDelegatedExtension.sol";
import {Tstorish} from "tstorish/Tstorish.sol";
import {DelegatecallGuard} from "./guards/DelegatecallGuard.sol";
import {IMulticall3} from "./interfaces/IMulticall3.sol";
import {ITrailsRouter} from "./interfaces/ITrailsRouter.sol";
import {TrailsSentinelLib} from "./libraries/TrailsSentinelLib.sol";
/// @title TrailsRouter
/// @author Miguel Mota, Shun Kakinoki
/// @notice Consolidated router for Trails operations including multicall routing, balance injection, and token sweeping
/// @dev Must be delegatecalled via the Sequence delegated extension module to access wallet storage/balances.
contract TrailsRouter is IDelegatedExtension, ITrailsRouter, DelegatecallGuard, Tstorish {
// -------------------------------------------------------------------------
// Libraries
// -------------------------------------------------------------------------
using SafeERC20 for IERC20;
// -------------------------------------------------------------------------
// Immutable Variables
// -------------------------------------------------------------------------
address public immutable MULTICALL3;
// -------------------------------------------------------------------------
// Constructor
// -------------------------------------------------------------------------
constructor(address _multicall3) {
MULTICALL3 = _multicall3;
}
// -------------------------------------------------------------------------
// Errors
// -------------------------------------------------------------------------
error NativeTransferFailed();
error InvalidDelegatedSelector(bytes4 selector);
error InvalidFunctionSelector(bytes4 selector);
error AllowFailureMustBeFalse(uint256 callIndex);
error SuccessSentinelNotSet();
error NoValueAvailable();
error NoTokensToPull();
error IncorrectValue(uint256 required, uint256 received);
error NoTokensToSweep();
error AmountOffsetOutOfBounds();
error PlaceholderMismatch();
error TargetCallFailed(bytes revertData);
// -------------------------------------------------------------------------
// Receive ETH
// -------------------------------------------------------------------------
/// @notice Allow direct native token transfers when contract is used standalone.
receive() external payable {}
// -------------------------------------------------------------------------
// Multicall3 Router Functions
// -------------------------------------------------------------------------
/// @inheritdoc ITrailsRouter
function execute(bytes calldata data) public payable returns (IMulticall3.Result[] memory returnResults) {
_validateRouterCall(data);
(bool success, bytes memory returnData) = MULTICALL3.delegatecall(data);
if (!success) revert TargetCallFailed(returnData);
return abi.decode(returnData, (IMulticall3.Result[]));
}
/// @inheritdoc ITrailsRouter
function pullAndExecute(address token, bytes calldata data)
public
payable
returns (IMulticall3.Result[] memory returnResults)
{
uint256 amount;
if (token == address(0)) {
if (msg.value == 0) revert NoValueAvailable();
amount = msg.value;
} else {
amount = _getBalance(token, msg.sender);
if (amount == 0) revert NoTokensToPull();
}
return pullAmountAndExecute(token, amount, data);
}
/// @inheritdoc ITrailsRouter
function pullAmountAndExecute(address token, uint256 amount, bytes calldata data)
public
payable
returns (IMulticall3.Result[] memory returnResults)
{
_validateRouterCall(data);
if (token == address(0)) {
if (msg.value != amount) revert IncorrectValue(amount, msg.value);
} else {
if (msg.value != 0) revert IncorrectValue(0, msg.value);
_safeTransferFrom(token, msg.sender, address(this), amount);
}
(bool success, bytes memory returnData) = MULTICALL3.delegatecall(data);
if (!success) revert TargetCallFailed(returnData);
returnResults = abi.decode(returnData, (IMulticall3.Result[]));
// Sweep remaining balance back to msg.sender to prevent dust from EXACT_OUTPUT swaps getting stuck.
// We sweep the full balance (not tracking initial) since TrailsRouter is stateless by design.
uint256 remaining = _getSelfBalance(token);
if (remaining > 0) {
if (token == address(0)) {
_transferNative(msg.sender, remaining);
} else {
_transferERC20(token, msg.sender, remaining);
}
emit Sweep(token, msg.sender, remaining);
}
}
// -------------------------------------------------------------------------
// Balance Injection Functions
// -------------------------------------------------------------------------
/// @inheritdoc ITrailsRouter
function injectSweepAndCall(
address token,
address target,
bytes calldata callData,
uint256 amountOffset,
bytes32 placeholder
) external payable {
uint256 callerBalance;
if (token == address(0)) {
callerBalance = msg.value;
if (callerBalance == 0) revert NoValueAvailable();
} else {
if (msg.value != 0) revert IncorrectValue(0, msg.value);
callerBalance = _getBalance(token, msg.sender);
if (callerBalance == 0) revert NoTokensToSweep();
_safeTransferFrom(token, msg.sender, address(this), callerBalance);
}
_injectAndExecuteCall(token, target, callData, amountOffset, placeholder, callerBalance);
}
/// @inheritdoc ITrailsRouter
function injectAndCall(
address token,
address target,
bytes calldata callData,
uint256 amountOffset,
bytes32 placeholder
) public payable {
if (token == address(0) && msg.value != 0) {
revert IncorrectValue(0, msg.value);
}
uint256 callerBalance = _getSelfBalance(token);
if (callerBalance == 0) {
if (token == address(0)) {
revert NoValueAvailable();
} else {
revert NoTokensToSweep();
}
}
_injectAndExecuteCall(token, target, callData, amountOffset, placeholder, callerBalance);
}
// -------------------------------------------------------------------------
// Token Sweeper Functions
// -------------------------------------------------------------------------
/// @inheritdoc ITrailsRouter
function sweep(address _token, address _recipient) public payable onlyDelegatecall {
uint256 amount = _getSelfBalance(_token);
if (amount > 0) {
if (_token == address(0)) {
_transferNative(_recipient, amount);
} else {
_transferERC20(_token, _recipient, amount);
}
emit Sweep(_token, _recipient, amount);
}
}
/// @inheritdoc ITrailsRouter
function refundAndSweep(address _token, address _refundRecipient, uint256 _refundAmount, address _sweepRecipient)
public
payable
onlyDelegatecall
{
uint256 current = _getSelfBalance(_token);
uint256 actualRefund = _refundAmount > current ? current : _refundAmount;
if (actualRefund != _refundAmount) {
emit ActualRefund(_token, _refundRecipient, _refundAmount, actualRefund);
}
if (actualRefund > 0) {
if (_token == address(0)) {
_transferNative(_refundRecipient, actualRefund);
} else {
_transferERC20(_token, _refundRecipient, actualRefund);
}
emit Refund(_token, _refundRecipient, actualRefund);
}
uint256 remaining = _getSelfBalance(_token);
if (remaining > 0) {
if (_token == address(0)) {
_transferNative(_sweepRecipient, remaining);
} else {
_transferERC20(_token, _sweepRecipient, remaining);
}
emit Sweep(_token, _sweepRecipient, remaining);
}
emit RefundAndSweep(_token, _refundRecipient, _refundAmount, _sweepRecipient, actualRefund, remaining);
}
/// @inheritdoc ITrailsRouter
function validateOpHashAndSweep(bytes32 opHash, address _token, address _recipient)
public
payable
onlyDelegatecall
{
uint256 slot = TrailsSentinelLib.successSlot(opHash);
if (_getTstorish(slot) != TrailsSentinelLib.SUCCESS_VALUE) {
revert SuccessSentinelNotSet();
}
sweep(_token, _recipient);
}
// -------------------------------------------------------------------------
// Sequence Delegated Extension Entry Point
// -------------------------------------------------------------------------
/// @inheritdoc IDelegatedExtension
function handleSequenceDelegateCall(
bytes32 _opHash,
uint256, /* _startingGas */
uint256, /* _index */
uint256, /* _numCalls */
uint256, /* _space */
bytes calldata _data
)
external
override(IDelegatedExtension, ITrailsRouter)
onlyDelegatecall
{
bytes4 selector;
if (_data.length >= 4) {
selector = bytes4(_data[0:4]);
}
// Balance Injection selectors
if (selector == this.injectAndCall.selector) {
(address token, address target, bytes memory callData, uint256 amountOffset, bytes32 placeholder) =
abi.decode(_data[4:], (address, address, bytes, uint256, bytes32));
_injectAndCallDelegated(token, target, callData, amountOffset, placeholder);
return;
}
// Token Sweeper selectors
if (selector == this.sweep.selector) {
(address token, address recipient) = abi.decode(_data[4:], (address, address));
sweep(token, recipient);
return;
}
if (selector == this.refundAndSweep.selector) {
(address token, address refundRecipient, uint256 refundAmount, address sweepRecipient) =
abi.decode(_data[4:], (address, address, uint256, address));
refundAndSweep(token, refundRecipient, refundAmount, sweepRecipient);
return;
}
if (selector == this.validateOpHashAndSweep.selector) {
(, address token, address recipient) = abi.decode(_data[4:], (bytes32, address, address));
validateOpHashAndSweep(_opHash, token, recipient);
return;
}
revert InvalidDelegatedSelector(selector);
}
// -------------------------------------------------------------------------
// Internal Helpers
// -------------------------------------------------------------------------
/// forge-lint: disable-next-line(mixed-case-function)
function _safeTransferFrom(address token, address from, address to, uint256 amount) internal {
IERC20 erc20 = IERC20(token);
SafeERC20.safeTransferFrom(erc20, from, to, amount);
}
/// forge-lint: disable-next-line(mixed-case-function)
function _transferNative(address _to, uint256 _amount) internal {
(bool success,) = payable(_to).call{value: _amount}("");
if (!success) revert NativeTransferFailed();
}
/// forge-lint: disable-next-line(mixed-case-function)
function _transferERC20(address _token, address _to, uint256 _amount) internal {
IERC20 erc20 = IERC20(_token);
SafeERC20.safeTransfer(erc20, _to, _amount);
}
/// forge-lint: disable-next-line(mixed-case-function)
function _getBalance(address token, address account) internal view returns (uint256) {
return token == address(0) ? account.balance : IERC20(token).balanceOf(account);
}
/// forge-lint: disable-next-line(mixed-case-function)
function _getSelfBalance(address token) internal view returns (uint256) {
return _getBalance(token, address(this));
}
/// forge-lint: disable-next-line(mixed-case-function)
function _injectAndCallDelegated(
address token,
address target,
bytes memory callData,
uint256 amountOffset,
bytes32 placeholder
) internal {
uint256 callerBalance = _getSelfBalance(token);
if (callerBalance == 0) {
if (token == address(0)) {
revert NoValueAvailable();
} else {
revert NoTokensToSweep();
}
}
_injectAndExecuteCall(token, target, callData, amountOffset, placeholder, callerBalance);
}
/// forge-lint: disable-next-line(mixed-case-function)
function _injectAndExecuteCall(
address token,
address target,
bytes memory callData,
uint256 amountOffset,
bytes32 placeholder,
uint256 callerBalance
) internal {
// Replace placeholder with actual balance if needed
bool shouldReplace = (amountOffset != 0 || placeholder != bytes32(0));
if (shouldReplace) {
if (callData.length < amountOffset + 32) revert AmountOffsetOutOfBounds();
bytes32 found;
assembly {
found := mload(add(add(callData, 32), amountOffset))
}
if (found != placeholder) revert PlaceholderMismatch();
assembly {
mstore(add(add(callData, 32), amountOffset), callerBalance)
}
}
// Execute call based on token type
if (token == address(0)) {
(bool success, bytes memory result) = target.call{value: callerBalance}(callData);
emit BalanceInjectorCall(token, target, placeholder, callerBalance, amountOffset, success, result);
if (!success) revert TargetCallFailed(result);
} else {
IERC20 erc20 = IERC20(token);
SafeERC20.forceApprove(erc20, target, callerBalance);
(bool success, bytes memory result) = target.call(callData);
emit BalanceInjectorCall(token, target, placeholder, callerBalance, amountOffset, success, result);
if (!success) revert TargetCallFailed(result);
}
}
/// forge-lint: disable-next-line(mixed-case-function)
function _validateRouterCall(bytes calldata callData) internal pure {
// Extract function selector
if (callData.length < 4) revert InvalidFunctionSelector(bytes4(0));
bytes4 selector = bytes4(callData[0:4]);
// Only allow `aggregate3Value` or `aggregate3` (0x174dea71 or 0x82ad56cb)
if (selector == 0x174dea71) {
// Decode and validate the Call3Value[] array to ensure allowFailure=false for all calls
IMulticall3.Call3Value[] memory calls = abi.decode(callData[4:], (IMulticall3.Call3Value[]));
// Iterate through all calls and verify allowFailure is false
for (uint256 i = 0; i < calls.length; i++) {
if (calls[i].allowFailure) {
revert AllowFailureMustBeFalse(i);
}
}
} else if (selector == 0x82ad56cb) {
// Decode and validate the Call3[] array to ensure allowFailure=false for all calls
IMulticall3.Call3[] memory calls = abi.decode(callData[4:], (IMulticall3.Call3[]));
// Iterate through all calls and verify allowFailure is false
for (uint256 i = 0; i < calls.length; i++) {
if (calls[i].allowFailure) {
revert AllowFailureMustBeFalse(i);
}
}
} else {
revert InvalidFunctionSelector(selector);
}
}
}
Compiler settingsShow
{
"viaIR": true,
"metadata": {
"bytecodeHash": "ipfs"
},
"libraries": {},
"optimizer": {
"runs": 4294967295,
"enabled": true
},
"evmVersion": "prague",
"remappings": [
":@/=src/",
":@eth-optimism/=lib/contracts/node_modules/@hop-protocol/sdk/node_modules/@eth-optimism/",
":@memview-sol/=lib/evm-cctp-contracts/lib/memview-sol/",
":@openzeppelin/contracts/=lib/openzeppelin-contracts/contracts/",
":@uniswap/=lib/contracts/node_modules/@uniswap/",
":Permit2/=lib/contracts/lib/Permit2/",
":account-abstraction/=lib/wallet-contracts-v3/lib/account-abstraction/contracts/",
":celer-network/=lib/contracts/lib/sgn-v2-contracts/",
":centre-tokens.git/=lib/evm-cctp-contracts/lib/centre-tokens.git/",
":contracts/=lib/contracts/src/",
":create3-factory/=lib/contracts/lib/create3-factory/",
":ds-test/=lib/evm-cctp-contracts/lib/ds-test/src/",
":erc2470-libs/=lib/erc2470-libs/",
":erc4626-tests/=lib/openzeppelin-contracts/lib/erc4626-tests/",
":eth-gas-reporter/=lib/contracts/node_modules/eth-gas-reporter/",
":evm-cctp-contracts/=lib/evm-cctp-contracts/",
":forge-gas-snapshot/=lib/contracts/lib/Permit2/lib/forge-gas-snapshot/src/",
":forge-std/=lib/forge-std/src/",
":halmos-cheatcodes/=lib/openzeppelin-contracts/lib/halmos-cheatcodes/src/",
":lifi-contracts/=lib/contracts/src/",
":lifi/=lib/contracts/src/",
":memview-sol/=lib/evm-cctp-contracts/lib/memview-sol/contracts/",
":openzeppelin-contracts/=lib/openzeppelin-contracts/",
":openzeppelin/=lib/contracts/lib/openzeppelin-contracts/contracts/",
":permit2/=lib/contracts/lib/Permit2/src/",
":sgn-v2-contracts/=lib/contracts/lib/sgn-v2-contracts/contracts/",
":solady/=lib/contracts/lib/solady/src/",
":solmate/=lib/contracts/lib/solmate/src/",
":tstorish/=lib/tstorish/src/",
":wallet-contracts-v3/=lib/wallet-contracts-v3/src/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.