Skip to content
This repository has been archived by the owner on Dec 5, 2021. It is now read-only.

Commit

Permalink
Merge pull request #28 from enyalabs/inomurko/safe_transfer_erc20
Browse files Browse the repository at this point in the history
use zeppelin safe transfer stuff
  • Loading branch information
InoMurko authored May 22, 2021
2 parents 3ff2bc5 + b3f7da7 commit 76dd67b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 109 deletions.
2 changes: 1 addition & 1 deletion contracts/ERC20.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity >=0.6.0 <0.8.0;
import { IERC20 } from "./interfaces/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

/**
* @title ERC20
Expand Down
38 changes: 19 additions & 19 deletions contracts/L1LiquidityPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,20 @@
pragma solidity >0.5.0;
pragma experimental ABIEncoderV2;

import "./interfaces/IERC20.sol";
import { iL2LiquidityPool } from "./interfaces/iL2LiquidityPool.sol";

/* Library Imports */
import { OVM_CrossDomainEnabled } from "enyalabs_contracts/build/contracts/libraries/bridge/OVM_CrossDomainEnabled.sol";

/* External Imports */
import '@openzeppelin/contracts/math/SafeMath.sol';

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
/**
* @dev An L1 LiquidityPool implementation
*/
contract L1LiquidityPool is OVM_CrossDomainEnabled {
using SafeERC20 for IERC20;
using SafeMath for uint256;
uint256 constant internal SAFE_GAS_STIPEND = 2300;

Expand All @@ -32,7 +33,7 @@ contract L1LiquidityPool is OVM_CrossDomainEnabled {
// this is to stop attacks where caller specifies l2contractaddress
// also acts as a whitelist
mapping(address => address) l2ContractAddress;

address owner;
address l2LiquidityPoolAddress;
uint256 fee;
Expand Down Expand Up @@ -132,7 +133,7 @@ contract L1LiquidityPool is OVM_CrossDomainEnabled {
// use with caution, can register only once
require(l2ContractAddress[_erc20L1ContractAddress] == address(0), "Token Address Already Registerd");
l2ContractAddress[_erc20L1ContractAddress] = _erc20L2ContractAddress;
}
}

/**
* @dev Receive ETH
Expand Down Expand Up @@ -236,14 +237,14 @@ contract L1LiquidityPool is OVM_CrossDomainEnabled {
function ownerAddERC20Liquidity(
uint256 _amount,
address _erc20L1ContractAddress
)
)
external
onlyOwner()
{
onlyOwner()
{
require(l2ContractAddress[_erc20L1ContractAddress] != address(0), "Token L2 address not registered");
IERC20 erc20Contract = IERC20(_erc20L1ContractAddress);
require(_amount <= erc20Contract.allowance(msg.sender, address(this)));
require(erc20Contract.transferFrom(msg.sender, address(this), _amount), "ERC20 token transfer was unsuccessful");
erc20Contract.safeTransferFrom(msg.sender, address(this), _amount);

// balances[_erc20L1ContractAddress] = balances[_erc20L1ContractAddress].add(_amount);

Expand All @@ -264,12 +265,12 @@ contract L1LiquidityPool is OVM_CrossDomainEnabled {
address _erc20L1ContractAddress
)
external
{
{
require(l2ContractAddress[_erc20L1ContractAddress] != address(0), "Token L2 address not registered");
IERC20 erc20Contract = IERC20(_erc20L1ContractAddress);
require(_amount <= erc20Contract.allowance(msg.sender, address(this)));
require(erc20Contract.transferFrom(msg.sender, address(this), _amount), "ERC20 token transfer was unsuccessful");
erc20Contract.safeTransferFrom(msg.sender, address(this), _amount);

//Augment the pool size for this ERC20
uint256 _swapFee = (_amount.mul(fee)).div(100);
uint256 _receivedAmount = _amount.sub(_swapFee);
Expand Down Expand Up @@ -310,14 +311,14 @@ contract L1LiquidityPool is OVM_CrossDomainEnabled {
)
external
onlyOwner()
{
{
if (_erc20ContractAddress != address(0)) {
IERC20 erc20Contract = IERC20(_erc20ContractAddress);
uint256 withdrawableLiquidity = (erc20Contract.balanceOf(address(this))).sub(fees[_erc20ContractAddress]);
require(withdrawableLiquidity >= _amount, "Not enough liquidity on the pool to withdraw");
// balances[_erc20ContractAddress] = balances[_erc20ContractAddress].sub(_amount);
// use safe erc20 transfer
require(erc20Contract.transfer(_to, _amount));
erc20Contract.safeTransfer(_to, _amount);
} else {
uint256 withdrawableLiquidity = (address(this).balance).sub(fees[_erc20ContractAddress]);
require(withdrawableLiquidity >= _amount, "Not enough liquidity on the pool to withdraw");
Expand Down Expand Up @@ -347,15 +348,14 @@ contract L1LiquidityPool is OVM_CrossDomainEnabled {
)
external
onlyOwner()
{
{
if (_erc20ContractAddress != address(0)) {
//we are dealing with an ERC20
IERC20 erc20Contract = IERC20(_erc20ContractAddress);
require(erc20Contract.balanceOf(address(this)) >= _amount);
fees[_erc20ContractAddress] = fees[_erc20ContractAddress].sub(_amount);
// balances[_erc20ContractAddress] = balances[_erc20ContractAddress].sub(_amount);
// use safe erc20 transfer
require(erc20Contract.transfer(_to, _amount));
erc20Contract.safeTransfer(_to, _amount);
} else {
//we are dealing with Ether
//address(this).balance is not supported
Expand Down Expand Up @@ -396,20 +396,20 @@ contract L1LiquidityPool is OVM_CrossDomainEnabled {
)
external
onlyFromCrossDomainAccount(address(l2LiquidityPoolAddress))
{
{
if (_erc20ContractAddress != address(0)) {
//dealing with an ERC20
IERC20 erc20Contract = IERC20(_erc20ContractAddress);
// balances[_erc20ContractAddress] = balances[_erc20ContractAddress].sub(_amount);
require(erc20Contract.transfer(_to, _amount));
erc20Contract.safeTransfer(_to, _amount);
} else {
//this is ETH
// balances[address(0)] = balances[address(0)].sub(_amount);
//_to.transfer(_amount); UNSAFE
(bool sent,) = _to.call{gas: SAFE_GAS_STIPEND, value: _amount}("");
require(sent, "Failed to send Ether");
}

emit clientPayL1_EVENT(
_to,
_amount,
Expand Down
28 changes: 15 additions & 13 deletions contracts/L2LiquidityPool.sol
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
// SPDX-License-Identifier: MIT
pragma solidity >0.5.0;

import "./interfaces/IERC20.sol";
import { iL1LiquidityPool } from "./interfaces/iL1LiquidityPool.sol";

/* Library Imports */
import { OVM_CrossDomainEnabled } from "enyalabs_contracts/build/contracts/libraries/bridge/OVM_CrossDomainEnabled.sol";

/* External Imports */
import '@openzeppelin/contracts/math/SafeMath.sol';
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";

/**
* @dev An L2 LiquidityPool implementation
*/

contract L2LiquidityPool is OVM_CrossDomainEnabled {
using SafeERC20 for IERC20;
using SafeMath for uint256;

/*************
Expand Down Expand Up @@ -54,7 +56,7 @@ contract L2LiquidityPool is OVM_CrossDomainEnabled {
/********************
* Event *
********************/

event ownerAddERC20Liquidity_EVENT(
address sender,
uint256 amount,
Expand Down Expand Up @@ -141,7 +143,7 @@ contract L2LiquidityPool is OVM_CrossDomainEnabled {
require(l1ContractAddress[_erc20L2ContractAddress] == address(0), "Token Address Already Registerd");
require(!isL2Eth[_erc20L2ContractAddress], "Cannot replace Eth Address");
l1ContractAddress[_erc20L2ContractAddress] = _erc20L1ContractAddress;
}
}

/**
* @dev Overridable getter for the *L2* gas limit of settling the deposit, in the case it may be
Expand Down Expand Up @@ -215,14 +217,14 @@ contract L2LiquidityPool is OVM_CrossDomainEnabled {
function ownerAddERC20Liquidity(
uint256 _amount,
address _erc20L2ContractAddress
)
)
external
onlyOwner()
onlyOwner()
{
require(l1ContractAddress[_erc20L2ContractAddress] != address(0) || isL2Eth[_erc20L2ContractAddress], "Token Address Not Registerd");
IERC20 erc20Contract = IERC20(_erc20L2ContractAddress);
require(_amount <= erc20Contract.allowance(msg.sender, address(this)));
require(erc20Contract.transferFrom(msg.sender, address(this), _amount), "ERC20 token transfer was unsuccessful");
erc20Contract.safeTransferFrom(msg.sender, address(this), _amount);

// balances[_erc20L2ContractAddress] = balances[_erc20L2ContractAddress].add(_amount);

Expand All @@ -247,7 +249,7 @@ contract L2LiquidityPool is OVM_CrossDomainEnabled {
require(l1ContractAddress[_erc20L2ContractAddress] != address(0) || isL2Eth[_erc20L2ContractAddress], "Token Address Not Registerd");
IERC20 erc20Contract = IERC20(_erc20L2ContractAddress);
require(_amount <= erc20Contract.allowance(msg.sender, address(this)));
require(erc20Contract.transferFrom(msg.sender, address(this), _amount), "ERC20 token transfer was unsuccessful");
erc20Contract.safeTransferFrom(msg.sender, address(this), _amount);

//Augment the pool size for this ERC20
uint256 _swapFee = (_amount.mul(fee)).div(100);
Expand Down Expand Up @@ -288,13 +290,13 @@ contract L2LiquidityPool is OVM_CrossDomainEnabled {
)
external
onlyOwner()
{
{
IERC20 erc20Contract = IERC20(_erc20ContractAddress);
uint256 withdrawableLiquidity = (erc20Contract.balanceOf(address(this))).sub(fees[_erc20ContractAddress]);
require(withdrawableLiquidity >= _amount, "Not enough liquidity on the pool to withdraw");
// balances[_erc20ContractAddress] = balances[_erc20ContractAddress].sub(_amount);
// use safe erc20 transfer
require(erc20Contract.transfer(_to, _amount));
erc20Contract.safeTransfer(_to, _amount);

emit ownerWithdrawLiquidity_EVENT(
msg.sender,
Expand Down Expand Up @@ -323,7 +325,7 @@ contract L2LiquidityPool is OVM_CrossDomainEnabled {
require(erc20Contract.balanceOf(address(this)) >= _amount);
fees[_erc20ContractAddress] = fees[_erc20ContractAddress].sub(_amount);
// balances[_erc20ContractAddress] = balances[_erc20ContractAddress].sub(_amount);
require(erc20Contract.transfer(_to, _amount));
erc20Contract.safeTransfer(_to, _amount);

emit ownerRecoverFee_EVENT(
msg.sender,
Expand Down Expand Up @@ -354,14 +356,14 @@ contract L2LiquidityPool is OVM_CrossDomainEnabled {
{
IERC20 erc20Contract = IERC20(_erc20ContractAddress);
// balances[_erc20ContractAddress] = balances[_erc20ContractAddress].sub(_amount);
require(erc20Contract.transfer(_to, _amount));
erc20Contract.safeTransfer(_to, _amount);



emit clientPayL2_EVENT(
_to,
_amount,
_erc20ContractAddress
);
}

}
53 changes: 0 additions & 53 deletions contracts/interfaces/IERC20.sol

This file was deleted.

23 changes: 0 additions & 23 deletions contracts/interfaces/IUniswapV2ERC20.sol

This file was deleted.

0 comments on commit 76dd67b

Please sign in to comment.