Links

CrossChainBridgeERC20

Manages deposits and releases for ERC20 bridge transactions

Functions

burnLpTokenForWithdrawalInOtherNetwork

Burns LP tokens and creates a bridge deposit in the amount of "burnedLPtokens - liquidity withdrawal fee". This function was introduced to allow liquidity withdrawals even if a network's current liquidity is insufficient for a withdrawal.
function burnLpTokenForWithdrawalInOtherNetwork(
IERC20 token,
uint256 amount,
address receiverAddress,
uint256 targetChainId
) public nonReentrant deactivateSwitch returns (uint256 depositAmount) {
// get LP token for token address provided
(, IERC20 lpToken) = liquidityManager.lpTokens(address(token));
// calculate withdrawal fee and deposit amount
// determine the fee rate to be used for this transaction (usually default liquidity withdrawal fee)
uint256 liquidityWithdrawalFee = liquidityManager.defaultLiquidityWithdrawalFee();
// if a specific fee rate is stored for this particular release token then we use this rate instead
if (liquidityManager.liquidityWithdrawalFees(address(token)) > 0) {
liquidityWithdrawalFee = liquidityManager.liquidityWithdrawalFees(address(token));
}
// calculate deposit amount
depositAmount = amount;
if (liquidityWithdrawalFee > 0) {
uint256 withdrawalFeeAmount = (amount * liquidityWithdrawalFee) / 1000000;
depositAmount = amount - withdrawalFeeAmount;
// transfer developer account fee, if applicable
if (withdrawalFeeAmount > 0) {
// transfer LP token in amount of withdrawal fee to dev account
lpToken.safeTransferFrom(_msgSender(), liquidityManager.devAddr(), withdrawalFeeAmount);
}
}
// burn LP token from user (must have approval from user)
ERC20Burnable(address(lpToken)).burnFrom(_msgSender(), depositAmount);
// call internal deposit function to check parameters and create event
_depositERC20(token, depositAmount, receiverAddress, targetChainId);
// emit event
emit LiquidityDeposited(address(token), depositAmount, receiverAddress, _getChainID(), targetChainId, depositCount);
return depositAmount;
}
// returns the ID of the network this contract is deployed in
function _getChainID() private view returns (uint256) {
uint256 id;
assembly {
id := chainid()
}
return id;
}

Parameters:

Name
Type
Description
token
IERC20
the ERC20 token in which liquidity was provided
amount
uint256
the amount to be withdrawn
receiverAddress
address
the address which should receive the bridged funds in the target network
targetChainId
uint256
chain ID of the target network

Return value:

Name
Type
Description
depositAmount
uint256
the amount that was deposited (= withdrawalAmount - fee)

depositERC20

Accepts ERC20 token deposits that should be bridged into another network (effectively starting a new bridge transaction)
function depositERC20(
IERC20 token,
uint256 amount,
address receiverAddress,
uint256 targetChainId
) public nonReentrant {
// check token balance before deposit
uint256 balanceBefore = token.balanceOf(address(this));
// Transfer to-be-deposited tokens from sender to this smart contract
token.safeTransferFrom(_msgSender(), address(this), amount);
// check if token balance has increased by amount (>> effectively preventing deflationary tokens to be added for now)
if (token.balanceOf(address(this)) != balanceBefore + amount) revert OperationFailed();
// call internal function that assigns depositID and emits event
_depositERC20(token, amount, receiverAddress, targetChainId);
}

Parameters:

Name
Type
Description
token
IERC20
the ERC20 token that should be deposited
amount
uin256
the amount to be deposited
receiverAddress
address
the address which should receive the bridged funds in the target network
targetChainId
uint256
chain ID of the target network

depositNative

Accepts native token (e.g. ETH, BNB, MATIC) deposits that should be bridged into another network (effectively starting a new bridge transaction)
function depositNative(
uint256 amount,
address receiverAddress,
uint256 targetChainId
) external payable nonReentrant {
// check input parameters
if (amount != msg.value) revert InvalidMessageValue(msg.value, amount);
// check native token balance before swap
uint256 contractBalance = wrappedNative.balanceOf(address(this));
// swap native to wrapped native ERC20 token
wrappedNative.deposit{value: msg.value}();
// check if wrapped native token balance has increased by deposit amount
if (wrappedNative.balanceOf(address(this)) != contractBalance + amount) revert OperationFailed();
// call depositERC20 function with wrapped native token to complete the deposit
_depositERC20(wrappedNative, amount, receiverAddress, targetChainId);
}

Parameters:

Name
Type
Description
amount
uint256
the amount that is being deposited
receiverAddress
address
the address which should receive the bridged funds in the target network
targetChainId
uint256
chain ID of the target network

forwardCollectedFees

In order to save gas, we introduced periodically fee transfers. This means that our bridge will collect fees (which should be sent to other contracts) and only periodically forwards them to their owners. Fees collected for RewardPools and LiquidityMiningPools are roughly forwarded every 24/48hrs and fees collected for BuyBackAndBurn are only forwarded on-demand (when a buyBackAndBurn is triggered). This function allows to trigger an immediate transfer of these fees.
function forwardCollectedFees(
IERC20 token,
bool _buyBackAndBurn,
bool _miningPools,
bool _rewardPools
) public nonReentrant {
if (token.balanceOf(address(this)) > 0) {
if (_buyBackAndBurn) {
buyBackAndBurn.depositERC20(token, collectedUnsentFees[address(token)][address(buyBackAndBurn)]);
collectedUnsentFees[address(token)][address(buyBackAndBurn)] = 0;
}
if (_miningPools) {
liquidityMiningPools.addRewards(token, collectedUnsentFees[address(token)][address(liquidityMiningPools)]);
collectedUnsentFees[address(token)][address(liquidityMiningPools)] = 0;
}
if (_rewardPools) {
rewardPools.addRewards(token, collectedUnsentFees[address(token)][address(rewardPools)]);
collectedUnsentFees[address(token)][address(rewardPools)] = 0;
}
}
}

Parameters:

Name
Type
Description
token
IERC20
the token in which fees were collected
_buyBackAndBurn
bool
true if collected fees should be sent to BuyBackAndBurn
_miningPools
bool
true if collected fees should be sent to LiquidityMiningPools
_rewardPools
bool
true if collected fees should be sent to RewardPools

releaseERC20

Releases ERC20 tokens in this network if a valid deposit was made in another network (effectively completing a bridge transaction)
function releaseERC20(
uint8[] memory sigV,
bytes32[] memory sigR,
bytes32[] memory sigS,
address receiverAddress,
address sourceNetworkTokenAddress,
uint256 amount,
uint256 depositChainId,
uint256 depositNumber
) external nonReentrant {
// forward call internally to private function with "false" parameter (=send ERC20 tokens to _msgSender())
_releaseERC20(
sigV,
sigR,
sigS,
receiverAddress,
sourceNetworkTokenAddress,
amount,
depositChainId,
depositNumber,
false
);
}

Parameters:

Name
Type
Description
sigV
uint8
Array of recovery Ids for the signature
sigR
bytes32
Array of R values of the signatures
sigS
bytes32
Array of S values of the signatures
receiverAddress
address
the address which should receive the bridged funds in this network
sourceNetworkTokenAddress
address
the address of the ERC20 contract in the network the deposit was made
amount
uint256
The amount of tokens to be released
depositChainId
uint256
chain ID of the network in which the deposit was made
depositNumber
uint256
The deposit ID of the corresponding deposit