AssetsRouter
Inherits: IAssetsRouter, AccessControl
Author: luoyhang003
Routes incoming ERC20 token transfers to a designated recipient address.
*Supports both automatic routing to the active recipient or manual routing by operators. Access is controlled via roles:
- DEFAULT_ADMIN_ROLE: manages role assignments
- ROUTER_ADMIN_ROLE: can add/remove route recipients
- ROUTER_OPERATOR_ROLE: can change active recipient, enable/disable auto-route, or manually route assets*
State Variables
ROUTER_ADMIN_ROLE
Role identifier for router admins (manage recipients list).
Only addresses with this role can call addRouteRecipient and removeRouteRecipient
Calculated as keccak256("ROUTER_ADMIN_ROLE").
bytes32 public constant ROUTER_ADMIN_ROLE = keccak256("ROUTER_ADMIN_ROLE");
ROUTER_OPERATOR_ROLE
Role identifier for router operators (manage routing settings).
Only addresses with this role can call setActiveRecipient, setAutoRouteEnabled and manualRoute
Calculated as keccak256("ROUTER_OPERATOR_ROLE").
bytes32 public constant ROUTER_OPERATOR_ROLE = keccak256("ROUTER_OPERATOR_ROLE");
_routeRecipients
List of approved route recipients.
address[] private _routeRecipients;
_isRouteRecipient
Mapping for quick lookup of approved recipients.
mapping(address => bool) private _isRouteRecipient;
_activeRecipient
The currently active recipient address where tokens are routed automatically.
address private _activeRecipient;
_isAutoRouteEnabled
Flag indicating whether automatic routing is enabled.
bool private _isAutoRouteEnabled;
Functions
constructor
Deploys the AssetsRouter contract.
constructor(
address _defaultAdmin,
address _adminRole,
address _operatorRole,
address[] memory _recipients,
address _active
);
Parameters
| Name | Type | Description |
|---|---|---|
_defaultAdmin | address | Address assigned as DEFAULT_ADMIN_ROLE. |
_adminRole | address | Address granted the ROUTER_ADMIN_ROLE. |
_operatorRole | address | Address granted the ROUTER_OPERATOR_ROLE. |
_recipients | address[] | Initial list of valid route recipients. |
_active | address | The active recipient address for auto-routing. |
route
Deposit tokens into the router and optionally forward them to the active recipient.
Pulls tokens from msg.sender into this contract.
If auto-route is enabled, immediately forwards them to _activeRecipient.
function route(address _token, uint256 _amount) external;
Parameters
| Name | Type | Description |
|---|---|---|
_token | address | The ERC20 token address being routed. |
_amount | uint256 | The amount of tokens to transfer. |
getRouteRecipients
Returns the list of approved route recipients.
Returns the full list of route recipients currently registered in the router.
function getRouteRecipients() external view returns (address[] memory routeRecipients_);
Returns
| Name | Type | Description |
|---|---|---|
routeRecipients_ | address[] | The array of all registered recipient addresses. |
isRouteRecipient
Checks if an address is a valid route recipient.
Checks if a given address is a registered route recipient.
function isRouteRecipient(address _addr) external view returns (bool);
Parameters
| Name | Type | Description |
|---|---|---|
_addr | address | The address to check. |
Returns
| Name | Type | Description |
|---|---|---|
<none> | bool | True if the address is a valid route recipient, otherwise false. |
getActiveRecipient
Returns the currently active route recipient.
Returns the currently active route recipient where assets are routed automatically.
function getActiveRecipient() external view returns (address activeRecipient_);
Returns
| Name | Type | Description |
|---|---|---|
activeRecipient_ | address | The address of the active route recipient. |
isAutoRouteEnabled
Returns whether auto-routing is enabled.
Indicates whether automatic routing is currently enabled.
function isAutoRouteEnabled() external view returns (bool enabled_);
Returns
| Name | Type | Description |
|---|---|---|
enabled_ | bool | Boolean flag, true if auto-routing is enabled, false otherwise. |
addRouteRecipient
Add a new valid route recipient.
Callable only by ROUTER_ADMIN_ROLE.
function addRouteRecipient(address _recipient) external onlyRole(ROUTER_ADMIN_ROLE);
Parameters
| Name | Type | Description |
|---|---|---|
_recipient | address | The address to add as a route recipient. |
removeRouteRecipient
Remove an existing route recipient.
Cannot remove the currently active recipient.
function removeRouteRecipient(address _recipient) external onlyRole(ROUTER_ADMIN_ROLE);
Parameters
| Name | Type | Description |
|---|---|---|
_recipient | address | The address to remove from the recipients list. |
setActiveRecipient
Set the active recipient for auto-routing.
Callable only by ROUTER_OPERATOR_ROLE.
function setActiveRecipient(address _active) external onlyRole(ROUTER_OPERATOR_ROLE);
Parameters
| Name | Type | Description |
|---|---|---|
_active | address | The address to set as the active recipient. |
setAutoRouteEnabled
Enable or disable automatic routing.
Callable only by ROUTER_OPERATOR_ROLE.
function setAutoRouteEnabled(bool _enabled) external onlyRole(ROUTER_OPERATOR_ROLE);
Parameters
| Name | Type | Description |
|---|---|---|
_enabled | bool | Boolean flag to set auto-routing state. |
manualRoute
Manually route tokens to a specified recipient.
Callable only by ROUTER_OPERATOR_ROLE.
function manualRoute(address _token, address _recipient, uint256 _amount) external onlyRole(ROUTER_OPERATOR_ROLE);
Parameters
| Name | Type | Description |
|---|---|---|
_token | address | The ERC20 token address to route. |
_recipient | address | The destination recipient address. |
_amount | uint256 | The amount of tokens to transfer. |