AssetsRouter

Git Source

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

NameTypeDescription
_defaultAdminaddressAddress assigned as DEFAULT_ADMIN_ROLE.
_adminRoleaddressAddress granted the ROUTER_ADMIN_ROLE.
_operatorRoleaddressAddress granted the ROUTER_OPERATOR_ROLE.
_recipientsaddress[]Initial list of valid route recipients.
_activeaddressThe 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

NameTypeDescription
_tokenaddressThe ERC20 token address being routed.
_amountuint256The 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

NameTypeDescription
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

NameTypeDescription
_addraddressThe address to check.

Returns

NameTypeDescription
<none>boolTrue 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

NameTypeDescription
activeRecipient_addressThe 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

NameTypeDescription
enabled_boolBoolean 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

NameTypeDescription
_recipientaddressThe 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

NameTypeDescription
_recipientaddressThe 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

NameTypeDescription
_activeaddressThe 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

NameTypeDescription
_enabledboolBoolean 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

NameTypeDescription
_tokenaddressThe ERC20 token address to route.
_recipientaddressThe destination recipient address.
_amountuint256The amount of tokens to transfer.