Verified contractExactMIT
Harbor_RouterV6
Address0xda5b8542d1202df00f8fe3d226bdd05f6c86c7c2
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.38
- Same code
1 more contract
Details
- Compiler
- solc 0.8.38+commit.1835d247
- Language
- solidity
- Optimization
- Yes, 200 runs
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,342,542
- Creation transaction
- 0x7ee8c053dc…67ec1d7372
contracts/Router.sol
// SPDX-License-Identifier: MIT
// -------------------
// Router Version: 6.0
// -------------------
pragma solidity 0.8.38;
// ERC20 Interface
interface iERC20 {
function balanceOf(address) external view returns (uint256);
}
// ROUTER Interface
interface iROUTER {
function depositWithExpiry(address, address, uint, string calldata, uint) external;
}
// Executor Interface
interface iExecutor {
function execute(address asset, uint256 amount, address target, bytes calldata payload) external payable returns (bytes memory);
}
// Harbor_RouterV6 is managed by XNode Vaults.
//
// V6 moves ERC-20 custody from the router to the vault EOAs. The router holds no
// token balance at rest and keeps no allowance ledger; it is a stateless,
// event-emitting pass-through. Vaults grant this router an ERC-20 allowance and
// every outbound is a `transferFrom` sourced from the calling vault.
//
// THE LOAD-BEARING SECURITY INVARIANT:
// No function may `transferFrom` any address other than `msg.sender`.
// Because vaults hold MAX allowances to this router, any deviation turns a router
// bug into a vault drain. Every `_safeTransferFrom` below passes `msg.sender` as
// the token owner, so an attacker calling any public function moves only their
// own funds. Preserve this property in any change to this contract.
//
// Event signatures for Deposit, TransferOut and TransferAllowance are byte-identical
// to Harbor_RouterV5, so the same topic0 hashes are produced and vaultd observes
// both routers concurrently through the V5→V6 migration with no parser changes.
contract Harbor_RouterV6 {
bytes4 private constant APPROVE_SIG = 0x095ea7b3; // approve(address,uint256)
bytes4 private constant TRANSFER_SIG = 0xa9059cbb; // transfer(address,uint256)
bytes4 private constant TRANSFER_FROM_SIG = 0x23b872dd; // transferFrom(address,address,uint256)
uint256 private constant _NOT_ENTERED = 1;
uint256 private constant _ENTERED = 2;
address private constant ETH_ASSET = address(0);
uint256 private _status;
// Immutable executor address
iExecutor public immutable executor;
// Emitted for all deposits, the memo distinguishes for swap, add, remove, donate etc
event Deposit(address indexed to, address indexed asset, uint amount, string memo);
// Emitted for all outgoing transfers, the vault dictates who sent it, memo used to track.
event TransferOut(address indexed vault, address indexed to, address asset, uint amount, string memo);
// Changes the spend allowance between vaults
event TransferAllowance(address indexed oldVault, address indexed newVault, address asset, uint amount, string memo);
// Emitted for all outgoing transferAndCalls via the executor.
event TransferOutAndCall(address indexed vault, address asset, uint256 assetAmount, address target, uint256 callValue, bytes callData, address refundAddress);
// Emitted when an ETH send fails and the funds are bounced back to the sender.
event TransferFailed(address indexed vault, address indexed to, address asset, uint amount, string memo);
modifier nonReentrant() {
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
_status = _ENTERED;
_;
_status = _NOT_ENTERED;
}
constructor(address _executor) {
require(_executor != address(0), "Harbor_Router: zero executor");
_status = _NOT_ENTERED;
executor = iExecutor(_executor);
}
//############################## DEPOSITS ##############################
// Deposit an asset with a memo. Funds move straight to the vault EOA — the
// router never takes custody. An expiration of 0 disables the deadline check.
// An ETH deposit to an address that refuses ETH bounces to the depositor,
// emitting TransferFailed before Deposit in the same transaction (upstream
// V6 behavior, kept). Vaults are EOAs and cannot refuse, and only vault
// recipients are credited, so the pair is only ever seen for non-vaults.
function depositWithExpiry(address payable vault, address asset, uint amount, string calldata memo, uint expiration) external payable nonReentrant {
if (expiration != 0) {
require(block.timestamp < expiration, "Harbor_Router: expired");
}
require(vault != address(this), "Harbor_Router: vault!=router");
_validateEthUsage(asset);
uint safeAmount = _executeTransfer(vault, asset, amount);
emit Deposit(vault, asset, safeAmount, memo);
}
//############################## ASSET TRANSFERS ##############################
// Any vault calls to transfer any asset to any recipient.
// ERC-20s are pulled from the calling vault, which must have approved this router.
function transferOut(address payable to, address asset, uint amount, string calldata memo) external payable nonReentrant {
_validateEthUsage(asset);
_executeTransfer(to, asset, amount);
emit TransferOut(msg.sender, to, asset, amount, memo);
}
// Batch outbound for mixed ETH/ERC-20 legs. The ETH legs must sum to exactly
// msg.value. Upstream clamps each leg to the ETH left over, which pays the
// tail legs zero when the supply is short and still passes the end check;
// every leg here is paid in full or the whole batch reverts. The final
// equality is what keeps ETH force-sent into the router (selfdestruct) from
// ever leaving it; the running check inside the loop fails an overdrawn
// batch at the first leg past msg.value, before that leg is sent.
// NOTE: included for future use — vaultd does not pack this call today.
function batchTransferOut(address payable[] calldata to, address[] calldata assets, uint[] calldata amounts, string[] calldata memos) external payable nonReentrant {
require(
to.length == assets.length &&
to.length == amounts.length &&
to.length == memos.length,
"Harbor_Router: length mismatch"
);
uint256 ethSpent = 0;
for (uint i = 0; i < to.length;) {
if (assets[i] == ETH_ASSET) {
ethSpent += amounts[i];
require(ethSpent <= msg.value, "Harbor_Router: eth amount mismatch");
_sendEth(to[i], amounts[i], memos[i]);
emit TransferOut(msg.sender, to[i], assets[i], amounts[i], memos[i]);
} else {
_safeTransferFrom(assets[i], amounts[i], to[i]);
emit TransferOut(msg.sender, to[i], assets[i], amounts[i], memos[i]);
}
unchecked { ++i; }
}
require(ethSpent == msg.value, "Harbor_Router: eth amount mismatch");
}
// Vault calls to execute an arbitrary call on a target via the executor contract.
// For ERC20: pulls tokens from the vault into the router, approves the executor,
// executor pulls them and calls the target.
// msg.value carries optional extra native value (call_value) forwarded alongside the token.
// For ETH (asset=0x0): msg.value IS the coin amount. The amount param is passed through
// to the event for observability but the contract uses msg.value for the actual transfer.
// The executor is a separate contract so msg.sender inside the target is the executor,
// not the router — preventing the target from exercising vault approvals on the router.
// The router's token balance is back to zero before the target is ever called, so a
// hostile target cannot reach transient router funds.
function transferOutAndCall(
address payable target,
address asset,
uint amount,
bytes calldata callData,
address payable refundAddress
) external payable nonReentrant {
uint256 _callValue = msg.value;
// If an ERC20 asset is specified, pull it from the vault and approve the executor
if (asset != ETH_ASSET && amount > 0) {
uint received = _safeTransferFrom(asset, amount, address(this));
// The executor is approved and the refund path pays out the requested
// amount, so a short receipt would leave both under-funded. Fee-on-transfer
// and rebasing tokens are not supported on the call path.
require(received == amount, "Harbor_Router: transfer amount mismatch");
_safeApprove(asset, address(executor), amount);
}
// Delegate the call to the executor contract
(bool success, ) = address(executor).call{value: _callValue}(
abi.encodeWithSelector(iExecutor.execute.selector, asset, amount, target, callData)
);
if (!success) {
require(refundAddress != address(0), "Harbor_Router: zero refund address");
if (_callValue > 0) {
(bool ethSuccess, ) = refundAddress.call{value: _callValue}("");
require(ethSuccess, "Harbor_Router: ETH refund failed");
}
if (asset != ETH_ASSET && amount > 0) {
(bool transferSuccess, bytes memory transferData) = asset.call(abi.encodeWithSelector(TRANSFER_SIG, refundAddress, amount));
require(transferSuccess && (transferData.length == 0 || abi.decode(transferData, (bool))));
}
}
// Revoke any leftover ERC20 approval to executor
if (asset != ETH_ASSET && amount > 0) {
_safeApprove(asset, address(executor), 0);
}
// For ETH, emit msg.value as the amount so observers see the real coin amount.
uint256 emitAmount = (asset == ETH_ASSET) ? msg.value : amount;
emit TransferOutAndCall(msg.sender, asset, emitAmount, target, _callValue, callData, refundAddress);
}
//############################## ALLOWANCE TRANSFERS ##############################
// Use for churning ERC20s (not ETH) to a new vault, as well as router migrations.
// A router is needed to emit a txOut memo at some point.
function transferAllowance(address router, address newVault, address asset, uint amount, string calldata memo) external nonReentrant {
require(asset != ETH_ASSET, "Harbor_Router: ETH unsupported");
if (router == address(this)) {
// Same-router churn: move the tokens vault -> vault directly.
_safeTransferFrom(asset, amount, newVault);
emit TransferAllowance(msg.sender, newVault, asset, amount, memo);
} else {
// Router migration: take custody, then deposit into the new router so it
// emits the memo. vaultd observes the Deposit event on the new router.
uint safeAmount = _safeTransferFrom(asset, amount, address(this));
_safeApprove(asset, router, safeAmount);
iROUTER(router).depositWithExpiry(newVault, asset, safeAmount, memo, type(uint).max);
// Clear any residue so a partially-consuming router cannot leave a standing
// allowance, and so approve-once tokens (USDT) accept the next migration.
_safeApprove(asset, router, 0);
}
}
//############################## HELPERS ##############################
// Backwards-compatible balance view. V5 read a router-held allowance ledger;
// under V6 the vault holds the tokens, so this proxies balanceOf. Keeping the
// signature lets vaultd's balance and reconciliation reads span both routers
// unchanged through the migration.
function vaultAllowance(address vault, address token) external view returns(uint amount) {
return iERC20(token).balanceOf(vault);
}
function _executeTransfer(address payable to, address asset, uint amount) internal returns (uint transferredAmount) {
if (asset == ETH_ASSET) {
require(msg.value == amount, "Harbor_Router: eth amount mismatch");
_sendEth(to, msg.value, "");
transferredAmount = msg.value;
} else {
transferredAmount = _safeTransferFrom(asset, amount, to);
}
return transferredAmount;
}
// Safe transferFrom in case asset charges transfer fees. Always sources from
// msg.sender — see the invariant at the top of this contract.
function _safeTransferFrom(address _asset, uint _amount, address _to) internal returns(uint amount) {
uint _startBal = iERC20(_asset).balanceOf(_to);
(bool success, bytes memory data) = _asset.call(abi.encodeWithSelector(TRANSFER_FROM_SIG, msg.sender, _to, _amount));
require(success && (data.length == 0 || abi.decode(data, (bool))), "Harbor_Router: transfer failed");
return (iERC20(_asset).balanceOf(_to) - _startBal);
}
// Safe approve for ERC20 tokens (handles non-standard tokens that return no data)
function _safeApprove(address _asset, address _spender, uint _amount) internal {
(bool success, bytes memory data) = _asset.call(abi.encodeWithSelector(APPROVE_SIG, _spender, _amount));
require(success && (data.length == 0 || abi.decode(data, (bool))), "Harbor_Router: approve failed");
}
// Recipients of ETH are given 30,000 gas to complete execution (enough for
// GnosisSafe and other complex multisigs). On failure the ETH is bounced back
// to the caller and TransferFailed is emitted alongside the caller's own event.
function _sendEth(address payable to, uint256 amount, string memory memo) internal {
(bool success, ) = to.call{value: amount, gas: 30000}("");
if (!success) {
(bool refundSuccess, ) = payable(msg.sender).call{value: amount}("");
require(refundSuccess, "Harbor_Router: ETH bounce failed");
emit TransferFailed(msg.sender, to, ETH_ASSET, amount, memo);
}
}
// ERC20 paths must not carry ETH, which would otherwise be locked in the router.
function _validateEthUsage(address asset) internal view {
if (asset != ETH_ASSET) {
require(msg.value == 0, "Harbor_Router: unexpected eth");
}
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": true
},
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.