Rebalancer

Git Source

Inherits: IRebalancer, HypernativeFirewallProtected

Title: Cross-chain rebalancer

Author: Malda Protocol

Manages bridge interactions and transfer size limits for cross-chain rebalancing.

State Variables

roles

Roles contract used for access control.

IRoles public roles

nonce

Incremental nonce used for logging messages.

uint256 public nonce

logs

Sent messages indexed by destination chain and nonce.

mapping(uint32 dstChainId => mapping(uint256 msgNonce => Msg message)) public logs

whitelistedBridges

Bridge whitelist status.

mapping(address bridge => bool status) public whitelistedBridges

allowedTokensPerBridge

Allowed tokens per bridge.

mapping(address bridge => mapping(address token => bool status)) public allowedTokensPerBridge

whitelistedDestinations

Destination chain whitelist status.

mapping(uint32 dstChainId => bool status) public whitelistedDestinations

allowedList

Markets allowed for rebalancing.

mapping(address market => bool status) public allowedList

admin

Admin address with elevated permissions.

address public admin

saveAddress

Address used to sweep saved assets.

address public saveAddress

maxTransferSizes

Per-chain token maximum transfer size.

mapping(uint32 dstChainId => mapping(address token => uint256 limit)) public maxTransferSizes

minTransferSizes

Per-chain token minimum transfer size.

mapping(uint32 dstChainId => mapping(address token => uint256 limit)) public minTransferSizes

currentTransferSize

Rolling transfer info for size-window enforcement.

mapping(uint32 dstChainId => mapping(address token => TransferInfo info)) public currentTransferSize

whitelistedMarkets

Market whitelist status.

mapping(address market => bool status) public whitelistedMarkets

transferTimeWindow

Duration of the rolling transfer size window.

uint256 public transferTimeWindow

Functions

constructor

Initializes the Rebalancer.

constructor(address _roles, address _saveAddress, address _admin, bytes memory initData) ;

Parameters

NameTypeDescription
_rolesaddressRoles contract.
_saveAddressaddressAddress to sweep saved assets to.
_adminaddressAdmin address.
initDatabytesOptional initialization data (InitInfo ABI-encoded).

initFirewall

Initialize firewall.

function initFirewall(address _firewall) external;

Parameters

NameTypeDescription
_firewalladdressFirewall address.

setAdmin

Set admin.

function setAdmin(address _account) external;

Parameters

NameTypeDescription
_accountaddressAdmin address.

setSaveAddress

Set save address.

function setSaveAddress(address _save) external;

Parameters

NameTypeDescription
_saveaddressSave address.

setAllowedTokens

Set allowed tokens for a bridge.

function setAllowedTokens(address bridge, address[] calldata tokens, bool status)
    external
    onlyFirewallApprovedAllowEOA;

Parameters

NameTypeDescription
bridgeaddressBridge address.
tokensaddress[]Token list to allow/disallow.
statusboolAllowance status.

setMarketStatus

Batch whitelist/unwhitelist markets.

function setMarketStatus(address[] calldata list, bool status) external onlyFirewallApprovedAllowEOA;

Parameters

NameTypeDescription
listaddress[]Market addresses.
statusboolWhitelist status.

setAllowList

Batch set allow-list status for markets.

function setAllowList(address[] calldata list, bool status) external onlyFirewallApprovedAllowEOA;

Parameters

NameTypeDescription
listaddress[]Market addresses.
statusboolAllow list status.

setWhitelistedBridgeStatus

Set whitelist status for a bridge.

function setWhitelistedBridgeStatus(address _bridge, bool _status) external onlyFirewallApprovedAllowEOA;

Parameters

NameTypeDescription
_bridgeaddressBridge address.
_statusboolWhitelist status.

setWhitelistedDestination

Set whitelist status for a destination chain.

function setWhitelistedDestination(uint32 _dstId, bool _status) external onlyFirewallApprovedAllowEOA;

Parameters

NameTypeDescription
_dstIduint32Destination chain id.
_statusboolWhitelist status.

saveEth

Sweep native ETH to the configured save address.

function saveEth() external onlyFirewallApprovedAllowEOA;

saveTokens

Sweep stray tokens to the given market.

function saveTokens(address token, address market) external;

Parameters

NameTypeDescription
tokenaddressToken address to sweep.
marketaddressMarket to receive tokens.

setMinTransferSize

Sets the minimum transfer size for a given destination and token.

function setMinTransferSize(uint32 _dstChainId, address _token, uint256 _limit)
    external
    onlyFirewallApprovedAllowEOA;

Parameters

NameTypeDescription
_dstChainIduint32Destination chain id.
_tokenaddressToken address.
_limituint256Minimum transfer size.

setMaxTransferSize

Sets the maximum transfer size for a given destination and token.

function setMaxTransferSize(uint32 _dstChainId, address _token, uint256 _limit)
    external
    onlyFirewallApprovedAllowEOA;

Parameters

NameTypeDescription
_dstChainIduint32Destination chain id.
_tokenaddressToken address.
_limituint256Maximum transfer size.

sendMsg

Sends a bridge message.

function sendMsg(address _bridge, address _market, uint256 _amount, Msg calldata _msg)
    external
    payable
    onlyFirewallApprovedAllowEOA;

Parameters

NameTypeDescription
_bridgeaddress
_marketaddressThe market to rebalance from address.
_amountuint256The amount to rebalance.
_msgMsgThe message data.

isMarketWhitelisted

Returns if a market is whitelisted.

function isMarketWhitelisted(address market) external view returns (bool);

Parameters

NameTypeDescription
marketaddressMarket address.

Returns

NameTypeDescription
<none>boolwhitelisted True if whitelisted.

isBridgeWhitelisted

Returns if a bridge implementation is whitelisted.

function isBridgeWhitelisted(address bridge) external view returns (bool);

Parameters

NameTypeDescription
bridgeaddressBridge address.

Returns

NameTypeDescription
<none>boolwhitelisted True if whitelisted.

isDestinationWhitelisted

Returns if a destination is whitelisted.

function isDestinationWhitelisted(uint32 dstId) external view returns (bool);

Parameters

NameTypeDescription
dstIduint32Destination chain ID.

Returns

NameTypeDescription
<none>boolwhitelisted True if whitelisted.

firewallRegister

Registers an account with the firewall.

function firewallRegister(address _account) public override(HypernativeFirewallProtected);

Parameters

NameTypeDescription
_accountaddressAccount to register.

_initFromData

Initializes the Rebalancer from initialization data.

function _initFromData(bytes memory initData) internal;

Parameters

NameTypeDescription
initDatabytesInitialization data.

_initMarkets

Initializes the markets.

function _initMarkets(address[] memory markets) internal;

Parameters

NameTypeDescription
marketsaddress[]Markets to initialize.

_initBridges

Initializes the bridges.

function _initBridges(address[] memory bridges) internal;

Parameters

NameTypeDescription
bridgesaddress[]Bridges to initialize.

_initDestinations

Initializes the destinations.

function _initDestinations(uint32[] memory destinations) internal;

Parameters

NameTypeDescription
destinationsuint32[]Destinations to initialize.

_initBridgeTokens

Initializes the bridge tokens.

function _initBridgeTokens(BridgeTokens[] memory bridgeTokens) internal;

Parameters

NameTypeDescription
bridgeTokensBridgeTokens[]Bridge tokens to initialize.

_sendMsgPreChecks

Checks the pre-conditions for sending a message.

function _sendMsgPreChecks(address _bridge, address _market, uint256 _amount, Msg calldata _msg) internal;

Parameters

NameTypeDescription
_bridgeaddressBridge address.
_marketaddressMarket address.
_amountuint256Amount to send.
_msgMsgMessage data.

_updateTransferWindowAndCheckMax

Updates the transfer window and checks the maximum transfer size.

function _updateTransferWindowAndCheckMax(uint256 amount, uint32 dstChainId, address token) internal;

Parameters

NameTypeDescription
amountuint256Amount to send.
dstChainIduint32Destination chain id.
tokenaddressToken address.

_setMarketStatus

Sets the market status.

function _setMarketStatus(address[] memory list, bool status) internal;

Parameters

NameTypeDescription
listaddress[]Market addresses.
statusboolMarket status.

_setAllowList

Sets the allow list.

function _setAllowList(address[] memory list, bool status) internal;

Parameters

NameTypeDescription
listaddress[]Market addresses.
statusboolAllow list status.

_setWhitelistedBridgeStatus

Sets the whitelisted bridge status.

function _setWhitelistedBridgeStatus(address _bridge, bool _status) internal;

Parameters

NameTypeDescription
_bridgeaddressBridge address.
_statusboolWhitelisted bridge status.

_setWhitelistedDestination

Sets the whitelisted destination.

function _setWhitelistedDestination(uint32 _dstId, bool _status) internal;

Parameters

NameTypeDescription
_dstIduint32Destination chain id.
_statusboolWhitelisted destination status.

_setAllowedTokens

Sets the allowed tokens.

function _setAllowedTokens(address bridge, address[] memory tokens, bool status) internal;

Parameters

NameTypeDescription
bridgeaddressBridge address.
tokensaddress[]Token addresses.
statusboolAllowed tokens status.

Structs

TransferInfo

struct TransferInfo {
    uint256 size;
    uint256 timestamp;
}

InitInfo

struct InitInfo {
    BridgeTokens[] bridgeTokens;
    address[] markets;
    address[] bridges;
    uint32[] destinations;
}

BridgeTokens

struct BridgeTokens {
    address bridge;
    address[] tokens;
}