LiquidityManager
Handles liquidity deposits and withdrawals for the ERC20 bridge
Adds ERC20 liquidity to an existing pool or creates a new one, if it does not exist yet
function addLiquidityERC20(IERC20 token, uint256 amount) external override nonReentrant {
// check ERC20 bridge token balance before deposit
uint256 balanceBefore = token.balanceOf(address(bridgeERC20));
// Transfer token amount from sender to ERC20bridge
token.safeTransferFrom(_msgSender(), address(bridgeERC20), amount);
// check if ERC20 bridge token balance has increased by amount (>> effectively preventing deflationary tokens to be added for now)
if (token.balanceOf(address(bridgeERC20)) != balanceBefore + amount) revert OperationFailed();
// call internal function to mint LP tokens and emit event
_addLiquidityERC20(token, amount);
// forward the minted LP tokens to msg_sender()
IERC20(address(lpTokens[address(token)].token)).safeTransfer(_msgSender(), amount);
}
Name | Type | Description |
---|---|---|
token | IERC20 | the address of the token for which liquidity should be added |
amount | uint256 | the amount of tokens to be added |
function addLiquidityNative(uint256 amount) external payable override nonReentrant {
// check input parameters
if (amount != msg.value) revert InvalidMessageValue(msg.value, amount);
// check native token balance before call
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();
// Transfer ERC20 tokens from this contract to ERC20bridge
IERC20(wrappedNative).safeTransfer(address(bridgeERC20), amount);
// call addLiquidityERC20 function with wrapped native token
_addLiquidityERC20(wrappedNative, amount);
// transfer LP tokens from this contract to msg.sender to complete the transaction
lpTokens[address(wrappedNative)].token.safeTransfer(_msgSender(), amount);
}
Name | Type | Description |
---|---|---|
amount | uint256 | the amount of tokens to be added |
function withdrawLiquidityERC20(IERC20 token, uint256 amount) external override nonReentrant {
// burn LP token in the amount of liquidity that should be removed
ERC20Burnable(address(lpTokens[address(token)].token)).burnFrom(_msgSender(), amount);
// call internal function to calculate fee and emit event
uint256 withdrawalAmount = _withdrawLiquidityERC20(token, amount);
// safely transfer token amount minus fees to the sender
token.safeTransferFrom(address(bridgeERC20), _msgSender(), withdrawalAmount);
}
Name | Type | Description |
---|---|---|
token | IERC20 | the address of the token for which liquidity should be removed |
amount | uint256 | the amount of tokens to be added |
function withdrawLiquidityNative(uint256 withdrawalAmount) external override nonReentrant {
// check if liquidity is sufficient
if (wrappedNative.balanceOf(address(bridgeERC20)) < withdrawalAmount) revert InvalidBalance();
// transfer LP tokens from msgSender to this contract
lpTokens[address(wrappedNative)].token.safeTransferFrom(_msgSender(), address(this), withdrawalAmount);
// check wrapped native token balance before ERC20 withdrawal
uint256 contractBalance = wrappedNative.balanceOf(address(this));
// withdraw ERC20 liquidity from bridge
uint256 withdrawalAmountAfterFees = _withdrawLiquidityERC20(wrappedNative, withdrawalAmount);
// transfer ERC20 tokens (minus fees) from bridge to this contract
IERC20(wrappedNative).safeTransferFrom(address(bridgeERC20), address(this), withdrawalAmountAfterFees);
// check wrapped native token balance after ERC20 withdrawal
if (wrappedNative.balanceOf(address(this)) != contractBalance + withdrawalAmountAfterFees) revert OperationFailed();
// check native token balance before call
contractBalance = address(this).balance;
// swap wrapped native ERC20 token back to native token
wrappedNative.withdraw(withdrawalAmountAfterFees);
// check if native token balance has increased by withdraw amount
if (address(this).balance != contractBalance + withdrawalAmountAfterFees) revert OperationFailed();
// send native token back to user
payable(_msgSender()).transfer(withdrawalAmountAfterFees);
}
Name | Type | Description |
---|---|---|
withdrawalAmount | uint256 | the amount of tokens to be added |
Last modified 11mo ago